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

Commit

Permalink
Adding Code Generation Files and Supporting the Resource lists
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hein <me@christopherhein.com>
  • Loading branch information
christopherhein committed Oct 16, 2018
1 parent daa0ac6 commit ffdc6d1
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 167 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
commitSHA := $(shell git describe --dirty --always)
dateStr := $(shell date +%s)
repo ?= github.com/awslab/aws-service-operator

.PHONY: build
build:
Expand All @@ -13,6 +14,10 @@ release:
dev-release:
goreleaser --rm-dist --snapshot --skip-publish

.PHONY: test
test:
go test -v -cover -race $(repo)/...

.PHONY: tag
tag:
git tag -a ${VERSION} -s
Expand Down
52 changes: 40 additions & 12 deletions cmd/aws-service-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ package main

import (
"fmt"
homedir "github.com/mitchellh/go-homedir"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/awslabs/aws-service-operator/pkg/config"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)

var (
// cfgFile, kubeConfig, awsRegion all help support passed in flags into the server
cfgFile, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket, accountID string
// cfgFile, masterURL, kubeConfig, awsRegion all help support passed in flags into the server
cfgFile, masterURL, kubeconfig, awsRegion, logLevel, logFile, resources, clusterName, bucket, accountID string

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

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "f", "Config file (default is $HOME/.aws-operator.yaml)")
rootCmd.PersistentFlags().StringVarP(&masterURL, "master-url", "u", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig.")
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to local kubeconfig file (mainly used for development)")
rootCmd.PersistentFlags().StringVarP(&awsRegion, "region", "r", "us-west-2", "AWS Region for resources to be created in")
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "Info", "Log level for the CLI")
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "", "", "Log level for the CLI")
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "s3bucket,dynamodb", "Comma delimited list of CRDs to deploy")
rootCmd.PersistentFlags().StringVarP(&resources, "resources", "", "cloudformationtemplates,dynamodb,ecrrepository,s3bucket,snssubscription,snstopic,sqsqueue", "Comma delimited list of CRDs to deploy")
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")
rootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "aws-operator", "To configure the operator you need a base bucket to contain the resources")
rootCmd.PersistentFlags().StringVarP(&accountID, "account-id", "a", "", "AWS Account ID, this is used to configure outputs and operate on the proper account.")

viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("masterurl", rootCmd.PersistentFlags().Lookup("master-url"))
viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
viper.BindPFlag("region", rootCmd.PersistentFlags().Lookup("region"))
viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))
Expand Down Expand Up @@ -83,22 +86,47 @@ func initConfig() {
}
}

func getConfig() (*config.Config, error) {
resourcesList := strings.Split(resources, ",")
config := &config.Config{
func getConfig() (c *config.Config, err error) {
resourcesMap := map[string]bool{}
for _, r := range strings.Split(resources, ",") {
resourcesMap[r] = true
}

ec2Session, err := session.NewSession()
metadata := ec2metadata.New(ec2Session)
if awsRegion == "" {
awsRegion, err = metadata.Region()
if err != nil {
return nil, err
}
}

sess, err := session.NewSession(&aws.Config{Region: aws.String(awsRegion)})
if err != nil {
return nil, err
}

c = &config.Config{
Region: awsRegion,
Kubeconfig: kubeconfig,
MasterURL: masterURL,
AWSSession: sess,
LoggingConfig: &config.LoggingConfig{
File: logFile,
Level: logLevel,
FullTimestamps: true,
DisableTimestamps: false,
},
Resources: resourcesList,
Resources: resourcesMap,
ClusterName: clusterName,
Bucket: bucket,
AccountID: accountID,
}

return config, nil
err = c.CreateContext(masterURL, kubeconfig)
if err != nil {
return nil, err
}

return c, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ items:
- {{$elem.Name}}
{{- end}}
singular: {{$element.Spec.Resource.Plural}}
scope: Namespaced
scope: {{$element.Spec.Resource.Scope}}
version: v1alpha1
{{end}}
- kind: ClusterRole
Expand Down
20 changes: 7 additions & 13 deletions code-generation/pkg/codegen/assets/base.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@ package base

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

type base struct {
config *config.Config
context *opkit.Context
awsClientset awsclient.ServiceoperatorV1alpha1Interface
}

func New(
config *config.Config,
context *opkit.Context,
awsClientset awsclient.ServiceoperatorV1alpha1Interface,
) *base {
return &base{
config: config,
context: context,
awsClientset: awsClientset,
}
}

func (b *base) Watch(namespace string, stopCh chan struct{}) (err error) {
{{- range $index, $element := .Items}}
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config, b.context, b.awsClientset)
err = {{$element.Spec.Resource.Name}}operator.StartWatch(namespace, stopCh)
if err != nil {
return err
}
if b.config.Resources["{{$element.Spec.Resource.Name}}"] {
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config)
err = {{$element.Spec.Resource.Name}}operator.StartWatch(namespace, stopCh)
if err != nil {
return err
}
}
{{- end}}

return nil
Expand Down
38 changes: 7 additions & 31 deletions code-generation/pkg/codegen/assets/operator.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,36 @@ import (
{{- if .Spec.IsCustomized}}
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/awslabs/aws-service-operator/pkg/helpers"
{{- end}}
"reflect"
{{- end}}

"github.com/awslabs/aws-service-operator/pkg/config"
{{- if .Spec.Queue}}
"github.com/awslabs/aws-service-operator/pkg/queue"
corev1 "k8s.io/api/core/v1"
"github.com/iancoleman/strcase"
"strings"
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
{{- end}}
opkit "github.com/christopherhein/operator-kit"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"github.com/awslabs/aws-service-operator/pkg/operator"
"k8s.io/client-go/tools/cache"

awsapi "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws"
awsV1alpha1 "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws/v1alpha1"
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
{{- if .Spec.Customizations.Package}}
"{{.Spec.Customizations.Package}}"
{{- end}}
)

// Resource is the object store definition
var Resource = opkit.CustomResource{
Name: "{{.Spec.Resource.Name}}",
Plural: "{{.Spec.Resource.Plural}}",
Group: awsapi.GroupName,
Version: awsapi.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Kind: reflect.TypeOf(awsV1alpha1.{{.Spec.Kind}}{}).Name(),
ShortNames: []string{
{{- if .Spec.Resource.Shortnames -}}
{{- range .Spec.Resource.Shortnames }}
"{{.Name}}",
{{- end}}
{{- end}}
},
}

// Operator represents a controller object for object store custom resources
type Operator struct {
config *config.Config
context *opkit.Context
awsclientset awsclient.ServiceoperatorV1alpha1Interface
topicARN string
}

// NewOperator create controller for watching object store custom resources created
func NewOperator(config *config.Config, context *opkit.Context, awsclientset awsclient.ServiceoperatorV1alpha1Interface) *Operator {
func NewOperator(config *config.Config) *Operator {
return &Operator{
config: config,
context: context,
awsclientset: awsclientset,
}
}

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

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

return nil
}
Expand Down
Loading

0 comments on commit ffdc6d1

Please sign in to comment.