@@ -19,8 +19,7 @@ package controller
1919import (
2020 "context"
2121 "fmt"
22- "os"
23- "strings"
22+
2423 "time"
2524
2625 appsv1 "k8s.io/api/apps/v1"
@@ -33,14 +32,12 @@ import (
3332 "k8s.io/client-go/tools/record"
3433 ctrl "sigs.k8s.io/controller-runtime"
3534 "sigs.k8s.io/controller-runtime/pkg/client"
36- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
35+
3736 "sigs.k8s.io/controller-runtime/pkg/log"
3837
3938 cachev1alpha1 "example.com/memcached/api/v1alpha1"
4039)
4140
42- const memcachedFinalizer = "cache.example.com/finalizer"
43-
4441// Definitions to manage status conditions
4542const (
4643 // typeAvailableMemcached represents the status of the Deployment reconciliation
@@ -117,79 +114,6 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
117114 }
118115 }
119116
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-
193117 // Check if the deployment already exists, if not create a new one
194118 found := & appsv1.Deployment {}
195119 err = r .Get (ctx , types.NamespacedName {Name : memcached .Name , Namespace : memcached .Namespace }, found )
@@ -282,37 +206,12 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
282206 return ctrl.Result {}, nil
283207}
284208
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- }
304-
305209// deploymentForMemcached returns a Memcached Deployment object
306210func (r * MemcachedReconciler ) deploymentForMemcached (
307211 memcached * cachev1alpha1.Memcached ) (* appsv1.Deployment , error ) {
308- ls := labelsForMemcached (memcached .Name )
309- replicas := memcached .Spec .Size
310212
311- // Get the Operand image
312- image , err := imageForMemcached ()
313- if err != nil {
314- return nil , err
315- }
213+ replicas := memcached .Spec .Size
214+ image := "memcached:1.6.26-alpine3.19"
316215
317216 dep := & appsv1.Deployment {
318217 ObjectMeta : metav1.ObjectMeta {
@@ -321,42 +220,9 @@ func (r *MemcachedReconciler) deploymentForMemcached(
321220 },
322221 Spec : appsv1.DeploymentSpec {
323222 Replicas : & replicas ,
324- Selector : & metav1.LabelSelector {
325- MatchLabels : ls ,
326- },
327223 Template : corev1.PodTemplateSpec {
328- ObjectMeta : metav1.ObjectMeta {
329- Labels : ls ,
330- },
331224 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- //},
225+
360226 SecurityContext : & corev1.PodSecurityContext {
361227 RunAsNonRoot : & []bool {true }[0 ],
362228 // IMPORTANT: seccomProfile was introduced with Kubernetes 1.19
@@ -383,7 +249,7 @@ func (r *MemcachedReconciler) deploymentForMemcached(
383249 },
384250 },
385251 Ports : []corev1.ContainerPort {{
386- ContainerPort : memcached . Spec . ContainerPort ,
252+ ContainerPort : 11211 ,
387253 Name : "memcached" ,
388254 }},
389255 Command : []string {"memcached" , "-m=64" , "-o" , "modern" , "-v" },
@@ -401,31 +267,6 @@ func (r *MemcachedReconciler) deploymentForMemcached(
401267 return dep , nil
402268}
403269
404- // labelsForMemcached returns the labels for selecting the resources
405- // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
406- func labelsForMemcached (name string ) map [string ]string {
407- var imageTag string
408- image , err := imageForMemcached ()
409- if err == nil {
410- imageTag = strings .Split (image , ":" )[1 ]
411- }
412- return map [string ]string {"app.kubernetes.io/name" : "project" ,
413- "app.kubernetes.io/version" : imageTag ,
414- "app.kubernetes.io/managed-by" : "MemcachedController" ,
415- }
416- }
417-
418- // imageForMemcached gets the Operand image which is managed by this controller
419- // from the MEMCACHED_IMAGE environment variable defined in the config/manager/manager.yaml
420- func imageForMemcached () (string , error ) {
421- var imageEnvVar = "MEMCACHED_IMAGE"
422- image , found := os .LookupEnv (imageEnvVar )
423- if ! found {
424- return "" , fmt .Errorf ("Unable to find %s environment variable with the image" , imageEnvVar )
425- }
426- return image , nil
427- }
428-
429270// SetupWithManager sets up the controller with the Manager.
430271// Note that the Deployment will be also watched in order to ensure its
431272// desirable state on the cluster
0 commit comments