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 #130 from christopherhein/chore/21-single-queue
Browse files Browse the repository at this point in the history
Single Queue With Multiple Topics Subscribed
  • Loading branch information
Christopher Hein committed Nov 28, 2018
2 parents d1d2eef + 0784af1 commit 85af64a
Show file tree
Hide file tree
Showing 30 changed files with 465 additions and 275 deletions.
2 changes: 1 addition & 1 deletion cloudformation/dynamodb.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Operator - Amazon DynamoDB'
Description: 'AWS Service Operator - Amazon DynamoDB (aso-1otq2cgc4)'
Parameters:
Namespace:
Description: >-
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/ecrrepository.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Operator - Amazon ECR Repository'
Description: 'AWS Service Operator - Amazon ECR Repository (aso-1otq2cgc9)'
Parameters:
Namespace:
Description: >-
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/s3bucket.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Operator - Amazon S3 Bucket'
Description: 'AWS Service Operator - Amazon S3 Bucket (aso-1otq2cgce)'
Parameters:
Namespace:
Description: >-
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/snssubscription.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Operator - Amazon SNS Subscription'
Description: 'AWS Service Operator - Amazon SNS Subscription (aso-1otq2cgcj)'
Parameters:
Namespace:
Description: >-
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/snstopic.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Operator - Amazon SNS Topic'
Description: 'AWS Service Operator - Amazon SNS Topic (aso-1otq2cgcp)'
Parameters:
Namespace:
Description: >-
Expand Down
2 changes: 1 addition & 1 deletion cloudformation/sqsqueue.yaml
@@ -1,5 +1,5 @@
AWSTemplateFormatVersion: 2010-09-09
Description: "AWS Service Operator - Amazon SQS Queue"
Description: "AWS Service Operator - Amazon SQS Queue (aso-1otq2cgcu)"
Parameters:
Namespace:
Type: String
Expand Down
13 changes: 11 additions & 2 deletions code-generation/pkg/codegen/assets/base.go.templ
Expand Up @@ -3,28 +3,37 @@ package base
import (
"context"
"github.com/awslabs/aws-service-operator/pkg/config"
"github.com/awslabs/aws-service-operator/pkg/queuemanager"
{{- range $index, $element := .Items}}
"github.com/awslabs/aws-service-operator/pkg/operators/{{$element.Spec.Resource.Name}}"
{{- end}}
)

type base struct {
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,
queueManager *queuemanager.QueueManager,
) *base {
return &base{
config: config,
queueManager: queueManager,
{{- range $index, $element := .Items}}
{{$element.Spec.Resource.Name}}: {{$element.Spec.Resource.Name}}.NewOperator(config, queueManager),
{{- end}}
}
}

func (b *base) Watch(ctx context.Context, namespace string) {
{{- range $index, $element := .Items}}
if b.config.Resources["{{$element.Spec.Resource.Name}}"] {
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config)
go {{$element.Spec.Resource.Name}}operator.StartWatch(ctx, namespace)
go b.{{$element.Spec.Resource.Name}}.StartWatch(ctx, namespace)
}
{{- end}}

Expand Down
33 changes: 20 additions & 13 deletions code-generation/pkg/codegen/assets/operator.go.templ
Expand Up @@ -14,12 +14,13 @@ import (
{{- 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"
"github.com/awslabs/aws-service-operator/pkg/queuemanager"
{{- if .Spec.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"
"github.com/awslabs/aws-service-operator/pkg/queue"
{{- end}}
"github.com/awslabs/aws-service-operator/pkg/operator"
"k8s.io/client-go/tools/cache"
Expand All @@ -34,12 +35,23 @@ import (
type Operator struct {
config *config.Config
topicARN string
queueManager *queuemanager.QueueManager
}

// NewOperator create controller for watching object store custom resources created
func NewOperator(config *config.Config) *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}}")
queueManager.Add(topicARN, queuemanager.HandlerFunc(QueueUpdater))
{{- end}}

return &Operator{
config: config,
{{- if .Spec.Queue}}
topicARN: topicARN,
{{- end}}
queueManager: queueManager,
}
}

Expand All @@ -50,19 +62,14 @@ func (c *Operator) StartWatch(ctx context.Context, namespace string) {
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
{{- if .Spec.Queue}}
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), ctx.Done())
{{- end}}

oper := operator.New("{{.Spec.Resource.Plural}}", namespace, resourceHandlers, c.config.AWSClientset.RESTClient())
oper.Watch(&awsV1alpha1.{{.Spec.Kind}}{}, ctx.Done())
}

{{- if .Spec.Queue}}
// QueueUpdater will take the messages from the queue and process them
func QueueUpdater(config *config.Config, msg *queue.MessageBody) error {
func QueueUpdater(config *config.Config, msg *queuemanager.MessageBody) error {
logger := config.Logger
var name, namespace string
if msg.Updatable {
Expand Down
16 changes: 8 additions & 8 deletions code-generation/pkg/codegen/templates.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion configs/aws-service-operator.yaml
Expand Up @@ -150,13 +150,15 @@ items:
- pods
- configmaps
- services
- events
verbs:
- get
- list
- watch
- create
- delete
- update
- patch
- apiGroups:
- apiextensions.k8s.io
resources:
Expand Down Expand Up @@ -203,7 +205,7 @@ items:
template:
metadata:
annotations:
iam.amazonaws.com/role: arn:aws:iam::<ACCOUNT_ID>:role/aws-service-operator
iam.amazonaws.com/role: aws-service-operator
labels:
app: aws-service-operator
spec:
Expand Down
7 changes: 3 additions & 4 deletions examples/cloudformationtemplates/dynamodb.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: dynamodb.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Operator - Amazon DynamoDB'
Description: 'AWS Service Operator - Amazon DynamoDB (aso-1otq2cgc4)'
Parameters:
Namespace:
Description: >-
Expand Down Expand Up @@ -70,7 +70,7 @@ data:
-
AttributeName: !Ref RangeAttributeName
KeyType: "RANGE"
AttributeDefinitions:
AttributeDefinitions:
-
AttributeName: !Ref HashAttributeName
AttributeType: "S"
Expand All @@ -97,5 +97,4 @@ data:
Value: !Ref DynamoDBTable
TableArn:
Description: Arn of the DynamoDB Table
Value: !GetAtt DynamoDBTable.Arn
Value: !GetAtt DynamoDBTable.Arn
4 changes: 2 additions & 2 deletions examples/cloudformationtemplates/ecrrepository.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: ecrrepository.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Operator - Amazon ECR Repository'
Description: 'AWS Service Operator - Amazon ECR Repository (aso-1otq2cgc9)'
Parameters:
Namespace:
Description: >-
Expand Down Expand Up @@ -40,4 +40,4 @@ data:
Description: Name of the topic
RepositoryARN:
Value: !GetAtt ECRRepository.Arn
Description: ARN of the Repository
Description: ARN of the Repository
64 changes: 56 additions & 8 deletions examples/cloudformationtemplates/s3bucket.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: s3bucket.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Operator - Amazon S3 Bucket'
Description: 'AWS Service Operator - Amazon S3 Bucket (aso-1otq2cgce)'
Parameters:
Namespace:
Description: >-
Expand Down Expand Up @@ -80,23 +80,46 @@ data:
- BucketOwnerFullControl
- AwsExecRead
Default: "Private"
EnableStaticSite:
Description: enable static site
Type: String
AllowedValues:
- 'true'
- 'false'
Default: 'false'
StaticSiteIndex:
Description: Index document for the S3 bucket static site
Type: String
Default: 'index.html'
StaticSiteError:
Description: Error document for the S3 bucket static site
Type: String
Default: "500.html"
Mappings: {}
Conditions:
UseLogging: !Equals
UseLogging: !Equals
- !Ref EnableLogging
- 'true'
UseGlacierLifeCycle: !Equals
UseGlacierLifeCycle: !Equals
- !Ref EnableGlacierLifeCycle
- 'true'
UseVersioning: !Equals
UseVersioning: !Equals
- !Ref EnableVersioning
- 'true'
UseAsStaticSite: !Equals
- !Ref EnableStaticSite
- 'true'
Resources:
S3bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Ref BucketName
AccessControl: !Ref BucketAccessControl
WebsiteConfiguration: !If
- UseAsStaticSite
- IndexDocument: !Ref StaticSiteIndex
ErrorDocument: !Ref StaticSiteError
- !Ref 'AWS::NoValue'
LifecycleConfiguration:
Rules:
- Id: GlacierRule
Expand All @@ -106,7 +129,7 @@ data:
Transitions:
- TransitionInDays: !Ref GlacierLifeCycleTransitionInDays
StorageClass: Glacier
LoggingConfiguration: !If
LoggingConfiguration: !If
- UseLogging
- DestinationBucketName: !Ref LoggingBucket
LogFilePrefix: !Ref LoggingPrefix
Expand All @@ -122,18 +145,38 @@ data:
Value: !Ref ClusterName
- Key: Heritage
Value: operator.aws
VersioningConfiguration: !If
VersioningConfiguration: !If
- UseVersioning
- Status: Enabled
- !Ref 'AWS::NoValue'
DeletionPolicy: Retain
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: UseAsStaticSite
Properties:
Bucket: !Ref S3bucket
PolicyDocument:
Statement:
-
Action:
- "s3:GetObject"
Effect: Allow
Principal: "*"
Resource: !Join:
- ""
-
- "arn:aws:s3:::"
- !Ref S3bucket
- "/*
LoggingBucket:
Condition: UseLogging
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
AccessControl: LogDeliveryWrite
BucketName: !Join
BucketName: !Join
- ''
- - !Ref BucketName
- logging
Expand All @@ -142,7 +185,12 @@ data:
Value: !Ref S3bucket
Description: Name of the sample Amazon S3 bucket.
BucketArn:
Value: !GetAtt
Value: !GetAtt
- S3bucket
- Arn
Description: Name of the Amazon S3 bucket
WebsiteURL:
Value: !GetAtt
- S3bucket
- WebsiteURL
Description: URL of the bucket that was created
4 changes: 2 additions & 2 deletions examples/cloudformationtemplates/snssubscription.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: snssubscription.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Operator - Amazon SNS Subscription'
Description: 'AWS Service Operator - Amazon SNS Subscription (aso-1otq2cgcj)'
Parameters:
Namespace:
Description: >-
Expand Down Expand Up @@ -76,4 +76,4 @@ data:
Outputs:
SubscriptionARN:
Value: !Ref SNSSubscription
Description: Subscription ARN
Description: Subscription ARN
4 changes: 2 additions & 2 deletions examples/cloudformationtemplates/snstopic.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: snstopic.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Operator - Amazon SNS Topic'
Description: 'AWS Service Operator - Amazon SNS Topic (aso-1otq2cgcp)'
Parameters:
Namespace:
Description: >-
Expand All @@ -31,4 +31,4 @@ data:
Outputs:
TopicARN:
Value: !Ref SNSTopic
Description: Name of the topic
Description: Name of the topic
4 changes: 2 additions & 2 deletions examples/cloudformationtemplates/sqsqueue.yaml
Expand Up @@ -6,7 +6,7 @@ data:
key: sqsqueue.yaml
template: |
AWSTemplateFormatVersion: 2010-09-09
Description: "AWS Operator - Amazon SQS Queue"
Description: "AWS Service Operator - Amazon SQS Queue (aso-1otq2cgcu)"
Parameters:
Namespace:
Type: String
Expand Down Expand Up @@ -139,4 +139,4 @@ data:
Value: !If [ CreateDeadLetterQueue, !GetAtt MyDeadLetterQueue.Arn, "" ]
DeadLetterQueueName:
Description: Name newly created SQS Queue
Value: !If [ CreateDeadLetterQueue, !GetAtt MyDeadLetterQueue.QueueName, "" ]
Value: !If [ CreateDeadLetterQueue, !GetAtt MyDeadLetterQueue.QueueName, "" ]
2 changes: 2 additions & 0 deletions pkg/config/types.go
Expand Up @@ -17,6 +17,8 @@ type Config struct {
Region string
Kubeconfig string
MasterURL string
QueueURL string
QueueARN string
AWSSession *session.Session
AWSClientset awsclient.ServiceoperatorV1alpha1Interface
KubeClientset kubernetes.Interface
Expand Down

0 comments on commit 85af64a

Please sign in to comment.