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

Commit b51e058

Browse files
author
Christopher Hein
authored
Merge pull request #142 from christopherhein/chore/refactor-config-pointer
Refactor Out Config Pointer
2 parents 7ff2311 + 6bb21f3 commit b51e058

File tree

35 files changed

+200
-187
lines changed

35 files changed

+200
-187
lines changed

cmd/aws-service-operator/main.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1010
"github.com/aws/aws-sdk-go/aws/session"
1111
"github.com/awslabs/aws-service-operator/pkg/config"
12+
"github.com/awslabs/aws-service-operator/pkg/logger"
13+
"github.com/awslabs/aws-service-operator/pkg/queue"
14+
goVersion "github.com/christopherhein/go-version"
1215
homedir "github.com/mitchellh/go-homedir"
1316
"github.com/spf13/cobra"
1417
"github.com/spf13/viper"
@@ -90,7 +93,14 @@ func initConfig() {
9093
}
9194
}
9295

93-
func getConfig() (c *config.Config, err error) {
96+
func getConfig() (c config.Config, err error) {
97+
loggingConfig := config.LoggingConfig{
98+
File: logFile,
99+
Level: logLevel,
100+
FullTimestamps: true,
101+
DisableTimestamps: false,
102+
}
103+
94104
resourcesMap := map[string]bool{}
95105
for _, r := range strings.Split(resources, ",") {
96106
resourcesMap[r] = true
@@ -101,37 +111,49 @@ func getConfig() (c *config.Config, err error) {
101111
if awsRegion == "" {
102112
awsRegion, err = metadata.Region()
103113
if err != nil {
104-
return nil, err
114+
return c, err
105115
}
106116
}
107117

108118
sess, err := session.NewSession(&aws.Config{Region: aws.String(awsRegion)})
109119
if err != nil {
110-
return nil, err
120+
return c, err
111121
}
112122

113-
c = &config.Config{
114-
Region: awsRegion,
115-
Kubeconfig: kubeconfig,
116-
MasterURL: masterURL,
117-
AWSSession: sess,
118-
LoggingConfig: &config.LoggingConfig{
119-
File: logFile,
120-
Level: logLevel,
121-
FullTimestamps: true,
122-
DisableTimestamps: false,
123-
},
123+
awsclientset, kubeclientset, restconfig, err := config.CreateContext(masterURL, kubeconfig)
124+
if err != nil {
125+
return c, err
126+
}
127+
128+
logger, err := logger.Configure(loggingConfig)
129+
if err != nil {
130+
return c, err
131+
}
132+
133+
queueURL, queueARN, err := queue.RegisterQueue(sess, clusterName, "cloudformation")
134+
if err != nil {
135+
return c, err
136+
}
137+
138+
c = config.Config{
139+
Region: awsRegion,
140+
Kubeconfig: kubeconfig,
141+
MasterURL: masterURL,
142+
Logger: logger,
143+
Version: goVersion.New(version, commit, date),
144+
AWSSession: sess,
145+
LoggingConfig: loggingConfig,
146+
AWSClientset: awsclientset,
147+
KubeClientset: kubeclientset,
148+
RESTConfig: restconfig,
149+
Recorder: config.CreateRecorder(logger, kubeclientset),
124150
Resources: resourcesMap,
125151
ClusterName: clusterName,
126152
Bucket: bucket,
127153
AccountID: accountID,
128154
DefaultNamespace: defaultNamespace,
155+
QueueURL: queueURL,
156+
QueueARN: queueARN,
129157
}
130-
131-
err = c.CreateContext(masterURL, kubeconfig)
132-
if err != nil {
133-
return nil, err
134-
}
135-
136158
return c, nil
137159
}

cmd/aws-service-operator/server.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os/signal"
77
"syscall"
88

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

25-
logger, err := logger.Configure(config.LoggingConfig)
26-
if err != nil {
27-
logrus.Fatalf("Failed to configure logging: '%s'" + err.Error())
28-
}
29-
config.Logger = logger
30-
3124
ctx, cancel := context.WithCancel(context.Background())
3225
signalChan := make(chan os.Signal, 1)
3326
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
3427

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

3730
<-signalChan
38-
logger.Info("shutdown signal received, exiting...")
31+
config.Logger.Info("shutdown signal received, exiting...")
3932
cancel()
4033
},
4134
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
)
1111

1212
type base struct {
13-
config *config.Config
13+
config config.Config
1414
queueManager *queuemanager.QueueManager
1515
{{- range $index, $element := .Items}}
1616
{{$element.Spec.Resource.Name}} *{{$element.Spec.Resource.Name}}.Operator
1717
{{- end}}
1818
}
1919

2020
func New(
21-
config *config.Config,
21+
config config.Config,
2222
queueManager *queuemanager.QueueManager,
2323
) *base {
2424
return &base{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

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

2626
// Cloudformation defines the {{.Spec.Resource.Name}} cfts
2727
type Cloudformation struct {
28-
config *config.Config
28+
config config.Config
2929
{{.Spec.Kind}} *awsV1alpha1.{{.Spec.Kind}}
3030
topicARN string
3131
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ import (
3333

3434
// Operator represents a controller object for object store custom resources
3535
type Operator struct {
36-
config *config.Config
36+
config config.Config
3737
topicARN string
3838
queueManager *queuemanager.QueueManager
3939
}
4040

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

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

199199

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

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

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

286-
func syncAdditionalResources(config *config.Config, s *awsV1alpha1.{{.Spec.Kind}}) (err error) {
286+
func syncAdditionalResources(config config.Config, s *awsV1alpha1.{{.Spec.Kind}}) (err error) {
287287
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
288288
resource, err := clientSet.{{.Spec.PluralName}}(s.Namespace).Get(s.Name, metav1.GetOptions{})
289289
if err != nil {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func New() Helpers {
2525
type Helpers struct {
2626
KubernetesResourceName func(string) (string)
2727
{{- range $index, $element := .Items }}
28-
Get{{$element.Spec.Kind}}ByName func(*config.Config, string, string) (interface{}, error)
28+
Get{{$element.Spec.Kind}}ByName func(config.Config, string, string) (interface{}, error)
2929
{{- end}}
3030
}
3131

3232
{{- range $index, $element := .Items }}
3333
// Get{{$element.Spec.Kind}}ByName will find the resource by name
34-
func Get{{$element.Spec.Kind}}ByName(config *config.Config, name string, namespace string) (interface{}, error) {
34+
func Get{{$element.Spec.Kind}}ByName(config config.Config, name string, namespace string) (interface{}, error) {
3535
logger := config.Logger
3636
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
3737
resource, err := clientSet.{{$element.Spec.PluralName}}(namespace).Get(name, metav1.GetOptions{})

code-generation/pkg/codegen/templates.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/types.go renamed to pkg/config/config.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import (
55

66
"github.com/aws/aws-sdk-go/aws/session"
77
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
8+
goVersion "github.com/christopherhein/go-version"
89
"github.com/sirupsen/logrus"
10+
corev1 "k8s.io/api/core/v1"
911
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/kubernetes/scheme"
13+
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
1014
"k8s.io/client-go/rest"
1115
"k8s.io/client-go/tools/clientcmd"
1216
"k8s.io/client-go/tools/record"
1317
)
1418

19+
const controllerName = "aws-service-operator"
20+
1521
// Config defines the configuration for the operator
1622
type Config struct {
23+
Version *goVersion.Info
1724
Region string
1825
Kubeconfig string
1926
MasterURL string
@@ -23,7 +30,7 @@ type Config struct {
2330
AWSClientset awsclient.ServiceoperatorV1alpha1Interface
2431
KubeClientset kubernetes.Interface
2532
RESTConfig *rest.Config
26-
LoggingConfig *LoggingConfig
33+
LoggingConfig LoggingConfig
2734
Logger *logrus.Entry
2835
Resources map[string]bool
2936
ClusterName string
@@ -48,25 +55,29 @@ func getKubeconfig(masterURL, kubeconfig string) (*rest.Config, error) {
4855
return rest.InClusterConfig()
4956
}
5057

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

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

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

67-
c.AWSClientset = awsclientset
68-
c.KubeClientset = clientset
69-
c.RESTConfig = config
75+
return awsclientset, clientset, config, nil
76+
}
7077

71-
return nil
78+
func CreateRecorder(logger *logrus.Entry, kubeclientset kubernetes.Interface) record.EventRecorder {
79+
eventBroadcaster := record.NewBroadcaster()
80+
eventBroadcaster.StartLogging(logger.Infof)
81+
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
82+
return eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerName})
7283
}

pkg/customizations/cloudformationtemplate/cloudformationtemplate.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"reflect"
66

77
"github.com/aws/aws-sdk-go/aws"
8-
"github.com/aws/aws-sdk-go/aws/session"
98
"github.com/aws/aws-sdk-go/service/s3"
109
awsV1alpha1 "github.com/awslabs/aws-service-operator/pkg/apis/service-operator.aws/v1alpha1"
1110
awsclient "github.com/awslabs/aws-service-operator/pkg/client/clientset/versioned/typed/service-operator.aws/v1alpha1"
@@ -14,11 +13,11 @@ import (
1413
)
1514

1615
// OnAdd will be fired when you add a new CFT
17-
func OnAdd(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
16+
func OnAdd(config config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
1817
logger := config.Logger
1918

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

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

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

4645
// OnDelete will be fired when you delete a CFT
47-
func OnDelete(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
46+
func OnDelete(config config.Config, cft *awsV1alpha1.CloudFormationTemplate) {
4847
logger := config.Logger
49-
50-
_, err := s3.New(config.AWSSession).DeleteObject(&s3.DeleteObjectInput{
48+
sess := config.AWSSession
49+
_, err := s3.New(sess).DeleteObject(&s3.DeleteObjectInput{
5150
Bucket: aws.String(config.Bucket),
5251
Key: aws.String(cft.Data.Key),
5352
})
5453
if err != nil {
55-
logger.WithError(err).Error("error uploading cloudformation")
54+
logger.WithError(err).Error("error deleting cloudformation")
5655
}
5756
logger.Infof("deleted cloudformationtemplate '%s'", cft.Name)
5857
}
5958

60-
func addFileToS3(s *session.Session, bucket string, filename string, template string) error {
59+
func addFileToS3(config config.Config, bucket string, filename string, template string) error {
6160
buffer := []byte(template)
62-
63-
svc := s3.New(s)
61+
sess := config.AWSSession
62+
svc := s3.New(sess)
6463

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

74-
func updateOutput(config *config.Config, cft *awsV1alpha1.CloudFormationTemplate, status string, reason string) error {
73+
func updateOutput(config config.Config, cft *awsV1alpha1.CloudFormationTemplate, status string, reason string) error {
7574
logger := config.Logger
7675
clientSet, _ := awsclient.NewForConfig(config.RESTConfig)
7776
resource, err := clientSet.CloudFormationTemplates(cft.Namespace).Get(cft.Name, metav1.GetOptions{})

pkg/helpers/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// CreateConfigMap will create a Kubernetes Servic Using ExternalName types
10-
func CreateConfigMap(config *config.Config, resource interface{}, svcName string, svcNamespace string, configMapTemplate map[string]string) string {
10+
func CreateConfigMap(config config.Config, resource interface{}, svcName string, svcNamespace string, configMapTemplate map[string]string) string {
1111
logger := config.Logger
1212
cmData := map[string]string{}
1313
for key, value := range configMapTemplate {

0 commit comments

Comments
 (0)