-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from fluxcd/no-cross-namespace-refs
Allow disabling cross-namespace references
- Loading branch information
Showing
9 changed files
with
197 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Copyright 2022 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2" | ||
apiacl "github.com/fluxcd/pkg/apis/acl" | ||
"github.com/fluxcd/pkg/apis/meta" | ||
"github.com/fluxcd/pkg/testserver" | ||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" | ||
. "github.com/onsi/gomega" | ||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestKustomizationReconciler_NoCrossNamespaceRefs(t *testing.T) { | ||
g := NewWithT(t) | ||
id := "force-" + randStringRunes(5) | ||
revision := "v1.0.0" | ||
|
||
err := createNamespace(id) | ||
g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") | ||
|
||
err = createKubeConfigSecret(id) | ||
g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") | ||
|
||
manifests := func(name string, data string) []testserver.File { | ||
return []testserver.File{ | ||
{ | ||
Name: "secret.yaml", | ||
Body: fmt.Sprintf(`--- | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: %[1]s | ||
stringData: | ||
key: "%[2]s" | ||
`, name, data), | ||
}, | ||
} | ||
} | ||
|
||
artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5))) | ||
g.Expect(err).NotTo(HaveOccurred(), "failed to create artifact from files") | ||
|
||
sourceNamespace := fmt.Sprintf("source-%v", id) | ||
err = createNamespace(sourceNamespace) | ||
g.Expect(err).NotTo(HaveOccurred(), "failed to create source namespace") | ||
|
||
repositoryName := types.NamespacedName{ | ||
Name: randStringRunes(5), | ||
Namespace: sourceNamespace, | ||
} | ||
|
||
err = applyGitRepository(repositoryName, artifact, revision) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
kustomizationKey := types.NamespacedName{ | ||
Name: fmt.Sprintf("force-%s", randStringRunes(5)), | ||
Namespace: id, | ||
} | ||
kustomization := &kustomizev1.Kustomization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: kustomizationKey.Name, | ||
Namespace: kustomizationKey.Namespace, | ||
}, | ||
Spec: kustomizev1.KustomizationSpec{ | ||
Interval: metav1.Duration{Duration: reconciliationInterval}, | ||
Path: "./", | ||
KubeConfig: &kustomizev1.KubeConfig{ | ||
SecretRef: meta.LocalObjectReference{ | ||
Name: "kubeconfig", | ||
}, | ||
}, | ||
SourceRef: kustomizev1.CrossNamespaceSourceReference{ | ||
Name: repositoryName.Name, | ||
Namespace: repositoryName.Namespace, | ||
Kind: sourcev1.GitRepositoryKind, | ||
}, | ||
TargetNamespace: id, | ||
}, | ||
} | ||
|
||
g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) | ||
|
||
resultK := &kustomizev1.Kustomization{} | ||
readyCondition := &metav1.Condition{} | ||
|
||
t.Run("reconciles from cross-namespace source", func(t *testing.T) { | ||
g.Eventually(func() bool { | ||
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) | ||
readyCondition = apimeta.FindStatusCondition(resultK.Status.Conditions, meta.ReadyCondition) | ||
return resultK.Status.LastAppliedRevision == revision | ||
}, timeout, time.Second).Should(BeTrue()) | ||
|
||
g.Expect(readyCondition.Reason).To(Equal(meta.ReconciliationSucceededReason)) | ||
}) | ||
|
||
t.Run("fails to reconcile from cross-namespace source", func(t *testing.T) { | ||
reconciler.NoCrossNamespaceRefs = true | ||
|
||
revision = "v2.0.0" | ||
err = applyGitRepository(repositoryName, artifact, revision) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
g.Eventually(func() bool { | ||
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) | ||
readyCondition = apimeta.FindStatusCondition(resultK.Status.Conditions, meta.ReadyCondition) | ||
return apimeta.IsStatusConditionFalse(resultK.Status.Conditions, meta.ReadyCondition) | ||
}, timeout, time.Second).Should(BeTrue()) | ||
|
||
g.Expect(readyCondition.Reason).To(Equal(apiacl.AccessDeniedReason)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters