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