From 3d5331bc856f4672effb77b54e1b10416cad6783 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 26 Jan 2023 15:44:53 +0100 Subject: [PATCH] fix linter issues Signed-off-by: odubajDT --- .../certificate_secret.go | 4 +- .../keptnwebhookcertificate_controller.go | 107 ++++++++++-------- .../webhook_cert_controller_test.go | 1 - .../eventfilter/eventfilter_test.go | 2 + klt-cert-manager/kubeutils/query.go | 6 +- klt-cert-manager/kubeutils/query_test.go | 3 +- klt-cert-manager/kubeutils/secret.go | 24 ++-- klt-cert-manager/kubeutils/secret_test.go | 36 +++--- 8 files changed, 96 insertions(+), 87 deletions(-) diff --git a/klt-cert-manager/controllers/keptnwebhookcontroller/certificate_secret.go b/klt-cert-manager/controllers/keptnwebhookcontroller/certificate_secret.go index dab5a77479..8ac9f18848 100644 --- a/klt-cert-manager/controllers/keptnwebhookcontroller/certificate_secret.go +++ b/klt-cert-manager/controllers/keptnwebhookcontroller/certificate_secret.go @@ -32,8 +32,8 @@ func newCertificateSecret(clt client.Client) *certificateSecret { } func (certSecret *certificateSecret) setSecretFromReader(ctx context.Context, namespace string, log logr.Logger) error { - query := kubeutils.NewSecretQuery(ctx, nil, certSecret.clt, log) - secret, err := query.Get(types.NamespacedName{Name: buildSecretName(), Namespace: namespace}) + query := kubeutils.NewSecretQuery(nil, certSecret.clt, log) + secret, err := query.Get(ctx, types.NamespacedName{Name: buildSecretName(), Namespace: namespace}) if k8serrors.IsNotFound(err) { certSecret.secret = kubeutils.NewSecret(buildSecretName(), namespace, map[string][]byte{}) diff --git a/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go b/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go index f1377f2607..06d03df943 100644 --- a/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go +++ b/klt-cert-manager/controllers/keptnwebhookcontroller/keptnwebhookcertificate_controller.go @@ -21,7 +21,6 @@ import ( // KeptnWebhookCertificateReconciler reconciles a KeptnWebhookCertificate object type KeptnWebhookCertificateReconciler struct { - ctx context.Context Client client.Client Scheme *runtime.Scheme CancelMgrFunc context.CancelFunc @@ -45,35 +44,24 @@ func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, reque r.Log.Info("reconciling webhook certificates", "namespace", request.Namespace, "name", request.Name) - r.ctx = ctx - - mutatingWebhookConfiguration, err := r.getMutatingWebhookConfiguration() + mutatingWebhookConfiguration, err := r.getMutatingWebhookConfiguration(ctx) if err != nil { r.Log.Error(err, "could not find mutating webhook configuration") } - validatingWebhookConfiguration, err := r.getValidatingWebhookConfiguration() + validatingWebhookConfiguration, err := r.getValidatingWebhookConfiguration(ctx) if err != nil { r.Log.Error(err, "could not find validating webhook configuration") } - crds := &apiv1.CustomResourceDefinitionList{} - crds, err = r.getCRDConfigurations() + crds, err := r.getCRDConfigurations(ctx) if err != nil { r.Log.Error(err, "could not find CRDs") } certSecret := newCertificateSecret(r.Client) - err = certSecret.setSecretFromReader(r.ctx, namespace, r.Log) - if err != nil { - r.Log.Error(err, "could not get secret") - return reconcile.Result{}, errors.WithStack(err) - } - - err = certSecret.setCertificates(namespace) - if err != nil { - r.Log.Error(err, "could not validate certificate") + if err := r.setCertificates(ctx, certSecret); err != nil { return reconcile.Result{}, errors.WithStack(err) } @@ -92,24 +80,7 @@ func (r *KeptnWebhookCertificateReconciler) Reconcile(ctx context.Context, reque return reconcile.Result{RequeueAfter: SuccessDuration}, nil } - if err = certSecret.createOrUpdateIfNecessary(r.ctx); err != nil { - return reconcile.Result{}, errors.WithStack(err) - } - - bundle, err := certSecret.loadCombinedBundle() - if err != nil { - return reconcile.Result{}, errors.WithStack(err) - } - - if err := r.updateClientConfigurations(bundle, mutatingWebhookConfigs, mutatingWebhookConfiguration); err != nil { - return reconcile.Result{}, errors.WithStack(err) - } - - if err := r.updateClientConfigurations(bundle, validatingWebhookConfigs, validatingWebhookConfiguration); err != nil { - return reconcile.Result{}, errors.WithStack(err) - } - - if err = r.updateCRDsConfiguration(crds, bundle); err != nil { + if err = r.updateConfigurations(ctx, certSecret, crds, mutatingWebhookConfigs, mutatingWebhookConfiguration, validatingWebhookConfigs, validatingWebhookConfiguration); err != nil { return reconcile.Result{}, errors.WithStack(err) } @@ -127,6 +98,48 @@ func (r *KeptnWebhookCertificateReconciler) SetupWithManager(mgr ctrl.Manager) e } +func (r *KeptnWebhookCertificateReconciler) setCertificates(ctx context.Context, certSecret *certificateSecret) error { + err := certSecret.setSecretFromReader(ctx, namespace, r.Log) + if err != nil { + r.Log.Error(err, "could not get secret") + return err + } + + err = certSecret.setCertificates(namespace) + if err != nil { + r.Log.Error(err, "could not validate certificate") + return err + } + + return nil +} + +func (r *KeptnWebhookCertificateReconciler) updateConfigurations(ctx context.Context, certSecret *certificateSecret, crds *apiv1.CustomResourceDefinitionList, + mutatingWebhookConfigs []*admissionregistrationv1.WebhookClientConfig, mutatingWebhookConfiguration *admissionregistrationv1.MutatingWebhookConfiguration, + validatingWebhookConfigs []*admissionregistrationv1.WebhookClientConfig, validatingWebhookConfiguration *admissionregistrationv1.ValidatingWebhookConfiguration) error { + if err := certSecret.createOrUpdateIfNecessary(ctx); err != nil { + return err + } + + bundle, err := certSecret.loadCombinedBundle() + if err != nil { + return err + } + + if err := r.updateClientConfigurations(ctx, bundle, mutatingWebhookConfigs, mutatingWebhookConfiguration); err != nil { + return err + } + + if err := r.updateClientConfigurations(ctx, bundle, validatingWebhookConfigs, validatingWebhookConfiguration); err != nil { + return err + } + + if err = r.updateCRDsConfiguration(ctx, crds, bundle); err != nil { + return err + } + return nil +} + func (r *KeptnWebhookCertificateReconciler) cancelMgr() { if r.CancelMgrFunc != nil { r.Log.Info("stopping manager after certificates creation") @@ -134,10 +147,10 @@ func (r *KeptnWebhookCertificateReconciler) cancelMgr() { } } -func (r *KeptnWebhookCertificateReconciler) getMutatingWebhookConfiguration() ( +func (r *KeptnWebhookCertificateReconciler) getMutatingWebhookConfiguration(ctx context.Context) ( *admissionregistrationv1.MutatingWebhookConfiguration, error) { var mutatingWebhook admissionregistrationv1.MutatingWebhookConfiguration - if err := r.Client.Get(r.ctx, client.ObjectKey{ + if err := r.Client.Get(ctx, client.ObjectKey{ Name: MutatingWebhookconfig, }, &mutatingWebhook); err != nil { return nil, err @@ -149,10 +162,10 @@ func (r *KeptnWebhookCertificateReconciler) getMutatingWebhookConfiguration() ( return &mutatingWebhook, nil } -func (r *KeptnWebhookCertificateReconciler) getValidatingWebhookConfiguration() ( +func (r *KeptnWebhookCertificateReconciler) getValidatingWebhookConfiguration(ctx context.Context) ( *admissionregistrationv1.ValidatingWebhookConfiguration, error) { var validatingWebhook admissionregistrationv1.ValidatingWebhookConfiguration - if err := r.Client.Get(r.ctx, client.ObjectKey{ + if err := r.Client.Get(ctx, client.ObjectKey{ Name: ValidatingWebhookconfig, }, &validatingWebhook); err != nil { return nil, err @@ -164,7 +177,7 @@ func (r *KeptnWebhookCertificateReconciler) getValidatingWebhookConfiguration() return &validatingWebhook, nil } -func (r *KeptnWebhookCertificateReconciler) updateClientConfigurations(bundle []byte, +func (r *KeptnWebhookCertificateReconciler) updateClientConfigurations(ctx context.Context, bundle []byte, webhookClientConfigs []*admissionregistrationv1.WebhookClientConfig, webhookConfig client.Object) error { if webhookConfig == nil || reflect.ValueOf(webhookConfig).IsNil() { return nil @@ -174,29 +187,29 @@ func (r *KeptnWebhookCertificateReconciler) updateClientConfigurations(bundle [] webhookClientConfigs[i].CABundle = bundle } - if err := r.Client.Update(r.ctx, webhookConfig); err != nil { + if err := r.Client.Update(ctx, webhookConfig); err != nil { return err } return nil } -func (r *KeptnWebhookCertificateReconciler) getCRDConfigurations() ( +func (r *KeptnWebhookCertificateReconciler) getCRDConfigurations(ctx context.Context) ( *apiv1.CustomResourceDefinitionList, error) { var crds apiv1.CustomResourceDefinitionList opt := client.MatchingLabels{ "crdGroup": crdGroup, } - if err := r.Client.List(r.ctx, &crds, opt); err != nil { + if err := r.Client.List(ctx, &crds, opt); err != nil { return nil, err } return &crds, nil } -func (r *KeptnWebhookCertificateReconciler) updateCRDsConfiguration(crds *apiv1.CustomResourceDefinitionList, bundle []byte) error { +func (r *KeptnWebhookCertificateReconciler) updateCRDsConfiguration(ctx context.Context, crds *apiv1.CustomResourceDefinitionList, bundle []byte) error { fail := false for _, crd := range crds.Items { - if err := r.updateCRDConfiguration(crd.Name, bundle); err != nil { + if err := r.updateCRDConfiguration(ctx, crd.Name, bundle); err != nil { fail = true } @@ -207,9 +220,9 @@ func (r *KeptnWebhookCertificateReconciler) updateCRDsConfiguration(crds *apiv1. return nil } -func (r *KeptnWebhookCertificateReconciler) updateCRDConfiguration(crdName string, bundle []byte) error { +func (r *KeptnWebhookCertificateReconciler) updateCRDConfiguration(ctx context.Context, crdName string, bundle []byte) error { var crd apiv1.CustomResourceDefinition - if err := r.Client.Get(r.ctx, types.NamespacedName{Name: crdName}, &crd); err != nil { + if err := r.Client.Get(ctx, types.NamespacedName{Name: crdName}, &crd); err != nil { return err } @@ -220,7 +233,7 @@ func (r *KeptnWebhookCertificateReconciler) updateCRDConfiguration(crdName strin // update crd crd.Spec.Conversion.Webhook.ClientConfig.CABundle = bundle - if err := r.Client.Update(r.ctx, &crd); err != nil { + if err := r.Client.Update(ctx, &crd); err != nil { return err } return nil diff --git a/klt-cert-manager/controllers/keptnwebhookcontroller/webhook_cert_controller_test.go b/klt-cert-manager/controllers/keptnwebhookcontroller/webhook_cert_controller_test.go index e2e558939d..acfe893623 100644 --- a/klt-cert-manager/controllers/keptnwebhookcontroller/webhook_cert_controller_test.go +++ b/klt-cert-manager/controllers/keptnwebhookcontroller/webhook_cert_controller_test.go @@ -404,7 +404,6 @@ func createTestSecret(_ *testing.T, certData map[string][]byte) *corev1.Secret { func prepareController(t *testing.T, clt client.Client) (*KeptnWebhookCertificateReconciler, reconcile.Request) { rec := &KeptnWebhookCertificateReconciler{ - ctx: context.TODO(), Client: clt, Log: testr.New(t), } diff --git a/klt-cert-manager/eventfilter/eventfilter_test.go b/klt-cert-manager/eventfilter/eventfilter_test.go index cf00c72648..ccff2e81b1 100644 --- a/klt-cert-manager/eventfilter/eventfilter_test.go +++ b/klt-cert-manager/eventfilter/eventfilter_test.go @@ -64,6 +64,7 @@ func TestForObjectNameAndNamespace(t *testing.T) { })) } +//nolint:dupl func TestForNamespace(t *testing.T) { deployment := &v1.Deployment{ ObjectMeta: metav1.ObjectMeta{ @@ -106,6 +107,7 @@ func TestForNamespace(t *testing.T) { assert.True(t, isInNamespace(deployment, testNamespace2)) } +//nolint:dupl func TestForName(t *testing.T) { deployment := &v1.Deployment{ ObjectMeta: metav1.ObjectMeta{ diff --git a/klt-cert-manager/kubeutils/query.go b/klt-cert-manager/kubeutils/query.go index b92c5dfa1e..ab8e19975a 100644 --- a/klt-cert-manager/kubeutils/query.go +++ b/klt-cert-manager/kubeutils/query.go @@ -1,8 +1,6 @@ package kubeutils import ( - "context" - "github.com/go-logr/logr" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -10,15 +8,13 @@ import ( type kubeQuery struct { kubeClient client.Client kubeReader client.Reader - ctx context.Context log logr.Logger } -func newKubeQuery(ctx context.Context, kubeClient client.Client, kubeReader client.Reader, log logr.Logger) kubeQuery { +func newKubeQuery(kubeClient client.Client, kubeReader client.Reader, log logr.Logger) kubeQuery { return kubeQuery{ kubeClient: kubeClient, kubeReader: kubeReader, - ctx: ctx, log: log, } } diff --git a/klt-cert-manager/kubeutils/query_test.go b/klt-cert-manager/kubeutils/query_test.go index 9e92a17a46..fb32375446 100644 --- a/klt-cert-manager/kubeutils/query_test.go +++ b/klt-cert-manager/kubeutils/query_test.go @@ -1,7 +1,6 @@ package kubeutils import ( - "context" "testing" "github.com/go-logr/logr/testr" @@ -10,5 +9,5 @@ import ( func TestKubeQuery(t *testing.T) { fakeClient := fake.NewClient() - _ = newKubeQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + _ = newKubeQuery(fakeClient, fakeClient, testr.New(t)) } diff --git a/klt-cert-manager/kubeutils/secret.go b/klt-cert-manager/kubeutils/secret.go index 31a5cc9fca..1660faeeeb 100644 --- a/klt-cert-manager/kubeutils/secret.go +++ b/klt-cert-manager/kubeutils/secret.go @@ -17,36 +17,36 @@ type SecretQuery struct { kubeQuery } -func NewSecretQuery(ctx context.Context, kubeClient client.Client, kubeReader client.Reader, log logr.Logger) SecretQuery { +func NewSecretQuery(kubeClient client.Client, kubeReader client.Reader, log logr.Logger) SecretQuery { return SecretQuery{ - newKubeQuery(ctx, kubeClient, kubeReader, log), + newKubeQuery(kubeClient, kubeReader, log), } } -func (query SecretQuery) Get(objectKey client.ObjectKey) (corev1.Secret, error) { +func (query SecretQuery) Get(ctx context.Context, objectKey client.ObjectKey) (corev1.Secret, error) { var secret corev1.Secret - err := query.kubeReader.Get(query.ctx, objectKey, &secret) + err := query.kubeReader.Get(ctx, objectKey, &secret) return secret, errors.WithStack(err) } -func (query SecretQuery) Create(secret corev1.Secret) error { +func (query SecretQuery) Create(ctx context.Context, secret corev1.Secret) error { query.log.Info("creating secret", "name", secret.Name, "namespace", secret.Namespace) - return errors.WithStack(query.kubeClient.Create(query.ctx, &secret)) + return errors.WithStack(query.kubeClient.Create(ctx, &secret)) } -func (query SecretQuery) Update(secret corev1.Secret) error { +func (query SecretQuery) Update(ctx context.Context, secret corev1.Secret) error { query.log.Info("updating secret", "name", secret.Name, "namespace", secret.Namespace) - return errors.WithStack(query.kubeClient.Update(query.ctx, &secret)) + return errors.WithStack(query.kubeClient.Update(ctx, &secret)) } -func (query SecretQuery) CreateOrUpdate(secret corev1.Secret) error { - currentSecret, err := query.Get(types.NamespacedName{Name: secret.Name, Namespace: secret.Namespace}) +func (query SecretQuery) CreateOrUpdate(ctx context.Context, secret corev1.Secret) error { + currentSecret, err := query.Get(ctx, types.NamespacedName{Name: secret.Name, Namespace: secret.Namespace}) if err != nil { if k8serrors.IsNotFound(err) { - err = query.Create(secret) + err = query.Create(ctx, secret) if err != nil { return errors.WithStack(err) } @@ -60,7 +60,7 @@ func (query SecretQuery) CreateOrUpdate(secret corev1.Secret) error { return nil } - err = query.Update(secret) + err = query.Update(ctx, secret) if err != nil { return errors.WithStack(err) } diff --git a/klt-cert-manager/kubeutils/secret_test.go b/klt-cert-manager/kubeutils/secret_test.go index 1a370e59c3..6b111e0a0e 100644 --- a/klt-cert-manager/kubeutils/secret_test.go +++ b/klt-cert-manager/kubeutils/secret_test.go @@ -35,9 +35,9 @@ func testGetSecret(t *testing.T) { } fakeClient := fake.NewClient(&secret) - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - foundSecret, err := secretQuery.Get(client.ObjectKey{Name: testSecretName, Namespace: testNamespace}) + foundSecret, err := secretQuery.Get(context.TODO(), client.ObjectKey{Name: testSecretName, Namespace: testNamespace}) assert.NoError(t, err) assert.True(t, AreSecretsEqual(secret, foundSecret)) @@ -46,7 +46,7 @@ func testGetSecret(t *testing.T) { func testCreateSecret(t *testing.T) { fakeClient := fake.NewClient() - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) secret := corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: testSecretName, @@ -55,7 +55,7 @@ func testCreateSecret(t *testing.T) { Data: map[string][]byte{testKey1: []byte(testSecretValue)}, } - err := secretQuery.Create(secret) + err := secretQuery.Create(context.TODO(), secret) assert.NoError(t, err) @@ -76,9 +76,9 @@ func testUpdateSecret(t *testing.T) { } fakeClient := fake.NewClient() - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.Update(secret) + err := secretQuery.Update(context.TODO(), secret) assert.Error(t, err) @@ -87,7 +87,7 @@ func testUpdateSecret(t *testing.T) { secretQuery.kubeClient = fakeClient - err = secretQuery.Update(secret) + err = secretQuery.Update(context.TODO(), secret) assert.NoError(t, err) @@ -107,9 +107,9 @@ func testCreateOrUpdateSecret(t *testing.T) { Data: map[string][]byte{testKey1: []byte(testSecretValue)}, } fakeClient := fake.NewClient() - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.CreateOrUpdate(secret) + err := secretQuery.CreateOrUpdate(context.TODO(), secret) assert.NoError(t, err) var createdSecret corev1.Secret @@ -129,7 +129,7 @@ func testCreateOrUpdateSecret(t *testing.T) { } secretQuery.kubeClient = fakeClient - err = secretQuery.CreateOrUpdate(secret) + err = secretQuery.CreateOrUpdate(context.TODO(), secret) assert.NoError(t, err) @@ -155,9 +155,9 @@ func testIdenticalSecretIsNotUpdated(t *testing.T) { }) secret := createTestSecret(labels, data) - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.CreateOrUpdate(*secret) + err := secretQuery.CreateOrUpdate(context.TODO(), *secret) assert.NoError(t, err) } @@ -176,9 +176,9 @@ func testUpdateSecretWhenDataChanged(t *testing.T) { }) secret := createTestSecret(labels, data) - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.CreateOrUpdate(*secret) + err := secretQuery.CreateOrUpdate(context.TODO(), *secret) assert.NoError(t, err) var updatedSecret corev1.Secret @@ -203,9 +203,9 @@ func testUpdateSecretWhenLabelsChanged(t *testing.T) { }) secret := createTestSecret(labels, data) - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.CreateOrUpdate(*secret) + err := secretQuery.CreateOrUpdate(context.TODO(), *secret) assert.NoError(t, err) var updatedSecret corev1.Secret @@ -229,9 +229,9 @@ func testCreateSecretInTargetNamespace(t *testing.T) { }) secret := createTestSecret(labels, data) - secretQuery := NewSecretQuery(context.TODO(), fakeClient, fakeClient, testr.New(t)) + secretQuery := NewSecretQuery(fakeClient, fakeClient, testr.New(t)) - err := secretQuery.CreateOrUpdate(*secret) + err := secretQuery.CreateOrUpdate(context.TODO(), *secret) assert.NoError(t, err)