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
18 changes: 17 additions & 1 deletion commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/exec"

"github.com/coreos/operator-sdk/pkg/generator"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a line here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!


"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)

func NewBuildCmd() *cobra.Command {
Expand All @@ -31,6 +35,7 @@ For example:
const (
build = "./tmp/build/build.sh"
dockerBuild = "./tmp/build/docker_build.sh"
configYaml = "./config/config.yaml"
)

func buildFunc(cmd *cobra.Command, args []string) {
Expand All @@ -53,5 +58,16 @@ func buildFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, err))
}
fmt.Fprintln(os.Stdout, string(o))
// TODO: generates Kubernetes manifests

c := &generator.Config{}
fp, err := ioutil.ReadFile(configYaml)
if err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to read config file %v: (%v)", configYaml, err))
}
if err = yaml.Unmarshal(fp, c); err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to unmarshal config file %v: (%v)", configYaml, err))
}
if err = generator.RenderDeployFiles(c, image); err != nil {
ExitWithError(ExitError, fmt.Errorf("failed to generate deploy/operator.yaml: (%v)", err))
}
}
33 changes: 33 additions & 0 deletions pkg/generator/deploy_tmpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package generator

const operatorYamlTmpl = `apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: {{.KindPlural}}.{{.GroupName}}
spec:
group: {{.GroupName}}
names:
kind: {{.Kind}}
listKind: {{.Kind}}List
plural: {{.KindPlural}}
singular: {{.KindSingular}}
scope: Namespaced
version: {{.Version}}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: {{.ProjectName}}
spec:
replicas: 1
template:
metadata:
labels:
name: {{.ProjectName}}
spec:
containers:
- name: {{.ProjectName}}
image: {{.Image}}
command:
- {{.ProjectName}}
`
47 changes: 47 additions & 0 deletions pkg/generator/gen_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package generator

import (
"fmt"
"io"
"strings"
"text/template"
)

const (
operatorTmplName = "deploy/operator.yaml"
)

// OperatorYaml contains all the customized data needed to generate deploy/operator.yaml for a new operator
// when pairing with operatorYamlTmpl template.
type OperatorYaml struct {
Kind string
KindSingular string
KindPlural string
GroupName string
Version string
ProjectName string
Image string
}

// renderOperatorYaml generates deploy/operator.yaml.
func renderOperatorYaml(w io.Writer, kind, apiVersion, projectName, image string) error {
t := template.New(operatorTmplName)
t, err := t.Parse(operatorYamlTmpl)
if err != nil {
return fmt.Errorf("failed to parse operator yaml template: %v", err)
}

ks := strings.ToLower(kind)
o := OperatorYaml{
Kind: kind,
KindSingular: ks,
// suffix KindSingular with "s" to create KindPlural.
// TODO: make this more grammatically correct for special nouns.
KindPlural: ks + "s",
GroupName: groupName(apiVersion),
Version: version(apiVersion),
ProjectName: projectName,
Image: image,
}
return t.Execute(w, o)
}
10 changes: 10 additions & 0 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
gopkgtoml = "Gopkg.toml"
gopkglock = "Gopkg.lock"
config = "config.yaml"
operatorYaml = deployDir + "/operator.yaml"
)

type Generator struct {
Expand Down Expand Up @@ -149,6 +150,15 @@ func (g *Generator) renderDeploy() error {
return nil
}

// RenderDeployFiles generates "deploy/operator.yaml".
func RenderDeployFiles(c *Config, image string) error {
buf := &bytes.Buffer{}
if err := renderOperatorYaml(buf, c.Kind, c.APIVersion, c.ProjectName, image); err != nil {
return err
}
return ioutil.WriteFile(operatorYaml, buf.Bytes(), defaultFileMode)
}

func (g *Generator) renderTmp() error {
bDir := filepath.Join(g.projectName, buildDir)
if err := os.MkdirAll(bDir, defaultDirFileMode); err != nil {
Expand Down
42 changes: 42 additions & 0 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,45 @@ func TestGenConfig(t *testing.T) {
t.Errorf("want %v, got %v", configExp, buf.String())
}
}

const operatorYamlExp = `apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: appservices.app.example.com
spec:
group: app.example.com
names:
kind: AppService
listKind: AppServiceList
plural: appservices
singular: appservice
scope: Namespaced
version: v1alpha1
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: app-operator
spec:
replicas: 1
template:
metadata:
labels:
name: app-operator
spec:
containers:
- name: app-operator
image: quay.io/coreos/operator-sdk-dev:app-operator
command:
- app-operator
`

func TestGenDeploy(t *testing.T) {
buf := &bytes.Buffer{}
if err := renderOperatorYaml(buf, "AppService", "app.example.com/v1alpha1", "app-operator", "quay.io/coreos/operator-sdk-dev:app-operator"); err != nil {
t.Error(err)
}
if operatorYamlExp != buf.String() {
t.Errorf("want %v, got %v", operatorYamlExp, buf.String())
}
}