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

fix #189: allows secret generation from literals #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions ksops.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type secretFrom struct {
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
BinaryFiles []string `json:"binaryFiles,omitempty" yaml:"binaryFiles,omitempty"`
Envs []string `json:"envs,omitempty" yaml:"envs,omitempty"`
Literals []string `json:"literals,omitempty" yaml:"literals,omitempty"`
Metadata types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
}
Expand Down Expand Up @@ -197,6 +198,11 @@ func generate(raw []byte) (string, error) {
}
}

for _, literal := range secretFrom.Literals {
k, v := parseLiteral(literal)
stringData[k] = v
}

s := kubernetesSecret{
APIVersion: "v1",
Kind: "Secret",
Expand Down Expand Up @@ -243,3 +249,12 @@ func fileKeyPath(file string) (string, string) {
}
return slices[0], slices[1]
}

func parseLiteral(literal string) (string, string) {
k, v, found := strings.Cut(literal, "=")
if !found {
fmt.Fprintf(os.Stderr, "error parsing literal %s", literal)
os.Exit(1)
}
return k, v
}
4 changes: 4 additions & 0 deletions ksops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func TestKSOPSPluginInstallation(t *testing.T) {
name: "KRM From Envs",
dir: "test/krm/envs",
},
{
name: "KRM From Literals",
dir: "test/krm/literals",
},
{
name: "KRM Override Key",
dir: "test/krm/override",
Expand Down
17 changes: 17 additions & 0 deletions test/krm/literals/generate-resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: viaduct.ai/v1
kind: ksops
metadata:
name: ksops-secret-from-generator
annotations:
config.kubernetes.io/function: |
exec:
# if the binary is your PATH, you can do
path: ksops
# otherwise, path should be relative to manifest files, like
# path: ../../../ksops
secretFrom:
- metadata:
name: mysecret
literals:
- password=1f2d1e2e67df
- username=admin
2 changes: 2 additions & 0 deletions test/krm/literals/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generators:
- ./generate-resources.yaml
7 changes: 7 additions & 0 deletions test/krm/literals/want.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: mysecret
stringData:
password: 1f2d1e2e67df
username: admin