1
0
mirror of https://github.com/rancher/types.git synced 2025-05-31 09:44:50 +00:00
types/config/context.go

156 lines
3.6 KiB
Go
Raw Normal View History

2017-11-29 00:30:08 +00:00
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"
2017-11-29 04:48:28 +00:00
"k8s.io/client-go/kubernetes"
2017-11-29 00:30:08 +00:00
"k8s.io/client-go/rest"
)
type ClusterContext struct {
RESTConfig rest.Config
UnversionedClient rest.Interface
2017-11-29 01:57:50 +00:00
Cluster clusterv1.Interface
Authorization authzv1.Interface
}
func (c *ClusterContext) controllers() []controller.Starter {
return []controller.Starter{
c.Cluster,
c.Authorization,
}
2017-11-29 00:30:08 +00:00
}
type WorkloadContext struct {
Cluster *ClusterContext
2017-11-29 01:57:50 +00:00
ClusterName string
2017-11-29 00:30:08 +00:00
RESTConfig rest.Config
UnversionedClient rest.Interface
2017-11-29 04:48:28 +00:00
K8sClient kubernetes.Interface
2017-11-29 01:57:50 +00:00
Apps appsv1beta2.Interface
Workload workloadv1.Interface
Core corev1.Interface
}
func (w *WorkloadContext) controllers() []controller.Starter {
return []controller.Starter{
w.Apps,
w.Workload,
w.Core,
}
2017-11-29 00:30:08 +00:00
}
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("Starting cluster controllers")
2017-11-29 01:57:50 +00:00
return controller.SyncThenSync(ctx, 5, c.controllers()...)
2017-11-29 00:30:08 +00:00
}
func (c *ClusterContext) StartAndWait() error {
ctx := signal.SigTermCancelContext(context.Background())
c.Start(ctx)
<-ctx.Done()
return ctx.Err()
}
2017-11-29 01:57:50 +00:00
func NewWorkloadContext(clusterConfig, config rest.Config, clusterName string) (*WorkloadContext, error) {
2017-11-29 00:30:08 +00:00
var err error
context := &WorkloadContext{
2017-11-29 03:59:54 +00:00
RESTConfig: config,
ClusterName: clusterName,
2017-11-29 00:30:08 +00:00
}
context.Cluster, err = NewClusterContext(clusterConfig)
if err != nil {
return nil, err
}
2017-11-29 04:48:28 +00:00
context.K8sClient, err = kubernetes.NewForConfig(&config)
if err != nil {
return nil, err
}
2017-11-29 00:30:08 +00:00
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
}
2017-11-29 01:57:50 +00:00
func (w *WorkloadContext) Start(ctx context.Context) error {
logrus.Info("Starting workload controllers")
controllers := w.Cluster.controllers()
controllers = append(controllers, w.controllers()...)
return controller.SyncThenSync(ctx, 5, controllers...)
}
func (w *WorkloadContext) StartAndWait() error {
ctx := signal.SigTermCancelContext(context.Background())
w.Start(ctx)
<-ctx.Done()
return ctx.Err()
}