Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 14e2679

Browse files
Adding Code Generation Source files
Signed-off-by: Christopher Hein <[email protected]>
1 parent e818fd3 commit 14e2679

File tree

14 files changed

+1531
-2
lines changed

14 files changed

+1531
-2
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ tag:
2020

2121
.PHONY: install-aws-codegen
2222
install-aws-codegen:
23-
go get -u github.com/christopherhein/aws-operator-codegen
23+
go install -ldflags "-X main.commit=$(commitSHA) -X main.date=$(dateStr)" ./code-generation/cmd/aws-service-operator-codegen
2424

2525
.PHONY: aws-codegen
2626
aws-codegen:
27-
aws-operator-codegen process
27+
aws-service-operator-codegen process
2828

2929
.PHONY: k8s-codegen
3030
k8s-codegen:

code-generation/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/aws-service-operator-codegen

code-generation/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
commitSHA := $(shell git describe --dirty --always)
2+
dateStr := $(shell date +%s)
3+
4+
.PHONY: build
5+
build:
6+
go build -ldflags "-X main.commit=$(commitSHA) -X main.date=$(dateStr)" ./cmd/aws-service-operator-codegen
7+
8+
.PHONY: install-bindata
9+
install-bindata:
10+
go get -u github.com/jteeuwen/go-bindata/...
11+
12+
.PHONY: update-bindata
13+
update-bindata:
14+
go generate ./pkg/codegen/
15+
16+
.PHONY: rebuild
17+
rebuild: update-bindata build
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
"os"
7+
)
8+
9+
var (
10+
// rootCmd represents the base command when called without any subcommands
11+
rootCmd = &cobra.Command{
12+
Use: "aws-operator-codegen",
13+
Short: "Processes AWS Operator Model files and outputs codegened operators",
14+
Long: `TODO WRITE THIS`,
15+
Run: func(c *cobra.Command, _ []string) {
16+
c.Help()
17+
},
18+
}
19+
)
20+
21+
func main() {
22+
if err := rootCmd.Execute(); err != nil {
23+
fmt.Println(err)
24+
os.Exit(1)
25+
}
26+
}
27+
28+
func init() {
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"github.com/christopherhein/aws-operator/code-generation/pkg/codegen"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var modelPath, rootPath string
9+
10+
var processCmd = &cobra.Command{
11+
Use: "process",
12+
Short: "Process the operator code based on the models files",
13+
Long: ``,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
generation := codegen.New(modelPath, rootPath)
16+
generation.Run()
17+
return
18+
},
19+
}
20+
21+
func init() {
22+
processCmd.Flags().StringVar(&modelPath, "model-path", "models/", "Model path used for regenerating the codebase")
23+
processCmd.Flags().StringVar(&rootPath, "root-path", "./", "Root path used for regenerating the codebase")
24+
rootCmd.AddCommand(processCmd)
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
goVersion "github.com/christopherhein/go-version"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
shortened = false
11+
version = "dev"
12+
commit = "none"
13+
date = "unknown"
14+
versionCmd = &cobra.Command{
15+
Use: "version",
16+
Short: "Version will output the current aws-operator build information",
17+
Long: ``,
18+
Run: func(_ *cobra.Command, _ []string) {
19+
var response string
20+
versionOutput := goVersion.New(version, commit, date)
21+
22+
if shortened {
23+
response = versionOutput.ToShortened()
24+
} else {
25+
response = versionOutput.ToJSON()
26+
}
27+
fmt.Printf("%+v", response)
28+
return
29+
},
30+
}
31+
)
32+
33+
func init() {
34+
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Use shortened output for version information.")
35+
rootCmd.AddCommand(versionCmd)
36+
}
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
// >>>>>>> DO NOT EDIT THIS FILE <<<<<<<<<<
2+
// This file is autogenerated via `aws-operator-codegen process`
3+
// If you'd like the change anything about this file make edits to the .templ
4+
// file in the pkg/codegen/assets directory.
5+
6+
package {{.Spec.Resource.Name}}
7+
8+
import (
9+
"errors"
10+
"github.com/aws/aws-sdk-go/aws"
11+
"github.com/aws/aws-sdk-go/service/cloudformation"
12+
awsV1alpha1 "github.com/christopherhein/aws-operator/pkg/apis/operator.aws/v1alpha1"
13+
"github.com/christopherhein/aws-operator/pkg/config"
14+
"github.com/christopherhein/aws-operator/pkg/helpers"
15+
)
16+
17+
// New generates a new object
18+
func New(config *config.Config, {{.Spec.Resource.Name}} *awsV1alpha1.{{.Spec.Kind}}, topicARN string) *Cloudformation {
19+
return &Cloudformation{
20+
{{.Spec.Kind}}: {{.Spec.Resource.Name}},
21+
config: config,
22+
topicARN: topicARN,
23+
}
24+
}
25+
26+
// Cloudformation defines the {{.Spec.Resource.Name}} cfts
27+
type Cloudformation struct {
28+
config *config.Config
29+
{{.Spec.Kind}} *awsV1alpha1.{{.Spec.Kind}}
30+
topicARN string
31+
}
32+
33+
// StackName returns the name of the stack based on the aws-operator-config
34+
func (s *Cloudformation) StackName() string {
35+
return helpers.StackName(s.config.ClusterName, "{{.Spec.Resource.Name}}", s.{{.Spec.Kind}}.Name, s.{{.Spec.Kind}}.Namespace)
36+
}
37+
38+
// GetOutputs return the stack outputs from the DescribeStacks call
39+
func (s *Cloudformation) GetOutputs() (map[string]string, error) {
40+
outputs := map[string]string{}
41+
sess := s.config.AWSSession
42+
svc := cloudformation.New(sess)
43+
44+
stackInputs := cloudformation.DescribeStacksInput{
45+
StackName: aws.String(s.StackName()),
46+
}
47+
48+
output, err := svc.DescribeStacks(&stackInputs)
49+
if err != nil {
50+
return nil, err
51+
}
52+
// Not sure if this is even possible
53+
if len(output.Stacks) != 1 {
54+
return nil, errors.New("no stacks returned with that stack name")
55+
}
56+
57+
for _, out := range output.Stacks[0].Outputs {
58+
outputs[*out.OutputKey] = *out.OutputValue
59+
}
60+
61+
return outputs, err
62+
}
63+
64+
// CreateStack will create the stack with the supplied params
65+
func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput, err error) {
66+
sess := s.config.AWSSession
67+
svc := cloudformation.New(sess)
68+
69+
cftemplate := helpers.GetCloudFormationTemplate(s.config, "{{.Spec.Resource.Name}}", s.{{.Spec.Kind}}.{{.Spec.Type}}.CloudFormationTemplateName, s.{{.Spec.Kind}}.{{.Spec.Type}}.CloudFormationTemplateNamespace)
70+
71+
stackInputs := cloudformation.CreateStackInput{
72+
StackName: aws.String(s.StackName()),
73+
TemplateURL: aws.String(cftemplate),
74+
NotificationARNs: []*string{
75+
aws.String(s.topicARN),
76+
},
77+
}
78+
79+
resourceName := helpers.CreateParam("ResourceName", s.{{.Spec.Kind}}.Name)
80+
resourceVersion := helpers.CreateParam("ResourceVersion", s.{{.Spec.Kind}}.ResourceVersion)
81+
namespace := helpers.CreateParam("Namespace", s.{{.Spec.Kind}}.Namespace)
82+
clusterName := helpers.CreateParam("ClusterName", s.config.ClusterName)
83+
{{- range $top := .Spec.Body.Schema.Properties}}
84+
{{- if .Properties}}
85+
{{- range $nested := .Properties}}
86+
87+
{{- if .Templatized}}
88+
{{$top.Key}}{{$nested.Key}}Temp := "{{$nested.Template}}"
89+
{{- else}}
90+
{{$top.Key}}{{$nested.Key}}Temp := "{{"{{"}}.Obj.{{$.Spec.Type}}.{{$top.StructKey}}.{{.StructKey}}{{"}}"}}"
91+
{{- end}}
92+
{{$top.Key}}{{$nested.Key}}Value, err := helpers.Templatize({{$top.Key}}{{$nested.Key}}Temp, helpers.Data{Obj: s.{{$.Spec.Kind}}, Config: s.config, Helpers: helpers.New()})
93+
if err != nil {
94+
return output, err
95+
}
96+
{{$top.Key}}{{$nested.Key}} := helpers.CreateParam("{{$nested.TemplateKey}}", helpers.Stringify({{$top.Key}}{{$nested.Key}}Value))
97+
{{- end}}
98+
{{- else}}
99+
{{- if (eq $top.Type "resourceName")}}
100+
{{$top.Key}} := helpers.CreateParam("{{$top.TemplateKey}}", helpers.Stringify(s.{{$.Spec.Kind}}.Name))
101+
{{- else}}
102+
103+
{{- if .Templatized}}
104+
{{$top.Key}}Temp := "{{$top.Template}}"
105+
{{- else}}
106+
{{$top.Key}}Temp := "{{"{{"}}.Obj.{{$.Spec.Type}}.{{$top.StructKey}}{{"}}"}}"
107+
{{- end}}
108+
{{$top.Key}}Value, err := helpers.Templatize({{$top.Key}}Temp, helpers.Data{Obj: s.{{$.Spec.Kind}}, Config: s.config, Helpers: helpers.New()})
109+
if err != nil {
110+
return output, err
111+
}
112+
{{$top.Key}} := helpers.CreateParam("{{$top.TemplateKey}}", helpers.Stringify({{$top.Key}}Value))
113+
{{- end}}
114+
{{- end}}
115+
{{- end}}
116+
117+
parameters := []*cloudformation.Parameter{}
118+
parameters = append(parameters, resourceName)
119+
parameters = append(parameters, resourceVersion)
120+
parameters = append(parameters, namespace)
121+
parameters = append(parameters, clusterName)
122+
{{- range $top := .Spec.Body.Schema.Properties}}
123+
{{- if .Properties}}
124+
{{- range $nested := .Properties}}
125+
parameters = append(parameters, {{$top.Key}}{{$nested.Key}})
126+
{{- end}}
127+
{{- else}}
128+
parameters = append(parameters, {{$top.Key}})
129+
{{- end}}
130+
{{- end}}
131+
132+
stackInputs.SetParameters(parameters)
133+
134+
resourceNameTag := helpers.CreateTag("ResourceName", s.{{.Spec.Kind}}.Name)
135+
resourceVersionTag := helpers.CreateTag("ResourceVersion", s.{{.Spec.Kind}}.ResourceVersion)
136+
namespaceTag := helpers.CreateTag("Namespace", s.{{.Spec.Kind}}.Namespace)
137+
clusterNameTag := helpers.CreateTag("ClusterName", s.config.ClusterName)
138+
139+
tags := []*cloudformation.Tag{}
140+
tags = append(tags, resourceNameTag)
141+
tags = append(tags, resourceVersionTag)
142+
tags = append(tags, namespaceTag)
143+
tags = append(tags, clusterNameTag)
144+
145+
stackInputs.SetTags(tags)
146+
147+
output, err = svc.CreateStack(&stackInputs)
148+
return
149+
}
150+
151+
// UpdateStack will update the existing stack
152+
func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.{{.Spec.Kind}}) (output *cloudformation.UpdateStackOutput, err error) {
153+
sess := s.config.AWSSession
154+
svc := cloudformation.New(sess)
155+
156+
cftemplate := helpers.GetCloudFormationTemplate(s.config, "{{.Spec.Resource.Name}}", updated.{{.Spec.Type}}.CloudFormationTemplateName, updated.{{.Spec.Type}}.CloudFormationTemplateNamespace)
157+
158+
stackInputs := cloudformation.UpdateStackInput{
159+
StackName: aws.String(s.StackName()),
160+
TemplateURL: aws.String(cftemplate),
161+
NotificationARNs: []*string{
162+
aws.String(s.topicARN),
163+
},
164+
}
165+
166+
resourceName := helpers.CreateParam("ResourceName", s.{{.Spec.Kind}}.Name)
167+
resourceVersion := helpers.CreateParam("ResourceVersion", s.{{.Spec.Kind}}.ResourceVersion)
168+
namespace := helpers.CreateParam("Namespace", s.{{.Spec.Kind}}.Namespace)
169+
clusterName := helpers.CreateParam("ClusterName", s.config.ClusterName)
170+
{{- range $top := .Spec.Body.Schema.Properties}}
171+
{{- if .Properties}}
172+
{{- range $nested := .Properties}}
173+
174+
{{- if .Templatized}}
175+
{{$top.Key}}{{$nested.Key}}Temp := "{{$nested.Template}}"
176+
{{- else}}
177+
{{$top.Key}}{{$nested.Key}}Temp := "{{"{{"}}.Obj.{{$.Spec.Type}}.{{$top.StructKey}}.{{.StructKey}}{{"}}"}}"
178+
{{- end}}
179+
{{$top.Key}}{{$nested.Key}}Value, err := helpers.Templatize({{$top.Key}}{{$nested.Key}}Temp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
180+
if err != nil {
181+
return output, err
182+
}
183+
{{$top.Key}}{{$nested.Key}} := helpers.CreateParam("{{$nested.TemplateKey}}", helpers.Stringify({{$top.Key}}{{$nested.Key}}Value))
184+
{{- end}}
185+
{{- else}}
186+
{{- if (eq $top.Type "resourceName")}}
187+
{{$top.Key}} := helpers.CreateParam("{{$top.TemplateKey}}", helpers.Stringify(s.{{$.Spec.Kind}}.Name))
188+
{{- else}}
189+
190+
{{- if .Templatized}}
191+
{{$top.Key}}Temp := "{{$top.Template}}"
192+
{{- else}}
193+
{{$top.Key}}Temp := "{{"{{"}}.Obj.{{$.Spec.Type}}.{{$top.StructKey}}{{"}}"}}"
194+
{{- end}}
195+
{{$top.Key}}Value, err := helpers.Templatize({{$top.Key}}Temp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
196+
if err != nil {
197+
return output, err
198+
}
199+
{{$top.Key}} := helpers.CreateParam("{{$top.TemplateKey}}", helpers.Stringify({{$top.Key}}Value))
200+
{{- end}}
201+
{{- end}}
202+
{{- end}}
203+
204+
parameters := []*cloudformation.Parameter{}
205+
parameters = append(parameters, resourceName)
206+
parameters = append(parameters, resourceVersion)
207+
parameters = append(parameters, namespace)
208+
parameters = append(parameters, clusterName)
209+
{{- range $top := .Spec.Body.Schema.Properties}}
210+
{{- if .Properties}}
211+
{{- range $nested := .Properties}}
212+
parameters = append(parameters, {{$top.Key}}{{$nested.Key}})
213+
{{- end}}
214+
{{- else}}
215+
parameters = append(parameters, {{$top.Key}})
216+
{{- end}}
217+
{{- end}}
218+
219+
stackInputs.SetParameters(parameters)
220+
221+
resourceNameTag := helpers.CreateTag("ResourceName", s.{{.Spec.Kind}}.Name)
222+
resourceVersionTag := helpers.CreateTag("ResourceVersion", s.{{.Spec.Kind}}.ResourceVersion)
223+
namespaceTag := helpers.CreateTag("Namespace", s.{{.Spec.Kind}}.Namespace)
224+
clusterNameTag := helpers.CreateTag("ClusterName", s.config.ClusterName)
225+
226+
tags := []*cloudformation.Tag{}
227+
tags = append(tags, resourceNameTag)
228+
tags = append(tags, resourceVersionTag)
229+
tags = append(tags, namespaceTag)
230+
tags = append(tags, clusterNameTag)
231+
232+
stackInputs.SetTags(tags)
233+
234+
output, err = svc.UpdateStack(&stackInputs)
235+
return
236+
}
237+
238+
// DeleteStack will delete the stack
239+
func (s *Cloudformation) DeleteStack() (err error) {
240+
sess := s.config.AWSSession
241+
svc := cloudformation.New(sess)
242+
243+
stackInputs := cloudformation.DeleteStackInput{}
244+
stackInputs.SetStackName(s.StackName())
245+
246+
_, err = svc.DeleteStack(&stackInputs)
247+
return
248+
}
249+
250+
// WaitUntilStackDeleted will delete the stack
251+
func (s *Cloudformation) WaitUntilStackDeleted() (err error) {
252+
sess := s.config.AWSSession
253+
svc := cloudformation.New(sess)
254+
255+
stackInputs := cloudformation.DescribeStacksInput{
256+
StackName: aws.String(s.StackName()),
257+
}
258+
259+
err = svc.WaitUntilStackDeleteComplete(&stackInputs)
260+
return
261+
}

0 commit comments

Comments
 (0)