Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Jan 26, 2023
1 parent 2d2043f commit 3d5331b
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -127,17 +98,59 @@ 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")
r.CancelMgrFunc()
}
}

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 2 additions & 0 deletions klt-cert-manager/eventfilter/eventfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestForObjectNameAndNamespace(t *testing.T) {
}))
}

//nolint:dupl
func TestForNamespace(t *testing.T) {
deployment := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 1 addition & 5 deletions klt-cert-manager/kubeutils/query.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package kubeutils

import (
"context"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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,
}
}
3 changes: 1 addition & 2 deletions klt-cert-manager/kubeutils/query_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kubeutils

import (
"context"
"testing"

"github.com/go-logr/logr/testr"
Expand All @@ -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))
}
24 changes: 12 additions & 12 deletions klt-cert-manager/kubeutils/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit 3d5331b

Please sign in to comment.