forked from intel/intel-ethernet-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
241 lines (206 loc) · 7.9 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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2021 Intel Corporation
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/otcshare/intel-ethernet-operator/pkg/utils"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
configv1 "github.com/openshift/api/config/v1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
ethernetv1 "github.com/otcshare/intel-ethernet-operator/apis/ethernet/v1"
flowconfigv1 "github.com/otcshare/intel-ethernet-operator/apis/flowconfig/v1"
flowconfigcontrollers "github.com/otcshare/intel-ethernet-operator/controllers/flowconfig"
fwddp_manager "github.com/otcshare/intel-ethernet-operator/pkg/fwddp-manager"
"github.com/otcshare/intel-ethernet-operator/pkg/utils/assets"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(configv1.Install(scheme))
utilruntime.Must(ethernetv1.AddToScheme(scheme))
utilruntime.Must(flowconfigv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
restConfig := ctrl.GetConfigOrDie()
var owner client.Object
err := setClusterType(restConfig)
if err != nil {
setupLog.Error(err, "unable to determine cluster type")
os.Exit(1)
}
runningInK8s := isRunningInPod()
var adHocClient client.Client
if adHocClient, err = client.New(restConfig, client.Options{Scheme: scheme}); err != nil {
setupLog.Error(err, "failed to create client")
os.Exit(1)
}
if runningInK8s {
owner = new(appsv1.Deployment)
if err := adHocClient.Get(
context.Background(),
client.ObjectKey{Name: "intel-ethernet-operator-controller-manager", Namespace: fwddp_manager.NAMESPACE},
owner,
); err != nil {
setupLog.Error(err, "unable to get operator deployment")
os.Exit(1)
}
}
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "8ee6d2ed.intel.com",
Namespace: fwddp_manager.NAMESPACE,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&fwddp_manager.EthernetClusterConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ethernet").WithName("EthernetClusterConfig"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EthernetClusterConfig")
os.Exit(1)
}
// Set min TLS for webhook server
mgr.GetWebhookServer().TLSMinVersion = "1.3"
// to disable webhook(e.g. when testing locally) run it as 'make run ENABLE_WEBHOOKS=false'
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&flowconfigv1.NodeFlowConfig{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "NodeFlowConfig")
os.Exit(1)
}
if err = (&flowconfigv1.ClusterFlowConfig{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ClusterFlowConfig")
os.Exit(1)
}
}
if err = (&flowconfigcontrollers.FlowConfigNodeAgentDeploymentReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("flowconfig").WithName("FlowConfigNodeAgentDeployment"),
Scheme: mgr.GetScheme(),
Owner: owner,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FlowConfigNodeAgentDeployment")
os.Exit(1)
}
if err = (&flowconfigcontrollers.ClusterFlowConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("flowconfig").WithName("ClusterFlowConfig"),
Scheme: mgr.GetScheme(),
Cluster2NodeRulesHashMap: make(map[types.NamespacedName]map[types.NamespacedName][]string),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterFlowConfig")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
if runningInK8s {
assetsToDeploy := []assets.Asset{
{ConfigMapName: "labeler-config", Path: "assets/100-labeler.yaml"},
{ConfigMapName: "daemon-config", Path: "assets/200-daemon.yaml", BlockingReadiness: assets.ReadinessPollConfig{Retries: 30, Delay: 20 * time.Second}},
}
if !utils.IsK8sDeployment() {
assetsToDeploy = append(assetsToDeploy, assets.Asset{ConfigMapName: "machine-config", Path: "assets/300-machine-config.yaml"})
}
assetsManager := &assets.Manager{
Client: adHocClient,
Namespace: fwddp_manager.NAMESPACE,
Log: ctrl.Log.WithName("manager"),
EnvPrefix: utils.IeoPrefix,
Scheme: scheme,
Owner: owner,
Assets: assetsToDeploy,
}
if err := assetsManager.DeployConfigMaps(context.Background()); err != nil {
setupLog.Error(err, "failed to deploy the config maps")
os.Exit(1)
}
if err := assetsManager.LoadFromConfigMapAndDeploy(context.Background()); err != nil {
setupLog.Error(err, "failed to deploy the assets")
os.Exit(1)
}
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func setClusterType(restConfig *rest.Config) error {
discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig)
if err != nil {
return fmt.Errorf("failed to create discoveryClient - %v", err)
}
apiList, err := discoveryClient.ServerGroups()
if err != nil {
return fmt.Errorf("issue occurred while fetching ServerGroups - %v", err)
}
for _, v := range apiList.Groups {
if v.Name == "route.openshift.io" {
setupLog.Info("found 'route.openshift.io' API - operator is running on OpenShift")
err := utils.SetOsEnvIfNotSet("ETHERNET_GENERIC_K8S", "false", setupLog)
if err != nil {
return fmt.Errorf("failed to set ETHERNET_GENERIC_K8S env variable - %v", err)
}
return nil
}
}
setupLog.Info("couldn't find 'route.openshift.io' API - operator is running on Kubernetes")
err = utils.SetOsEnvIfNotSet("ETHERNET_GENERIC_K8S", "true", setupLog)
if err != nil {
return fmt.Errorf("failed to set ETHERNET_GENERIC_K8S env variable - %v", err)
}
return nil
}
// isRunningInPod checks if we are running in K8s Pod or not. Assumption here is that in Pod env KUBERNETES_SERVICE_HOST env will be set by K8s
func isRunningInPod() bool {
_, keyPresent := os.LookupEnv("KUBERNETES_SERVICE_HOST")
return keyPresent
}