2 ==================================================================================
4 Copyright (c) 2023 Samsung
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
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"
35 ricdeployv1 "ricdeploy/api/v1"
38 // RicPlatformReconciler reconciles a RicPlatform object
39 type RicPlatformReconciler struct {
41 Scheme *runtime.Scheme
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
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
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) {
59 logger := log.FromContext(ctx)
60 logger.Info("Reconcilling RIC")
61 instance := &ricdeployv1.RicPlatform{}
62 err := r.Get(context.TODO(), req.NamespacedName, instance)
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
68 // error reading the object, requeue the request
69 return ctrl.Result{}, err
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
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
93 // Stop reconciliation as the item is being deleted
94 return ctrl.Result{}, nil
97 return ctrl.Result{}, nil
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{}).