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

Add test for varsub in secret #505

Merged
merged 2 commits into from
Dec 2, 2021
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
18 changes: 17 additions & 1 deletion controllers/kustomization_varsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"testing"

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/v1beta1"
Expand All @@ -31,6 +30,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
)

func TestKustomizationReconciler_Varsub(t *testing.T) {
Expand Down Expand Up @@ -58,6 +59,18 @@ metadata:
environment: ${env:=dev}
region: "${_Region}"
zone: "${zone}"
`, name),
},
{
Name: "secret.yaml",
Body: fmt.Sprintf(`---
apiVersion: v1
kind: Secret
metadata:
name: %[1]s
namespace: %[1]s
stringData:
zone: ${zone}
`, name),
},
}
Expand Down Expand Up @@ -145,6 +158,7 @@ metadata:

resultK := &kustomizev1.Kustomization{}
resultSA := &corev1.ServiceAccount{}
resultSecret := &corev1.Secret{}

t.Run("reconciles successfully", func(t *testing.T) {
g.Eventually(func() bool {
Expand All @@ -158,6 +172,7 @@ metadata:
}, timeout, interval).Should(BeTrue())

g.Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: id, Namespace: id}, resultSA)).Should(Succeed())
g.Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: id, Namespace: id}, resultSecret)).Should(Succeed())
})

t.Run("sets status", func(t *testing.T) {
Expand All @@ -170,6 +185,7 @@ metadata:
g.Expect(resultSA.Labels["environment"]).To(Equal("dev"))
g.Expect(resultSA.Labels["region"]).To(Equal("eu-central-1"))
g.Expect(resultSA.Labels["zone"]).To(Equal("az-1b"))
g.Expect(string(resultSecret.Data["zone"])).To(Equal("az-1b"))
})

t.Run("sets owner labels", func(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions docs/spec/v1beta2/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,19 @@ spec:
name: cluster-secret-vars
```

Note that for substituting variables in a secret, `spec.stringData` field must be used i.e

```yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line above this please.

apiVersion: v1
kind: Secret
metadata:
name: secret
namespace: flux-system
type: Opaque
stringData:
token: ${token}
```

The var values which are specified in-line with `substitute`
take precedence over the ones in `substituteFrom`.

Expand Down