Skip to content

Commit afcbd03

Browse files
authored
Merge pull request #4915 from camilamacedo86/update-getting-started-sample-comply-k8s-api-convetions
📖 Update Getting Started sample to comply with K8s API conventions
2 parents fb53f01 + 5a1a35e commit afcbd03

File tree

8 files changed

+41
-24
lines changed

8 files changed

+41
-24
lines changed

docs/book/src/getting-started/testdata/project/api/v1alpha1/memcached_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ type MemcachedSpec struct {
3333
// The following markers will use OpenAPI v3 schema to validate the value
3434
// More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
3535

36-
// Size defines the number of Memcached instances
36+
// size defines the number of Memcached instances
3737
// The following markers will use OpenAPI v3 schema to validate the value
3838
// More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
3939
// +kubebuilder:validation:Minimum=1
4040
// +kubebuilder:validation:Maximum=3
4141
// +kubebuilder:validation:ExclusiveMaximum=false
42-
Size int32 `json:"size,omitempty"`
42+
// +optional
43+
Size *int32 `json:"size,omitempty"`
4344
}
4445

4546
// MemcachedStatus defines the observed state of Memcached.

docs/book/src/getting-started/testdata/project/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/src/getting-started/testdata/project/config/crd/bases/cache.example.com_memcacheds.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ spec:
4141
properties:
4242
size:
4343
description: |-
44-
Size defines the number of Memcached instances
44+
size defines the number of Memcached instances
4545
The following markers will use OpenAPI v3 schema to validate the value
4646
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
4747
format: int32

docs/book/src/getting-started/testdata/project/dist/chart/templates/crd/cache.example.com_memcacheds.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ spec:
4747
properties:
4848
size:
4949
description: |-
50-
Size defines the number of Memcached instances
50+
size defines the number of Memcached instances
5151
The following markers will use OpenAPI v3 schema to validate the value
5252
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
5353
format: int32

docs/book/src/getting-started/testdata/project/dist/install.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ spec:
4949
properties:
5050
size:
5151
description: |-
52-
Size defines the number of Memcached instances
52+
size defines the number of Memcached instances
5353
The following markers will use OpenAPI v3 schema to validate the value
5454
More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
5555
format: int32

docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controller
1919
import (
2020
"context"
2121
"fmt"
22-
2322
"time"
2423

2524
appsv1 "k8s.io/api/apps/v1"
@@ -149,13 +148,18 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
149148
return ctrl.Result{}, err
150149
}
151150

151+
// If the size is not defined in the Custom Resource then we will set the desired replicas to 0
152+
var desiredReplicas int32 = 0
153+
if memcached.Spec.Size != nil {
154+
desiredReplicas = *memcached.Spec.Size
155+
}
156+
152157
// The CRD API defines that the Memcached type have a MemcachedSpec.Size field
153158
// to set the quantity of Deployment instances to the desired state on the cluster.
154159
// Therefore, the following code will ensure the Deployment size is the same as defined
155160
// via the Size spec of the Custom Resource which we are reconciling.
156-
size := memcached.Spec.Size
157-
if *found.Spec.Replicas != size {
158-
found.Spec.Replicas = &size
161+
if found.Spec.Replicas == nil || *found.Spec.Replicas != desiredReplicas {
162+
found.Spec.Replicas = ptr.To(desiredReplicas)
159163
if err = r.Update(ctx, found); err != nil {
160164
log.Error(err, "Failed to update Deployment",
161165
"Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
@@ -191,7 +195,7 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
191195
// The following implementation will update the status
192196
meta.SetStatusCondition(&memcached.Status.Conditions, metav1.Condition{Type: typeAvailableMemcached,
193197
Status: metav1.ConditionTrue, Reason: "Reconciling",
194-
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, size)})
198+
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, desiredReplicas)})
195199

196200
if err := r.Status().Update(ctx, memcached); err != nil {
197201
log.Error(err, "Failed to update Memcached status")
@@ -213,7 +217,6 @@ func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error {
213217
// deploymentForMemcached returns a Memcached Deployment object
214218
func (r *MemcachedReconciler) deploymentForMemcached(
215219
memcached *cachev1alpha1.Memcached) (*appsv1.Deployment, error) {
216-
replicas := memcached.Spec.Size
217220
image := "memcached:1.6.26-alpine3.19"
218221

219222
dep := &appsv1.Deployment{
@@ -222,7 +225,7 @@ func (r *MemcachedReconciler) deploymentForMemcached(
222225
Namespace: memcached.Namespace,
223226
},
224227
Spec: appsv1.DeploymentSpec{
225-
Replicas: &replicas,
228+
Replicas: memcached.Spec.Size,
226229
Selector: &metav1.LabelSelector{
227230
MatchLabels: map[string]string{"app.kubernetes.io/name": "project"},
228231
},

docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121

2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
24+
2425
appsv1 "k8s.io/api/apps/v1"
2526
"k8s.io/apimachinery/pkg/api/errors"
2627
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/utils/ptr"
2729
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2830

2931
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -53,7 +55,7 @@ var _ = Describe("Memcached Controller", func() {
5355
Namespace: "default",
5456
},
5557
Spec: cachev1alpha1.MemcachedSpec{
56-
Size: 1,
58+
Size: ptr.To(int32(1)),
5759
},
5860
}
5961
Expect(k8sClient.Create(ctx, resource)).To(Succeed())

hack/docs/internal/getting-started/generate_getting_started.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func (sp *Sample) updateControllerTest() {
5454
filepath.Join(sp.ctx.Dir, file),
5555
". \"github.com/onsi/gomega\"",
5656
`. "github.com/onsi/gomega"
57+
58+
"k8s.io/utils/ptr"
5759
appsv1 "k8s.io/api/apps/v1"`,
5860
)
5961
hackutils.CheckError("add imports apis", err)
@@ -62,7 +64,7 @@ func (sp *Sample) updateControllerTest() {
6264
filepath.Join(sp.ctx.Dir, file),
6365
"// TODO(user): Specify other spec details if needed.",
6466
`Spec: cachev1alpha1.MemcachedSpec{
65-
Size: 1,
67+
Size: ptr.To(int32(1)),
6668
},`,
6769
)
6870
hackutils.CheckError("add spec apis", err)
@@ -244,13 +246,14 @@ func (sp *Sample) CodeGen() {
244246

245247
const (
246248
oldSpecAPI = "// foo is an example field of Memcached. Edit memcached_types.go to remove/update\n\t// +optional\n\tFoo *string `json:\"foo,omitempty\"`"
247-
newSpecAPI = `// Size defines the number of Memcached instances
249+
newSpecAPI = `// size defines the number of Memcached instances
248250
// The following markers will use OpenAPI v3 schema to validate the value
249251
// More info: https://book.kubebuilder.io/reference/markers/crd-validation.html
250252
// +kubebuilder:validation:Minimum=1
251253
// +kubebuilder:validation:Maximum=3
252254
// +kubebuilder:validation:ExclusiveMaximum=false
253-
Size int32 ` + "`json:\"size,omitempty\"`"
255+
// +optional
256+
Size *int32 ` + "`json:\"size,omitempty\"`"
254257
)
255258

256259
const oldStatusAPI = `// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
@@ -278,7 +281,6 @@ const sampleSizeFragment = `# TODO(user): edit the following value to ensure the
278281

279282
const controllerImports = `"context"
280283
"fmt"
281-
282284
"time"
283285
284286
appsv1 "k8s.io/api/apps/v1"
@@ -387,13 +389,18 @@ const controllerReconcileImplementation = `// Fetch the Memcached instance
387389
return ctrl.Result{}, err
388390
}
389391
392+
// If the size is not defined in the Custom Resource then we will set the desired replicas to 0
393+
var desiredReplicas int32 = 0
394+
if memcached.Spec.Size != nil {
395+
desiredReplicas = *memcached.Spec.Size
396+
}
397+
390398
// The CRD API defines that the Memcached type have a MemcachedSpec.Size field
391399
// to set the quantity of Deployment instances to the desired state on the cluster.
392400
// Therefore, the following code will ensure the Deployment size is the same as defined
393401
// via the Size spec of the Custom Resource which we are reconciling.
394-
size := memcached.Spec.Size
395-
if *found.Spec.Replicas != size {
396-
found.Spec.Replicas = &size
402+
if found.Spec.Replicas == nil || *found.Spec.Replicas != desiredReplicas {
403+
found.Spec.Replicas = ptr.To(desiredReplicas)
397404
if err = r.Update(ctx, found); err != nil {
398405
log.Error(err, "Failed to update Deployment",
399406
"Deployment.Namespace", found.Namespace, "Deployment.Name", found.Name)
@@ -429,7 +436,7 @@ const controllerReconcileImplementation = `// Fetch the Memcached instance
429436
// The following implementation will update the status
430437
meta.SetStatusCondition(&memcached.Status.Conditions, metav1.Condition{Type: typeAvailableMemcached,
431438
Status: metav1.ConditionTrue, Reason: "Reconciling",
432-
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, size)})
439+
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", memcached.Name, desiredReplicas)})
433440
434441
if err := r.Status().Update(ctx, memcached); err != nil {
435442
log.Error(err, "Failed to update Memcached status")
@@ -439,7 +446,6 @@ const controllerReconcileImplementation = `// Fetch the Memcached instance
439446
const controllerDeploymentFunc = `// deploymentForMemcached returns a Memcached Deployment object
440447
func (r *MemcachedReconciler) deploymentForMemcached(
441448
memcached *cachev1alpha1.Memcached) (*appsv1.Deployment, error) {
442-
replicas := memcached.Spec.Size
443449
image := "memcached:1.6.26-alpine3.19"
444450
445451
dep := &appsv1.Deployment{
@@ -448,7 +454,7 @@ func (r *MemcachedReconciler) deploymentForMemcached(
448454
Namespace: memcached.Namespace,
449455
},
450456
Spec: appsv1.DeploymentSpec{
451-
Replicas: &replicas,
457+
Replicas: memcached.Spec.Size,
452458
Selector: &metav1.LabelSelector{
453459
MatchLabels: map[string]string{"app.kubernetes.io/name": "project"},
454460
},

0 commit comments

Comments
 (0)