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

feat(scaffold): add support for variables #84

Merged
merged 2 commits into from
Aug 2, 2024
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
11 changes: 11 additions & 0 deletions pkg/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ScaffoldOptions struct {
replicas int32
targetCPUUtilizationPercentage int32
targetMemoryUtilizationPercentage int32
variables map[string]string
}

var scaffoldOpts = ScaffoldOptions{}
Expand All @@ -47,6 +48,7 @@ type appConfig struct {
RuntimeConfig string
TargetCPUUtilizationPercentage int32
TargetMemoryUtilizationPercentage int32
Variables map[string]string
}

var manifestStr = `apiVersion: core.spinoperator.dev/v1alpha1
Expand All @@ -61,6 +63,13 @@ spec:
{{- else }}
replicas: {{ .Replicas }}
{{- end}}
{{- if .Variables }}
variables:
{{- range $key, $value := .Variables }}
- name: {{ $key }}
value: {{ $value }}
{{- end }}
{{- end }}
{{- if or .CPULimit .MemoryLimit }}
resources:
limits:
Expand Down Expand Up @@ -253,6 +262,7 @@ func scaffold(opts ScaffoldOptions) ([]byte, error) {
TargetMemoryUtilizationPercentage: opts.targetMemoryUtilizationPercentage,
Autoscaler: opts.autoscaler,
ImagePullSecrets: opts.imagePullSecrets,
Variables: opts.variables,
}

if opts.configfile != "" {
Expand Down Expand Up @@ -312,6 +322,7 @@ func init() {
scaffoldCmd.Flags().StringVarP(&scaffoldOpts.output, "out", "o", "", "Path to file to write manifest yaml")
scaffoldCmd.Flags().StringVarP(&scaffoldOpts.configfile, "runtime-config-file", "c", "", "Path to runtime config file")
scaffoldCmd.Flags().StringSliceVarP(&scaffoldOpts.imagePullSecrets, "image-pull-secret", "s", []string{}, "Secrets in the same namespace to use for pulling the image")
scaffoldCmd.PersistentFlags().StringToStringVarP(&scaffoldOpts.variables, "variable", "v", nil, "Application variable (name=value) to be provided to the application")

if err := scaffoldCmd.MarkFlagRequired("from"); err != nil {
log.Fatal(err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func TestScaffoldOutput(t *testing.T) {
},
expected: "keda_autoscaler.yml",
},
{
name: "variables are provided",
opts: ScaffoldOptions{
from: "ghcr.io/foo/example-app:v0.1.0",
replicas: 2,
executor: "containerd-shim-spin",
variables: map[string]string{
"bar": "yee",
"foo": "yoo",
},
},
expected: "variables.yml",
},
}

for _, tc := range testcases {
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/testdata/variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: core.spinoperator.dev/v1alpha1
kind: SpinApp
metadata:
name: example-app
spec:
image: "ghcr.io/foo/example-app:v0.1.0"
executor: containerd-shim-spin
replicas: 2
variables:
- name: bar
value: yee
- name: foo
value: yoo