mirror of
https://github.com/rancher/types.git
synced 2025-09-18 07:52:41 +00:00
132
config/context.go
Normal file
132
config/context.go
Normal file
@@ -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
|
||||
}
|
@@ -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
|
||||
|
28
vendor/github.com/rancher/norman/signal/sigterm.go
generated
vendored
Normal file
28
vendor/github.com/rancher/norman/signal/sigterm.go
generated
vendored
Normal file
@@ -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
|
||||
}
|
Reference in New Issue
Block a user