From 1910c3f82e288e6da4b2b2dfffe5084a729ee6e4 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 28 Nov 2017 17:30:08 -0700 Subject: [PATCH 1/2] Add config --- config/context.go | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 config/context.go diff --git a/config/context.go b/config/context.go new file mode 100644 index 00000000..56a63c00 --- /dev/null +++ b/config/context.go @@ -0,0 +1,132 @@ +package config + +import ( + "context" + + "github.com/rancher/norman/controller" + "github.com/rancher/norman/signal" + appsv1beta2 "github.com/rancher/types/apis/apps/v1beta2" + authzv1 "github.com/rancher/types/apis/authorization.cattle.io/v1" + clusterv1 "github.com/rancher/types/apis/cluster.cattle.io/v1" + corev1 "github.com/rancher/types/apis/core/v1" + workloadv1 "github.com/rancher/types/apis/workload.cattle.io/v1" + "github.com/sirupsen/logrus" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/rest" +) + +type ClusterContext struct { + RESTConfig rest.Config + UnversionedClient rest.Interface + Cluster clusterv1.Interface + Authorization authzv1.Interface +} + +type WorkloadContext struct { + Cluster *ClusterContext + RESTConfig rest.Config + UnversionedClient rest.Interface + Apps appsv1beta2.Interface + Workload workloadv1.Interface + Core corev1.Interface +} + +func NewClusterContext(config rest.Config) (*ClusterContext, error) { + var err error + + context := &ClusterContext{ + RESTConfig: config, + } + + context.Cluster, err = clusterv1.NewForConfig(config) + if err != nil { + return nil, err + } + + context.Authorization, err = authzv1.NewForConfig(config) + if err != nil { + return nil, err + } + + dynamicConfig := config + if dynamicConfig.NegotiatedSerializer == nil { + configConfig := dynamic.ContentConfig() + dynamicConfig.NegotiatedSerializer = configConfig.NegotiatedSerializer + } + + context.UnversionedClient, err = rest.UnversionedRESTClientFor(&dynamicConfig) + if err != nil { + return nil, err + } + + return context, err +} + +func (c *ClusterContext) Start(ctx context.Context) error { + logrus.Info("Syncing cluster controllers") + err := controller.Sync(ctx, + c.Cluster, + c.Authorization) + if err != nil { + return err + } + + logrus.Info("Starting cluster controllers") + if err := c.Cluster.Start(ctx, 5); err != nil { + return err + } + + if err := c.Authorization.Start(ctx, 5); err != nil { + return err + } + + logrus.Info("Cluster context started") + return nil +} + +func (c *ClusterContext) StartAndWait() error { + ctx := signal.SigTermCancelContext(context.Background()) + c.Start(ctx) + <-ctx.Done() + return ctx.Err() +} + +func NewWorkloadContext(clusterConfig, config rest.Config) (*WorkloadContext, error) { + var err error + context := &WorkloadContext{ + RESTConfig: config, + } + + context.Cluster, err = NewClusterContext(clusterConfig) + if err != nil { + return nil, err + } + + context.Apps, err = appsv1beta2.NewForConfig(config) + if err != nil { + return nil, err + } + + context.Workload, err = workloadv1.NewForConfig(config) + if err != nil { + return nil, err + } + + context.Core, err = corev1.NewForConfig(config) + if err != nil { + return nil, err + } + + dynamicConfig := config + if dynamicConfig.NegotiatedSerializer == nil { + configConfig := dynamic.ContentConfig() + dynamicConfig.NegotiatedSerializer = configConfig.NegotiatedSerializer + } + + context.UnversionedClient, err = rest.UnversionedRESTClientFor(&dynamicConfig) + if err != nil { + return nil, err + } + + return context, err +} From 4b4de1dd9dd70ea8b0613be8e4f6203af9559a45 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 28 Nov 2017 17:36:07 -0700 Subject: [PATCH 2/2] Update vendor --- vendor.conf | 2 +- .../rancher/norman/signal/sigterm.go | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 vendor/github.com/rancher/norman/signal/sigterm.go diff --git a/vendor.conf b/vendor.conf index 0cdc3200..011529e2 100644 --- a/vendor.conf +++ b/vendor.conf @@ -3,5 +3,5 @@ github.com/rancher/types k8s.io/kubernetes v1.8.3 transitive=true,staging=true bitbucket.org/ww/goautoneg a547fc61f48d567d5b4ec6f8aee5573d8efce11d https://github.com/rancher/goautoneg.git -github.com/rancher/norman 9be32ca0a540082e23a24a2b46127b5c205db262 +github.com/rancher/norman 391a96f33dd0b1f893f8b194e2d7cad3e26d1351 golang.org/x/sync fd80eb99c8f653c847d294a001bdf2a3a6f768f5 diff --git a/vendor/github.com/rancher/norman/signal/sigterm.go b/vendor/github.com/rancher/norman/signal/sigterm.go new file mode 100644 index 00000000..cf2e3b55 --- /dev/null +++ b/vendor/github.com/rancher/norman/signal/sigterm.go @@ -0,0 +1,28 @@ +package signal + +import ( + "context" + "os" + "os/signal" + "syscall" + + "github.com/sirupsen/logrus" +) + +func SigTermCancelContext(ctx context.Context) context.Context { + term := make(chan os.Signal) + signal.Notify(term, os.Interrupt, syscall.SIGTERM) + + ctx, cancel := context.WithCancel(ctx) + + go func() { + select { + case <-term: + logrus.Infof("Received SIGTERM, cancelling") + cancel() + case <-ctx.Done(): + } + }() + + return ctx +}