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

Commit 3f46646

Browse files
authored
Merge pull request #132 from tantona/fix-operator-shutdown
Fixed operator signal interruption using context WithCancel
2 parents c797f3b + a531461 commit 3f46646

File tree

370 files changed

+61782
-290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

370 files changed

+61782
-290
lines changed

Gopkg.lock

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

cmd/aws-service-operator/server.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
49
"github.com/awslabs/aws-service-operator/pkg/logger"
510
"github.com/awslabs/aws-service-operator/pkg/server"
611
"github.com/sirupsen/logrus"
712
"github.com/spf13/cobra"
8-
"os"
9-
"os/signal"
10-
"syscall"
1113
)
1214

1315
var serverCmd = &cobra.Command{
@@ -26,21 +28,15 @@ var serverCmd = &cobra.Command{
2628
}
2729
config.Logger = logger
2830

31+
ctx, cancel := context.WithCancel(context.Background())
2932
signalChan := make(chan os.Signal, 1)
30-
stopChan := make(chan struct{})
3133
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
3234

33-
server.New(config).Run(stopChan)
34-
35-
for {
36-
select {
37-
case <-signalChan:
38-
logger.Info("shutdown signal received, exiting...")
39-
close(stopChan)
40-
return
41-
}
42-
}
35+
go server.New(config).Run(ctx)
4336

37+
<-signalChan
38+
logger.Info("shutdown signal received, exiting...")
39+
cancel()
4440
},
4541
}
4642

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package base
22

33
import (
4+
"context"
45
"github.com/awslabs/aws-service-operator/pkg/config"
56
{{- range $index, $element := .Items}}
67
"github.com/awslabs/aws-service-operator/pkg/operators/{{$element.Spec.Resource.Name}}"
@@ -19,16 +20,13 @@ func New(
1920
}
2021
}
2122

22-
func (b *base) Watch(namespace string, stopCh chan struct{}) (err error) {
23+
func (b *base) Watch(ctx context.Context, namespace string) {
2324
{{- range $index, $element := .Items}}
2425
if b.config.Resources["{{$element.Spec.Resource.Name}}"] {
2526
{{$element.Spec.Resource.Name}}operator := {{$element.Spec.Resource.Name}}.NewOperator(b.config)
26-
err = {{$element.Spec.Resource.Name}}operator.StartWatch(namespace, stopCh)
27-
if err != nil {
28-
return err
29-
}
27+
go {{$element.Spec.Resource.Name}}operator.StartWatch(ctx, namespace)
3028
}
3129
{{- end}}
3230

33-
return nil
31+
<-ctx.Done()
3432
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package {{.Spec.Resource.Name}}
77

88
import (
9+
"context"
910
{{- if .Spec.IsCustomized}}
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"github.com/awslabs/aws-service-operator/pkg/helpers"
@@ -43,7 +44,7 @@ func NewOperator(config *config.Config) *Operator {
4344
}
4445

4546
// StartWatch watches for instances of Object Store custom resources and acts on them
46-
func (c *Operator) StartWatch(namespace string, stopCh chan struct{}) error {
47+
func (c *Operator) StartWatch(ctx context.Context, namespace string) {
4748
resourceHandlers := cache.ResourceEventHandlerFuncs{
4849
AddFunc: c.onAdd,
4950
UpdateFunc: c.onUpdate,
@@ -52,13 +53,11 @@ func (c *Operator) StartWatch(namespace string, stopCh chan struct{}) error {
5253
{{- if .Spec.Queue}}
5354
queuectrl := queue.New(c.config, c.config.AWSClientset, 1)
5455
c.topicARN, _, _, _ = queuectrl.Register("{{.Spec.Resource.Name}}", &awsV1alpha1.{{.Spec.Kind}}{})
55-
go queuectrl.StartWatch(queue.HandlerFunc(QueueUpdater), stopCh)
56+
go queuectrl.StartWatch(queue.HandlerFunc(QueueUpdater), ctx.Done())
5657
{{- end}}
5758

5859
oper := operator.New("{{.Spec.Resource.Plural}}", namespace, resourceHandlers, c.config.AWSClientset.RESTClient())
59-
go oper.Watch(&awsV1alpha1.{{.Spec.Kind}}{}, stopCh)
60-
61-
return nil
60+
oper.Watch(&awsV1alpha1.{{.Spec.Kind}}{}, ctx.Done())
6261
}
6362

6463
{{- if .Spec.Queue}}

code-generation/pkg/codegen/templates.go

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

pkg/apis/service-operator.aws/v1alpha1/zz_generated.deepcopy.go

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

pkg/client/clientset/versioned/clientset.go

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

pkg/client/clientset/versioned/doc.go

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

pkg/client/clientset/versioned/fake/clientset_generated.go

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

pkg/client/clientset/versioned/fake/doc.go

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

0 commit comments

Comments
 (0)