2020-08-07 21:49:04 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-08-09 17:04:05 +00:00
|
|
|
package controllermanager
|
2020-08-07 21:49:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
k8sinformers "k8s.io/client-go/informers"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
2020-08-09 17:04:05 +00:00
|
|
|
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
2020-08-07 21:49:04 +00:00
|
|
|
|
|
|
|
"github.com/suzerain-io/controller-go"
|
2020-08-09 17:04:05 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/controller/apicerts"
|
2020-08-07 21:49:04 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/controller/logindiscovery"
|
2020-08-09 17:04:05 +00:00
|
|
|
"github.com/suzerain-io/placeholder-name/internal/provider"
|
2020-08-07 21:49:04 +00:00
|
|
|
placeholderclientset "github.com/suzerain-io/placeholder-name/kubernetes/1.19/client-go/clientset/versioned"
|
|
|
|
placeholderinformers "github.com/suzerain-io/placeholder-name/kubernetes/1.19/client-go/informers/externalversions"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
singletonWorker = 1
|
|
|
|
defaultResyncInterval = 3 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
// Prepare the controllers and their informers and return a function that will start them when called.
|
|
|
|
func PrepareControllers(
|
|
|
|
serverInstallationNamespace string,
|
|
|
|
discoveryURLOverride *string,
|
2020-08-11 01:53:53 +00:00
|
|
|
dynamicCertProvider provider.DynamicTLSServingCertProvider,
|
2020-08-07 21:49:04 +00:00
|
|
|
) (func(ctx context.Context), error) {
|
|
|
|
// Create k8s clients.
|
2020-08-09 17:04:05 +00:00
|
|
|
k8sClient, aggregatorClient, placeholderClient, err := createClients()
|
2020-08-07 21:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create clients for the controllers: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:04:05 +00:00
|
|
|
// Create informers. Don't forget to make sure they get started in the function returned below.
|
|
|
|
kubePublicNamespaceK8sInformers, installationNamespaceK8sInformers, installationNamespacePlaceholderInformers :=
|
|
|
|
createInformers(serverInstallationNamespace, k8sClient, placeholderClient)
|
2020-08-07 21:49:04 +00:00
|
|
|
|
|
|
|
// Create controller manager.
|
|
|
|
controllerManager := controller.
|
|
|
|
NewManager().
|
|
|
|
WithController(
|
|
|
|
logindiscovery.NewPublisherController(
|
|
|
|
serverInstallationNamespace,
|
|
|
|
discoveryURLOverride,
|
|
|
|
placeholderClient,
|
2020-08-09 17:04:05 +00:00
|
|
|
kubePublicNamespaceK8sInformers.Core().V1().ConfigMaps(),
|
|
|
|
installationNamespacePlaceholderInformers.Crds().V1alpha1().LoginDiscoveryConfigs(),
|
|
|
|
controller.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsManagerController(
|
|
|
|
serverInstallationNamespace,
|
|
|
|
k8sClient,
|
|
|
|
aggregatorClient,
|
|
|
|
installationNamespaceK8sInformers.Core().V1().Secrets(),
|
|
|
|
controller.WithInformer,
|
2020-08-11 17:14:57 +00:00
|
|
|
controller.WithInitialEvent,
|
2020-08-09 17:04:05 +00:00
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsObserverController(
|
|
|
|
serverInstallationNamespace,
|
|
|
|
dynamicCertProvider,
|
|
|
|
installationNamespaceK8sInformers.Core().V1().Secrets(),
|
2020-08-07 21:49:04 +00:00
|
|
|
controller.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return a function which starts the informers and controllers.
|
|
|
|
return func(ctx context.Context) {
|
2020-08-09 17:04:05 +00:00
|
|
|
kubePublicNamespaceK8sInformers.Start(ctx.Done())
|
|
|
|
installationNamespaceK8sInformers.Start(ctx.Done())
|
|
|
|
installationNamespacePlaceholderInformers.Start(ctx.Done())
|
|
|
|
|
2020-08-07 21:49:04 +00:00
|
|
|
go controllerManager.Start(ctx)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the k8s clients that will be used by the controllers.
|
2020-08-09 17:04:05 +00:00
|
|
|
func createClients() (
|
|
|
|
k8sClient *kubernetes.Clientset,
|
|
|
|
aggregatorClient *aggregatorclient.Clientset,
|
|
|
|
placeholderClient *placeholderclientset.Clientset,
|
|
|
|
err error,
|
|
|
|
) {
|
2020-08-07 21:49:04 +00:00
|
|
|
// Load the Kubernetes client configuration (kubeconfig),
|
|
|
|
kubeConfig, err := restclient.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("could not load in-cluster configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// explicitly use protobuf when talking to built-in kube APIs
|
|
|
|
protoKubeConfig := createProtoKubeConfig(kubeConfig)
|
|
|
|
|
|
|
|
// Connect to the core Kubernetes API.
|
2020-08-09 17:04:05 +00:00
|
|
|
k8sClient, err = kubernetes.NewForConfig(protoKubeConfig)
|
2020-08-07 21:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the Kubernetes aggregation API.
|
2020-08-09 17:04:05 +00:00
|
|
|
aggregatorClient, err = aggregatorclient.NewForConfig(protoKubeConfig)
|
2020-08-07 21:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("could not initialize Kubernetes client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the placeholder API.
|
|
|
|
// I think we can't use protobuf encoding here because we are using CRDs
|
|
|
|
// (for which protobuf encoding is not supported).
|
2020-08-09 17:04:05 +00:00
|
|
|
placeholderClient, err = placeholderclientset.NewForConfig(kubeConfig)
|
2020-08-07 21:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("could not initialize placeholder client: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:04:05 +00:00
|
|
|
//nolint: nakedret
|
|
|
|
return
|
2020-08-07 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the informers that will be used by the controllers.
|
|
|
|
func createInformers(
|
|
|
|
serverInstallationNamespace string,
|
|
|
|
k8sClient *kubernetes.Clientset,
|
|
|
|
placeholderClient *placeholderclientset.Clientset,
|
2020-08-09 17:04:05 +00:00
|
|
|
) (
|
|
|
|
kubePublicNamespaceK8sInformers k8sinformers.SharedInformerFactory,
|
|
|
|
installationNamespaceK8sInformers k8sinformers.SharedInformerFactory,
|
|
|
|
installationNamespacePlaceholderInformers placeholderinformers.SharedInformerFactory,
|
|
|
|
) {
|
|
|
|
kubePublicNamespaceK8sInformers = k8sinformers.NewSharedInformerFactoryWithOptions(
|
|
|
|
k8sClient,
|
|
|
|
defaultResyncInterval,
|
|
|
|
k8sinformers.WithNamespace(logindiscovery.ClusterInfoNamespace),
|
|
|
|
)
|
|
|
|
installationNamespaceK8sInformers = k8sinformers.NewSharedInformerFactoryWithOptions(
|
2020-08-07 21:49:04 +00:00
|
|
|
k8sClient,
|
|
|
|
defaultResyncInterval,
|
2020-08-09 17:04:05 +00:00
|
|
|
k8sinformers.WithNamespace(serverInstallationNamespace),
|
2020-08-07 21:49:04 +00:00
|
|
|
)
|
2020-08-09 17:04:05 +00:00
|
|
|
installationNamespacePlaceholderInformers = placeholderinformers.NewSharedInformerFactoryWithOptions(
|
2020-08-07 21:49:04 +00:00
|
|
|
placeholderClient,
|
|
|
|
defaultResyncInterval,
|
|
|
|
placeholderinformers.WithNamespace(serverInstallationNamespace),
|
|
|
|
)
|
2020-08-09 17:04:05 +00:00
|
|
|
return
|
2020-08-07 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a copy of the input config with the ContentConfig set to use protobuf.
|
|
|
|
// Do not use this config to communicate with any CRD based APIs.
|
|
|
|
func createProtoKubeConfig(kubeConfig *restclient.Config) *restclient.Config {
|
|
|
|
protoKubeConfig := restclient.CopyConfig(kubeConfig)
|
|
|
|
const protoThenJSON = runtime.ContentTypeProtobuf + "," + runtime.ContentTypeJSON
|
|
|
|
protoKubeConfig.AcceptContentTypes = protoThenJSON
|
|
|
|
protoKubeConfig.ContentType = runtime.ContentTypeProtobuf
|
|
|
|
return protoKubeConfig
|
|
|
|
}
|