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"
|
|
|
|
corev1 "github.com/rancher/types/apis/core/v1"
|
2017-12-09 16:19:33 +00:00
|
|
|
extv1beta1 "github.com/rancher/types/apis/extensions/v1beta1"
|
2017-12-04 23:42:18 +00:00
|
|
|
managementv3 "github.com/rancher/types/apis/management.cattle.io/v3"
|
|
|
|
projectv3 "github.com/rancher/types/apis/project.cattle.io/v3"
|
2017-12-12 04:50:19 +00:00
|
|
|
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
|
2017-11-29 00:30:08 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
type ManagementContext struct {
|
2017-12-11 22:42:51 +00:00
|
|
|
LocalConfig *rest.Config
|
2017-11-29 00:30:08 +00:00
|
|
|
RESTConfig rest.Config
|
|
|
|
UnversionedClient rest.Interface
|
2017-11-29 01:57:50 +00:00
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
Management managementv3.Interface
|
2017-11-29 01:57:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
func (c *ManagementContext) controllers() []controller.Starter {
|
2017-11-29 01:57:50 +00:00
|
|
|
return []controller.Starter{
|
2017-12-04 23:42:18 +00:00
|
|
|
c.Management,
|
2017-11-29 01:57:50 +00:00
|
|
|
}
|
2017-11-29 00:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
type ClusterContext struct {
|
|
|
|
Management *ManagementContext
|
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
|
|
|
|
2017-12-09 16:19:33 +00:00
|
|
|
Apps appsv1beta2.Interface
|
|
|
|
Project projectv3.Interface
|
|
|
|
Core corev1.Interface
|
|
|
|
RBAC rbacv1.Interface
|
|
|
|
Extensions extv1beta1.Interface
|
2017-11-29 01:57:50 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
func (w *ClusterContext) controllers() []controller.Starter {
|
2017-11-29 01:57:50 +00:00
|
|
|
return []controller.Starter{
|
|
|
|
w.Apps,
|
2017-12-04 23:42:18 +00:00
|
|
|
w.Project,
|
2017-11-29 01:57:50 +00:00
|
|
|
w.Core,
|
2017-12-09 16:19:33 +00:00
|
|
|
w.RBAC,
|
|
|
|
w.Extensions,
|
2017-11-29 01:57:50 +00:00
|
|
|
}
|
2017-11-29 00:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
func NewManagementContext(config rest.Config) (*ManagementContext, error) {
|
2017-11-29 00:30:08 +00:00
|
|
|
var err error
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
context := &ManagementContext{
|
2017-11-29 00:30:08 +00:00
|
|
|
RESTConfig: config,
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
context.Management, err = managementv3.NewForConfig(config)
|
2017-11-29 00:30:08 +00:00
|
|
|
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-12-04 23:42:18 +00:00
|
|
|
func (c *ManagementContext) Start(ctx context.Context) error {
|
|
|
|
logrus.Info("Starting management controllers")
|
2017-11-29 01:57:50 +00:00
|
|
|
return controller.SyncThenSync(ctx, 5, c.controllers()...)
|
2017-11-29 00:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
func (c *ManagementContext) StartAndWait() error {
|
2017-11-29 00:30:08 +00:00
|
|
|
ctx := signal.SigTermCancelContext(context.Background())
|
|
|
|
c.Start(ctx)
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2017-12-11 22:42:51 +00:00
|
|
|
func NewClusterContext(managementConfig, config rest.Config, clusterName string) (*ClusterContext, error) {
|
2017-11-29 00:30:08 +00:00
|
|
|
var err error
|
2017-12-04 23:42:18 +00:00
|
|
|
context := &ClusterContext{
|
2017-11-29 03:59:54 +00:00
|
|
|
RESTConfig: config,
|
|
|
|
ClusterName: clusterName,
|
2017-11-29 00:30:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 22:42:51 +00:00
|
|
|
context.Management, err = NewManagementContext(managementConfig)
|
2017-11-29 00:30:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
context.Core, err = corev1.NewForConfig(config)
|
2017-11-29 00:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
context.Project, err = projectv3.NewForConfig(config)
|
2017-11-29 00:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-09 16:19:33 +00:00
|
|
|
context.RBAC, err = rbacv1.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Extensions, err = extv1beta1.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:30:08 +00:00
|
|
|
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
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
func (w *ClusterContext) Start(ctx context.Context) error {
|
|
|
|
logrus.Info("Starting cluster controllers")
|
|
|
|
controllers := w.Management.controllers()
|
2017-11-29 01:57:50 +00:00
|
|
|
controllers = append(controllers, w.controllers()...)
|
|
|
|
return controller.SyncThenSync(ctx, 5, controllers...)
|
|
|
|
}
|
|
|
|
|
2017-12-11 22:42:51 +00:00
|
|
|
func (w *ClusterContext) StartAndWait(ctx context.Context) error {
|
|
|
|
ctx = signal.SigTermCancelContext(ctx)
|
2017-11-29 01:57:50 +00:00
|
|
|
w.Start(ctx)
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|