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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 depxappv1 "depxapp/api/v1"
32 // XappDepReconciler reconciles a XappDep object
33 type XappDepReconciler struct {
35 Scheme *runtime.Scheme
38 func (r *XappDepReconciler) handle_deploy_using_generated_go_code(usage string) {
40 if usage == "create" {
44 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps,verbs=get;list;watch;create;update;patch;delete
45 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/status,verbs=get;update;patch
46 //+kubebuilder:rbac:groups=depxapp.xapp.com,resources=xappdeps/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 XappDep 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 *XappDepReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
58 logger := log.FromContext(ctx)
59 logger.Info("Reconcilling xapp")
60 instance := &depxappv1.XappDep{}
61 err := r.Get(context.TODO(), req.NamespacedName, instance)
63 if errors.IsNotFound(err) {
64 // object not found, could have been deleted after reconcile request, hence don't requeue
65 return ctrl.Result{}, nil
67 // error reading the object, requeue the request
68 return ctrl.Result{}, err
71 // name of our custom finalizer
72 myFinalizerName := "batch.tutorial.kubebuilder.io/finalizer"
73 // examine DeletionTimestamp to determine if object is under deletion
74 if instance.ObjectMeta.DeletionTimestamp.IsZero() {
75 // Adding a Finaliser also adds the DeletionTimestamp while deleting
76 if !controllerutil.ContainsFinalizer(instance, myFinalizerName) {
77 // Would Be Called only during CR-Creation
78 logger.Info("--- Job is in Creation state")
79 r.handle_deploy_using_generated_go_code("create")
80 logger.Info("--- Job has been Created")
81 controllerutil.AddFinalizer(instance, myFinalizerName)
82 if err := r.Update(ctx, instance); err != nil {
83 return ctrl.Result{}, err
87 // The object is being deleted
88 if controllerutil.ContainsFinalizer(instance, myFinalizerName) {
89 // remove our finalizer from the list and update it.
90 controllerutil.RemoveFinalizer(instance, myFinalizerName)
91 if err := r.Update(ctx, instance); err != nil {
92 return ctrl.Result{}, err
96 // Stop reconciliation as the item is being deleted
97 return ctrl.Result{}, nil
99 return ctrl.Result{}, nil
103 // SetupWithManager sets up the controller with the Manager.
104 func (r *XappDepReconciler) SetupWithManager(mgr ctrl.Manager) error {
105 return ctrl.NewControllerManagedBy(mgr).
106 For(&depxappv1.XappDep{}).