github.com/verrazzano/verrazzano@v1.7.0/application-operator/controllers/metricsbinding/metricsbinding_controller.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package metricsbinding
     5  
     6  import (
     7  	"context"
     8  
     9  	vzapi "github.com/verrazzano/verrazzano/application-operator/apis/app/v1alpha1"
    10  	"github.com/verrazzano/verrazzano/application-operator/controllers/clusters"
    11  	vzconst "github.com/verrazzano/verrazzano/pkg/constants"
    12  	vzlogInit "github.com/verrazzano/verrazzano/pkg/log"
    13  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    14  	"go.uber.org/zap"
    15  	k8sruntime "k8s.io/apimachinery/pkg/runtime"
    16  	k8scontroller "sigs.k8s.io/controller-runtime"
    17  	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
    18  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    19  )
    20  
    21  // Reconciler reconciles a metrics workload object
    22  type Reconciler struct {
    23  	k8sclient.Client
    24  	Log     *zap.SugaredLogger
    25  	Scheme  *k8sruntime.Scheme
    26  	Scraper string
    27  }
    28  
    29  const controllerName = "metricsbinding"
    30  
    31  // SetupWithManager creates controller for the MetricsBinding
    32  func (r *Reconciler) SetupWithManager(mgr k8scontroller.Manager) error {
    33  	return k8scontroller.NewControllerManagedBy(mgr).For(&vzapi.MetricsBinding{}).Complete(r)
    34  }
    35  
    36  // Reconcile reconciles a workload to keep the Prometheus ConfigMap scrape job configuration up to date.
    37  // No kubebuilder annotations are used as the application RBAC for the application operator is now manually managed.
    38  func (r *Reconciler) Reconcile(ctx context.Context, req k8scontroller.Request) (k8scontroller.Result, error) {
    39  
    40  	// We do not want any resource to get reconciled if it is in namespace kube-system
    41  	// This is due to a bug found in OKE, it should not affect functionality of any vz operators
    42  	// If this is the case then return success
    43  	if req.Namespace == vzconst.KubeSystem {
    44  		log := zap.S().With(vzlogInit.FieldResourceNamespace, req.Namespace, vzlogInit.FieldResourceName, req.Name, vzlogInit.FieldController, controllerName)
    45  		log.Infof("Metrics binding resource %v should not be reconciled in kube-system namespace, ignoring", req.NamespacedName)
    46  		return reconcile.Result{}, nil
    47  	}
    48  
    49  	if ctx == nil {
    50  		ctx = context.Background()
    51  	}
    52  	metricsBinding := vzapi.MetricsBinding{}
    53  	if err := r.Client.Get(context.TODO(), req.NamespacedName, &metricsBinding); err != nil {
    54  		return clusters.IgnoreNotFoundWithLog(err, zap.S())
    55  	}
    56  	log, err := clusters.GetResourceLogger("metricsbinding", req.NamespacedName, &metricsBinding)
    57  	if err != nil {
    58  		zap.S().Errorf("Failed to create controller logger for metrics binding resource: %v", err)
    59  		return clusters.NewRequeueWithDelay(), nil
    60  	}
    61  	log.Oncef("Reconciling metrics binding resource %v, generation %v", req.NamespacedName, metricsBinding.Generation)
    62  
    63  	res, err := r.doReconcile(ctx, metricsBinding, log)
    64  	if clusters.ShouldRequeue(res) {
    65  		return res, nil
    66  	}
    67  	if err != nil {
    68  		return clusters.NewRequeueWithDelay(), err
    69  	}
    70  	log.Oncef("Finished reconciling metrics binding %v", req.NamespacedName)
    71  
    72  	return k8scontroller.Result{}, nil
    73  }
    74  
    75  // doReconcile performs the reconciliation operations for the ingress trait
    76  func (r *Reconciler) doReconcile(ctx context.Context, metricsBinding vzapi.MetricsBinding, log vzlog.VerrazzanoLogger) (k8scontroller.Result, error) {
    77  	// Reconcile based on the status of the deletion timestamp
    78  	if metricsBinding.GetDeletionTimestamp().IsZero() {
    79  		return r.reconcileBindingCreateOrUpdate(ctx, &metricsBinding, log)
    80  	}
    81  	return r.reconcileBindingDelete(ctx, &metricsBinding, log)
    82  }