Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/controller/istiocsr/certificates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package istiocsr

import (
"context"
"fmt"
"maps"

Expand All @@ -16,7 +17,7 @@ import (
"github.com/openshift/cert-manager-operator/pkg/operator/assets"
)

func (r *Reconciler) createOrApplyCertificates(istiocsr *v1alpha1.IstioCSR, resourceLabels map[string]string, istioCSRCreateRecon bool) error {
func (r *Reconciler) createOrApplyCertificates(ctx context.Context, istiocsr *v1alpha1.IstioCSR, resourceLabels map[string]string, istioCSRCreateRecon bool) error {
desired, err := r.getCertificateObject(istiocsr, resourceLabels)
if err != nil {
return fmt.Errorf("failed to generate certificate resource for creation in %s: %w", istiocsr.GetNamespace(), err)
Expand All @@ -25,7 +26,7 @@ func (r *Reconciler) createOrApplyCertificates(istiocsr *v1alpha1.IstioCSR, reso
certificateName := fmt.Sprintf("%s/%s", desired.GetNamespace(), desired.GetName())
r.log.V(4).Info("reconciling certificate resource", "name", certificateName)
fetched := &certmanagerv1.Certificate{}
exist, err := r.Exists(r.ctx, client.ObjectKeyFromObject(desired), fetched)
exist, err := r.Exists(ctx, client.ObjectKeyFromObject(desired), fetched)
if err != nil {
return common.FromClientError(err, "failed to check %s certificate resource already exists", certificateName)
}
Expand All @@ -36,7 +37,7 @@ func (r *Reconciler) createOrApplyCertificates(istiocsr *v1alpha1.IstioCSR, reso
}
if hasObjectChanged(desired, fetched) {
r.log.V(1).Info("certificate has been modified, updating to desired state", "name", certificateName)
if err := r.UpdateWithRetry(r.ctx, desired); err != nil {
if err := r.UpdateWithRetry(ctx, desired); err != nil {
return common.FromClientError(err, "failed to update %s certificate resource", certificateName)
}
r.eventRecorder.Eventf(istiocsr, corev1.EventTypeNormal, "Reconciled", "certificate resource %s reconciled back to desired state", certificateName)
Expand All @@ -46,7 +47,7 @@ func (r *Reconciler) createOrApplyCertificates(istiocsr *v1alpha1.IstioCSR, reso
}

if !exist {
if err := r.Create(r.ctx, desired); err != nil {
if err := r.Create(ctx, desired); err != nil {
return common.FromClientError(err, "failed to create %s certificate resource", certificateName)
}
r.eventRecorder.Eventf(istiocsr, corev1.EventTypeNormal, "Reconciled", "certificate resource %s created", certificateName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/istiocsr/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestCreateOrApplyCertificates(t *testing.T) {
}, istiocsr); err != nil {
t.Errorf("test error: %v", err)
}
err := r.createOrApplyCertificates(istiocsr, controllerDefaultResourceLabels, false)
err := r.createOrApplyCertificates(context.Background(), istiocsr, controllerDefaultResourceLabels, false)
if (tt.wantErr != "" || err != nil) && (err == nil || err.Error() != tt.wantErr) {
t.Errorf("createOrApplyCertificates() err: %v, wantErr: %v", err, tt.wantErr)
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/controller/istiocsr/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const RequestEnqueueLabelValue = "cert-manager-istio-csr"
type Reconciler struct {
common.CtrlClient

ctx context.Context
eventRecorder record.EventRecorder
log logr.Logger
scheme *runtime.Scheme
Expand All @@ -58,7 +57,6 @@ func New(mgr ctrl.Manager) (*Reconciler, error) {
}
return &Reconciler{
CtrlClient: c,
ctx: context.Background(),
eventRecorder: mgr.GetEventRecorderFor(ControllerName),
log: ctrl.Log.WithName(ControllerName),
scheme: mgr.GetScheme(),
Expand Down Expand Up @@ -178,7 +176,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if !istiocsr.DeletionTimestamp.IsZero() {
r.log.V(1).Info("istiocsr.openshift.operator.io is marked for deletion", "namespace", req.NamespacedName)

if requeue, err := r.cleanUp(istiocsr); err != nil {
if requeue, err := r.cleanUp(ctx, istiocsr); err != nil {
return ctrl.Result{}, fmt.Errorf("clean up failed for %q istiocsr.openshift.operator.io instance deletion: %w", req.NamespacedName, err)
} else if requeue {
return ctrl.Result{RequeueAfter: defaultRequeueTime}, nil
Expand All @@ -197,25 +195,25 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, fmt.Errorf("failed to update %q istiocsr.openshift.operator.io with finalizers: %w", req.NamespacedName, err)
}

return r.processReconcileRequest(istiocsr, req.NamespacedName)
return r.processReconcileRequest(ctx, istiocsr, req.NamespacedName)
}

func (r *Reconciler) processReconcileRequest(istiocsr *v1alpha1.IstioCSR, req types.NamespacedName) (ctrl.Result, error) {
func (r *Reconciler) processReconcileRequest(ctx context.Context, istiocsr *v1alpha1.IstioCSR, req types.NamespacedName) (ctrl.Result, error) {
istioCSRCreateRecon := false
if !containsProcessedAnnotation(istiocsr) && reflect.DeepEqual(istiocsr.Status, v1alpha1.IstioCSRStatus{}) {
r.log.V(1).Info("starting reconciliation of newly created istiocsr", "namespace", istiocsr.GetNamespace(), "name", istiocsr.GetName())
istioCSRCreateRecon = true
}

if err := r.disallowMultipleIstioCSRInstances(istiocsr); err != nil {
if err := r.disallowMultipleIstioCSRInstances(ctx, istiocsr); err != nil {
if common.IsMultipleInstanceError(err) {
r.eventRecorder.Eventf(istiocsr, corev1.EventTypeWarning, "MultiIstioCSRInstance", "creation of multiple istiocsr instances is not supported, will not be processed")
err = nil
}
return ctrl.Result{}, err
}

reconcileErr := r.reconcileIstioCSRDeployment(istiocsr, istioCSRCreateRecon)
reconcileErr := r.reconcileIstioCSRDeployment(ctx, istiocsr, istioCSRCreateRecon)
if reconcileErr != nil {
r.log.Error(reconcileErr, "failed to reconcile IstioCSR deployment", "request", req)
}
Expand All @@ -225,7 +223,7 @@ func (r *Reconciler) processReconcileRequest(istiocsr *v1alpha1.IstioCSR, req ty
reconcileErr,
r.log.WithValues("namespace", istiocsr.GetNamespace(), "name", istiocsr.GetName()),
func(prependErr error) error {
return r.updateCondition(istiocsr, prependErr)
return r.updateCondition(ctx, istiocsr, prependErr)
},
defaultRequeueTime,
)
Expand All @@ -234,7 +232,7 @@ func (r *Reconciler) processReconcileRequest(istiocsr *v1alpha1.IstioCSR, req ty
// cleanUp handles deletion of istiocsr.openshift.operator.io gracefully.
//
//nolint:unparam // error return is kept for future implementation
func (r *Reconciler) cleanUp(istiocsr *v1alpha1.IstioCSR) (bool, error) {
func (r *Reconciler) cleanUp(_ context.Context, istiocsr *v1alpha1.IstioCSR) (bool, error) {
// TODO: For GA, handle cleaning up of resources created for installing istio-csr operand.
// This might require a validation webhook to check for usage of service as GRPC endpoint in
// any of OpenShift Service Mesh or Istiod deployments to avoid disruptions across cluster.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/istiocsr/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ func TestProcessReconcileRequest(t *testing.T) {
}
r.CtrlClient = mock
istiocsr := tt.getIstioCSR()
_, err := r.processReconcileRequest(istiocsr,
_, err := r.processReconcileRequest(context.Background(), istiocsr,
types.NamespacedName{Name: istiocsr.GetName(), Namespace: istiocsr.GetNamespace()})
if (tt.wantErr != "" || err != nil) && (err == nil || err.Error() != tt.wantErr) {
t.Errorf("processReconcileRequest() err: %v, wantErr: %v", err, tt.wantErr)
Expand Down
Loading