Skip to content

Commit

Permalink
add support for runtime config to scaffold cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Jindal <[email protected]>
  • Loading branch information
rajatjindal committed Feb 16, 2024
1 parent d5bafb5 commit 70c2432
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
40 changes: 34 additions & 6 deletions pkg/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"encoding/base64"
"fmt"
"log"
"os"
Expand All @@ -12,17 +13,19 @@ import (
)

type ScaffoldOptions struct {
from string
replicas int32
output string
from string
replicas int32
output string
configfile string
}

var scaffoldOpts = ScaffoldOptions{}

type appConfig struct {
Name string
Image string
Replicas int32
Name string
Image string
Replicas int32
RuntimeConfig string
}

var manifestStr = `apiVersion: core.spinoperator.dev/v1
Expand All @@ -32,6 +35,20 @@ metadata:
spec:
image: "{{ .Image }}"
replicas: {{ .Replicas }}
{{- if .RuntimeConfig }}
runtimeConfig:
loadFromSecret: {{ .Name }}-runtime-config
{{- end }}
{{ if .RuntimeConfig -}}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ .Name }}-runtime-config
type: Opaque
data:
runtime-config.toml: {{ .RuntimeConfig }}
{{ end -}}
`

var scaffoldCmd = &cobra.Command{
Expand Down Expand Up @@ -71,6 +88,15 @@ func scaffold(opts ScaffoldOptions) ([]byte, error) {
Replicas: opts.replicas,
}

if opts.configfile != "" {
raw, err := os.ReadFile(opts.configfile)
if err != nil {
return nil, err
}

config.RuntimeConfig = base64.StdEncoding.EncodeToString(raw)
}

tmpl, err := template.New("spinapp").Parse(manifestStr)
if err != nil {
return nil, err
Expand All @@ -89,6 +115,8 @@ func init() {
scaffoldCmd.Flags().Int32VarP(&scaffoldOpts.replicas, "replicas", "r", 2, "Number of replicas for the spin app")
scaffoldCmd.Flags().StringVarP(&scaffoldOpts.from, "from", "f", "", "Reference in the registry of the Spin application")
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.MarkFlagRequired("from")

rootCmd.AddCommand(scaffoldCmd)
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func TestScaffoldCmd(t *testing.T) {
},
expected: "scaffold_image.yml",
},
{
name: "runtime config is provided",
opts: ScaffoldOptions{
from: "ghcr.io/foo/example-app:v0.1.0",
replicas: 2,
configfile: "testdata/runtime-config.toml",
},
expected: "scaffold_runtime_config.yml",
},
}

for _, tc := range testcases {
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/testdata/runtime-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
log_dir = "/asdf"
17 changes: 17 additions & 0 deletions pkg/cmd/testdata/scaffold_runtime_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: core.spinoperator.dev/v1
kind: SpinApp
metadata:
name: example-app
spec:
image: "ghcr.io/foo/example-app:v0.1.0"
replicas: 2
runtimeConfig:
loadFromSecret: example-app-runtime-config
---
apiVersion: v1
kind: Secret
metadata:
name: example-app-runtime-config
type: Opaque
data:
runtime-config.toml: bG9nX2RpciA9ICIvYXNkZiIK

0 comments on commit 70c2432

Please sign in to comment.