Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v0.10] Skip CA bundle secret creation with empty payload #2923

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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
58 changes: 46 additions & 12 deletions integrationtests/gitjob/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ var _ = Describe("GitJob controller", func() {
gitRepoName string
job batchv1.Job
jobName string
secretName string
caBundle []byte
)

JustBeforeEach(func() {
expectedCommit = commit
gitRepo = createGitRepo(gitRepoName)
gitRepo.Spec.CABundle = []byte("LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tZm9vLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=")
gitRepo.Spec.CABundle = caBundle

Expect(k8sClient.Create(ctx, &gitRepo)).ToNot(HaveOccurred())
Eventually(func() string {
Expand Down Expand Up @@ -117,21 +117,55 @@ var _ = Describe("GitJob controller", func() {
g.Expect(k8sClient.Get(ctx, ns, &rb)).ToNot(HaveOccurred())
Expect(rb.ObjectMeta).To(beOwnedBy(gitRepoOwnerRef))
}).Should(Succeed())
})

// it should create a secret for the CA bundle
Eventually(func(g Gomega) {
secretName = fmt.Sprintf("%s-cabundle", gitRepoName)
When("a job is created without a specified CA bundle", func() {
BeforeEach(func() {
gitRepoName = "no-ca-bundle"
caBundle = nil
})

It("does not create a secret for the CA bundle", func() {
secretName := fmt.Sprintf("%s-cabundle", gitRepoName)
ns := types.NamespacedName{Name: secretName, Namespace: gitRepo.Namespace}
var secret corev1.Secret

err := k8sClient.Get(ctx, ns, &secret)
g.Expect(err).ToNot(HaveOccurred())
Expect(secret.ObjectMeta).To(beOwnedBy(gitRepoOwnerRef))
Consistently(func(g Gomega) {
err := k8sClient.Get(ctx, ns, &secret)

data, ok := secret.Data["additional-ca.crt"]
g.Expect(ok).To(BeTrue())
g.Expect(data).To(Equal(gitRepo.Spec.CABundle))
}).Should(Succeed())
g.Expect(err).ToNot(BeNil())
g.Expect(errors.IsNotFound(err)).To(BeTrue(), err)
}, time.Second*5, time.Second*1).Should(Succeed())
})
})

When("a job is created with a CA bundle", func() {
BeforeEach(func() {
gitRepoName = "with-ca-bundle"
caBundle = []byte("LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tZm9vLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=")
})

It("Creates a secret for the CA bundle", func() {
gitRepoOwnerRef := metav1.OwnerReference{
Kind: "GitRepo",
APIVersion: "fleet.cattle.io/v1alpha1",
Name: gitRepoName,
}

secretName := fmt.Sprintf("%s-cabundle", gitRepoName)
ns := types.NamespacedName{Name: secretName, Namespace: gitRepo.Namespace}
var secret corev1.Secret

Eventually(func(g Gomega) {
err := k8sClient.Get(ctx, ns, &secret)
g.Expect(err).ToNot(HaveOccurred())
Expect(secret.ObjectMeta).To(beOwnedBy(gitRepoOwnerRef))

data, ok := secret.Data["additional-ca.crt"]
g.Expect(ok).To(BeTrue())
g.Expect(data).To(Equal(gitRepo.Spec.CABundle))
}).Should(Succeed())
})
})

When("a job completes successfully", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (r *GitJobReconciler) createTargetsConfigMap(ctx context.Context, gitrepo *
}

func (r *GitJobReconciler) createCABundleSecret(ctx context.Context, gitrepo *v1alpha1.GitRepo) error {
if len(gitrepo.Spec.CABundle) == 0 {
return nil
}

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: gitrepo.ObjectMeta.Namespace,
Expand Down
Loading