forked from Azure/azure-service-operator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
146 lines (121 loc) · 5.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
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"
"github.com/Azure/azure-service-operator/controllers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
resourcemanagerconfig "github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/secrets"
keyvaultSecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault"
k8sSecrets "github.com/Azure/azure-service-operator/pkg/secrets/kube"
// +kubebuilder:scaffold:imports
)
var (
masterURL, kubeconfig, resources, clusterName string
cloudName, tenantID, subscriptionID, clientID, clientSecret string
useAADPodIdentity bool
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
aadPodIdentityGroupVersion := schema.GroupVersion{Group: aadpodv1.CRDGroup, Version: aadpodv1.CRDVersion}
scheme.AddKnownTypes(aadPodIdentityGroupVersion,
&aadpodv1.AzureIdentity{},
&aadpodv1.AzureIdentityList{},
&aadpodv1.AzureIdentityBinding{},
&aadpodv1.AzureIdentityBindingList{},
&aadpodv1.AzureAssignedIdentity{},
&aadpodv1.AzureAssignedIdentityList{},
&aadpodv1.AzurePodIdentityException{},
&aadpodv1.AzurePodIdentityExceptionList{})
metav1.AddToGroupVersion(scheme, aadPodIdentityGroupVersion)
}
// +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 = cache.MultiNamespacedCacheBuilder(targetNamespaces)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
NewCache: cacheFunc,
LeaderElection: enableLeaderElection,
LivenessEndpointName: "/healthz",
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(), config.SecretNamingVersion())
} else {
setupLog.Info("Instantiating secrets client for keyvault " + keyvaultName)
secretClient = keyvaultSecrets.New(
keyvaultName,
config.GlobalCredentials(),
config.SecretNamingVersion(),
config.PurgeDeletedKeyVaultSecrets(),
config.RecoverSoftDeletedKeyVaultSecrets())
}
if config.SelectedMode().IncludesWatchers() {
if err := controllers.RegisterReconcilers(mgr, scheme, secretClient); err != nil {
setupLog.Error(err, "unable to create controller")
os.Exit(1)
}
}
if config.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)
}
}