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

Commit

Permalink
Merge pull request #142 from christopherhein/chore/refactor-config-po…
Browse files Browse the repository at this point in the history
…inter

Refactor Out Config Pointer
  • Loading branch information
Christopher Hein committed Dec 5, 2018
2 parents 7ff2311 + 6bb21f3 commit b51e058
Show file tree
Hide file tree
Showing 35 changed files with 200 additions and 187 deletions.
62 changes: 42 additions & 20 deletions cmd/aws-service-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/awslabs/aws-service-operator/pkg/config"
"github.com/awslabs/aws-service-operator/pkg/logger"
"github.com/awslabs/aws-service-operator/pkg/queue"
goVersion "github.com/christopherhein/go-version"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -90,7 +93,14 @@ func initConfig() {
}
}

func getConfig() (c *config.Config, err error) {
func getConfig() (c config.Config, err error) {
loggingConfig := config.LoggingConfig{
File: logFile,
Level: logLevel,
FullTimestamps: true,
DisableTimestamps: false,
}

resourcesMap := map[string]bool{}
for _, r := range strings.Split(resources, ",") {
resourcesMap[r] = true
Expand All @@ -101,37 +111,49 @@ func getConfig() (c *config.Config, err error) {
if awsRegion == "" {
awsRegion, err = metadata.Region()
if err != nil {
return nil, err
return c, err
}
}

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

c = &config.Config{
Region: awsRegion,
Kubeconfig: kubeconfig,
MasterURL: masterURL,
AWSSession: sess,
LoggingConfig: &config.LoggingConfig{
File: logFile,
Level: logLevel,
FullTimestamps: true,
DisableTimestamps: false,
},
awsclientset, kubeclientset, restconfig, err := config.CreateContext(masterURL, kubeconfig)
if err != nil {
return c, err
}

logger, err := logger.Configure(loggingConfig)
if err != nil {
return c, err
}

queueURL, queueARN, err := queue.RegisterQueue(sess, clusterName, "cloudformation")
if err != nil {
return c, err
}

c = config.Config{
Region: awsRegion,
Kubeconfig: kubeconfig,
MasterURL: masterURL,
Logger: logger,
Version: goVersion.New(version, commit, date),
AWSSession: sess,
LoggingConfig: loggingConfig,
AWSClientset: awsclientset,
KubeClientset: kubeclientset,
RESTConfig: restconfig,
Recorder: config.CreateRecorder(logger, kubeclientset),
Resources: resourcesMap,
ClusterName: clusterName,
Bucket: bucket,
AccountID: accountID,
DefaultNamespace: defaultNamespace,
QueueURL: queueURL,
QueueARN: queueARN,
}

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

return c, nil
}
9 changes: 1 addition & 8 deletions cmd/aws-service-operator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os/signal"
"syscall"

"github.com/awslabs/aws-service-operator/pkg/logger"
"github.com/awslabs/aws-service-operator/pkg/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -22,20 +21,14 @@ var serverCmd = &cobra.Command{
logrus.Fatalf("%s", err)
}

logger, err := logger.Configure(config.LoggingConfig)
if err != nil {
logrus.Fatalf("Failed to configure logging: '%s'" + err.Error())
}
config.Logger = logger

ctx, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

go server.New(config).Run(ctx)

<-signalChan
logger.Info("shutdown signal received, exiting...")
config.Logger.Info("shutdown signal received, exiting...")
cancel()
},
}
Expand Down
4 changes: 2 additions & 2 deletions code-generation/pkg/codegen/assets/base.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

type base struct {
config *config.Config
config config.Config
queueManager *queuemanager.QueueManager
{{- range $index, $element := .Items}}
{{$element.Spec.Resource.Name}} *{{$element.Spec.Resource.Name}}.Operator
{{- end}}
}

func New(
config *config.Config,
config config.Config,
queueManager *queuemanager.QueueManager,
) *base {
return &base{
Expand Down
4 changes: 2 additions & 2 deletions code-generation/pkg/codegen/assets/cft.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// New generates a new object
func New(config *config.Config, {{.Spec.Resource.Name}} *awsV1alpha1.{{.Spec.Kind}}, topicARN string) *Cloudformation {
func New(config config.Config, {{.Spec.Resource.Name}} *awsV1alpha1.{{.Spec.Kind}}, topicARN string) *Cloudformation {
return &Cloudformation{
{{.Spec.Kind}}: {{.Spec.Resource.Name}},
config: config,
Expand All @@ -25,7 +25,7 @@ func New(config *config.Config, {{.Spec.Resource.Name}} *awsV1alpha1.{{.Spec.Kin

// Cloudformation defines the {{.Spec.Resource.Name}} cfts
type Cloudformation struct {
config *config.Config
config config.Config
{{.Spec.Kind}} *awsV1alpha1.{{.Spec.Kind}}
topicARN string
}
Expand Down
14 changes: 7 additions & 7 deletions code-generation/pkg/codegen/assets/operator.go.templ
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (

// Operator represents a controller object for object store custom resources
type Operator struct {
config *config.Config
config config.Config
topicARN string
queueManager *queuemanager.QueueManager
}

// NewOperator create controller for watching object store custom resources created
func NewOperator(config *config.Config, queueManager *queuemanager.QueueManager) *Operator {
func NewOperator(config config.Config, queueManager *queuemanager.QueueManager) *Operator {
{{- if .Spec.Queue}}
queuectrl := queue.New(config, config.AWSClientset, 10)
topicARN, _ := queuectrl.Register("{{.Spec.Resource.Name}}")
Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *Operator) StartWatch(ctx context.Context, namespace string) {

{{- if .Spec.Queue}}
// QueueUpdater will take the messages from the queue and process them
func QueueUpdater(config *config.Config, msg *queuemanager.MessageBody) error {
func QueueUpdater(config config.Config, msg *queuemanager.MessageBody) error {
logger := config.Logger
var name, namespace string
if msg.Updatable {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (c *Operator) onDelete(obj interface{}) {


{{- if .Spec.IsCustomized}}
func incrementRollbackCount(config *config.Config, name string, namespace string) error {
func incrementRollbackCount(config config.Config, name string, namespace string) error {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
Expand All @@ -218,7 +218,7 @@ func incrementRollbackCount(config *config.Config, name string, namespace string
return nil
}

func updateStatus(config *config.Config, name string, namespace string, stackID string, status string, reason string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
func updateStatus(config config.Config, name string, namespace string, stackID string, status string, reason string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
Expand Down Expand Up @@ -264,7 +264,7 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
return resourceCopy, nil
}

func deleteStack(config *config.Config, name string, namespace string, stackID string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
func deleteStack(config config.Config, name string, namespace string, stackID string) (*awsV1alpha1.{{.Spec.Kind}}, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
Expand All @@ -283,7 +283,7 @@ func deleteStack(config *config.Config, name string, namespace string, stackID s
return resource, err
}

func syncAdditionalResources(config *config.Config, s *awsV1alpha1.{{.Spec.Kind}}) (err error) {
func syncAdditionalResources(config config.Config, s *awsV1alpha1.{{.Spec.Kind}}) (err error) {
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{.Spec.PluralName}}(s.Namespace).Get(s.Name, metav1.GetOptions{})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func New() Helpers {
type Helpers struct {
KubernetesResourceName func(string) (string)
{{- range $index, $element := .Items }}
Get{{$element.Spec.Kind}}ByName func(*config.Config, string, string) (interface{}, error)
Get{{$element.Spec.Kind}}ByName func(config.Config, string, string) (interface{}, error)
{{- end}}
}

{{- range $index, $element := .Items }}
// Get{{$element.Spec.Kind}}ByName will find the resource by name
func Get{{$element.Spec.Kind}}ByName(config *config.Config, name string, namespace string) (interface{}, error) {
func Get{{$element.Spec.Kind}}ByName(config config.Config, name string, namespace string) (interface{}, error) {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.{{$element.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})
Expand Down
16 changes: 8 additions & 8 deletions code-generation/pkg/codegen/templates.go

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions pkg/config/types.go → pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import (

"github.com/aws/aws-sdk-go/aws/session"
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
goVersion "github.com/christopherhein/go-version"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
)

const controllerName = "aws-service-operator"

// Config defines the configuration for the operator
type Config struct {
Version *goVersion.Info
Region string
Kubeconfig string
MasterURL string
Expand All @@ -23,7 +30,7 @@ type Config struct {
AWSClientset awsclient.ServiceoperatorV1alpha1Interface
KubeClientset kubernetes.Interface
RESTConfig *rest.Config
LoggingConfig *LoggingConfig
LoggingConfig LoggingConfig
Logger *logrus.Entry
Resources map[string]bool
ClusterName string
Expand All @@ -48,25 +55,29 @@ func getKubeconfig(masterURL, kubeconfig string) (*rest.Config, error) {
return rest.InClusterConfig()
}

func (c *Config) CreateContext(masterURL, kubeconfig string) error {
// CreateContext will create all the contexts for the informers
func CreateContext(masterURL, kubeconfig string) (awsclient.ServiceoperatorV1alpha1Interface, kubernetes.Interface, *rest.Config, error) {
config, err := getKubeconfig(masterURL, kubeconfig)
if err != nil {
return fmt.Errorf("failed to get k8s config. %+v", err)
return nil, nil, nil, fmt.Errorf("failed to get k8s config. %+v", err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to get k8s client. %+v", err)
return nil, nil, nil, fmt.Errorf("failed to get k8s client. %+v", err)
}

awsclientset, err := awsclient.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create object store clientset. %+v", err)
return nil, nil, nil, fmt.Errorf("failed to create object store clientset. %+v", err)
}

c.AWSClientset = awsclientset
c.KubeClientset = clientset
c.RESTConfig = config
return awsclientset, clientset, config, nil
}

return nil
func CreateRecorder(logger *logrus.Entry, kubeclientset kubernetes.Interface) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logger.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
return eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerName})
}
25 changes: 12 additions & 13 deletions pkg/customizations/cloudformationtemplate/cloudformationtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
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"
Expand All @@ -14,11 +13,11 @@ import (
)

// OnAdd will be fired when you add a new CFT
func OnAdd(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
func OnAdd(config config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
logger := config.Logger

updateOutput(config, cft, "UPLOAD_IN_PROGRESS", "")
err := addFileToS3(config.AWSSession, config.Bucket, cft.Data.Key, cft.Data.Template)
err := addFileToS3(config, config.Bucket, cft.Data.Key, cft.Data.Template)
if err != nil {
logger.WithError(err).Error("error uploading cloudformation")
updateOutput(config, cft, "UPLOAD_FAILED", err.Error())
Expand All @@ -28,12 +27,12 @@ func OnAdd(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
}

// OnUpdate will be fired when you update a CFT
func OnUpdate(config *config.Config, oldcft *awsV1alpha1.CloudFormationTemplate, newcft *awsV1alpha1.CloudFormationTemplate) {
func OnUpdate(config config.Config, oldcft *awsV1alpha1.CloudFormationTemplate, newcft *awsV1alpha1.CloudFormationTemplate) {
if !reflect.DeepEqual(oldcft.Data, newcft.Data) {
logger := config.Logger

updateOutput(config, newcft, "UPLOAD_IN_PROGRESS", "")
err := addFileToS3(config.AWSSession, config.Bucket, oldcft.Data.Key, newcft.Data.Template)
err := addFileToS3(config, config.Bucket, oldcft.Data.Key, newcft.Data.Template)
if err != nil {
logger.WithError(err).Error("error uploading cloudformation")
updateOutput(config, newcft, "UPLOAD_FAILED", err.Error())
Expand All @@ -44,23 +43,23 @@ func OnUpdate(config *config.Config, oldcft *awsV1alpha1.CloudFormationTemplate,
}

// OnDelete will be fired when you delete a CFT
func OnDelete(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
func OnDelete(config config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
logger := config.Logger

_, err := s3.New(config.AWSSession).DeleteObject(&s3.DeleteObjectInput{
sess := config.AWSSession
_, err := s3.New(sess).DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(config.Bucket),
Key: aws.String(cft.Data.Key),
})
if err != nil {
logger.WithError(err).Error("error uploading cloudformation")
logger.WithError(err).Error("error deleting cloudformation")
}
logger.Infof("deleted cloudformationtemplate '%s'", cft.Name)
}

func addFileToS3(s *session.Session, bucket string, filename string, template string) error {
func addFileToS3(config config.Config, bucket string, filename string, template string) error {
buffer := []byte(template)

svc := s3.New(s)
sess := config.AWSSession
svc := s3.New(sess)

_, err := svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Expand All @@ -71,7 +70,7 @@ func addFileToS3(s *session.Session, bucket string, filename string, template st
return err
}

func updateOutput(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate, status string, reason string) error {
func updateOutput(config config.Config, cft *awsV1alpha1.CloudFormationTemplate, status string, reason string) error {
logger := config.Logger
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
resource, err := clientSet.CloudFormationTemplates(cft.Namespace).Get(cft.Name, metav1.GetOptions{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/helpers/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// CreateConfigMap will create a Kubernetes Servic Using ExternalName types
func CreateConfigMap(config *config.Config, resource interface{}, svcName string, svcNamespace string, configMapTemplate map[string]string) string {
func CreateConfigMap(config config.Config, resource interface{}, svcName string, svcNamespace string, configMapTemplate map[string]string) string {
logger := config.Logger
cmData := map[string]string{}
for key, value := range configMapTemplate {
Expand Down
Loading

0 comments on commit b51e058

Please sign in to comment.