2017-11-29 00:30:08 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/rancher/norman/controller"
|
2017-12-16 08:41:33 +00:00
|
|
|
"github.com/rancher/norman/event"
|
2017-11-29 00:30:08 +00:00
|
|
|
"github.com/rancher/norman/signal"
|
2017-12-16 08:41:33 +00:00
|
|
|
"github.com/rancher/norman/types"
|
2017-11-29 00:30:08 +00:00
|
|
|
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"
|
2017-12-16 08:41:33 +00:00
|
|
|
managementSchema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
|
2017-12-04 23:42:18 +00:00
|
|
|
projectv3 "github.com/rancher/types/apis/project.cattle.io/v3"
|
2017-12-23 06:26:07 +00:00
|
|
|
projectSchema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
|
2017-12-12 04:50:19 +00:00
|
|
|
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
|
2017-12-23 06:26:07 +00:00
|
|
|
projectClient "github.com/rancher/types/client/project/v3"
|
2017-11-29 00:30:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-12-16 08:41:33 +00:00
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-11-29 00:30:08 +00:00
|
|
|
"k8s.io/client-go/dynamic"
|
2017-11-29 04:48:28 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2017-12-16 08:41:33 +00:00
|
|
|
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
2017-11-29 00:30:08 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2017-12-16 08:41:33 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2017-11-29 00:30:08 +00:00
|
|
|
)
|
|
|
|
|
2017-12-23 06:26:07 +00:00
|
|
|
var (
|
|
|
|
ProjectTypes = []string{
|
|
|
|
projectClient.BasicAuthType,
|
|
|
|
projectClient.CertificateType,
|
|
|
|
projectClient.DockerCredentialType,
|
|
|
|
projectClient.ServiceAccountTokenType,
|
|
|
|
projectClient.SecretType,
|
|
|
|
projectClient.SSHAuthType,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
type ManagementContext struct {
|
2017-12-16 08:41:33 +00:00
|
|
|
eventBroadcaster record.EventBroadcaster
|
|
|
|
|
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-12-16 08:41:33 +00:00
|
|
|
K8sClient kubernetes.Interface
|
|
|
|
Events record.EventRecorder
|
|
|
|
EventLogger event.Logger
|
|
|
|
Schemas *types.Schemas
|
|
|
|
Scheme *runtime.Scheme
|
2017-11-29 01:57:50 +00:00
|
|
|
|
2017-12-04 23:42:18 +00:00
|
|
|
Management managementv3.Interface
|
2017-12-17 04:16:29 +00:00
|
|
|
RBAC rbacv1.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-12-17 04:16:29 +00:00
|
|
|
c.RBAC,
|
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-20 04:48:52 +00:00
|
|
|
func (w *ClusterContext) WorkloadContext() *WorkloadContext {
|
|
|
|
return &WorkloadContext{
|
|
|
|
ClusterName: w.ClusterName,
|
|
|
|
RESTConfig: w.RESTConfig,
|
|
|
|
UnversionedClient: w.UnversionedClient,
|
|
|
|
K8sClient: w.K8sClient,
|
|
|
|
|
|
|
|
Apps: w.Apps,
|
|
|
|
Project: w.Project,
|
|
|
|
Core: w.Core,
|
|
|
|
RBAC: w.RBAC,
|
|
|
|
Extensions: w.Extensions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type WorkloadContext struct {
|
|
|
|
ClusterName string
|
|
|
|
RESTConfig rest.Config
|
|
|
|
UnversionedClient rest.Interface
|
|
|
|
K8sClient kubernetes.Interface
|
|
|
|
|
|
|
|
Apps appsv1beta2.Interface
|
|
|
|
Project projectv3.Interface
|
|
|
|
Core corev1.Interface
|
|
|
|
RBAC rbacv1.Interface
|
|
|
|
Extensions extv1beta1.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WorkloadContext) controllers() []controller.Starter {
|
|
|
|
return []controller.Starter{
|
|
|
|
w.Apps,
|
|
|
|
w.Project,
|
|
|
|
w.Core,
|
|
|
|
w.RBAC,
|
|
|
|
w.Extensions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-16 08:41:33 +00:00
|
|
|
context.K8sClient, err = kubernetes.NewForConfig(&config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-17 04:16:29 +00:00
|
|
|
context.RBAC, err = rbacv1.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
|
|
|
|
}
|
|
|
|
|
2017-12-16 08:41:33 +00:00
|
|
|
context.Schemas = types.NewSchemas().
|
|
|
|
AddSchemas(managementSchema.Schemas)
|
|
|
|
|
2017-12-23 06:26:07 +00:00
|
|
|
for _, projectType := range ProjectTypes {
|
|
|
|
schema := projectSchema.Schemas.Schema(&projectSchema.Version, projectType)
|
|
|
|
context.Schemas.AddSchema(*schema)
|
|
|
|
}
|
|
|
|
|
2017-12-16 08:41:33 +00:00
|
|
|
context.Scheme = runtime.NewScheme()
|
|
|
|
managementv3.AddToScheme(context.Scheme)
|
|
|
|
|
|
|
|
context.eventBroadcaster = record.NewBroadcaster()
|
|
|
|
context.Events = context.eventBroadcaster.NewRecorder(context.Scheme, v1.EventSource{
|
|
|
|
Component: "CattleManagementServer",
|
|
|
|
})
|
|
|
|
context.EventLogger = event.NewLogger(context.Events)
|
|
|
|
|
2017-11-29 00:30:08 +00:00
|
|
|
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-12-16 08:41:33 +00:00
|
|
|
|
|
|
|
watcher := c.eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{
|
|
|
|
Interface: c.K8sClient.CoreV1().Events(""),
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
watcher.Stop()
|
|
|
|
}()
|
|
|
|
|
2017-12-20 04:48:52 +00:00
|
|
|
return controller.SyncThenStart(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()...)
|
2017-12-20 04:48:52 +00:00
|
|
|
return controller.SyncThenStart(ctx, 5, controllers...)
|
2017-11-29 01:57:50 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2017-12-20 04:48:52 +00:00
|
|
|
|
|
|
|
func NewWorkloadContext(config rest.Config, clusterName string) (*WorkloadContext, error) {
|
|
|
|
var err error
|
|
|
|
context := &WorkloadContext{
|
|
|
|
RESTConfig: config,
|
|
|
|
ClusterName: clusterName,
|
|
|
|
}
|
|
|
|
|
|
|
|
context.K8sClient, err = kubernetes.NewForConfig(&config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Apps, err = appsv1beta2.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Core, err = corev1.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Project, err = projectv3.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.RBAC, err = rbacv1.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Extensions, err = extv1beta1.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 (w *WorkloadContext) Start(ctx context.Context) error {
|
|
|
|
logrus.Info("Starting workload controllers")
|
|
|
|
return controller.SyncThenStart(ctx, 5, w.controllers()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WorkloadContext) StartAndWait(ctx context.Context) error {
|
|
|
|
ctx = signal.SigTermCancelContext(ctx)
|
|
|
|
w.Start(ctx)
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|