Removal of a committer
[ric-plt/ricdms.git] / 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 func (r *XappDepReconciler) handle_deploy_using_generated_go_code(usage string) {
39         // return
40         if usage == "create" {
41                 r.CreateAll()
42         } else {
43                 r.DeleteAll()
44         }
45 }
46
47 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps,verbs=get;list;watch;create;update;patch;delete
48 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/status,verbs=get;update;patch
49 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/finalizers,verbs=update
50
51 // Reconcile is part of the main kubernetes reconciliation loop which aims to
52 // move the current state of the cluster closer to the desired state.
53 // TODO(user): Modify the Reconcile function to compare the state specified by
54 // the XappDep object against the actual cluster state, and then
55 // perform operations to make the cluster state reflect the state specified by
56 // the user.
57 //
58 // For more details, check Reconcile and its Result here:
59 // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile
60 func (r *XappDepReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
61         logger := log.FromContext(ctx)
62         logger.Info("Reconcilling xapp")
63         instance := &depxappv1.XappDep{}
64         err := r.Get(context.TODO(), req.NamespacedName, instance)
65         if err != nil {
66                 if errors.IsNotFound(err) {
67                         // object not found, could have been deleted after reconcile request, hence don't requeue
68                         return ctrl.Result{}, nil
69                 }
70                 // error reading the object, requeue the request
71                 return ctrl.Result{}, err
72         }
73
74         // name of our custom finalizer
75         myFinalizerName := "batch.tutorial.kubebuilder.io/finalizer"
76         // examine DeletionTimestamp to determine if object is under deletion
77         if instance.ObjectMeta.DeletionTimestamp.IsZero() {
78                 // Adding a Finaliser also adds the DeletionTimestamp while deleting
79                 if !controllerutil.ContainsFinalizer(instance, myFinalizerName) {
80                         // Would Be Called only during CR-Creation
81                         logger.Info("--- Job is in Creation state")
82                         r.handle_deploy_using_generated_go_code("create")
83                         logger.Info("--- Job has been Created")
84                         controllerutil.AddFinalizer(instance, myFinalizerName)
85                         if err := r.Update(ctx, instance); err != nil {
86                                 return ctrl.Result{}, err
87                         }
88                 }
89         } else {
90                 // The object is being deleted
91                 if controllerutil.ContainsFinalizer(instance, myFinalizerName) {
92                         // remove our finalizer from the list and update it.
93                         logger.Info("--- Job is in Deletion state")
94                         r.handle_deploy_using_generated_go_code("delete")
95                         logger.Info("--- Job has been Delete")
96                         controllerutil.RemoveFinalizer(instance, myFinalizerName)
97                         if err := r.Update(ctx, instance); err != nil {
98                                 return ctrl.Result{}, err
99                         }
100                 }
101
102                 // Stop reconciliation as the item is being deleted
103                 return ctrl.Result{}, nil
104         }
105         return ctrl.Result{}, nil
106
107 }
108
109 // SetupWithManager sets up the controller with the Manager.
110 func (r *XappDepReconciler) SetupWithManager(mgr ctrl.Manager) error {
111         return ctrl.NewControllerManagedBy(mgr).
112                 For(&depxappv1.XappDep{}).
113                 Complete(r)
114 }