-
Notifications
You must be signed in to change notification settings - Fork 203
/
main.go
144 lines (118 loc) · 4.78 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
// 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)
}
}