зеркало из
1
0
Форкнуть 0
azure-service-operator/main.go

145 строки
4.8 KiB
Go

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package main
import (
"flag"
"os"
aadpodv1 "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity/v1"
"k8s.io/apimachinery/pkg/runtime"
kscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
azurev1alpha2 "github.com/Azure/azure-service-operator/api/v1alpha2"
azurev1beta1 "github.com/Azure/azure-service-operator/api/v1beta1"
resourcemanagerconfig "github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
keyvaultSecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault"
k8sSecrets "github.com/Azure/azure-service-operator/pkg/secrets/kube"
"github.com/Azure/azure-service-operator/controllers"
"github.com/Azure/azure-service-operator/pkg/secrets"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = kscheme.AddToScheme(scheme)
_ = azurev1alpha1.AddToScheme(scheme)
_ = azurev1beta1.AddToScheme(scheme)
_ = azurev1alpha2.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
// We need to query AAD identity types, so add them to the scheme
_ = aadpodv1.AddToScheme(scheme)
}
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=azure.microsoft.com,resources=events,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
func main() {
var metricsAddr string
var enableLeaderElection bool
var secretClient secrets.SecretClient
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(false), zap.WriteTo(os.Stdout)))
err := resourcemanagerconfig.ParseEnvironment()
if err != nil {
setupLog.Error(err, "unable to parse settings required to provision resources in Azure")
os.Exit(1)
}
setupLog.V(0).Info("Configuration details", "Configuration", resourcemanagerconfig.ConfigString())
targetNamespaces := resourcemanagerconfig.TargetNamespaces()
var cacheFunc cache.NewCacheFunc
if targetNamespaces != nil {
cacheFunc = func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
opts.DefaultNamespaces = make(map[string]cache.Config, len(targetNamespaces))
for _, ns := range targetNamespaces {
opts.DefaultNamespaces[ns] = cache.Config{}
}
return cache.New(config, opts)
}
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
NewCache: cacheFunc,
LeaderElection: enableLeaderElection,
LivenessEndpointName: "/healthz",
Metrics: server.Options{
BindAddress: metricsAddr,
},
WebhookServer: webhook.NewServer(webhook.Options{
Port: 9443,
}),
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
keyvaultName := resourcemanagerconfig.GlobalCredentials().OperatorKeyvault()
if keyvaultName == "" {
setupLog.Info("Keyvault name is empty")
secretClient = k8sSecrets.New(mgr.GetClient(), resourcemanagerconfig.SecretNamingVersion())
} else {
setupLog.Info("Instantiating secrets client for keyvault " + keyvaultName)
secretClient = keyvaultSecrets.New(
keyvaultName,
resourcemanagerconfig.GlobalCredentials(),
resourcemanagerconfig.SecretNamingVersion(),
resourcemanagerconfig.PurgeDeletedKeyVaultSecrets(),
resourcemanagerconfig.RecoverSoftDeletedKeyVaultSecrets())
}
if resourcemanagerconfig.SelectedMode().IncludesWatchers() {
if err := controllers.RegisterReconcilers(mgr, scheme, secretClient); err != nil {
setupLog.Error(err, "unable to create controller")
os.Exit(1)
}
}
if resourcemanagerconfig.SelectedMode().IncludesWebhooks() {
if err := controllers.RegisterWebhooks(mgr); err != nil {
setupLog.Error(err, "unable to create webhook")
os.Exit(1)
}
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}