Skip to content

Commit

Permalink
Merge branch 'master' into context-nstemplateset
Browse files Browse the repository at this point in the history
  • Loading branch information
filariow authored Nov 8, 2023
2 parents 107e4bd + 8b6f611 commit 35883d1
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
return reconcile.Result{}, err
}

if err := r.handleAutoscalerDeploy(reqLogger, crtConfig, request.Namespace); err != nil {
if err := r.handleAutoscalerDeploy(ctx, crtConfig, request.Namespace); err != nil {
return reconcile.Result{}, err
}

if err := r.handleWebhookDeploy(reqLogger, crtConfig, request.Namespace); err != nil {
if err := r.handleWebhookDeploy(ctx, crtConfig, request.Namespace); err != nil {
return reconcile.Result{}, err
}

Expand All @@ -72,15 +72,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
return reconcile.Result{}, nil
}

func (r *Reconciler) handleAutoscalerDeploy(logger logr.Logger, cfg Configuration, namespace string) error {
func (r *Reconciler) handleAutoscalerDeploy(ctx context.Context, cfg Configuration, namespace string) error {
logger := log.FromContext(ctx)
if cfg.Autoscaler().Deploy() {
logger.Info("(Re)Deploying autoscaling buffer")
if err := autoscaler.Deploy(r.Client, r.Client.Scheme(), namespace, cfg.Autoscaler().BufferMemory(), cfg.Autoscaler().BufferReplicas()); err != nil {
return err
}
logger.Info("(Re)Deployed autoscaling buffer")
} else {
deleted, err := autoscaler.Delete(r.Client, r.Client.Scheme(), namespace)
deleted, err := autoscaler.Delete(ctx, r.Client, r.Client.Scheme(), namespace)
if err != nil {
return err
}
Expand All @@ -93,13 +94,14 @@ func (r *Reconciler) handleAutoscalerDeploy(logger logr.Logger, cfg Configuratio
return nil
}

func (r *Reconciler) handleWebhookDeploy(logger logr.Logger, cfg Configuration, namespace string) error {
func (r *Reconciler) handleWebhookDeploy(ctx context.Context, cfg Configuration, namespace string) error {
logger := log.FromContext(ctx)
// By default the users' pods webhook will be deployed, however in some cases (eg. e2e tests) there can be multiple member operators
// installed in the same cluster. In those cases only 1 webhook is needed because the MutatingWebhookConfiguration is a cluster-scoped resource and naming can conflict.
if cfg.Webhook().Deploy() {
webhookImage := os.Getenv("MEMBER_OPERATOR_WEBHOOK_IMAGE")
logger.Info("(Re)Deploying users' pods webhook")
if err := deploy.Webhook(r.Client, r.Client.Scheme(), namespace, webhookImage); err != nil {
if err := deploy.Webhook(ctx, r.Client, r.Client.Scheme(), namespace, webhookImage); err != nil {
return err
}
logger.Info("(Re)Deployed users' pods webhook")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -124,9 +125,10 @@ func TestHandleAutoscalerDeploy(t *testing.T) {
controller, cl := prepareReconcile(t, config)
actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
err = controller.handleAutoscalerDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleAutoscalerDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -140,12 +142,12 @@ func TestHandleAutoscalerDeploy(t *testing.T) {
// given
config := commonconfig.NewMemberOperatorConfigWithReset(t, testconfig.Autoscaler().Deploy(true))
controller, cl := prepareReconcile(t, config)

actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
err = controller.handleAutoscalerDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleAutoscalerDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -160,9 +162,10 @@ func TestHandleAutoscalerDeploy(t *testing.T) {
updatedConfig, err := ForceLoadConfiguration(cl)
require.NoError(t, err)
require.False(t, updatedConfig.Autoscaler().Deploy())
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
err = controller.handleAutoscalerDeploy(controller.Log, updatedConfig, test.MemberOperatorNs)
err = controller.handleAutoscalerDeploy(ctx, updatedConfig, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -179,12 +182,13 @@ func TestHandleAutoscalerDeploy(t *testing.T) {
controller, cl := prepareReconcile(t, config)
actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
cl.(*test.FakeClient).MockGet = func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return fmt.Errorf("client error")
}
err = controller.handleAutoscalerDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleAutoscalerDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.NotNil(t, err)
Expand All @@ -202,12 +206,13 @@ func TestHandleAutoscalerDeploy(t *testing.T) {
controller, cl := prepareReconcile(t, config, actualPrioClass)
actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
cl.(*test.FakeClient).MockDelete = func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return fmt.Errorf("client error")
}
err = controller.handleAutoscalerDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleAutoscalerDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.EqualError(t, err, "cannot delete autoscaling buffer object: client error")
Expand All @@ -219,12 +224,12 @@ func TestHandleWebhookDeploy(t *testing.T) {
// given
config := commonconfig.NewMemberOperatorConfigWithReset(t, testconfig.Webhook().Deploy(false))
controller, cl := prepareReconcile(t, config)

actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
err = controller.handleWebhookDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleWebhookDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -240,9 +245,10 @@ func TestHandleWebhookDeploy(t *testing.T) {
controller, cl := prepareReconcile(t, config)
actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
err = controller.handleWebhookDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleWebhookDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -257,12 +263,13 @@ func TestHandleWebhookDeploy(t *testing.T) {
controller, cl := prepareReconcile(t, config)
actualConfig, err := GetConfiguration(cl)
require.NoError(t, err)
ctx := log.IntoContext(context.TODO(), controller.Log)

// when
cl.(*test.FakeClient).MockGet = func(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
return fmt.Errorf("client error")
}
err = controller.handleWebhookDeploy(controller.Log, actualConfig, test.MemberOperatorNs)
err = controller.handleWebhookDeploy(ctx, actualConfig, test.MemberOperatorNs)

// then
require.EqualError(t, err, "cannot deploy webhook template: client error")
Expand Down
6 changes: 3 additions & 3 deletions pkg/autoscaler/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Deploy(cl runtimeclient.Client, s *runtime.Scheme, namespace, requestsMemor

// Delete deletes the autoscaling buffer app if it's deployed. Does nothing if it's not.
// Returns true if the app was deleted.
func Delete(cl client.Client, s *runtime.Scheme, namespace string) (bool, error) {
func Delete(ctx context.Context, cl client.Client, s *runtime.Scheme, namespace string) (bool, error) {
objs, err := getTemplateObjects(s, namespace, "0", 0)
if err != nil {
return false, err
Expand All @@ -46,12 +46,12 @@ func Delete(cl client.Client, s *runtime.Scheme, namespace string) (bool, error)
for _, obj := range objs {
unst := &unstructured.Unstructured{}
unst.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
if err := cl.Get(context.TODO(), types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, unst); err != nil {
if err := cl.Get(ctx, types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, unst); err != nil {
if !errors.IsNotFound(err) { // Ignore not found
return false, errs.Wrap(err, "cannot get autoscaling buffer object")
}
} else {
if err := cl.Delete(context.TODO(), unst); err != nil {
if err := cl.Delete(ctx, unst); err != nil {
return false, errs.Wrap(err, "cannot delete autoscaling buffer object")
}
deleted = true
Expand Down
8 changes: 4 additions & 4 deletions pkg/autoscaler/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestDelete(t *testing.T) {
AssertThatNamespace(t, test.MemberOperatorNs, fakeClient).HasResource(dm.Name, &appsv1.Deployment{})

// when
deleted, err := Delete(fakeClient, s, test.MemberOperatorNs)
deleted, err := Delete(context.TODO(), fakeClient, s, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -113,7 +113,7 @@ func TestDelete(t *testing.T) {
fakeClient := test.NewFakeClient(t)

// when
deleted, err := Delete(fakeClient, s, test.MemberOperatorNs)
deleted, err := Delete(context.TODO(), fakeClient, s, test.MemberOperatorNs)

// then
require.NoError(t, err)
Expand All @@ -128,7 +128,7 @@ func TestDelete(t *testing.T) {
}

// when
deleted, err := Delete(fakeClient, s, test.MemberOperatorNs)
deleted, err := Delete(context.TODO(), fakeClient, s, test.MemberOperatorNs)

// then
assert.EqualError(t, err, "cannot get autoscaling buffer object: some error")
Expand All @@ -143,7 +143,7 @@ func TestDelete(t *testing.T) {
}

// when
deleted, err := Delete(fakeClient, s, test.MemberOperatorNs)
deleted, err := Delete(context.TODO(), fakeClient, s, test.MemberOperatorNs)

// then
assert.EqualError(t, err, "cannot delete autoscaling buffer object: some error")
Expand Down
8 changes: 4 additions & 4 deletions pkg/cert/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ const (
Expiration = 365 * 24 * time.Hour
)

func EnsureSecret(cl client.Client, namespace, certSecretName, serviceName string, expiration time.Duration) ([]byte, error) {
func EnsureSecret(ctx context.Context, cl client.Client, namespace, certSecretName, serviceName string, expiration time.Duration) ([]byte, error) {
certSecret := &corev1.Secret{}
if err := cl.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: certSecretName}, certSecret); err != nil && !errors.IsNotFound(err) {
if err := cl.Get(ctx, types.NamespacedName{Namespace: namespace, Name: certSecretName}, certSecret); err != nil && !errors.IsNotFound(err) {
return nil, err
} else if err != nil {
// does not exist, so let's create it
certSecret, err := newSecret(certSecretName, namespace, serviceName, expiration)
if err != nil {
return nil, err
}
if err := cl.Create(context.TODO(), certSecret); err != nil {
if err := cl.Create(ctx, certSecret); err != nil {
return nil, err
}
return certSecret.Data[CACert], nil
Expand All @@ -64,7 +64,7 @@ func EnsureSecret(cl client.Client, namespace, certSecretName, serviceName strin
return nil, err
}
newSecret.SetResourceVersion(certSecret.GetResourceVersion())
if err := cl.Update(context.TODO(), newSecret); err != nil {
if err := cl.Update(ctx, newSecret); err != nil {
return nil, err
}
return newSecret.Data[CACert], nil
Expand Down
12 changes: 6 additions & 6 deletions pkg/cert/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestEnsureCertSecret(t *testing.T) {
fakeClient := test.NewFakeClient(t)

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestEnsureCertSecret(t *testing.T) {
fakeClient := test.NewFakeClient(t, secret)

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)

// then
require.NoError(t, err)
Expand All @@ -100,7 +100,7 @@ func TestEnsureCertSecret(t *testing.T) {
time.Sleep(shortExpiration / 2)

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, shortExpiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, shortExpiration)

// then
require.NoError(t, err)
Expand All @@ -124,7 +124,7 @@ func TestEnsureCertSecret(t *testing.T) {
fakeClient := test.NewFakeClient(t, secret)

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)

// then
require.NoError(t, err)
Expand All @@ -145,7 +145,7 @@ func TestEnsureCertSecret(t *testing.T) {
}

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
fmt.Println()

// then
Expand All @@ -164,7 +164,7 @@ func TestEnsureCertSecret(t *testing.T) {
}

// when
caCert, err := EnsureSecret(fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)
caCert, err := EnsureSecret(context.TODO(), fakeClient, test.MemberOperatorNs, certSecretName, serviceName, Expiration)

// then
require.Error(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

// GetRouteURL gets the URL of the route with the given name and namespace using the given client
func GetRouteURL(cl client.Client, namespace, name string) (string, error) {
func GetRouteURL(ctx context.Context, cl client.Client, namespace, name string) (string, error) {
route := &routev1.Route{}
namespacedName := types.NamespacedName{Namespace: namespace, Name: name}
err := cl.Get(context.TODO(), namespacedName, route)
err := cl.Get(ctx, namespacedName, route)
if err != nil {
return "", err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/utils/route/route_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package route

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -36,7 +37,7 @@ func TestGetRouteURL(t *testing.T) {
cl := test.NewFakeClient(t, route)

// when
routeURL, err := GetRouteURL(cl, ns, name)
routeURL, err := GetRouteURL(context.TODO(), cl, ns, name)

// then
require.NoError(t, err)
Expand All @@ -51,7 +52,7 @@ func TestGetRouteURL(t *testing.T) {
cl := test.NewFakeClient(t, r)

// when
routeURL, err := GetRouteURL(cl, ns, name)
routeURL, err := GetRouteURL(context.TODO(), cl, ns, name)

// then
require.NoError(t, err)
Expand All @@ -66,7 +67,7 @@ func TestGetRouteURL(t *testing.T) {
cl := test.NewFakeClient(t, r)

// when
routeURL, err := GetRouteURL(cl, ns, name)
routeURL, err := GetRouteURL(context.TODO(), cl, ns, name)

// then
require.NoError(t, err)
Expand All @@ -81,7 +82,7 @@ func TestGetRouteURL(t *testing.T) {
cl := test.NewFakeClient(t, r)

// when
routeURL, err := GetRouteURL(cl, ns, name)
routeURL, err := GetRouteURL(context.TODO(), cl, ns, name)

// then
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/webhook/deploy/deployment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deploy

import (
"context"
"encoding/base64"

"github.com/codeready-toolchain/member-operator/pkg/cert"
Expand All @@ -23,8 +24,8 @@ const (
serviceName = "member-operator-webhook"
)

func Webhook(cl runtimeclient.Client, s *runtime.Scheme, namespace, image string) error {
caBundle, err := cert.EnsureSecret(cl, namespace, certSecretName, serviceName, cert.Expiration)
func Webhook(ctx context.Context, cl runtimeclient.Client, s *runtime.Scheme, namespace, image string) error {
caBundle, err := cert.EnsureSecret(ctx, cl, namespace, certSecretName, serviceName, cert.Expiration)
if err != nil {
return errs.Wrap(err, "cannot deploy webhook template")
}
Expand Down
Loading

0 comments on commit 35883d1

Please sign in to comment.