forked from tigera/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
266 lines (230 loc) · 8.86 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"net/url"
"os"
goruntime "runtime"
"github.com/cloudflare/cfssl/log"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
v3 "github.com/tigera/api/pkg/apis/projectcalico/v3"
operatorv1 "github.com/tigera/operator/api/v1"
operatorv1beta1 "github.com/tigera/operator/api/v1beta1"
"github.com/tigera/operator/controllers"
"github.com/tigera/operator/pkg/apis"
"github.com/tigera/operator/pkg/awssgsetup"
"github.com/tigera/operator/pkg/components"
"github.com/tigera/operator/pkg/controller/options"
"github.com/tigera/operator/pkg/controller/utils"
"github.com/tigera/operator/pkg/dns"
"github.com/tigera/operator/version"
// +kubebuilder:scaffold:imports
)
var (
defaultMetricsPort int32 = 8484
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(operatorv1.AddToScheme(scheme))
utilruntime.Must(operatorv1beta1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
utilruntime.Must(apis.AddToScheme(scheme))
}
func printVersion() {
log.Info(fmt.Sprintf("Version: %v", version.VERSION))
log.Info(fmt.Sprintf("Go Version: %s", goruntime.Version()))
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", goruntime.GOOS, goruntime.GOARCH))
// TODO: Add this back if we can
//log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
}
func main() {
var enableLeaderElection bool
// urlOnlyKubeconfig is a slight hack; we need to get the apiserver from the
// kubeconfig but should use the in-cluster service account
var urlOnlyKubeconfig string
var showVersion bool
var sgSetup bool
flag.BoolVar(&enableLeaderElection, "enable-leader-election", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&urlOnlyKubeconfig, "url-only-kubeconfig", "",
"Path to a kubeconfig, but only for the apiserver url.")
flag.BoolVar(&showVersion, "version", false,
"Show version information")
flag.BoolVar(&sgSetup, "aws-sg-setup", false,
"Setup Security Groups in AWS (should only be used on OpenShift).")
opts := zap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.WriteTo(os.Stdout), zap.UseFlagOptions(&opts)))
if showVersion {
fmt.Println("Operator:", version.VERSION, "Calico:", components.CalicoRelease, "Enterprise:", components.EnterpriseRelease)
for _, x := range components.CalicoComponents {
ref, _ := components.GetReference(x, "", "", nil)
fmt.Println(ref)
}
for _, x := range components.EnterpriseComponents {
ref, _ := components.GetReference(x, "", "", nil)
fmt.Println(ref)
}
os.Exit(0)
}
if urlOnlyKubeconfig != "" {
if err := setKubernetesServiceEnv(urlOnlyKubeconfig); err != nil {
setupLog.Error(err, "Terminating")
os.Exit(1)
}
}
printVersion()
ctx := context.Background()
if sgSetup {
log.Info("Setting up AWS Security Groups")
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
client, err := client.New(cfg, client.Options{})
if err != nil {
log.Error(err, "")
os.Exit(1)
}
err = awssgsetup.SetupAWSSecurityGroups(ctx, client)
if err != nil {
log.Error(err, "")
os.Exit(1)
}
os.Exit(0)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr(),
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "operator-lock",
// We should test this again in the future to see if the problem with LicenseKey updates
// being missed is resolved. Prior to controller-runtime 0.7 we observed Test failures
// where LicenseKey updates would be missed and the client cache did not have the LicenseKey.
// The controller-runtime was updated and we made use of this ClientDisableCacheFor feature
// for the LicenseKey. We should test again in the future to see if the cache issue is fixed
// and we can remove this. Here is a link to the upstream issue
// https://github.com/kubernetes-sigs/controller-runtime/issues/1316
ClientDisableCacheFor: []client.Object{
&v3.LicenseKey{},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
log.Error(err, "Failed to get client for auto provider discovery")
os.Exit(1)
}
// Attempt to auto discover the provider
provider, err := utils.AutoDiscoverProvider(ctx, clientset)
if err != nil {
setupLog.Error(err, "Auto discovery of Provider failed")
os.Exit(1)
}
setupLog.WithValues("provider", provider).Info("Checking type of cluster")
// Determine if we need to start the TSEE specific controllers.
enterpriseCRDExists, err := utils.RequiresTigeraSecure(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "Failed to determine if TSEE is required")
os.Exit(1)
}
setupLog.WithValues("required", enterpriseCRDExists).Info("Checking if TSEE controllers are required")
amazonCRDExists, err := utils.RequiresAmazonController(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "Failed to determine if AmazonCloudIntegration is required")
os.Exit(1)
}
clusterDomain, err := dns.GetClusterDomain(dns.DefaultResolveConfPath)
if err != nil {
clusterDomain = dns.DefaultClusterDomain
log.Error(err, fmt.Sprintf("Couldn't find the cluster domain from the resolv.conf, defaulting to %s", clusterDomain))
}
options := options.AddOptions{
DetectedProvider: provider,
EnterpriseCRDExists: enterpriseCRDExists,
AmazonCRDExists: amazonCRDExists,
ClusterDomain: clusterDomain,
}
err = controllers.AddToManager(mgr, options)
if err != nil {
setupLog.Error(err, "unable to create controllers")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// setKubernetesServiceEnv configured the environment with the location of the Kubernetes API
// based on the provided kubeconfig file. We need this since we can't rely on the kube-proxy being present,
// since this operator may be the one installing the proxy! It's based off of logic in the cluster-network-operator.
// https://github.com/openshift/cluster-network-operator/blob/4d8a780f7b0f8b6a258aaba002a77d3313fa8fc8/cmd/cluster-network-operator/main.go#L32-L72
func setKubernetesServiceEnv(kubeconfigFile string) error {
kubeconfig, err := clientcmd.LoadFromFile(kubeconfigFile)
if err != nil {
return err
}
clusterName := kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster
apiURL := kubeconfig.Clusters[clusterName].Server
url, err := url.Parse(apiURL)
if err != nil {
return err
}
// The kubernetes in-cluster functions don't let you override the apiserver
// directly; gotta "pass" it via environment vars.
log.Info("Overriding kubernetes api to %s", apiURL)
os.Setenv("KUBERNETES_SERVICE_HOST", url.Hostname())
os.Setenv("KUBERNETES_SERVICE_PORT", url.Port())
return nil
}
// metricsAddr processes user-specified metrics host and port and sets
// default values accordingly.
func metricsAddr() string {
metricsHost := os.Getenv("METRICS_HOST")
metricsPort := os.Getenv("METRICS_PORT")
// if neither are specified, disable metrics.
if metricsHost == "" && metricsPort == "" {
// the controller-runtime accepts '0' to denote that metrics should be disabled.
return "0"
}
// if just a host is specified, listen on port 8484 of that host.
if metricsHost != "" && metricsPort == "" {
// the controller-runtime will choose a random port if none is specified.
// so use the defaultMetricsPort in that case.
return fmt.Sprintf("%s:%d", metricsHost, defaultMetricsPort)
}
// finally, handle cases where just a port is specified or both are specified in the same case
// since controller-runtime correctly uses all interfaces if no host is specified.
return fmt.Sprintf("%s:%s", metricsHost, metricsPort)
}