Skip to content

Commit 9153231

Browse files
Improve and Simplify the Getting Started Guide
- Refined the sample code implementation to keep it minimal, helping users better understand the core concepts. - Streamlined the guide by focusing on the main information and removing extraneous details. - Ensured that the code and samples in the guide are consistent with the generated project. - Simplified navigation by hiding the full code, making it easier for users to follow along. - Replaced the use of the deploy image plugin with direct commands to provide more comprehensive and straightforward information.
1 parent f3f07ae commit 9153231

File tree

7 files changed

+460
-466
lines changed

7 files changed

+460
-466
lines changed

docs/book/src/getting-started.md

Lines changed: 175 additions & 330 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
// +kubebuilder:docs-gen:collapse=Apache License
17+
18+
/*
19+
*/
1620

1721
package v1alpha1
1822

@@ -23,6 +27,8 @@ import (
2327
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2428
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2529

30+
// +kubebuilder:docs-gen:collapse=Imports
31+
2632
// MemcachedSpec defines the desired state of Memcached
2733
type MemcachedSpec struct {
2834
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
@@ -35,9 +41,6 @@ type MemcachedSpec struct {
3541
// +kubebuilder:validation:Maximum=3
3642
// +kubebuilder:validation:ExclusiveMaximum=false
3743
Size int32 `json:"size,omitempty"`
38-
39-
// Port defines the port that will be used to init the container with the image
40-
ContainerPort int32 `json:"containerPort,omitempty"`
4144
}
4245

4346
// MemcachedStatus defines the observed state of Memcached

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ spec:
3939
spec:
4040
description: MemcachedSpec defines the desired state of Memcached
4141
properties:
42-
containerPort:
43-
description: Port defines the port that will be used to init the container
44-
with the image
45-
format: int32
46-
type: integer
4742
size:
4843
description: |-
4944
Size defines the number of Memcached instances

docs/book/src/getting-started/testdata/project/config/samples/cache_v1alpha1_memcached.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ spec:
77
# of Pods/Instances your Operand must have on cluster
88
size: 1
99

10-
# TODO(user): edit the following value to ensure the container has the right port to be initialized
11-
containerPort: 11211

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

Lines changed: 16 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
// +kubebuilder:docs-gen:collapse=Apache License
1617

1718
package controller
1819

@@ -33,13 +34,16 @@ import (
3334
"k8s.io/client-go/tools/record"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
36-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
37+
3738
"sigs.k8s.io/controller-runtime/pkg/log"
3839

3940
cachev1alpha1 "example.com/memcached/api/v1alpha1"
4041
)
4142

42-
const memcachedFinalizer = "cache.example.com/finalizer"
43+
// +kubebuilder:docs-gen:collapse=Import
44+
45+
/*
46+
*/
4347

4448
// Definitions to manage status conditions
4549
const (
@@ -66,6 +70,7 @@ type MemcachedReconciler struct {
6670
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
6771
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
6872
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch
73+
// +kubebuilder:docs-gen:collapse=Markers
6974

7075
// Reconcile is part of the main kubernetes reconciliation loop which aims to
7176
// move the current state of the cluster closer to the desired state.
@@ -117,79 +122,6 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
117122
}
118123
}
119124

120-
// Let's add a finalizer. Then, we can define some operations which should
121-
// occur before the custom resource is deleted.
122-
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/finalizers
123-
if !controllerutil.ContainsFinalizer(memcached, memcachedFinalizer) {
124-
log.Info("Adding Finalizer for Memcached")
125-
if ok := controllerutil.AddFinalizer(memcached, memcachedFinalizer); !ok {
126-
log.Error(err, "Failed to add finalizer into the custom resource")
127-
return ctrl.Result{Requeue: true}, nil
128-
}
129-
130-
if err = r.Update(ctx, memcached); err != nil {
131-
log.Error(err, "Failed to update custom resource to add finalizer")
132-
return ctrl.Result{}, err
133-
}
134-
}
135-
136-
// Check if the Memcached instance is marked to be deleted, which is
137-
// indicated by the deletion timestamp being set.
138-
isMemcachedMarkedToBeDeleted := memcached.GetDeletionTimestamp() != nil
139-
if isMemcachedMarkedToBeDeleted {
140-
if controllerutil.ContainsFinalizer(memcached, memcachedFinalizer) {
141-
log.Info("Performing Finalizer Operations for Memcached before delete CR")
142-
143-
// Let's add here a status "Downgrade" to reflect that this resource began its process to be terminated.
144-
meta.SetStatusCondition(&memcached.Status.Conditions, metav1.Condition{Type: typeDegradedMemcached,
145-
Status: metav1.ConditionUnknown, Reason: "Finalizing",
146-
Message: fmt.Sprintf("Performing finalizer operations for the custom resource: %s ", memcached.Name)})
147-
148-
if err := r.Status().Update(ctx, memcached); err != nil {
149-
log.Error(err, "Failed to update Memcached status")
150-
return ctrl.Result{}, err
151-
}
152-
153-
// Perform all operations required before removing the finalizer and allow
154-
// the Kubernetes API to remove the custom resource.
155-
r.doFinalizerOperationsForMemcached(memcached)
156-
157-
// TODO(user): If you add operations to the doFinalizerOperationsForMemcached method
158-
// then you need to ensure that all worked fine before deleting and updating the Downgrade status
159-
// otherwise, you should requeue here.
160-
161-
// Re-fetch the memcached Custom Resource before updating the status
162-
// so that we have the latest state of the resource on the cluster and we will avoid
163-
// raising the error "the object has been modified, please apply
164-
// your changes to the latest version and try again" which would re-trigger the reconciliation
165-
if err := r.Get(ctx, req.NamespacedName, memcached); err != nil {
166-
log.Error(err, "Failed to re-fetch memcached")
167-
return ctrl.Result{}, err
168-
}
169-
170-
meta.SetStatusCondition(&memcached.Status.Conditions, metav1.Condition{Type: typeDegradedMemcached,
171-
Status: metav1.ConditionTrue, Reason: "Finalizing",
172-
Message: fmt.Sprintf("Finalizer operations for custom resource %s name were successfully accomplished", memcached.Name)})
173-
174-
if err := r.Status().Update(ctx, memcached); err != nil {
175-
log.Error(err, "Failed to update Memcached status")
176-
return ctrl.Result{}, err
177-
}
178-
179-
log.Info("Removing Finalizer for Memcached after successfully perform the operations")
180-
if ok := controllerutil.RemoveFinalizer(memcached, memcachedFinalizer); !ok {
181-
log.Error(err, "Failed to remove finalizer for Memcached")
182-
return ctrl.Result{Requeue: true}, nil
183-
}
184-
185-
if err := r.Update(ctx, memcached); err != nil {
186-
log.Error(err, "Failed to remove finalizer for Memcached")
187-
return ctrl.Result{}, err
188-
}
189-
}
190-
return ctrl.Result{}, nil
191-
}
192-
193125
// Check if the deployment already exists, if not create a new one
194126
found := &appsv1.Deployment{}
195127
err = r.Get(ctx, types.NamespacedName{Name: memcached.Name, Namespace: memcached.Namespace}, found)
@@ -282,27 +214,15 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
282214
return ctrl.Result{}, nil
283215
}
284216

285-
// finalizeMemcached will perform the required operations before delete the CR.
286-
func (r *MemcachedReconciler) doFinalizerOperationsForMemcached(cr *cachev1alpha1.Memcached) {
287-
// TODO(user): Add the cleanup steps that the operator
288-
// needs to do before the CR can be deleted. Examples
289-
// of finalizers include performing backups and deleting
290-
// resources that are not owned by this CR, like a PVC.
291-
292-
// Note: It is not recommended to use finalizers with the purpose of deleting resources which are
293-
// created and managed in the reconciliation. These ones, such as the Deployment created on this reconcile,
294-
// are defined as dependent of the custom resource. See that we use the method ctrl.SetControllerReference.
295-
// to set the ownerRef which means that the Deployment will be deleted by the Kubernetes API.
296-
// More info: https://kubernetes.io/docs/tasks/administer-cluster/use-cascading-deletion/
297-
298-
// The following implementation will raise an event
299-
r.Recorder.Event(cr, "Warning", "Deleting",
300-
fmt.Sprintf("Custom Resource %s is being deleted from the namespace %s",
301-
cr.Name,
302-
cr.Namespace))
303-
}
217+
// deploymentForMemcached returns a Memcached Deployment object
218+
/*
219+
220+
Note that we are defining the Deployment with our Memcached Image.
221+
If not created on the Cluster our reconciliation will create it.
304222
223+
*/
305224
// deploymentForMemcached returns a Memcached Deployment object
225+
306226
func (r *MemcachedReconciler) deploymentForMemcached(
307227
memcached *cachev1alpha1.Memcached) (*appsv1.Deployment, error) {
308228
ls := labelsForMemcached(memcached.Name)
@@ -329,34 +249,7 @@ func (r *MemcachedReconciler) deploymentForMemcached(
329249
Labels: ls,
330250
},
331251
Spec: corev1.PodSpec{
332-
// TODO(user): Uncomment the following code to configure the nodeAffinity expression
333-
// according to the platforms which are supported by your solution. It is considered
334-
// best practice to support multiple architectures. build your manager image using the
335-
// makefile target docker-buildx. Also, you can use docker manifest inspect <image>
336-
// to check what are the platforms supported.
337-
// More info: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity
338-
//Affinity: &corev1.Affinity{
339-
// NodeAffinity: &corev1.NodeAffinity{
340-
// RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
341-
// NodeSelectorTerms: []corev1.NodeSelectorTerm{
342-
// {
343-
// MatchExpressions: []corev1.NodeSelectorRequirement{
344-
// {
345-
// Key: "kubernetes.io/arch",
346-
// Operator: "In",
347-
// Values: []string{"amd64", "arm64", "ppc64le", "s390x"},
348-
// },
349-
// {
350-
// Key: "kubernetes.io/os",
351-
// Operator: "In",
352-
// Values: []string{"linux"},
353-
// },
354-
// },
355-
// },
356-
// },
357-
// },
358-
// },
359-
//},
252+
360253
SecurityContext: &corev1.PodSecurityContext{
361254
RunAsNonRoot: &[]bool{true}[0],
362255
// IMPORTANT: seccomProfile was introduced with Kubernetes 1.19
@@ -383,7 +276,7 @@ func (r *MemcachedReconciler) deploymentForMemcached(
383276
},
384277
},
385278
Ports: []corev1.ContainerPort{{
386-
ContainerPort: memcached.Spec.ContainerPort,
279+
ContainerPort: 11211,
387280
Name: "memcached",
388281
}},
389282
Command: []string{"memcached", "-m=64", "-o", "modern", "-v"},

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ var _ = Describe("Memcached controller", func() {
7575
Namespace: namespace.Name,
7676
},
7777
Spec: cachev1alpha1.MemcachedSpec{
78-
Size: 1,
79-
ContainerPort: 11211,
78+
Size: 1,
8079
},
8180
}
8281

0 commit comments

Comments
 (0)