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

VaultSecrets allow to specify Secret's labels #102

Merged
merged 2 commits into from
Jan 6, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ $(GOLANGCILINT): $(LOCALBIN)
.PHONY: vault
vault: $(VAULT) ## Download vault locally if necessary.
$(VAULT): $(LOCALBIN)
wget https://releases.hashicorp.com/vault/$(VAULT_VERSION)/vault_$(VAULT_VERSION)_$(GO_OS)_$(GO_ARCH).zip -O $(LOCALBIN)/vault.zip
curl -o $(LOCALBIN)/vault.zip -L https://releases.hashicorp.com/vault/$(VAULT_VERSION)/vault_$(VAULT_VERSION)_$(GO_OS)_$(GO_ARCH).zip
unzip -o $(LOCALBIN)/vault.zip -d $(LOCALBIN)
rm $(LOCALBIN)/vault.zip

Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

// Package v1alpha1 contains API Schema definitions for the vault.finleap.cloud v1alpha1 API group
//+kubebuilder:object:generate=true
//+groupName=vault.finleap.cloud
// +kubebuilder:object:generate=true
// +groupName=vault.finleap.cloud
package v1alpha1

import (
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha1/vaultsecret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type VaultSecretSpec struct {
// Array of vault path references where to gather data from for the secret.
// +optional
DataFrom []VaultSecretDataRef `json:"dataFrom,omitempty"`
// Array of labels for the created secret.
// +optional
SecretLabels map[string]string `json:"secretLabels,omitempty"`
}

// VaultSecretStatus defines the observed state of VaultSecret
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 5 additions & 2 deletions charts/vault-operator/templates/crds.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{{- if .Values.deployCRDs }}
# Generated by 'make manifests'
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
Expand Down Expand Up @@ -183,6 +182,11 @@ spec:
- path
type: object
type: array
secretLabels:
additionalProperties:
type: string
description: Array of labels for the created secret.
type: object
secretName:
description: Optional name of secret which is created by this object.
type: string
Expand Down Expand Up @@ -239,4 +243,3 @@ status:
plural: ""
conditions: []
storedVersions: []
{{- end }}
5 changes: 5 additions & 0 deletions config/crd/bases/vault.finleap.cloud_vaultsecrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ spec:
- path
type: object
type: array
secretLabels:
additionalProperties:
type: string
description: Array of labels for the created secret.
type: object
secretName:
description: Optional name of secret which is created by this object.
type: string
Expand Down
10 changes: 10 additions & 0 deletions controllers/vaultsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ func (r *VaultSecretReconciler) updateSecret(secret *corev1.Secret, vaultSecret
secret.Type = corev1.SecretTypeDockerConfigJson
}

// Update secret labels
if vaultSecret.Spec.SecretLabels != nil && len(vaultSecret.Spec.SecretLabels) > 0 {
if secret.ObjectMeta.Labels == nil {
secret.ObjectMeta.Labels = make(map[string]string)
}
for k, v := range vaultSecret.Spec.SecretLabels {
secret.ObjectMeta.Labels[k] = v
}
}

// Update secret data
if vaultSecret.Spec.Data != nil && len(vaultSecret.Spec.Data) > 0 {
for _, data := range vaultSecret.Spec.Data {
Expand Down
22 changes: 22 additions & 0 deletions controllers/vaultsecret_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,28 @@ var _ = Describe("VaultSecretReconciler", func() {
Expect(s.Type).To(Equal(corev1.SecretTypeTLS))
})
})
It("can specify secret's labels", func() {
Context("new secret", func() {
vs := mustCreateNewVaultSecret(func(spec *vaultv1alpha1.VaultSecretSpec) {
spec.Data[0].Name = "secret-with-labels"
spec.Data = append(spec.Data, vaultv1alpha1.VaultSecretData{
Name: corev1.TLSPrivateKeyKey,
Location: &vaultv1alpha1.VaultSecretLocation{
Path: "app/test/bar",
Field: "baz",
},
})
spec.SecretLabels = map[string]string{"frog": "prince"}
})
mustReconcile(vs)

s := &corev1.Secret{}
Eventually(func() bool {
return k8sClient.Get(ctx, namespacedName(vs), s) == nil
}, timeout, interval).Should(BeTrue())
Expect(s.ObjectMeta.Labels["frog"]).To(Equal("prince"))
})
})
It("can use templating", func() {
Context("with variables", func() {
vs := mustCreateNewVaultSecret(func(spec *vaultv1alpha1.VaultSecretSpec) {
Expand Down