@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
16+ // +kubebuilder:docs-gen:collapse=Apache License
1617
1718package 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
4549const (
@@ -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+
306226func (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" },
0 commit comments