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

Commit ffdc6d1

Browse files
Adding Code Generation Files and Supporting the Resource lists
Signed-off-by: Christopher Hein <[email protected]>
1 parent daa0ac6 commit ffdc6d1

File tree

21 files changed

+166
-167
lines changed

21 files changed

+166
-167
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
commitSHA := $(shell git describe --dirty --always)
22
dateStr := $(shell date +%s)
3+
repo ?= github.com/awslab/aws-service-operator
34

45
.PHONY: build
56
build:
@@ -13,6 +14,10 @@ release:
1314
dev-release:
1415
goreleaser --rm-dist --snapshot --skip-publish
1516

17+
.PHONY: test
18+
test:
19+
go test -v -cover -race $(repo)/...
20+
1621
.PHONY: tag
1722
tag:
1823
git tag -a ${VERSION} -s

cmd/aws-service-operator/main.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ package main
22

33
import (
44
"fmt"
5-
homedir "github.com/mitchellh/go-homedir"
6-
5+
"github.com/aws/aws-sdk-go/aws"
6+
"github.com/aws/aws-sdk-go/aws/ec2metadata"
7+
"github.com/aws/aws-sdk-go/aws/session"
78
"github.com/awslabs/aws-service-operator/pkg/config"
8-
9+
homedir "github.com/mitchellh/go-homedir"
910
"github.com/spf13/cobra"
1011
"github.com/spf13/viper"
1112
"os"
1213
"strings"
1314
)
1415

1516
var (
16-
// cfgFile, kubeConfig, awsRegion all help support passed in flags into the server
17-
cfgFile, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket, accountID string
17+
// cfgFile, masterURL, kubeConfig, awsRegion all help support passed in flags into the server
18+
cfgFile, masterURL, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket, accountID string
1819

1920
// rootCmd represents the base command when called without any subcommands
2021
rootCmd = &cobra.Command{
2122
Use: "aws-operator",
2223
Short: "AWS Operator manages your AWS Infrastructure using CRDs and Operators",
23-
Long: `AWS Operator manages your AWS Infrastructure using CRDs and Operators.
24+
Long: `AWS Operator manages your AWS Infrastructure using CRDs and Operators.
2425
With a single manifest file you can now model both the application and the resource necessary to run it.`,
2526
Run: func(c *cobra.Command, _ []string) {
2627
c.Help()
@@ -39,16 +40,18 @@ func main() {
3940

4041
func init() {
4142
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "f", "Config file (default is $HOME/.aws-operator.yaml)")
43+
rootCmd.PersistentFlags().StringVarP(&masterURL, "master-url", "u", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig.")
4244
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to local kubeconfig file (mainly used for development)")
4345
rootCmd.PersistentFlags().StringVarP(&awsRegion, "region", "r", "us-west-2", "AWS Region for resources to be created in")
4446
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "Info", "Log level for the CLI")
4547
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "Log level for the CLI")
46-
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "s3bucket,dynamodb", "Comma delimited list of CRDs to deploy")
48+
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "cloudformationtemplates,dynamodb,ecrrepository,s3bucket,snssubscription,snstopic,sqsqueue", "Comma delimited list of CRDs to deploy")
4749
rootCmd.PersistentFlags().StringVarP(&clusterName, "cluster-name", "i", "aws-operator", "Cluster name for the Application to run as, used to label the Cloudformation templated to avoid conflict")
4850
rootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "aws-operator", "To configure the operator you need a base bucket to contain the resources")
4951
rootCmd.PersistentFlags().StringVarP(&accountID, "account-id", "a", "", "AWS Account ID, this is used to configure outputs and operate on the proper account.")
5052

5153
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
54+
viper.BindPFlag("masterurl", rootCmd.PersistentFlags().Lookup("master-url"))
5255
viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
5356
viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region"))
5457
viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))
@@ -83,22 +86,47 @@ func initConfig() {
8386
}
8487
}
8588

86-
func getConfig() (*config.Config, error) {
87-
resourcesList := strings.Split(resources, ",")
88-
config := &config.Config{
89+
func getConfig() (c *config.Config, err error) {
90+
resourcesMap := map[string]bool{}
91+
for _, r := range strings.Split(resources, ",") {
92+
resourcesMap[r] = true
93+
}
94+
95+
ec2Session, err := session.NewSession()
96+
metadata := ec2metadata.New(ec2Session)
97+
if awsRegion == "" {
98+
awsRegion, err = metadata.Region()
99+
if err != nil {
100+
return nil, err
101+
}
102+
}
103+
104+
sess, err := session.NewSession(&aws.Config{Region: aws.String(awsRegion)})
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
c = &config.Config{
89110
Region: awsRegion,
90111
Kubeconfig: kubeconfig,
112+
MasterURL: masterURL,
113+
AWSSession: sess,
91114
LoggingConfig: &config.LoggingConfig{
92115
File: logFile,
93116
Level: logLevel,
94117
FullTimestamps: true,
95118
DisableTimestamps: false,
96119
},
97-
Resources: resourcesList,
120+
Resources: resourcesMap,
98121
ClusterName: clusterName,
99122
Bucket: bucket,
100123
AccountID: accountID,
101124
}
102125

103-
return config, nil
126+
err = c.CreateContext(masterURL, kubeconfig)
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
return c, nil
104132
}

code-generation/pkg/codegen/assets/aws-service-operator.yaml.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ items:
2626
- {{$elem.Name}}
2727
{{- end}}
2828
singular: {{$element.Spec.Resource.Plural}}
29-
scope: Namespaced
29+
scope: {{$element.Spec.Resource.Scope}}
3030
version: v1alpha1
3131
{{end}}
3232
- kind: ClusterRole

code-generation/pkg/codegen/assets/base.go.templ

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,32 @@ package base
22

33
import (
44
"github.com/awslabs/aws-service-operator/pkg/config"
5-
opkit "github.com/christopherhein/operator-kit"
6-
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
75
{{- range $index, $element := .Items}}
86
"github.com/awslabs/aws-service-operator/pkg/operators/{{$element.Spec.Resource.Name}}"
97
{{- end}}
108
)
119

1210
type base struct {
1311
config *config.Config
14-
context *opkit.Context
15-
awsClientset awsclient.ServiceoperatorV1alpha1Interface
1612
}
1713

1814
func New(
1915
config *config.Config,
20-
context *opkit.Context,
21-
awsClientset awsclient.ServiceoperatorV1alpha1Interface,
2216
) *base {
2317
return &base{
2418
config: config,
25-
context: context,
26-
awsClientset: awsClientset,
2719
}
2820
}
2921

3022
func (b *base) Watch(namespace string, stopCh chan struct{}) (err error) {
3123
{{- range $index, $element := .Items}}
32-
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config, b.context, b.awsClientset)
33-
err = {{$element.Spec.Resource.Name}}operator.StartWatch(namespace, stopCh)
34-
if err != nil {
35-
return err
36-
}
24+
if b.config.Resources["{{$element.Spec.Resource.Name}}"] {
25+
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config)
26+
err = {{$element.Spec.Resource.Name}}operator.StartWatch(namespace, stopCh)
27+
if err != nil {
28+
return err
29+
}
30+
}
3731
{{- end}}
3832

3933
return nil

code-generation/pkg/codegen/assets/operator.go.templ

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,36 @@ import (
99
{{- if .Spec.IsCustomized}}
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
"github.com/awslabs/aws-service-operator/pkg/helpers"
12-
{{- end}}
1312
"reflect"
13+
{{- end}}
1414

1515
"github.com/awslabs/aws-service-operator/pkg/config"
1616
{{- if .Spec.Queue}}
1717
"github.com/awslabs/aws-service-operator/pkg/queue"
1818
corev1 "k8s.io/api/core/v1"
1919
"github.com/iancoleman/strcase"
2020
"strings"
21+
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
2122
{{- end}}
22-
opkit "github.com/christopherhein/operator-kit"
23-
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
23+
"github.com/awslabs/aws-service-operator/pkg/operator"
2424
"k8s.io/client-go/tools/cache"
2525

26-
awsapi "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws"
2726
awsV1alpha1 "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws/v1alpha1"
28-
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
2927
{{- if .Spec.Customizations.Package}}
3028
"{{.Spec.Customizations.Package}}"
3129
{{- end}}
3230
)
3331

34-
// Resource is the object store definition
35-
var Resource = opkit.CustomResource{
36-
Name: "{{.Spec.Resource.Name}}",
37-
Plural: "{{.Spec.Resource.Plural}}",
38-
Group: awsapi.GroupName,
39-
Version: awsapi.Version,
40-
Scope: apiextensionsv1beta1.NamespaceScoped,
41-
Kind: reflect.TypeOf(awsV1alpha1.{{.Spec.Kind}}{}).Name(),
42-
ShortNames: []string{
43-
{{- if .Spec.Resource.Shortnames -}}
44-
{{- range .Spec.Resource.Shortnames }}
45-
"{{.Name}}",
46-
{{- end}}
47-
{{- end}}
48-
},
49-
}
50-
5132
// Operator represents a controller object for object store custom resources
5233
type Operator struct {
5334
config *config.Config
54-
context *opkit.Context
55-
awsclientset awsclient.ServiceoperatorV1alpha1Interface
5635
topicARN string
5736
}
5837

5938
// NewOperator create controller for watching object store custom resources created
60-
func NewOperator(config *config.Config, context *opkit.Context, awsclientset awsclient.ServiceoperatorV1alpha1Interface) *Operator {
39+
func NewOperator(config *config.Config) *Operator {
6140
return &Operator{
6241
config: config,
63-
context: context,
64-
awsclientset: awsclientset,
6542
}
6643
}
6744

@@ -73,14 +50,13 @@ func (c *Operator) StartWatch(namespace string, stopCh chan struct{}) error {
7350
DeleteFunc: c.onDelete,
7451
}
7552
{{- if .Spec.Queue}}
76-
queuectrl := queue.New(c.config, c.context, c.awsclientset, 1)
53+
queuectrl := queue.New(c.config, c.config.AWSClientset, 1)
7754
c.topicARN, _, _, _ = queuectrl.Register("{{.Spec.Resource.Name}}", &awsV1alpha1.{{.Spec.Kind}}{})
7855
go queuectrl.StartWatch(queue.HandlerFunc(QueueUpdater), stopCh)
7956
{{- end}}
8057

81-
restClient := c.awsclientset.RESTClient()
82-
watcher := opkit.NewWatcher(Resource, namespace, resourceHandlers, restClient)
83-
go watcher.Watch(&awsV1alpha1.{{.Spec.Kind}}{}, stopCh)
58+
oper := operator.New("{{.Spec.Resource.Plural}}", namespace, resourceHandlers, c.config.AWSClientset.RESTClient())
59+
go oper.Watch(&awsV1alpha1.{{.Spec.Kind}}{}, stopCh)
8460

8561
return nil
8662
}

0 commit comments

Comments
 (0)