100% found this document useful (1 vote)
26 views

Kubernetes Programming with Go: Programming Kubernetes Clients and Operators Using Go and the Kubernetes API 1st Edition Philippe Martin - The ebook in PDF/DOCX format is ready for download now

The document provides information on downloading the book 'Kubernetes Programming with Go' by Philippe Martin, which focuses on programming Kubernetes clients and operators using Go and the Kubernetes API. It includes links to additional resources and related books available on ebookmass.com. The document also contains details about the book's content, structure, and authorship.

Uploaded by

mansiajuste
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
26 views

Kubernetes Programming with Go: Programming Kubernetes Clients and Operators Using Go and the Kubernetes API 1st Edition Philippe Martin - The ebook in PDF/DOCX format is ready for download now

The document provides information on downloading the book 'Kubernetes Programming with Go' by Philippe Martin, which focuses on programming Kubernetes clients and operators using Go and the Kubernetes API. It includes links to additional resources and related books available on ebookmass.com. The document also contains details about the book's content, structure, and authorship.

Uploaded by

mansiajuste
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Visit ebookmass.

com to download the full version and


explore more ebook or textbook

Kubernetes Programming with Go: Programming


Kubernetes Clients and Operators Using Go and the
Kubernetes API 1st Edition Philippe Martin

_____ Click the link below to download _____


https://ebookmass.com/product/kubernetes-programming-with-
go-programming-kubernetes-clients-and-operators-using-go-
and-the-kubernetes-api-1st-edition-philippe-martin/

Explore and download more ebook or textbook at ebookmass.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Kubernetes Programming with Go: Programming Kubernetes


Clients and Operators Using Go and the Kubernetes API 1st
Edition Philippe Martin
https://ebookmass.com/product/kubernetes-programming-with-go-
programming-kubernetes-clients-and-operators-using-go-and-the-
kubernetes-api-1st-edition-philippe-martin-2/

Ansible for Kubernetes by Example: Automate Your


Kubernetes Cluster with Ansible 1st Edition Luca Berton

https://ebookmass.com/product/ansible-for-kubernetes-by-example-
automate-your-kubernetes-cluster-with-ansible-1st-edition-luca-
berton-2/

Ansible for Kubernetes by Example: Automate Your


Kubernetes Cluster with Ansible 1st Edition Luca Berton

https://ebookmass.com/product/ansible-for-kubernetes-by-example-
automate-your-kubernetes-cluster-with-ansible-1st-edition-luca-berton/

Software Development with Go: Cloud-Native Programming


using Golang with Linux and Docker 1st Edition Nanik
Tolaram
https://ebookmass.com/product/software-development-with-go-cloud-
native-programming-using-golang-with-linux-and-docker-1st-edition-
nanik-tolaram/
Deploy Container Applications Using Kubernetes:
Implementations with microk8s and AWS EKS Shiva
Subramanian
https://ebookmass.com/product/deploy-container-applications-using-
kubernetes-implementations-with-microk8s-and-aws-eks-shiva-
subramanian/

Kubernetes Secrets Handbook Emmanouil Gkatziouras

https://ebookmass.com/product/kubernetes-secrets-handbook-emmanouil-
gkatziouras/

The Kubernetes Book: 2024 Edition Nigel Poulton

https://ebookmass.com/product/the-kubernetes-book-2024-edition-nigel-
poulton/

Observability with Grafana: Monitor, control, and


visualize your Kubernetes and cloud platforms using the
LGTM stack Rob Chapman
https://ebookmass.com/product/observability-with-grafana-monitor-
control-and-visualize-your-kubernetes-and-cloud-platforms-using-the-
lgtm-stack-rob-chapman/

Beginning Cloud Native Development with MicroProfile,


Jakarta EE, and Kubernetes 1st Edition Tarun Telang

https://ebookmass.com/product/beginning-cloud-native-development-with-
microprofile-jakarta-ee-and-kubernetes-1st-edition-tarun-telang/
Kubernetes
Programming
with Go
Programming Kubernetes Clients
and Operators Using Go and
the Kubernetes API

Philippe Martin
Kubernetes Programming
with Go
Programming Kubernetes Clients
and Operators Using
Go and the Kubernetes API

Philippe Martin
Kubernetes Programming with Go: Programming Kubernetes Clients and Operators
Using Go and the Kubernetes API

Philippe Martin
Blanquefort, France

ISBN-13 (pbk): 978-1-4842-9025-5 ISBN-13 (electronic): 978-1-4842-9026-2


https://doi.org/10.1007/978-1-4842-9026-2

Copyright © 2023 by Philippe Martin


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Divya Modi
Development Editor: James Markham
Coordinating Editor: Divya Modi
Copy Editor: Kim Burton Wiseman
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or
visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is
Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware
corporation.
For information on translations, please e-mail [email protected]; for reprint,
paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers
on GitHub via the book's product page, located at https://github.com/Apress/Kubernetes-Programming-
with-Go-by-Philippe-Martin. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
To Mélina and Elsa, my constant source of truth
Table of Contents
About the Author��������������������������������������������������������������������������������������������������� xiii

About the Technical Reviewers�������������������������������������������������������������������������������xv


Acknowledgments�������������������������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

Chapter 1: Kubernetes API Introduction������������������������������������������������������������������� 1


Kubernetes Platform at a Glance�������������������������������������������������������������������������������������������������� 1
OpenAPI Specification������������������������������������������������������������������������������������������������������������������� 3
Verbs and Kinds���������������������������������������������������������������������������������������������������������������������������� 5
Group-Version-Resource��������������������������������������������������������������������������������������������������������������� 6
Sub-resources������������������������������������������������������������������������������������������������������������������������������ 8
Official API Reference Documentation������������������������������������������������������������������������������������������ 9
The Deployment Documentation������������������������������������������������������������������������������������������� 10
Operations Documentation���������������������������������������������������������������������������������������������������� 12
The Pod Documentation�������������������������������������������������������������������������������������������������������� 14
One-Page Version of the Documentation������������������������������������������������������������������������������� 15
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 16

Chapter 2: Kubernetes API Operations������������������������������������������������������������������� 17


Examining Requests������������������������������������������������������������������������������������������������������������������� 17
Making Requests������������������������������������������������������������������������������������������������������������������������ 18
Using kubectl as a Proxy������������������������������������������������������������������������������������������������������� 18
Creating a Resource�������������������������������������������������������������������������������������������������������������� 19
Getting Information About a Resource����������������������������������������������������������������������������������� 20
Getting the List of Resources������������������������������������������������������������������������������������������������ 20

v
Table of Contents

Filtering the Result of a List�������������������������������������������������������������������������������������������������� 21


Deleting a Resource�������������������������������������������������������������������������������������������������������������� 26
Deleting a Collection of Resources���������������������������������������������������������������������������������������� 26
Updating a Resource������������������������������������������������������������������������������������������������������������� 27
Managing Conflicts When Updating a Resource�������������������������������������������������������������������� 28
Using a Strategic Merge Patch to Update a Resource����������������������������������������������������������� 32
Applying Resources Server-side�������������������������������������������������������������������������������������������� 38
Watching Resources�������������������������������������������������������������������������������������������������������������� 44
Filtering During a Watch Session������������������������������������������������������������������������������������������� 45
Watching After Listing Resources������������������������������������������������������������������������������������������ 45
Restarting a watch Request�������������������������������������������������������������������������������������������������� 46
Allowing Bookmarks to Efficiently Restart a watch Request������������������������������������������������� 47
Paginating Results����������������������������������������������������������������������������������������������������������������� 50
Getting Results in Various Formats��������������������������������������������������������������������������������������������� 52
Getting Results as a Table����������������������������������������������������������������������������������������������������� 52
Using the YAML Format��������������������������������������������������������������������������������������������������������� 54
Using the Protobuf Format���������������������������������������������������������������������������������������������������� 54
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 55

Chapter 3: Working with API Resources in Go�������������������������������������������������������� 57


API Library Sources and Import�������������������������������������������������������������������������������������������������� 57
Content of a Package������������������������������������������������������������������������������������������������������������������ 58
types.go��������������������������������������������������������������������������������������������������������������������������������� 58
register.go������������������������������������������������������������������������������������������������������������������������������ 59
doc.go������������������������������������������������������������������������������������������������������������������������������������ 60
generated.pb.go and generated.proto����������������������������������������������������������������������������������� 60
types_swagger_doc_generated.go��������������������������������������������������������������������������������������� 60
zz_generated.deepcopy.go���������������������������������������������������������������������������������������������������� 61
Specific Content in core/v1��������������������������������������������������������������������������������������������������������� 61
ObjectReference�������������������������������������������������������������������������������������������������������������������� 61
ResourceList�������������������������������������������������������������������������������������������������������������������������� 62
Taint��������������������������������������������������������������������������������������������������������������������������������������� 64

vi
Table of Contents

Toleration������������������������������������������������������������������������������������������������������������������������������� 65
Well-Known Labels���������������������������������������������������������������������������������������������������������������� 66
Writing Kubernetes Resources in Go������������������������������������������������������������������������������������������ 67
Importing the Package���������������������������������������������������������������������������������������������������������� 67
The TypeMeta Fields�������������������������������������������������������������������������������������������������������������� 68
The ObjectMeta Fields����������������������������������������������������������������������������������������������������������� 69
Spec and Status�������������������������������������������������������������������������������������������������������������������� 76
Comparison with Writing YAML Manifests����������������������������������������������������������������������������� 76
A Complete Example������������������������������������������������������������������������������������������������������������������� 78
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 83

Chapter 4: Using Common Types���������������������������������������������������������������������������� 85


Pointers��������������������������������������������������������������������������������������������������������������������������������������� 85
Getting the Reference of a Value������������������������������������������������������������������������������������������� 85
Dereferencing a Pointer��������������������������������������������������������������������������������������������������������� 86
Comparing Two Referenced Values��������������������������������������������������������������������������������������� 86
Quantities������������������������������������������������������������������������������������������������������������������������������������ 87
Parsing a String as Quantity�������������������������������������������������������������������������������������������������� 87
Using an inf.Dec as a Quantity����������������������������������������������������������������������������������������������� 88
Using a Scaled Integer as a Quantity������������������������������������������������������������������������������������� 89
Operations on Quantities������������������������������������������������������������������������������������������������������� 90
IntOrString����������������������������������������������������������������������������������������������������������������������������������� 90
Time�������������������������������������������������������������������������������������������������������������������������������������������� 92
Factory Methods�������������������������������������������������������������������������������������������������������������������� 92
Operations on Time���������������������������������������������������������������������������������������������������������������� 92
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 93

Chapter 5: The API Machinery�������������������������������������������������������������������������������� 95


The Schema Package����������������������������������������������������������������������������������������������������������������� 96
Scheme��������������������������������������������������������������������������������������������������������������������������������������� 97
Initialization��������������������������������������������������������������������������������������������������������������������������� 98
Mapping������������������������������������������������������������������������������������������������������������������������������� 100

vii
Table of Contents

Conversion��������������������������������������������������������������������������������������������������������������������������� 101
Serialization������������������������������������������������������������������������������������������������������������������������� 103
RESTMapper����������������������������������������������������������������������������������������������������������������������������� 105
Kind to Resource����������������������������������������������������������������������������������������������������������������� 106
Resource to Kind����������������������������������������������������������������������������������������������������������������� 107
Finding Resources��������������������������������������������������������������������������������������������������������������� 107
The DefaultRESTMapper Implementation���������������������������������������������������������������������������� 107
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 108

Chapter 6: The Client-go Library��������������������������������������������������������������������������� 109


Connecting to the Cluster��������������������������������������������������������������������������������������������������������� 110
In-cluster Configuration������������������������������������������������������������������������������������������������������� 110
Out-of-Cluster Configuration����������������������������������������������������������������������������������������������� 110
Getting a Clientset��������������������������������������������������������������������������������������������������������������������� 115
Using the Clientset�������������������������������������������������������������������������������������������������������������������� 116
Examining the Requests����������������������������������������������������������������������������������������������������������� 119
Creating a Resource������������������������������������������������������������������������������������������������������������������ 120
Getting Information About a Resource�������������������������������������������������������������������������������������� 122
Getting List of Resources���������������������������������������������������������������������������������������������������������� 123
Filtering the Result of a List������������������������������������������������������������������������������������������������������ 125
Setting LabelSelector Using the Labels Package���������������������������������������������������������������� 125
Setting Fieldselector Using the Fields Package������������������������������������������������������������������ 128
Deleting a Resource������������������������������������������������������������������������������������������������������������������ 130
Deleting a Collection of Resources������������������������������������������������������������������������������������������� 133
Updating a Resource����������������������������������������������������������������������������������������������������������������� 134
Using a Strategic Merge Patch to Update a Resource�������������������������������������������������������������� 135
Applying Resources Server-side with Patch����������������������������������������������������������������������������� 138
Server-side Apply Using Apply Configurations�������������������������������������������������������������������������� 140
Building an ApplyConfiguration from Scratch���������������������������������������������������������������������� 142
Building an ApplyConfiguration from an Existing Resource������������������������������������������������ 143

viii
Table of Contents

Watching Resources����������������������������������������������������������������������������������������������������������������� 145


Errors and Statuses������������������������������������������������������������������������������������������������������������������ 149
Definition of the metav1.Status Structure��������������������������������������������������������������������������� 149
Error Returned by Clientset Operations������������������������������������������������������������������������������� 153
RESTClient�������������������������������������������������������������������������������������������������������������������������������� 154
Building the Request����������������������������������������������������������������������������������������������������������� 154
Executing the Request��������������������������������������������������������������������������������������������������������� 161
Exploiting the Result������������������������������������������������������������������������������������������������������������ 161
Getting Result as a Table����������������������������������������������������������������������������������������������������������� 162
Discovery Client������������������������������������������������������������������������������������������������������������������������ 164
RESTMapper����������������������������������������������������������������������������������������������������������������������������� 165
PriorityRESTMapper������������������������������������������������������������������������������������������������������������ 165
DeferredDiscoveryRESTMapper������������������������������������������������������������������������������������������ 167
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 167

Chapter 7: Testing Applications Using Client-go�������������������������������������������������� 169


Fake Clientset��������������������������������������������������������������������������������������������������������������������������� 170
Checking the Result of the Function������������������������������������������������������������������������������������ 171
Reacting to Actions�������������������������������������������������������������������������������������������������������������� 173
Checking the Actions����������������������������������������������������������������������������������������������������������� 177
Fake REST Client����������������������������������������������������������������������������������������������������������������������� 185
FakeDiscovery Client���������������������������������������������������������������������������������������������������������������� 188
Stubbing the ServerVersion������������������������������������������������������������������������������������������������� 189
Actions��������������������������������������������������������������������������������������������������������������������������������� 190
Mocking Resources������������������������������������������������������������������������������������������������������������� 190
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 191

Chapter 8: Extending Kubernetes API with Custom Resources Definitions���������� 193


Performing Operations in Go����������������������������������������������������������������������������������������������������� 194
The CustomResourceDefinition in Detail����������������������������������������������������������������������������������� 195
Naming the Resource���������������������������������������������������������������������������������������������������������� 196
Definition of the Resource Versions������������������������������������������������������������������������������������� 197
Converting Between Versions���������������������������������������������������������������������������������������������� 198
ix
Table of Contents

Schema of the Resource����������������������������������������������������������������������������������������������������������� 199


Deploying a Custom Resource Definition���������������������������������������������������������������������������������� 201
Additional Printer Columns������������������������������������������������������������������������������������������������������� 204
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 207

Chapter 9: Working with Custom Resources�������������������������������������������������������� 209


Generating a Clientset��������������������������������������������������������������������������������������������������������������� 209
Using deepcopy-gen������������������������������������������������������������������������������������������������������������ 211
Using client-gen������������������������������������������������������������������������������������������������������������������ 213
Using the Generated Clientset��������������������������������������������������������������������������������������������� 216
Using the Generated fake Clientset������������������������������������������������������������������������������������� 217
Using the Unstructured Package and Dynamic Client��������������������������������������������������������������� 217
The Unstructured Type��������������������������������������������������������������������������������������������������������� 217
The UnstructuredList Type��������������������������������������������������������������������������������������������������� 221
Converting Between Typed and Unstructured Objects�������������������������������������������������������� 223
The Dynamic Client�������������������������������������������������������������������������������������������������������������� 223
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 229

Chapter 10: Writing Operators withthe Controller-­Runtime Library��������������������� 231


The Manager����������������������������������������������������������������������������������������������������������������������������� 232
The Controller��������������������������������������������������������������������������������������������������������������������������� 235
Creating a Controller����������������������������������������������������������������������������������������������������������� 236
Watching Resources������������������������������������������������������������������������������������������������������������ 237
A First Example������������������������������������������������������������������������������������������������������������������� 241
Using the Controller Builder������������������������������������������������������������������������������������������������ 244
A Second Example Using the ControllerBuilder������������������������������������������������������������������� 245
Injecting Manager Resources into the Reconciler��������������������������������������������������������������� 247
Using the Client������������������������������������������������������������������������������������������������������������������������� 250
Getting Information About a Resource��������������������������������������������������������������������������������� 250
Listing Resources���������������������������������������������������������������������������������������������������������������� 251
Creating a Resource������������������������������������������������������������������������������������������������������������ 254
Deleting a Resource������������������������������������������������������������������������������������������������������������ 254

x
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Table of Contents

Deleting a Collection of Resources�������������������������������������������������������������������������������������� 255


Updating a Resource����������������������������������������������������������������������������������������������������������� 256
Patching a Resource������������������������������������������������������������������������������������������������������������ 256
Updating the Status of a Resource�������������������������������������������������������������������������������������� 259
Patching the Status of a Resource�������������������������������������������������������������������������������������� 260
Logging������������������������������������������������������������������������������������������������������������������������������������� 261
Verbosity������������������������������������������������������������������������������������������������������������������������������ 262
Predefined Values���������������������������������������������������������������������������������������������������������������� 262
Logger Name����������������������������������������������������������������������������������������������������������������������� 262
Getting the Logger from Context����������������������������������������������������������������������������������������� 263
Events��������������������������������������������������������������������������������������������������������������������������������������� 263
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 265

Chapter 11: Writing the Reconcile Loop��������������������������������������������������������������� 267


Writing the Reconcile Function������������������������������������������������������������������������������������������������� 268
Checking Whether the Resource Exists������������������������������������������������������������������������������� 268
Implementing the Reconciled Resource������������������������������������������������������������������������������ 268
Simple Implementation Example����������������������������������������������������������������������������������������� 270
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 275

Chapter 12: Testing the Reconcile Loop��������������������������������������������������������������� 277


The envtest Package����������������������������������������������������������������������������������������������������������������� 277
Installing envtest Binaries��������������������������������������������������������������������������������������������������� 277
Using envtest����������������������������������������������������������������������������������������������������������������������� 278
Defining a ginkgo Suite������������������������������������������������������������������������������������������������������������� 279
Writing the Tests����������������������������������������������������������������������������������������������������������������������� 283
Test 1����������������������������������������������������������������������������������������������������������������������������������� 284
Test 2����������������������������������������������������������������������������������������������������������������������������������� 285
Test 3����������������������������������������������������������������������������������������������������������������������������������� 285
Test 4����������������������������������������������������������������������������������������������������������������������������������� 285
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 290

xi
Table of Contents

Chapter 13: Creating an Operator with Kubebuilder�������������������������������������������� 291


Installing Kubebuilder��������������������������������������������������������������������������������������������������������������� 291
Creating a Project��������������������������������������������������������������������������������������������������������������������� 291
Adding a Custom Resource to the Project�������������������������������������������������������������������������������� 293
Building and Deploying Manifests�������������������������������������������������������������������������������������������� 295
Running the Manager Locally��������������������������������������������������������������������������������������������������� 295
Personalizing the Custom Resource����������������������������������������������������������������������������������������� 296
Editing the Go Structures���������������������������������������������������������������������������������������������������� 297
Enabling the Status Subresource���������������������������������������������������������������������������������������� 297
Defining Printer Columns����������������������������������������������������������������������������������������������������� 297
Regenerating the Files�������������������������������������������������������������������������������������������������������� 298
Implementing the Reconcile Function�������������������������������������������������������������������������������������� 299
Adding RBAC Annotations��������������������������������������������������������������������������������������������������������� 299
Deploying the Operator on the Cluster�������������������������������������������������������������������������������������� 300
Creating a New Version of the Resource����������������������������������������������������������������������������������� 301
Defining a New Version������������������������������������������������������������������������������������������������������� 302
Implementing Hub and Convertible������������������������������������������������������������������������������������� 303
Setting Up the webhook������������������������������������������������������������������������������������������������������ 305
Updating kustomization Files���������������������������������������������������������������������������������������������� 306
Using Various Versions�������������������������������������������������������������������������������������������������������� 306
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 308

Index��������������������������������������������������������������������������������������������������������������������� 309

xii
About the Author
Philippe Martin has been working with Kubernetes for
five years, first by creating an Operator to deploy video
CDNs into the cloud, later helping companies deploy their
applications into Kubernetes, then writing a Client to help
developers work in a Kubernetes environment. Philippe
has passed the CKAD, CKA, and CKS certifications. He has
extensive experience with distributed systems and open-
source software: he started his career 20 years ago creating
thin clients based on the Linux kernel and open-­source
components. He is currently working at Red Hat on the
Development Tools team.
Philippe has been active in the development of Kubernetes, especially its
documentation, and participates in the translation of the official documentation into
French, has edited two reference books about the Kubernetes API and kubectl, and is
responsible for the French translation of the Kubernetes Dashboard. He participated in
Google Season of Docs to create the new Kubernetes API Reference section of the official
documentation and is maintaining it.

xiii
About the Technical Reviewers
Bartosz Majsak writes code for fun and profit while proudly
wearing a red fedora (also known as the Red Hat). He has
been long-time open-source contributor and Java developer
turned into Golang aficionado. Bartosz is overly enthusiastic
about coffee, open source, and speaking at conferences,
not necessarily in that order. One thing that perhaps proves
he is not a total geek is his addiction to alpine skiing (and
running).

Prasanth is a Blockchain Certified Professional, Professional


Scrum Master, and Microsoft Certified Trainer who is
passionate about helping others learn how to use and gain
benefits from the latest technologies. He is a thought leader
and practitioner in Blockchain, Cloud, and Scrum. He also
handles the Agile Methodology, Cloud, and Blockchain
technology community initiatives within TransUnion
through coaching, mentoring, and grooming techniques.
Prasanth is an adjunct professor and a technical speaker.
He was selected as a speaker at the China International
Industry Big Data Expo 2018 by the Chinese government and also was invited to the
International Blockchain Council by the Government of Telangana and Goa. In addition,
he received accolades from the Chinese government for his presentation at China
International Industry Big Data Expo 2018. Prasanth has published his Patent, entitled
“Digital Educational Certificate Management System Using IPFS-Based Blockchain.”
To date, he has interacted extensively, reaching more than 50,000 students,
mostly within the technical domain. Prasanth is a working group member of the
CryptoCurrency Certification Consortium, the Scrum Alliance, the Scrum Organization,
and the International Institute of Business Analysis.

xv
Acknowledgments
I would like to thank the whole Anevia “CDN” team who started working with me on
Kubernetes back in 2018: David, Ansou, Hossam, Yassine, Étienne, Jason, and Michaël.
Special thanks to Damien Lucas for initiating this project and for having trusted us with
this challenge.
My discovery of Kubernetes has been much easier and pleasant thanks to the TGIK
channel and its numerous episodes, hosted by Joe Beda, Kris Nova, and many others.
Plus, thanks to all the Kubernetes community for such a great ecosystem!

xvii
Introduction
Back in 2017, I was working for a company building video streaming software. At the end
of that year, a small team, including me, got assigned a new job to work on deploying
the Video CDN developed by the company on Kubernetes. We decided to explore the
concept of Custom Resources and Operators to deploy this CDN.
The current Kubernetes release was 1.9, the concept of Custom Resource Definition
had just been released in 1.7, and the sample-controller repository was the only
documentation we knew of to help build an Operator. The Kubernetes ecosystem,
being especially lively, had tools appearing in the following months, specifically the
Kubebuilder SDK. Thus, our project was launched.
From that moment on, I spent numerous days exploring how to build Operators and
other programs interacting with the Kubernetes API. But the damage was done: I had
started to learn Kubernetes programming from specific to general, and it took me a long
time to fully understand the innards of the Kubernetes API.
I have written this book in the hope that it can teach new Kubernetes developers how
to program, from general to specific, with the Kubernetes API in Go.

Chapters at a Glance
The target reader for this book has some experience working with REST APIs, accessing
them either by HTTP or using clients for specific languages; and has some knowledge of
the Kubernetes platform, essentially as a user—for example, some experience deploying
such APIs or frontend applications with the help of YAML manifests.

• Chapter 1 of the book explores the Kubernetes API and how it


implements the principles of REST. It especially focuses on the
Group-Version-Resource organization and the Kind concept
proposed by the API.
• Chapter 2 continues by covering the operations proposed by the API
and the details of each operation, using the HTTP protocol.

xix
Introduction

• Chapters 3 to 5 describe the common and “low-level” Go libraries to


work with the Kubernetes API: the API and API Machinery Libraries.

• Chapters 6 and 7 cover the Client-go Library—the high-level library


to work with the Kubernetes API in Go—and how to unit test code
using this library.

At this point in the book, the reader should be comfortable with building Go
applications working with native resources of the Kubernetes API.

• Chapters 8 and 9 introduce the concept of Custom Resources and


how to work with them in Go.

• Chapters 10 to 12 cover the implementation of Kubernetes Operators


using the controller-runtime library.

• Chapter 13 explores the Kubebuilder SDK, a tool to help develop and


deploy Kubernetes Operators.

By the end of the book, the reader should be able to start building Kubernetes
operators in Go and have a very good understanding of what happens behind the scenes.

xx
CHAPTER 1

Kubernetes API
Introduction
Kubernetes is a platform to orchestrate containers operating in the declarative mode.
There are one-thousand-and-one ways to describe how the Kubernetes platform is
constructed. This book focuses on programming with the platform.
The entry point of the Kubernetes platform is the API. This chapter explores the
Kubernetes architecture by highlighting the central role of the Kubernetes API. It then
focuses on the HTTP REST nature of the Kubernetes API, and on the extensions added to
organize the many resources managed by it.
Finally, you will learn how to navigate the reference documentation effectively to be
able to extract the maximum quantity of useful information daily.

Kubernetes Platform at a Glance


On one side of the chain, the user declares the high-level resources to build applications
to deploy: Deployments, Ingresses, and so on.
In the middle, controllers are activated to transform these resources into low-level
resources (Pods), and the scheduler distributes these resources into nodes. On the other
side of the chain, the node agents deploy the low-level resources onto nodes.
The main elements of the Kubernetes platform (commonly called the control-plane)
are highlighted in Figure 1-1 and described in the following:

1. The API server – this is the central point on the control-plane; the
user and the various pieces of the control-plane contact this API to
create, get, delete, update, and watch resources.

2. The etcd database – this is only accessible by the API Server, is


used to persist the data relative to resources.

1
© Philippe Martin 2023
P. Martin, Kubernetes Programming with Go, https://doi.org/10.1007/978-1-4842-9026-2_1
Chapter 1 Kubernetes API Introduction

3. The Controller manager – this runs the controllers that transform


high-level resources declared by the user into low-level resources
to be deployed on nodes. The controllers are connected to the API
Server, watching for high-level resources and creating, deleting,
and updating low-level resources to satisfy the specifications
declared in high-level resources.

4. Scheduler – this distributes the low-level resources on the various


nodes. The Scheduler is connected to the API Server to watch for
unaffected resources and connect them to nodes.

5. Kubelet – this is an agent running on all nodes of the cluster,


and each agent manages the workloads affected to its node. The
kubelet is connected to the API Server to watch for Pods resources
affected to its node and to deploy the associated containers using
the local container runtime.

6. Kube proxy – this is an agent running on all nodes of the cluster,


and each agent manages the network configurations affected to
its node. The kube proxy is connected to the API Server to watch
for Service resources and to configure associated network rules on
its node.

2
Chapter 1 Kubernetes API Introduction

etcd
Kubectl
---

Control-plane
cre
at watch
de e set
up lete,
d
Controllers
wa ate
tch
API Server crea
te
te
dele te
a
upd
tch
wa

wa ate
tch
up
d
Kubelet
watch

Scheduler
no Kube-proxy
de
s

Figure 1-1. The architecture of Kubernetes

O
 penAPI Specification
The Kubernetes API is an HTTP REST API. The Kubernetes team provides a specification
for this API in the OpenAPI format, either in v2 format at https://github.com/
kubernetes/kubernetes/tree/master/api/openapi-spec or in Kubernetes v1.24,
in v3 format, at https://github.com/kubernetes/kubernetes/tree/master/api/
openapi-spec/v3.
These specifications also are accessible from the API Server at these paths:
/openapi/v2 and /openapi/v3.
An OpenAPI specification is made up of various parts and, among these, are a list of
paths and a list of definitions. The paths are the URLs you use to request this API, and
for each path, the specification gives the distinct operations such as get, delete, or post.
Then for each operation, the specification indicates what are the parameters and body
format for the request, and what are the possible response codes and associated body
format for the response.

3
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Other documents randomly have
different content
conclude that she and you have some private understanding upon
the point. I told her so one day, and all the answer I received
consisted of a laugh and a blush."

It could have been nothing to the blush that rose to Robert's face
now; brow, ears, neck, all were dyed blood-red. The terrible
consciousness of how untrue this was, how untrue it was obliged to
be, was smiting him with reproachful sting. Mr. Grubb mistook the
signs.

"I think," he said, "that former parting was a mistake. It was


perfectly right and just that Mrs. Dalrymple should have been well
provided for, but——"

"You think I should have taken Moat Grange myself, and procured
another home for my mother," interrupted Robert. "Most people do
think so. But, if you knew how I hated the sight of the Grange!—
never a single room of it but my poor dead father's face seemed to
rise up to confront me."

"It might have been best that you should remain in your own
home; we will not discuss it now. What I want to say is this—that if
you and Mary have been really living upon hope, I don't see why you
need live upon it any longer. A portion of your own revenues you
may surely claim, a few hundreds yearly; and Mary shall bring as
much grist to the mill on her side."

"You are very kind, very thoughtful," murmured Robert.

"But there must be a proviso to that," continued Mr. Grubb.


"Reports have reached me that Robert Dalrymple is going headlong
to the bad—pardon me if I speak out the whispers freely—that he is
becoming reckless, a gamester, I know not what all. I do not believe
this, Robert; I do not wish to believe it. I have seen nothing to
confirm it, myself; you are in one set of London men, I am in
another. In a young man situated as you are, alone, without home-
ties, some latitude of conduct may be pardoned if he be a good man
and true, he will soon pull himself straight again. If you can assure
me on your honour it is nothing more than this, well and good. If it
be more—if the worst of the whispers but indicate the truth, you
cannot of course think of Mary. Robert, I say I leave this to your
honour."

"I should like to pull myself up beyond any earthly thing," spoke
the young man, in a flash of what looked far more like despair than
hope. "If I could do it—and if Mary were my wife—I—I should have
no fear. Let us talk of this another day. Let me see her!"

Mary was just then alone in what they called the grey drawing-
room. A lovely room; as indeed all the rooms were in Mr. Grubb's
house, made so by him in his love for his wife. He went in search of
his wife, giving Robert the opportunity of seeing Mary alone.

Let no woman go to the altar cherishing dislike or contempt of him


who is to be her husband. Marriages of indifference are made in
plenty, and in time they may become unions of affection. But the
other!—it is the most fatal mistake that can be made. Lady Adela
treated her husband with scorn, did so systematically; she did not
attempt to conceal her dislike; she threw his love back upon him. On
the very day of their marriage, when she, in what appeared to be a
fit of petulance, drew down all the blinds of the chariot as they
drove away from Lord Acorn's door, and he, taking advantage of the
privacy, laid his hand on hers, and bent to whisper a word of love,
perhaps to take a kiss from her cheek, she effectually repressed him.
"Pray do not attempt these—endearments," she said in a scornful
tone, "they are not agreeable." Francis Grubb drew back to his
corner of the carriage, and a bitter blight fell upon his spirit.

For some months past now, Lady Adela had been pale and thin,
sick and ill. She resented the indisposition strongly, for it prevented
her joining in the gaiety she loved, and went about wishing fretfully
that her baby was born.
"Oh, Robert! Robert!"

Mary Lynn had started up with a cry, so surprised was she to see
him enter. She stood blushing even to tears. And Robert? Conscious
how unworthy he was of her, how impossible it was that he should
dare to claim her, while the love within him was beating on his heart
with lively pain, he sat down with a groan and covered his face with
his hands. She thought he was ill. She went to him and knelt down,
and looked up at him in appealing fear.

"Robert, what is it—what is amiss?"

And for answer, Robert Dalrymple, utterly overcome by the vivid


sense of the remorseful past, of despair for the future, let his face
fall upon her shoulder, and burst into a fit of heart-rending sobs so
terrible for a man to yield himself to.

VII.

DESPERATION.

Alone in the oak-parlour at Moat Grange, playing soft bits of


melody in the summer twilight, sat Selina Dalrymple, her very pretty
face slightly flushed, her bright hair pushed from her face. Ordinarily
of a calm and equable temperament, Selina was yet rather given to
work herself up to restlessness on occasion. She was expecting
Oscar Dalrymple; and though the excitement did not arise for
himself, it did for the news he might bring.
"There he is!" she cried, as a step was heard on the gravel. "He
has walked up from the station."

Oscar Dalrymple came in, very quiet as usual, not a speck of dust
or other sign of travel upon him, looking spick and span, as though
he had but come out of the next room. Oscar Dalrymple's place, a
small patrimony called Knutford, lay some three or four miles off; he
would probably walk on there by-and-by, if he did not sleep at the
Grange.

"I thought you would come!" exclaimed Selina, gladly springing


towards him.

"I told Mrs. Dalrymple I should return before Saturday," was his
answer, as he took her hand, and kept it in his. "Where is she?"

"Gone with Alice to dine at Court Netherleigh," replied Selina. "I


sent an excuse: I was impatient to see you."

"Thank you, Selina!" he whispered in low, warm tones. "That is a


great admission from you."

"Not to see you; but for what you might have to tell," she
hastened to say. "Oscar, how vain you are!"

She sat down in the bow-window, in what remaining light there


was, and he took a chair opposite to her. Then she asked him his
news.

"Do you know exactly why I went up?" he inquired with some
hesitation, in doubt how far he ought to speak.

"I know all," she answered pointedly. "I saw Reuben's letter to
mamma; and her fears are my fears. We keep it from poor Alice."

In a hushed voice, befitting the subject and the twilight hour,


Oscar related to her what he had gathered in London. The very
worst impression lay on his own mind: namely, that Robert was
going rapidly to the dogs, money and honour and peace, and all;
nay; had already gone; but he did not make the worst of it to Selina.
He said that Robert seemed to be on a downward course, and would
not listen to any sort of reason.

Selina sat in dismay; her soft dark eyes fixed on the evening sky,
her hands clasped on the dress of blue silk she wore. The evening
star shone in the heavens.

"What will be the end of it, Oscar?"

Oscar did not immediately answer. The end of it, as he fully


believed, would be ruin. Utter ruin for Robert; and that would
involve ruin for his mother and sisters.

"Does Robert really play?" pursued Selina.

"I fear he does. Yes."

"Could—could he play away our home—Moat Grange?"

"For his own life. That is, mortgage its revenues."

"But you don't, surely, fear it will come to this?" she cried in
agitation.

"Selina, I hardly know what I fear. Robert is not my brother, and I


could not—I had no right—to question too closely. Neither, if I had
questioned, and—and heard the worst—do I see what I could have
done. Matters have gone too far for any aid, any suggestion, that I
could have given."

"What would become of us? Poor mamma! Poor Alice! Oh, what a
trouble!"
"You, at least, can escape the trouble, Selina; you can let me take
you out of it. My home is not the luxurious home you have been
accustomed to here; but it will afford you every comfort—if you will
only come to it. Oh, my love, why do you let me plead to you so
long in vain!"

Selina Dalrymple pouted her pretty red lips. Oscar loved her to
folly. She did not discourage him; did not absolutely encourage him.
She liked him very well, and she liked his homage, for she was one
of the vainest girls living; but, as to marrying him?—that was
another thing. Had he possessed the rent-roll of a duke, she would
have had him tomorrow; his income was a small one, and she loved
pomp and show.

"Now, Oscar!" she remonstrated, putting him off as usual. "Is it a


time to bring in that nonsense, when we are talking and thinking of
poor Robert? And here come mamma and Alice, for that's Miss
Upton's carriage bringing them. They said they should be home
early."

And now we have to go back some few hours. It is very


inconvenient, as the world knows, to tell two portions of a story at
one and the same time.

Turning out of one of the handsomest houses in Grosvenor


Square, in the bright sunshine of this same Friday afternoon in June,
went Robert Dalrymple, his step spiritless, a look of perplexity and
pain on his young and attractive face. He had been saying farewell
to Mary Lynn, and he felt, in his despairing heart, that it must be for
life. Just a hint he whispered to her of the worst—that he had been
heedless and reckless, and was ruined; but, woman-like, fond and
confiding, she had told him she never would believe it, and if it was
so, there existed all the more reason for her clinging to him.
Ah, if it only might be! If the prospect just suggested to him by
that good man, Francis Grubb, might only be realized! If he could
pull up at any cost, and enter upon a peaceful life! If! None knew
better than himself that there was no chance of it. All he had was
gone—and, had not Mr. Grubb left it to his honour?

Robert Dalrymple was ruined. Bitterly was the fact impressing


itself upon him, as he walked there under the summer sunlight. Not
only were all his available funds spent, but he had entered into
liabilities thick and threefold, far beyond what the rent-roll at the
Grange would be sufficient to meet. He had told Oscar Dalrymple
this very morning that he did not play much the previous night.
Oscar did not believe it, but it was true. Why did he not play much?
Because he had nothing left to play with, and had sat, gloomy and
morose, looking on at the other players. Introduced to the evil
fascinations of play by Colonel Haughton, he was drawn on until the
unhappy mania took hold upon himself. To remain away from the
gambling table for one night would have been intolerable, for the
feverish disease was raging within him. Poor infatuated man!—poor
infatuated men, all of them, who thus lose themselves!—he was
positively still indulging a vision of success and hope. Every time that
he approached the pernicious table, it was rife within him, buoying
him up, and urging him on—that luck might turn in his favour, and
he might win the Grange back—or, rather, the money he had lost
upon it. Thus it is with all gamblers who are comparatively fresh to
the vice; only the vile old sinners such as Colonel Haughton and his
confederate, Piggott, know what such is worth. The ignis-fatuus,
delusive hope, beckoning ever onwards, lures them to their
destruction. Pandora's box, you know, contained every imaginable
evil, but Hope lay at the bottom. Even now, as Robert is walking to
South Audley Street, a feverish gleam of hope is positively rising up
within him. If he had only money to go to the tables that night, who
knew but luck might turn, and he could extricate himself from his
most pressing debts, and so be able to tell the whole truth to Mr.
Grubb?—and how carefully he would avoid all evil in future, when
Mary should be his wife! But—where was the use of conjuring up
these fantastic visions, he asked himself, as he flung himself into a
chair in his sitting-room, when he had no money to stake?

Everything was gone, every available thing; he had nothing left


but the watch he had about him, and the ring he wore—and a few
loose shillings in his pocket. Nothing whatever, in the house, or out
of it.

Yes, he had, but it was not his. Farmer Lee, wishing to invest a
few hundred pounds in the funds, had prayed his young landlord to
transact the business for him, and save him a journey to London.
Robert good-naturedly acquiesced. Had any man told him he could
touch that money for his own purposes, he would have knocked the
offender down in his indignation. The cheque, for the money to be
transferred, had come from Mr. Lee that morning. There it lay now,
on the table at his elbow, and there sat Robert, striving to turn his
covetous eyes from it, yet unable, for it was beginning to bear for
him the fascination of the basilisk. He wished it was in the midst of
some blazing fire, rather than lying there to tempt him. For the
notion had seized upon his mind that it was with this money, if he
might dare to stake it, he might win back a portion of what he had
lost. With a shudder he shook off the idea, and looked at his watch.
Was it too late to take the cheque to its destination? Yes, it was; the
afternoon was waning, and business places would be closed. Robert
felt half inclined to hand it to Reuben, and tell him to keep it in
safety.

While in this frame of mind, that choice friend of his, Mr. Piggott,
honoured him with a call. Whether that worthy gentleman scented
the presence of the cheque, or heard of it casually from Robert, who
was candid to a fault, certain it was that he did not leave Robert
afterwards, but sat with him until the dinner-hour, and then took him
out to dine. Robert locked up the cheque in his desk before he went.

About eleven o'clock he came home again, heated with wine.


Opening his desk, he snatched out the cheque and hid it away in his
breast-pocket, as if it were something he had a horror of looking at.
Piggott and Colonel Haughton had plied him with something besides
wine; alluring hopes. Turning to leave the room, buttoning his coat
over what it contained, he saw Reuben standing there.

"Mr. Robert!—do not go out again tonight."

Robert stared at the man.

"Sir, I carried you in my arms when you were a child; your father,
the very day he died, told me to give you a word of warning, if I saw
you going wrong; let that be my excuse for speaking to you as you
may think I have no right to do," pleaded Reuben, the tears standing
in his faithful old eyes. "Do not go out again, sir; for this night, at
any rate, stay away from the set; they are nothing but blacklegs.
There's that Piggott waiting for you outside the door."

"Reuben, don't be a fool. How dare you say my friends are


blacklegs?"

"They are so, sir. And you are losing your substance to them; and
it won't be their fault if they don't get it all."

Robert, eager to go out to his ruin, hot with wine, would not
waste more words. He moved to the door, but Reuben moved more
quickly than he, and stood with his back against it.

"What farce is this?" cried Robert, in his temper. "Stand away from
the door, or I shall be tempted to fling you from it."

"Oh, sir, hear reason!" And the man's manner was so painfully
urgent, that a half-doubt crossed his master's mind whether he
could know what it was he was about to stake. "Three or four and
twenty years ago, Mr. Robert—I'm not sure as to a year—I stood, in
like manner, praying your uncle Claude not to go out to his ruin. He
had come to London, sir, as fine and generous a young man as you,
and the gamblers got hold of him, and drew him into their ways, and
stuck to him like a leech, till all he had was gone. Moat Grange was
played away, mortgaged, or bartered, or whatever it might be, for
the term of his life; there's a clause in its deeds, as I take it you
know, sir, that prevents its owner from encumbering it for longer—
and, perhaps, that's usual with other estates——"

"You are an idiot, Reuben," interrupted Robert, his tone less fierce.

"A night came when Mr. Claude was half mad," continued Reuben,
unheeding the interruption. "I saw he was; and I stood before him,
and prayed him not to go out with them, as I am now praying you.
It was of no use, and he went. If I tell you what that night brought
forth, sir, will you regard it as a warning?"

"What did it bring forth?" demanded Robert, arrested to interest.

"I will tell you, sir, if you will take warning by it, and break with
those gamblers this night, and never go amongst them more. Will
you promise, Mr. Robert?"

"Out of the way, Reuben!" was the impatient rejoinder. "You are
getting into your dotage. If you have nothing to tell me, let me go."

"Listen, then," cried Reuben, bending his head forward, in his


excitement. "At three o'clock that same morning, Mr. Dalrymple
returned. He had been half-mad, I say, when he went, he was
wholly mad when he came back; mad with despair and
despondency. He came in, his head down, his steps lagging, and
went into his bedroom. I went to mine, and was undressing, when
he called me back. He had got his portmanteau from against the
wall, opened it, and was standing over it, looking in, his coat and
cravat off, and the collar of his shirt unbuttoned. 'Reuben,' said he, 'I
have made up my mind to leave London, and take a journey.'

"'Down to the Grange, sir?' I asked, my heart leaping within me at


the good news.
"'No, not to the Grange this time; it's farther than that. But as I
have not informed any one of my intention I must leave a word with
you, in case I am inquired after.'

"'Am I not to attend you, sir?' I interrupted.

"'No, I shan't want you particularly,' he answered; 'you'll do more


good here. Tell all who may inquire for me, and especially my
brother' (your father, sir, you know), 'that although they may think I
did wrong to start alone on a road where I have never been, I am
obliged to do so. I cannot help myself. Tell them I deliberated upon
it before making up my mind, and that I undertake it in the
possession of all my faculties and senses.' Those were the words."

"Well," cried Robert, impatient for the end of the tale.

"I found these words somewhat strange," continued Reuben, "but


his true meaning never struck me—Oh," wailed the old man,
clasping his hands, "it never struck me. My thoughts only turned to
Scotland; for my master had been talking of going there to see a
Scotch laird, a friend of his, and I believed he had now taken a
sudden resolution to pay the visit; I thought he had pulled out his
trunk to put in some things before I packed it. I asked him when he
intended to start, and he replied that I should know all in the
morning; and I went back to my bed."

Robert sat down on the nearest chair: his eyes were strained on
Reuben. Had he a foreshadowing of what was to come?

"In the morning one of the women-servants came and woke me.
Her face startled me the moment I opened my eyes; it was white
and terror-stricken, and she asked me what that stream of red
meant that had trickled from under the door of the master's
chamber. I went there when I had put a thing or two on. Master
Robert," he added, dropping his voice to a dread whisper, his
thoughts wholly back in the past, "he had indeed gone on his long
journey."
"Was he dead?"

"He had been dead for hours. The razor was lying beside him near
the door. I have never quite got over that dreadful sight: and the
thought has always haunted me that, had I understood his meaning
properly, it might have been prevented."

"His trunk—what did he get that out for?" asked Robert, after a
pause.

"To blind me, sir—as I have believed since—while he gave the


message."

"Why did he commit the deed?" gloomily continued Robert, whom


the account seemed to have partially sobered.

"He had fallen into the clutches of the same sort of people that
you have, sir, and they had fleeced him down to beggary and
shame, and he had not the resolution to leave them, and face the
poverty; that was why he did it. His worst enemy was Captain
Haughton. He is Colonel Haughton now."

"What do you mean?" cried Robert Dalrymple, after a pause of


astonishment.

"Yes, sir, the same man. He is your evil genius, and he was your
uncle's before you. The last time I saw him, in the old days, was
when we both stood together over my master's dead body; he came
in, along with others. 'He must have been stark mad,' was his
exclamation. 'Perhaps so, Captain Haughton,' I answered, 'but the
guilt lies on those who drove him so.' He took my meaning, and he
slunk away out of the room. Mr. Robert," added the old man, the
tears streaming down his cheeks, "do you know what I like to fancy
—and to hope?"

Robert lifted his eyes.


"Why, that the punishment will lie with these wretched tempters,
as well as the guilt. The good God is just and merciful."

Robert did not speak. Reuben resumed.

"The first time that Haughton called here upon you, sir, I knew
him, and he knew me; and I don't think he liked it. He has never
come here himself since; I don't know whether you've noticed it, sir,
he has sent that Piggott—the man that's waiting for you outside
now. Mr. Robert, you had better have fallen into the meshes of the
Fiend himself than into that man Haughton's."

"My uncle must have been insane when he did that," broke from
Robert Dalrymple.

"The jury said otherwise," sadly answered Reuben. "They brought


it in felo-de-se; and he was buried by torchlight, without the burial-
service."

The news had told upon Robert. His mind just then was a chaos.
Nothing tangible showing out of it, save that his plight was as bad as
his uncle Claude's had been, and that he was looking, in his
infatuation, for that night to redeem it. Could he go on with his work
—with that example before him? For a while he sat thinking, his
head bent, his eyes closed; then he rose up, and signed to Reuben
to let him pass. The latter's spirit sank within him.

"Is what I have told you of no avail, Mr. Robert? Are you still bent
on going forth to those wicked men? It will be your ruin."

"It is that already, Reuben. As it was with my uncle, so it is with


me: I am ruined, and worse than ruined, and after tonight I will
know Colonel Haughton no more. But I have resolved to make one
desperate effort this night to redeem myself; something whispers to
me that I shall have luck; and—and you don't know how much lies
upon it."
He was thinking of his union with Mary Lynn, poor infatuated man.
Could he redeem himself in a degree this night, he would disclose
his position to Mr. Grubb, entreat his condonation of the past, and
forswear play for ever. A tempting prospect. Nevertheless the tale
had staggered him.

"Don't go, don't go, Mr. Robert. I ask you on my bended knees."

"Get up, Reuben! don't be foolish. Perhaps I will not go. But I
must tell Piggott: I cannot keep him waiting there all night."

Reuben could do no more. He stood aside, and his young master


went forth, hesitating.

What strange infatuation could it have been, that it should so cling


to him? Any one who has never been drawn into the fiery vortex of
gambling would have a difficulty in understanding it. Robert
Dalrymple was a desperate man, and yet a hopeful one, for this
night might lift him out of despair. Moreover, the feverish yearning
for play, in itself, was strong upon him: as it always was now at that
night hour. As yet, the penalty he had incurred was but
embarrassment and poverty: he was now about to stake what was
not his, and risk guilt. And yet, he went forth: for the dreadful vice
had got fast hold of him; and he knew that the hesitation in his mind
was but worthless hesitation; a species of sophistry.

Mr. Piggott had been cooling his heels and his patience outside,
not blessing his young friend for the unnecessary and unexpected
delay, and not doing the opposite. He was of too equable a nature to
curse and swear: he left that to his peppery partner, Haughton.

"I thought you were gone to bed," he said, when Robert


appeared: "in another minute I should have come in to see after
you."

And it was a wonder he did not go in. But Colonel Haughton had
whispered a word of caution as to Reuben, and neither of them
cared to pursue the master too persistently in the man's sight.
Robert Dalrymple spoke of his hesitation, saying he was not sure he
should play that night. He did want to keep the farce of prudence
up, even to himself.

"You have that cheque in your pocket, I suppose?" sharply


questioned Piggott.

"Yes. But——"

"Come on, then; we'll talk of it as we go along." And Robert linked


his arm within Mr. Piggott's and walked on in the direction of Jermyn
Street.

They entered the "hell." It is not a pleasant word for polite pens
and ears, but it is an exceedingly appropriate one. It was blazing
with light, and as hot as its name; and fiery countenances of
impassioned triumph, and agonized countenances of vacillating
suspense, and sullen countenances of despair were crowding there.
Colonel Haughton was in a private room: it was mostly kept for
himself and his friends, a choice knot of whom stood around. Poor
Robert's infatuation, under Mr. Piggott's able tuition, had returned
upon him. Down he sat at the green cloth, wild and eager.

"It is of no use to make fools of us," whispered Colonel Haughton.


"You know you do not possess another stiver; why take up a place?"

"Now, Haughton, you are too stringent," benevolently interposed


Mr. Piggott, laying hold of the colonel's arm, and giving it a peculiar
pinch. "Here is Dalrymple, with an impression that luck will be upon
him tonight, a conviction of it, indeed, and you are afraid of giving
him his revenge. It is his turn to win now. As to stakes, he says he
has something with him that will do."

Robert drew the cheque from his pocket, and dashed it before
Colonel Haughton. "I am prepared to stake this," he said. "Nothing
risk, nothing win. Luck must favour me tonight; even Piggott says
so, and he knows how bad it has been."

Colonel Haughton ran his spectacles over the cheque. "I see," he
said: "it will do. The risking it is your business, not ours."

"Of course it is mine," answered Robert.

"Then put your signature to it. Here by the side of the other."

It was done, and they sat down to play. "Nothing risk, nothing
win," Robert had said; he had better have said, "Nothing risk,
nothing lose;" and have acted upon it. A little past midnight, he went
staggering out of that house, a doomed man. All was over, all lost.
Farmer Lee's money, or the cheque representing it, had passed out
of his possession, and he was a criminal. A criminal in the sight of
himself, soon to be a criminal in the sight of the world; liable to be
arrested and tried at the bar of Justice, a common felon.

He had tasted nothing since he entered, yet he reeled about the


pavement as one who is the worse for drink. What was to become of
him? Involuntarily the fate his unfortunate uncle Claude had resorted
to came across his mind: nay, it had not been away from it. Even in
the mad turmoil of that last hour, when the suspense was awful to
bear, and hope and dread had fought with each other as a meeting
whirlwind, the facts of that dark history had been thrusting
themselves forward.

His face was burning without, and his brain was burning within. It
was a remarkably windy night, and he took off his hat and suffered
the breeze to blow on his miserable brow. And so he paced the
streets, going from home, not to it. Where could he go? he with the
brand of crime and shame upon him? He got to Charing Cross, and
there he halted, and listened to the different clocks striking one.
Should he turn back to South Audley Street? And encounter Reuben,
who had tried to save him, and had failed? And go to bed, and wait,
with what calmness he might, till the law claimed him? Hardly.
Anywhere but home. The breeze was stronger now: it blew from the
direction of the water. Robert Dalrymple replaced his hat, pulled it
firmly on his head to hide his eyes from the night, and dragged his
steps towards Westminster Bridge.

Of all places in the world!—the bridge and the tempting stream!—


what evil power impelled him thither?

CHAPTER VIII.

PERVERSITY.

In the bed of a large and luxurious chamber, her delicate face


pressing the pillow, her eyes closed to the shaded light, lay Lady
Adela Grubb. The baby she so wished for had come at last. Not that
it was the baby itself she wanted, but that she might be at liberty
through renewed health to mingle with the great world again. To be
deprived of its gaiety and obliged to keep herself very much at home
had been to her a species of intolerable thraldom.

The baby was born on Friday night: a few hours subsequent to


Robert Dalrymple's interview with Mr. Grubb and Mary Lynn. Mary,
only in Grosvenor Square for the afternoon, returned to Blackheath
unconscious of the close approach of the event. The illness had been
a favourable one; and Adela, on this Sunday morning, was going on
well towards recovery. She had taken her breakfast, and was ready
to see her husband. The doctor had only now gone out.
A wee cry from the cradle caused her to open her eyes. An elderly
woman, with soft step, bent over the cradle, and would have hushed
the baby to sleep again.

"Put him here, nurse. I want to look at him."

The nurse took up the white bundle, and laid it in the great bed,
beside Lady Adela. The little pale face was turned to her; for it was a
pale face, not a red one; and she lay looking at it. The child opened
its eyes: and, young though it was, one could see it had the
beautiful grey-blue eyes of its father. Her own brilliant yet soft brown
eyes grew fond as she gazed on the still face.

"Is he quite healthy, nurse?" she suddenly asked.

For the space of half a moment the nurse hesitated. "He was born
quite healthy, my lady; but I think he might get on better if you
nursed him. Some infants require their mother more than others do.
I suspect this one does."

She made no reply; except by an all but imperceptible toss of the


head: one can't toss effectively lying down. There had been some
trouble with Lady Adela on the score of nursing the child. Nothing
would induce her to do it. It would be well for her and well for the
little one, Dr. Dove had said. Adela would not listen. Her mother,
Lady Acorn, had treated her to a sharp scolding the day before,
Saturday, and told her she was "unnatural." All the same: Adela
indignantly demanded whether they thought she should give up the
season for any infant in the world. She was also obstinate on
another score—she would not allow, would not hear of, a nurse
being sought to supply her place. And there she lay this morning:
her own head on one pillow, the child's on another. One of the
windows was open behind the drawn blind, admitting a breath of the
warm June air. On a stand at Lady Adela's elbow lay a bouquet of
sweet-scented, lovely hot-house flowers.
"Little wee thing!" she fondly cried, stretching out her fingers to
stroke the baby's soft face, and its fragile hand that lay so still.

A tap at the door. The nurse answered it and admitted Mr. Grubb;
she herself then retiring to the next room, which opened from this
one. He came to the bed, bent over his wife and gently kissed her.

"Oh, don't!" she cried, turning her cheek ungraciously from him,
just as she had for the most part done ever since their wedding-day.
It had grown into a habit now.

"Adela," he whispered, biting his trembling lips to keep down the


pain, "should not this little treasure, our child, teach you to be more
of a loving wife to me?"

"I am very sorry it has come," she answered in fretful tones. "I'm
sure I shall be if they are going to worry me over it. You should hear
mamma go on:—and Grace, too!—with their old-fashioned notions."

"No one shall worry you," he fondly said. "Tell me, Adela, what
you would like his name to be?"

"His name!" she repeated, looking up in quick surprise. "Time


enough for that."

"Dr. Dove thinks it may be as well to have him baptized. He came


into the library just now, as he went out; and, in talking of one thing
and another, he chanced to mention this." Chanced to mention this!
Mr. Grubb was cautious not to alarm his wife.

"The baby is not ill! Is it?"

"No, no, I trust not, Adela. It is a delicate little thing; all babies
are, perhaps: and—and it is as well, you know, to be on the safe
side."
"But I should like a christening. A grand, proper christening; to be
held when I get well."

"Of course. His being baptized now will make no difference to


that. I think it must be done, my dear."

"In this room, then; by my bedside. I should like to see it."

"You shall. And now, what name?"

Adela lay back on the pillow, her cheeks slightly flushed with their
delicate pink, fresh and pure as the hue of a seashell, her eyes cast
upwards in thought.

"I should like it to have papa's name—George."

"George Frederick?"

"Not Frederick: I don't care about the name. George—would you


like also your own name—Francis?" she broke off to ask. "George
Francis?"

"Would you care to have it Francis?" he returned, his tone one of


emotion, bending over her until his face nearly touched hers.

She heard the tone, she saw the wet eyelashes shading the
wonderful grey eyes, with their yearning, earnest expression. It
flashed into her mind to remember how few men were his equals, in
looks, in worth, in loving indulgence to a rebellious wife. Adela was
not quite proof against her better nature. She was not always hard.

"Yes, I should; and he has your eyes," she whispered softly, in


answer to the question, her own sweet eyes lifted to her husband's.

"Adela," he breathed, his voice low with its agitation, "you do love
me a little! You surely do!"
"Just a very little—sometimes," she whispered in a half-saucy,
half-loving tone. And, when he let his face fall on hers, she for once
held it there, and welcomed the kisses from his lips.

It was all the work of the baby, his child and hers, thought he in
his glad heart. But no. Now and again, at rare intervals, Adela did
feel a spark of tenderness for him: though instead of letting it come
to fruit, of allowing him to see it, she forced it back to the coldness
she had taken up, and resolutely steeled her heart against him.
Illness had just now somewhat softened her spirit.

He went round the bed to the side where the baby lay, and looked
at it long and earnestly. The doctor had just told him that he did not
feel altogether easy on the score of the child; could not be sure that
it was likely to live.

"It is a pale little blossom, Adela. I thought babies were generally


red."

"Frightfully red. I have seen them."

"Well, we will get it baptized; and then——"

"What?" she cried—for he had stopped.

"And then, I was going to say, whether it lives or dies, it will be


safe in its Saviour's arms."

"But you do not think it will die?" she cried, taking up some alarm.
"Oh, Francis, I should not like him to die, now he has come!"

He went round to soothe her, the word "Francis" causing his heart
to leap. For in a general way she persistently called him "Mr. Grubb,"
and not graciously either.

"My darling, I assure you there is no cause for alarm. So far as I


know, the child is not ill; it will, I hope, do well. Dr. Dove does not
think him particularly strong—but what can be expected of a two-
day-old baby?"

"True," answered Adela, feeling reassured again. "Francis, I do


believe there's mamma coming up! Yes, it is her voice. Mind you
don't tell her——"

Lady Acorn came swiftly in; and, what he was not to tell her, Mr.
Grubb never knew. She had dressed early for church, and came
round to see Adela on her way to it. Grace was with her. One of the
daughters had married during the past year, but it was not Grace. It
was Harriet; she had espoused a little Scotch laird, Sir Sandy
MacIvor. Peppery and red, in came the countess, for she had just
heard something that vexed her; Lady Grace, so calm and still,
presented a contrast to her vivacious mother.

"Well, and now what's this I hear about things not going on well?"
began Lady Acorn, subduing her voice with difficulty to the
requisition of a sick-room.

"I am going on very well, mamma—how do you mean?" returned


Adela, assuming the doubt must apply to herself. "I have made a
famous breakfast. They let me have an egg and some buttered
toast."

"You are all right, Dove says—we have just met him," returned
Lady Acorn. "But he does not think the baby is. And you have
yourself to thank for it, Adela."

The pink tinge on Lady Adela's cheeks increased to rose colour, as


she armed herself to do battle with her mother.

"Dove says the baby wants its proper food; not that gruel stuff, or
milk-and-water, or whatever rubbish it is, that it is being dosed with.
And it is not too late for you to reform, Adela, and do what you
ought."
"It is too late," retorted Adela, with flaming cheeks. "And if you
begin about it again, mamma, you will make me ill. Francis"—
stretching out her arm for her husband—"don't let me be worried.
You promised me, you know."

With a loving word to his wife, a reassuring pressure of her hand,


which he kept in his, he turned to Lady Acorn, and spoke to her in a
low tone.

"Talk to her when she's better and more able to bear it!" repeated
the countess, taking up his words aloud. "Why, my good man, it
would be too late. And—you do not want to lose your child, I
suppose!"

"Indeed, I do not. But, better lose my child than my wife."

"She is well enough, and safe enough," spoke the mother, secure
in her superior knowledge. "Adela has been an indulged girl all her
life, and you, her husband, continue the indulgence. It is not good
for her; mark you that. With regard to this caprice of hers, the not
undertaking the poor sickly baby, you ought to hold her to her duty,
Mr. Grubb, and insist upon her fulfilling it."

He turned to his wife, his eyes unconsciously wearing a pleading


look. "If you would only suffer yourself to be persuaded, Adela! For
the child's sake."

Adela looked at them separately; at her husband, at her mother,


at Grace, standing with a cold and impassive countenance that did
not betoken approbation; and she took up an idea that they were in
league with one another to "hold her to her duty," and enforce
obedience. Had not the doctor talked to her that very morning: had
not the nurse subsequently presumed to hint at an opinion? Yes,
they were all in league together. Lady Adela turned rebellious, and
flung her husband's hand away with passionate anger.
"Why do you come into my room at all?" she exclaimed to him.
"You know I do not want you."

At that moment the nurse looked in from the adjoining apartment


and made a sign to Mr. Grubb. He obeyed it at once, taking no
notice of his wife or her cruel words.

"There! you have driven him away now!" cried Lady Acorn, on the
eve of an explosion: for she had not seen the summons of the
nurse. "You will never go to heaven, Adela, for your wickedness to
your husband."

Adela did not make any answer: perhaps she was feeling a little
sorry in her heart: and there ensued a silence. The sweet-toned
bells, calling people to service, rang out on the air.

Mr. Grubb came in again. Feeling more alarmed in his heart at the
doctor's words than he allowed to appear, and anxious for the child,
he had written a note as the medical man left him, and sent it to a
young assistant clergyman whose lodgings were close by. He had
now called, on his way to church, ready to perform the ceremony at
once if it were wished for, and a servant had come up to inform the
nurse.

"Mr. Wilkinson has called, and is asking after you," began Mr.
Grubb to his wife, voice and demeanour a model of quietness, not to
say indifference. "It struck me, Adela, that he might as well baptize
the child—as he is here. He has time to do it before service."

"What a hurry you are in!" she returned, ungraciously.

"As well take the opportunity of his being here, Adela. And then it
will be over."

"Oh, well, yes—if it has to be done," conceded she. "I'm sure


there's no necessity for it. Let Wilkinson come up."
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like