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