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 support for runtime config to scaffold cmd #16

Merged
merged 4 commits into from
Feb 20, 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/spf13/cobra v1.8.0
github.com/spinkube/spin-operator v0.0.0-20240202155520-0b092c8f1da2
github.com/stretchr/testify v1.8.4
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/cli-runtime v0.29.1
Expand Down Expand Up @@ -61,6 +62,7 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
114 changes: 74 additions & 40 deletions pkg/cmd/scaffold.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"bytes"
"encoding/base64"
"fmt"
"log"
"os"
"strings"
Expand All @@ -9,10 +12,20 @@ import (
"github.com/spf13/cobra"
)

type ScaffoldOptions struct {
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 @@ -22,67 +35,88 @@ 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{
Use: "scaffold",
Short: "scaffold SpinApp manifest",
RunE: func(cmd *cobra.Command, args []string) error {
artifact, err := cmd.Flags().GetString("from")
content, err := scaffold(scaffoldOpts)
if err != nil {
return err
}

reference := strings.Split(artifact, ":")[0]
referenceParts := strings.Split(reference, "/")
name := referenceParts[len(referenceParts)-1]
if scaffoldOpts.output != "" {
err = os.WriteFile(scaffoldOpts.output, content, 0644)
if err != nil {
return err
}

replicas, err = cmd.Flags().GetInt32("replicas")
if err != nil {
return err
}
log.Printf("\nSpinApp manifest saved to %s\n", scaffoldOpts.output)
return nil

config := appConfig{
Name: name,
Image: artifact,
Replicas: replicas,
}

tmpl, err := template.New("spinapp").Parse(manifestStr)
if err != nil {
panic(err)
}
fmt.Fprint(os.Stdout, string(content))

output, err := cmd.Flags().GetString("out")
return nil
},
}

func scaffold(opts ScaffoldOptions) ([]byte, error) {
reference := strings.Split(opts.from, ":")[0]
referenceParts := strings.Split(reference, "/")
name := referenceParts[len(referenceParts)-1]

config := appConfig{
Name: name,
Image: opts.from,
Replicas: opts.replicas,
}

if opts.configfile != "" {
raw, err := os.ReadFile(opts.configfile)
if err != nil {
return err
return nil, err
}
if output != "" {
// Create a new file.
file, err := os.Create(output)
if err != nil {
return err
}
defer file.Close()

err = tmpl.Execute(file, config)
if err != nil {
return err
}
config.RuntimeConfig = base64.StdEncoding.EncodeToString(raw)
}

log.Printf("\nSpinApp manifest saved to %s\n", output)
return nil
tmpl, err := template.New("spinapp").Parse(manifestStr)
if err != nil {
return nil, err
}

}
var output bytes.Buffer
err = tmpl.Execute(&output, config)
if err != nil {
return nil, err
}

return tmpl.Execute(os.Stdout, config)
},
return output.Bytes(), nil
}

func init() {
scaffoldCmd.Flags().Int32P("replicas", "r", 2, "Number of replicas for the spin app")
scaffoldCmd.Flags().StringP("from", "f", "", "Reference in the registry of the Spin application")
scaffoldCmd.Flags().StringP("out", "o", "", "path to file to write manifest yaml")
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")
bacongobbler marked this conversation as resolved.
Show resolved Hide resolved

scaffoldCmd.MarkFlagRequired("from")

rootCmd.AddCommand(scaffoldCmd)
Expand Down
47 changes: 47 additions & 0 deletions pkg/cmd/scaffold_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestScaffoldCmd(t *testing.T) {
testcases := []struct {
name string
opts ScaffoldOptions
expected string
}{
{
name: "only image is provided",
opts: ScaffoldOptions{
from: "ghcr.io/foo/example-app:v0.1.0",
replicas: 2,
},
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 {
t.Run(tc.name, func(t *testing.T) {
output, err := scaffold(tc.opts)
require.Nil(t, err)

expectedContent, err := os.ReadFile(filepath.Join("testdata", tc.expected))
require.Nil(t, err)

require.Equal(t, string(expectedContent), string(output))
})
}
}
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"
7 changes: 7 additions & 0 deletions pkg/cmd/testdata/scaffold_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: core.spinoperator.dev/v1
kind: SpinApp
metadata:
name: example-app
spec:
image: "ghcr.io/foo/example-app:v0.1.0"
replicas: 2
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