Skip to content

Commit

Permalink
Update fuzzer with new SecretRef type
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Stogner <nicholas.stogner@gmail.com>
  • Loading branch information
nstogner committed Apr 27, 2022
1 parent 82a14e2 commit 4babc32
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 66 deletions.
2 changes: 1 addition & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/fluxcd/pkg/apis/kustomize v0.3.3
github.com/fluxcd/pkg/apis/meta v0.12.2
github.com/fluxcd/pkg/apis/meta v0.13.0
k8s.io/apiextensions-apiserver v0.23.5
k8s.io/apimachinery v0.23.5
sigs.k8s.io/controller-runtime v0.11.2
Expand Down
4 changes: 2 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fluxcd/pkg/apis/kustomize v0.3.3 h1:bPN29SdVzWl0yhgivuf/83IAe2R6vUuDVcB3LzyVU8E=
github.com/fluxcd/pkg/apis/kustomize v0.3.3/go.mod h1:5HTOFZfQFVMMqR2rvuxpbZhpb+sQpcTT6RCQZOhjFzA=
github.com/fluxcd/pkg/apis/meta v0.12.2 h1:AiKAZxLyPtV150y63WC+mL1Qm4x5qWQmW6r4mLy1i8c=
github.com/fluxcd/pkg/apis/meta v0.12.2/go.mod h1:Z26X5uTU5LxAyWETGueRQY7TvdPaGfKU7Wye9bdUlho=
github.com/fluxcd/pkg/apis/meta v0.13.0 h1:0QuNKEExSjk+Rv0I6a85p2H3xOlWhdxZRsh10waEL/c=
github.com/fluxcd/pkg/apis/meta v0.13.0/go.mod h1:Z26X5uTU5LxAyWETGueRQY7TvdPaGfKU7Wye9bdUlho=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
14 changes: 4 additions & 10 deletions api/v1beta2/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,17 @@ type Decryption struct {

// KubeConfig references a Kubernetes secret that contains a kubeconfig file.
type KubeConfig struct {
// SecretRef holds the name to a secret that contains
// the kubeconfig file as the value. It must be in the same namespace as
// SecretRef holds the name of a secret that contains a key with
// the kubeconfig file as the value. If no key is set, the key will default
// to 'value'. The secret must be in the same namespace as
// the Kustomization.
// It is recommended that the kubeconfig is self-contained, and the secret
// is regularly updated if credentials such as a cloud-access-token expire.
// Cloud specific `cmd-path` auth helpers will not function without adding
// binaries and credentials to the Pod that is responsible for reconciling
// the Kustomization.
// +required
SecretRef SecretRef `json:"secretRef,omitempty"`
}

type SecretRef struct {
// Name of the Secret.
Name string `json:"name"`
// Key in the Secret. If not specified it defaults to 'value'.
Key string `json:"key"`
SecretRef meta.SecretKeyReference `json:"secretRef,omitempty"`
}

// PostBuild describes which actions to perform on the YAML manifest
Expand Down
15 changes: 0 additions & 15 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -698,23 +698,23 @@ spec:
is empty.
properties:
secretRef:
description: SecretRef holds the name to a secret that contains
the kubeconfig file as the value. It must be in the same namespace
as the Kustomization. It is recommended that the kubeconfig
description: SecretRef holds the name of a secret that contains
a key with the kubeconfig file as the value. If no key is set,
the key will default to 'value'. The secret must be in the same
namespace as the Kustomization. It is recommended that the kubeconfig
is self-contained, and the secret is regularly updated if credentials
such as a cloud-access-token expire. Cloud specific `cmd-path`
auth helpers will not function without adding binaries and credentials
to the Pod that is responsible for reconciling the Kustomization.
properties:
key:
description: Key in the Secret. If not specified it defaults
to 'value'.
description: Key in the Secret, when not specified an implementation-specific
default key is used.
type: string
name:
description: Name of the Secret.
type: string
required:
- key
- name
type: object
type: object
Expand Down
2 changes: 1 addition & 1 deletion controllers/kustomization_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ stringData:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/kustomization_decryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/kustomization_dependson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ spec:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/kustomization_force_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ stringData:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
20 changes: 8 additions & 12 deletions controllers/kustomization_impersonation.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,15 @@ func (ki *KustomizeImpersonation) getKubeConfig(ctx context.Context) ([]byte, er
}

var kubeConfig []byte
if refkey := ki.kustomization.Spec.KubeConfig.SecretRef.Key; refkey != "" {
kubeConfig = secret.Data[refkey]
if key := ki.kustomization.Spec.KubeConfig.SecretRef.Key; key != "" {
kubeConfig = secret.Data[key]
} else if val, ok := secret.Data["value"]; ok {
kubeConfig = val
} else if val, ok := secret.Data["value.yaml"]; ok {
kubeConfig = val
} else {
for k := range secret.Data {
if k == "value" || k == "value.yaml" {
kubeConfig = secret.Data[k]
break
}
}
}

if len(kubeConfig) == 0 {
return nil, fmt.Errorf("KubeConfig secret '%s' doesn't contain a 'value' key ", secretName.String())
// User did not specify a key, and the 'value' key was not defined.
return nil, fmt.Errorf("KubeConfig secret '%s' does not contain a 'value' key", secretName)
}

return kubeConfig, nil
Expand Down
112 changes: 111 additions & 1 deletion controllers/kustomization_impersonation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ data:
Interval: metav1.Duration{Duration: time.Minute},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down Expand Up @@ -208,3 +208,113 @@ data:
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
}

func TestKustomizationReconciler_KubeConfig(t *testing.T) {
g := NewWithT(t)
id := "kc-" + randStringRunes(5)
revision := "v1.0.0"

err := createNamespace(id)
g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace")

manifests := func(name string, data string) []testserver.File {
return []testserver.File{
{
Name: "config.yaml",
Body: fmt.Sprintf(`---
apiVersion: v1
kind: ConfigMap
metadata:
name: %[1]s
data:
key: "%[2]s"
`, name, data),
},
}
}

artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5)))
g.Expect(err).NotTo(HaveOccurred(), "failed to create artifact from files")

repositoryName := types.NamespacedName{
Name: randStringRunes(5),
Namespace: id,
}

err = applyGitRepository(repositoryName, artifact, revision)
g.Expect(err).NotTo(HaveOccurred())

const (
secretName = "user-defined-name"
secretKey = "user-defined-key"
)
kustomizationKey := types.NamespacedName{
Name: randStringRunes(5),
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: time.Minute},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: meta.SecretKeyReference{
Name: secretName,
Key: secretKey,
},
},
SourceRef: kustomizev1.CrossNamespaceSourceReference{
Name: repositoryName.Name,
Namespace: repositoryName.Namespace,
Kind: sourcev1.GitRepositoryKind,
},
TargetNamespace: id,
Prune: true,
},
}

g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed())

resultK := &kustomizev1.Kustomization{}
readyCondition := &metav1.Condition{}

t.Run("fails to reconcile with missing kubeconfig secret", 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 apimeta.IsStatusConditionFalse(resultK.Status.Conditions, meta.ReadyCondition)
}, timeout, time.Second).Should(BeTrue())

g.Expect(readyCondition.Reason).To(Equal(kustomizev1.ReconciliationFailedReason))
g.Expect(readyCondition.Message).To(ContainSubstring(`Secret "%s" not found`, secretName))
})

secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: id,
},
Data: map[string][]byte{
secretKey: kubeConfig,
},
}
g.Expect(k8sClient.Create(context.Background(), secret)).NotTo(HaveOccurred(), "failed to create kubeconfig secret")

t.Run("reconciles successfully after secret is created", func(t *testing.T) {
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 resultK.Status.LastAppliedRevision == revision
}, timeout, time.Second).Should(BeTrue())

g.Expect(readyCondition.Reason).To(Equal(kustomizev1.ReconciliationSucceededReason))
})

}
2 changes: 1 addition & 1 deletion controllers/kustomization_inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ stringData:
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
7 changes: 4 additions & 3 deletions controllers/kustomization_prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/testserver"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -97,7 +98,7 @@ data:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down Expand Up @@ -225,7 +226,7 @@ data:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down Expand Up @@ -369,7 +370,7 @@ data:
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
7 changes: 4 additions & 3 deletions controllers/kustomization_transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/pkg/apis/kustomize"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestKustomizationReconciler_KustomizeTransformer(t *testing.T) {
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down Expand Up @@ -196,7 +197,7 @@ func TestKustomizationReconciler_KustomizeTransformerFiles(t *testing.T) {
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down Expand Up @@ -315,7 +316,7 @@ func TestKustomizationReconciler_FluxTransformers(t *testing.T) {
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
3 changes: 2 additions & 1 deletion controllers/kustomization_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestKustomizationReconciler_Validation(t *testing.T) {
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
KubeConfig: &kustomizev1.KubeConfig{
SecretRef: kustomizev1.SecretRef{
SecretRef: meta.SecretKeyReference{
Name: "kubeconfig",
},
},
Expand Down
Loading

0 comments on commit 4babc32

Please sign in to comment.