5bff8d526a038ed588a51ddcbe11dd5507270058
[ric-plt/ricdms.git] / xappKubernetesOperator / internal / controller / xappdep_controller.go
1 /*
2 Copyright 2023.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package controller
18
19 import (
20         "context"
21
22         "k8s.io/apimachinery/pkg/api/errors"
23         "k8s.io/apimachinery/pkg/runtime"
24         ctrl "sigs.k8s.io/controller-runtime"
25         "sigs.k8s.io/controller-runtime/pkg/client"
26         "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
27         "sigs.k8s.io/controller-runtime/pkg/log"
28
29         depxappv1 "depxapp/api/v1"
30 )
31
32 // XappDepReconciler reconciles a XappDep object
33 type XappDepReconciler struct {
34         client.Client
35         Scheme *runtime.Scheme
36 }
37
38 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps,verbs=get;list;watch;create;update;patch;delete
39 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/status,verbs=get;update;patch
40 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/finalizers,verbs=update
41
42 // Reconcile is part of the main kubernetes reconciliation loop which aims to
43 // move the current state of the cluster closer to the desired state.
44 // TODO(user): Modify the Reconcile function to compare the state specified by
45 // the XappDep object against the actual cluster state, and then
46 // perform operations to make the cluster state reflect the state specified by
47 // the user.
48 //
49 // For more details, check Reconcile and its Result here:
50 // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile
51 func (r *XappDepReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
52         logger := log.FromContext(ctx)
53         logger.Info("Reconcilling xapp")
54         instance := &depxappv1.XappDep{}
55         err := r.Get(context.TODO(), req.NamespacedName, instance)
56         if err != nil {
57                 if errors.IsNotFound(err) {
58                         // object not found, could have been deleted after reconcile request, hence don't requeue
59                         return ctrl.Result{}, nil
60                 }
61                 // error reading the object, requeue the request
62                 return ctrl.Result{}, err
63         }
64
65         // name of our custom finalizer
66         myFinalizerName := "batch.tutorial.kubebuilder.io/finalizer"
67         // examine DeletionTimestamp to determine if object is under deletion
68         if instance.ObjectMeta.DeletionTimestamp.IsZero() {
69                 // Adding a Finaliser also adds the DeletionTimestamp while deleting
70                 if !controllerutil.ContainsFinalizer(instance, myFinalizerName) {
71                         // Would Be Called only during CR-Creation
72                         controllerutil.AddFinalizer(instance, myFinalizerName)
73                         if err := r.Update(ctx, instance); err != nil {
74                                 return ctrl.Result{}, err
75                         }
76                 }
77         } else {
78                 // The object is being deleted
79                 if controllerutil.ContainsFinalizer(instance, myFinalizerName) {
80                         // remove our finalizer from the list and update it.
81                         controllerutil.RemoveFinalizer(instance, myFinalizerName)
82                         if err := r.Update(ctx, instance); err != nil {
83                                 return ctrl.Result{}, err
84                         }
85                 }
86
87                 // Stop reconciliation as the item is being deleted
88                 return ctrl.Result{}, nil
89         }
90         return ctrl.Result{}, nil
91
92 }
93
94 // SetupWithManager sets up the controller with the Manager.
95 func (r *XappDepReconciler) SetupWithManager(mgr ctrl.Manager) error {
96         return ctrl.NewControllerManagedBy(mgr).
97                 For(&depxappv1.XappDep{}).
98                 Complete(r)
99 }