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

Commit 842efb3

Browse files
Adding SNS Topic Creation
**Why:** * allows you to create SNS topics via `kubectl` **This change addresses the need by:** * closes #33 Signed-off-by: Christopher Hein <[email protected]>
1 parent e70cd04 commit 842efb3

File tree

6 files changed

+102
-19
lines changed

6 files changed

+102
-19
lines changed

examples/snstopic.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: operator.aws/v1alpha1
2+
kind: SNSTopic
3+
metadata:
4+
name: sns-topic-10

models/snstopic.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: operator.aws/v1alpha1
2+
kind: ModelDefinition
3+
metadata:
4+
name: SNSResource
5+
spec:
6+
kind: SNSTopic
7+
type: Spec # can be Spec or Data
8+
queue: true
9+
useCloudFormation: true
10+
resource:
11+
name: snstopic
12+
plural: snstopics
13+
shortNames:
14+
- name: sns
15+
- name: topic
16+
- name: topics
17+
body:
18+
schema:
19+
type: object
20+
properties:
21+
22+
output:
23+
schema:
24+
type: object
25+
properties:
26+
- key: topicName
27+
type: string
28+
description: |
29+
TopicName is the output topicname incase it changed
30+
structKey: TopicName
31+
templateKey: TopicName

pkg/helpers/helpers.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/aws/aws-sdk-go/service/cloudformation"
66
awsclient "github.com/christopherhein/aws-operator/pkg/client/clientset/versioned/typed/operator.aws/v1alpha1"
77
"github.com/christopherhein/aws-operator/pkg/config"
8-
apiv1 "k8s.io/api/core/v1"
98
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"reflect"
1110
"strconv"
@@ -69,24 +68,6 @@ func Templatize(tempStr string, data interface{}) (resp string, err error) {
6968
return tpl.String(), err
7069
}
7170

72-
// ServiceType will return the service object
73-
func ServiceType(str string) apiv1.ServiceType {
74-
var sType apiv1.ServiceType
75-
switch str {
76-
case "ClusterIP":
77-
sType = apiv1.ServiceTypeClusterIP
78-
case "NodePort":
79-
sType = apiv1.ServiceTypeNodePort
80-
case "LoadBalancer":
81-
sType = apiv1.ServiceTypeLoadBalancer
82-
case "ExternalName":
83-
sType = apiv1.ServiceTypeExternalName
84-
default:
85-
sType = apiv1.ServiceTypeClusterIP
86-
}
87-
return sType
88-
}
89-
9071
// GetCloudFormationTemplate will return the url to the CFT from the CFT resource
9172
func GetCloudFormationTemplate(config *config.Config, rType string, name string, namespace string) string {
9273
logger := config.Logger

pkg/helpers/service.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package helpers
2+
3+
import (
4+
"github.com/christopherhein/aws-operator/pkg/config"
5+
apiv1 "k8s.io/api/core/v1"
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
)
8+
9+
type Data struct {
10+
Obj interface{}
11+
Config *config.Config
12+
}
13+
14+
// CreateExternalNameService will create a Kubernetes Servic Using ExternalName types
15+
func CreateExternalNameService(config *config.Config, resource interface{}, svcName string, svcNamespace string, externalNameTemplate string, svcPort int32) string {
16+
logger := config.Logger
17+
18+
externalName, err := Templatize(externalNameTemplate, Data{Obj: resource, Config: config})
19+
if err != nil {
20+
logger.WithError(err).Error("error parsing external name template")
21+
return ""
22+
}
23+
24+
service := &apiv1.Service{
25+
ObjectMeta: metav1.ObjectMeta{
26+
Name: svcName,
27+
},
28+
Spec: apiv1.ServiceSpec{
29+
Type: apiv1.ServiceTypeExternalName,
30+
ExternalName: externalName,
31+
Ports: []apiv1.ServicePort{
32+
apiv1.ServicePort{
33+
Port: svcPort,
34+
},
35+
},
36+
},
37+
}
38+
39+
newService, err := config.Context.Clientset.CoreV1().Services(svcNamespace).Create(service)
40+
if err != nil {
41+
logger.WithError(err).Error("error creating service")
42+
}
43+
return newService.Name
44+
}
45+
46+
// func serviceType(str string) apiv1.ServiceType {
47+
// var sType apiv1.ServiceType
48+
// switch str {
49+
// case "ClusterIP":
50+
// sType = apiv1.ServiceTypeClusterIP
51+
// case "NodePort":
52+
// sType = apiv1.ServiceTypeNodePort
53+
// case "LoadBalancer":
54+
// sType = apiv1.ServiceTypeLoadBalancer
55+
// case "ExternalName":
56+
// sType = apiv1.ServiceTypeExternalName
57+
// default:
58+
// sType = apiv1.ServiceTypeClusterIP
59+
// }
60+
// return sType
61+
// }

pkg/queue/queue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ func process(q *Queue, svc *sqs.SQS, h Handler, stopCh <-chan struct{}) error {
224224
logger.WithError(err).Error("error parsing message body")
225225
}
226226
mb.ParseMessage()
227+
logger.Debugf("%+v", mb)
227228

228229
err = h.HandleMessage(config, mb)
229230
if err != nil {

pkg/server/server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/christopherhein/aws-operator/pkg/operator/cloudformationtemplate"
1313
"github.com/christopherhein/aws-operator/pkg/operator/dynamodb"
1414
"github.com/christopherhein/aws-operator/pkg/operator/s3bucket"
15+
"github.com/christopherhein/aws-operator/pkg/operator/snstopic"
1516
"github.com/christopherhein/aws-operator/pkg/operator/sqsqueue"
1617
opkit "github.com/christopherhein/operator-kit"
1718
"k8s.io/api/core/v1"
@@ -48,6 +49,7 @@ func (c *Server) Run(stopChan chan struct{}) {
4849
s3bucket.Resource,
4950
dynamodb.Resource,
5051
sqsqueue.Resource,
52+
snstopic.Resource,
5153
}
5254
err = opkit.CreateCustomResources(*context, resources)
5355
if err != nil {
@@ -91,6 +93,9 @@ func (c *Server) Run(stopChan chan struct{}) {
9193

9294
sqscontroller := sqsqueue.NewController(config, context, awsClientset)
9395
sqscontroller.StartWatch(v1.NamespaceAll, stopChan)
96+
97+
snscontroller := snstopic.NewController(config, context, awsClientset)
98+
snscontroller.StartWatch(v1.NamespaceAll, stopChan)
9499
}
95100

96101
func getClientConfig(kubeconfig string) (*rest.Config, error) {

0 commit comments

Comments
 (0)