Skip to content
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
31 changes: 31 additions & 0 deletions pkg/generator/gen_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generator

import (
"io"

yaml "gopkg.in/yaml.v2"
)

type Config struct {
// APIVersion is the kubernetes apiVersion that has the format of $GROUP_NAME/$VERSION.
APIVersion string `yaml:"apiVersion"`
// Kind is the kubernetes resource kind.
Kind string `yaml:"kind"`
// ProjectName is name of the new operator application
// and is also the name of the base directory.
ProjectName string `yaml:"projectName"`
}

func renderConfigFile(w io.Writer, apiVersion, kind, projectName string) error {
o, err := yaml.Marshal(&Config{
APIVersion: apiVersion,
Kind: kind,
ProjectName: projectName,
})
if err != nil {
return err
}

_, err = w.Write(o)
return err
}
15 changes: 12 additions & 3 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
updateGenerated = "update-generated.sh"
gopkgtoml = "Gopkg.toml"
gopkglock = "Gopkg.lock"
config = "config.yaml"
)

type Generator struct {
Expand Down Expand Up @@ -125,11 +126,19 @@ func renderCmdFiles(cmdProjectDir, repoPath string) error {
}

func (g *Generator) renderConfig() error {
if err := os.MkdirAll(filepath.Join(g.projectName, configDir), defaultDirFileMode); err != nil {
cp := filepath.Join(g.projectName, configDir)
if err := os.MkdirAll(cp, defaultDirFileMode); err != nil {
return err
}
// TODO render files.
return nil
return renderConfigFiles(cp, g.apiVersion, g.kind, g.projectName)
}

func renderConfigFiles(configDir, apiVersion, kind, projectName string) error {
buf := &bytes.Buffer{}
if err := renderConfigFile(buf, apiVersion, kind, projectName); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(configDir, config), buf.Bytes(), defaultFileMode)
}

func (g *Generator) renderDeploy() error {
Expand Down
15 changes: 15 additions & 0 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,18 @@ func TestGenGopkg(t *testing.T) {
t.Errorf("want %v, got %v", gopkgLockTmpl, buf.String())
}
}

const configExp = `apiVersion: app.example.com/v1alpha1
kind: AppService
projectName: app-operator
`

func TestGenConfig(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderConfigFile(buf, "app.example.com/v1alpha1", "AppService", "app-operator"); err != nil {
t.Error(err)
}
if configExp != buf.String() {
t.Errorf("want %v, got %v", configExp, buf.String())
}
}