Skip to content

Commit

Permalink
Remove secret_controller and test; Use EnqueueRequestsFromMapFunc (re…
Browse files Browse the repository at this point in the history
…dhat-appstudio#670)

Signed-off-by: Keith Chong <kykchong@redhat.com>
  • Loading branch information
keithchong authored and jgwest committed Aug 29, 2023
1 parent 126ea86 commit 46c9737
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package managedgitops
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -101,8 +104,52 @@ func (dppelp *DefaultPreProcessEventLoopProcessor) callPreprocessEventLoopForMan
// SetupWithManager sets up the controller with the Manager.
func (r *GitOpsDeploymentManagedEnvironmentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironment{},
For(&managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironment{}).
Watches(
&source.Kind{Type: &corev1.Secret{}},
handler.EnqueueRequestsFromMapFunc(r.findSecretsForManagedEnvironment),
builder.WithPredicates(predicate.GenerationChangedPredicate{})).
// Watches(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForObject{}).
Complete(r)
}

func (r *GitOpsDeploymentManagedEnvironmentReconciler) findSecretsForManagedEnvironment(secret client.Object) []reconcile.Request {
ctx := context.Background()
handlerLog := log.FromContext(ctx).
WithName(logutil.LogLogger_managed_gitops)

secretObj, ok := secret.(*corev1.Secret)

if !ok {
handlerLog.Error(nil, "incompatible object in the Environment mapping function, expected a Secret")
return []reconcile.Request{}
}

if secretObj.Type != sharedutil.ManagedEnvironmentSecretType {
return []reconcile.Request{}
}

managedEnvList := managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironmentList{}

if err := r.List(ctx, &managedEnvList, &client.ListOptions{Namespace: secretObj.Namespace}); err != nil {
return []reconcile.Request{}
}

requests := []reconcile.Request{}

for idx := range managedEnvList.Items {
managedEnvCR := managedEnvList.Items[idx]

if managedEnvCR.Namespace != secretObj.Namespace {
// Sanity check that the managed environment resource is in the same namespace as the Secret
continue
}

if managedEnvCR.Spec.ClusterCredentialsSecret == secretObj.Name {
requests = append(requests, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&managedEnvCR),
})
}
}

return requests
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package managedgitops

import (
"context"

"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
managedgitopsv1alpha1 "github.com/redhat-appstudio/managed-gitops/backend-shared/apis/managed-gitops/v1alpha1"
sharedutil "github.com/redhat-appstudio/managed-gitops/backend-shared/util"
"github.com/redhat-appstudio/managed-gitops/backend-shared/util/tests"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,9 +31,11 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("GitOpsDeploymentManagedEnvironment Controller Test", func() {
//ctx := context.Background()

Context("Generic tests", func() {

Expand All @@ -41,6 +45,8 @@ var _ = Describe("GitOpsDeploymentManagedEnvironment Controller Test", func() {
var reconciler GitOpsDeploymentManagedEnvironmentReconciler
var mockProcessor mockPreprocessEventLoopProcessor

var secretName = "test-secret"

BeforeEach(func() {
scheme, argocdNamespace, kubesystemNamespace, _, err := tests.GenericTestSetup()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -67,25 +73,142 @@ var _ = Describe("GitOpsDeploymentManagedEnvironment Controller Test", func() {

})

It("reconciles on a managed-env", func() {
// secret with the right type, and 2 managed envs referring to it
// expect: 2
secret := createSecretForManagedEnv("my-secret", true, *namespace, k8sClient)
managedEnv := createManagedEnvTargetingSecret("managed-env1", secret, *namespace, k8sClient)
_, err := reconciler.Reconcile(context.Background(), ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: managedEnv.Namespace,
Name: managedEnv.Name,
},
})
Expect(err).ToNot(HaveOccurred())
Expect(mockProcessor.requestsReceived).Should(HaveLen(1))
Context("Basic reconcile managed environment", func() {

It("reconciles on a managed-env", func() {
// secret with the right type, and 2 managed envs referring to it
// expect: 2
secret := createSecretForManagedEnv("my-secret", true, *namespace, k8sClient)
managedEnv := createManagedEnvTargetingSecret("managed-env1", secret, *namespace, k8sClient)
_, err := reconciler.Reconcile(context.Background(), ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: managedEnv.Namespace,
Name: managedEnv.Name,
},
})
Expect(err).ToNot(HaveOccurred())
Expect(mockProcessor.requestsReceived).Should(HaveLen(1))

})
})

Context("Test findSecretsForManagedEnvironment function", func() {

When("GitOpsDeploymentManagedEnvironment references a secret in the same namespace", func() {

It("should return a managed environment", func() {
By("create a managed environment secret type")
secret := createSecretForManagedEnv(secretName, true, *namespace, k8sClient)

By("create a managed environment that references the secret")

managedEnv := createManagedEnvTargetingSecret("testManagedEnv", secret, *namespace, k8sClient)

Expect(reconciler.findSecretsForManagedEnvironment(&secret)).To(Equal([]reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&managedEnv)}}))
})
})

When("GitOpsDeploymentManagedEnvironment references a secret of a different type. Expect no Managed Environment to be reconciled", func() {

It("should return a managed environment", func() {
By("create a NON-managed environment secret type")
secret := createSecretForManagedEnv(secretName, false, *namespace, k8sClient)

By("create a managed environment that references the secret")

createManagedEnvTargetingSecret("testManagedEnv", secret, *namespace, k8sClient)

Expect(reconciler.findSecretsForManagedEnvironment(&secret)).To(BeEmpty())
})
})

When("Five GitOpsDeploymentManagedEnvironment reference a secret in the same namespace", func() {

It("should return two managed environments", func() {
By("create a managed environment secret type")
secret := createSecretForManagedEnv(secretName, true, *namespace, k8sClient)

By("create a managed environments that references the secret")
for i := 1; i <= 5; i++ {
managedEnvCR := createManagedEnvTargetingSecret("my-managed-env-"+fmt.Sprint(i), secret, *namespace, k8sClient)
Expect(managedEnvCR).ToNot(BeNil())
}

Expect(reconciler.findSecretsForManagedEnvironment(&secret)).To(HaveLen(5))

expectedArray := []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Namespace: "my-user",
Name: "my-managed-env-1",
},
},
{
NamespacedName: types.NamespacedName{
Namespace: "my-user",
Name: "my-managed-env-2",
},
},
{
NamespacedName: types.NamespacedName{
Namespace: "my-user",
Name: "my-managed-env-3",
},
},
{
NamespacedName: types.NamespacedName{
Namespace: "my-user",
Name: "my-managed-env-4",
},
},
{
NamespacedName: types.NamespacedName{
Namespace: "my-user",
Name: "my-managed-env-5",
},
},
}
Expect(reconciler.findSecretsForManagedEnvironment(&secret)).To(ContainElements(expectedArray))
})
})
})
})
})

func createSecretForManagedEnv(name string, validSecret bool, namespace corev1.Namespace, k8sClient client.Client) corev1.Secret {
secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace.Name,
},
Type: sharedutil.ManagedEnvironmentSecretType,
}

if !validSecret {
secret.Type = ""
}

err := k8sClient.Create(context.Background(), &secret)
Expect(err).ToNot(HaveOccurred())
return secret
}

func createManagedEnvTargetingSecret(name string, secret corev1.Secret, namespace corev1.Namespace, k8sClient client.Client) managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironment {
managedEnv := managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace.Name,
},
Spec: managedgitopsv1alpha1.GitOpsDeploymentManagedEnvironmentSpec{
ClusterCredentialsSecret: secret.Name,
},
}
err := k8sClient.Create(context.Background(), &managedEnv)
Expect(err).ToNot(HaveOccurred())
return managedEnv

}

// mockPreprocessEventLoopProcessor keeps track of ctrl.Requests that are sent to the preprocess event loop listener, so
// that we can verify that the correct ones were sent.
type mockPreprocessEventLoopProcessor struct {
Expand Down
Loading

0 comments on commit 46c9737

Please sign in to comment.