-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
262 lines (222 loc) · 9.73 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
/*
Copyright 2021.
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"
"errors"
"flag"
"fmt"
"github.com/vmware/cbcontainers-operator/cbcontainers/communication/gateway"
"github.com/vmware/cbcontainers-operator/cbcontainers/remote_configuration"
"github.com/vmware/cbcontainers-operator/cbcontainers/state"
"github.com/vmware/cbcontainers-operator/cbcontainers/state/agent_applyment"
"github.com/vmware/cbcontainers-operator/cbcontainers/state/applyment"
"github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
"github.com/vmware/cbcontainers-operator/cbcontainers/state/operator"
coreV1 "k8s.io/api/core/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"strings"
"sync"
"github.com/vmware/cbcontainers-operator/cbcontainers/processors"
// 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"
operatorcontainerscarbonblackiov1 "github.com/vmware/cbcontainers-operator/api/v1"
certificatesUtils "github.com/vmware/cbcontainers-operator/cbcontainers/utils/certificates"
"github.com/vmware/cbcontainers-operator/controllers"
"k8s.io/apimachinery/pkg/runtime"
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/healthz"
// +kubebuilder:scaffold:imports
"sigs.k8s.io/controller-runtime/pkg/cache"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
const (
NamespaceIdentifier = "default"
httpProxyEnv = "HTTP_PROXY"
httpsProxyEnv = "HTTPS_PROXY"
noProxyEnv = "NO_PROXY"
namespaceEnv = "OPERATOR_NAMESPACE"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(operatorcontainerscarbonblackiov1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func setupNoProxyEnv(namespace string) error {
httpProxyLen := len(strings.TrimSpace(os.Getenv(httpProxyEnv)))
httpsProxyLen := len(strings.TrimSpace(os.Getenv(httpsProxyEnv)))
_, foundNoProxyVar := os.LookupEnv(noProxyEnv)
// Don't set NO_PROXY if we don't have any proxies defined, or we already
// have a NO_PROXY env var present (even if it's empty)
if httpProxyLen+httpsProxyLen == 0 || foundNoProxyVar {
return nil
}
noProxy, err := controllers.GetDefaultNoProxyValue(namespace)
if err != nil {
return fmt.Errorf("unable to detect default NO_PROXY value: %w", err)
}
if err = os.Setenv(noProxyEnv, noProxy); err != nil {
return fmt.Errorf("could not set NO_PROXY value: %w", err)
}
setupLog.Info(fmt.Sprintf("using NO_PROXY value %q", noProxy))
return nil
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "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", true,
"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)))
setupLog.Info("Getting the namespace where operator is running and which should host the agent")
operatorNamespace := os.Getenv(namespaceEnv)
if operatorNamespace == "" {
setupLog.Info(fmt.Sprintf("Operator namespace variable was not found. Falling back to default %s", common.DataPlaneNamespaceName))
operatorNamespace = common.DataPlaneNamespaceName
}
setupLog.Info(fmt.Sprintf("Operator and agent namespace: %s", operatorNamespace))
if err := setupNoProxyEnv(operatorNamespace); err != nil {
setupLog.Error(err, "unable to setup default proxy settings")
os.Exit(1)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "d27fd235.operator.containers.carbonblack.io",
Logger: ctrl.Log,
Cache: cache.Options{
Namespaces: []string{operatorNamespace},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
clusterIdentifier, k8sVersion := extractConfigurationVariables(mgr)
operatorVersionProvider := operator.NewEnvVersionProvider()
var processorGatewayCreator processors.APIGatewayCreator = func(cbContainersCluster *operatorcontainerscarbonblackiov1.CBContainersAgent, accessToken string) (processors.APIGateway, error) {
return gateway.NewDefaultGatewayCreator().CreateGateway(cbContainersCluster, accessToken)
}
cbContainersAgentLogger := ctrl.Log.WithName("controllers").WithName("CBContainersAgent")
if err = (&controllers.CBContainersAgentController{
Client: mgr.GetClient(),
Log: cbContainersAgentLogger,
Scheme: mgr.GetScheme(),
K8sVersion: k8sVersion,
Namespace: operatorNamespace,
AccessTokenProvider: operator.NewSecretAccessTokenProvider(mgr.GetClient()),
ClusterProcessor: processors.NewAgentProcessor(cbContainersAgentLogger, processorGatewayCreator, operatorVersionProvider, clusterIdentifier),
StateApplier: state.NewStateApplier(mgr.GetAPIReader(), agent_applyment.NewAgentComponent(applyment.NewComponentApplier(mgr.GetClient())), k8sVersion, operatorNamespace, clusterIdentifier, certificatesUtils.NewCertificateCreator(), cbContainersAgentLogger),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CBContainersAgent")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("check", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
k8sClient := mgr.GetClient()
log := ctrl.Log.WithName("configurator")
operatorVersion, err := operatorVersionProvider.GetOperatorVersion()
if err != nil && !errors.Is(err, operator.ErrNotSemVer) {
setupLog.Error(err, "unable to read the running operator's version from environment variable")
os.Exit(1)
}
var validatorCreator remote_configuration.ValidatorCreator
if err != nil && errors.Is(err, operator.ErrNotSemVer) {
setupLog.Info(fmt.Sprintf("Detected operator version (%s) is not a semantic version. Compatibility checks for remote configuration will be disabled", operatorVersion))
validatorCreator = func(_ remote_configuration.ApiGateway) (remote_configuration.ChangeValidator, error) {
return &remote_configuration.EmptyConfigurationChangeValidator{}, nil
}
} else {
validatorCreator = func(gateway remote_configuration.ApiGateway) (remote_configuration.ChangeValidator, error) {
return remote_configuration.NewConfigurationChangeValidator(operatorVersion, gateway)
}
}
var configuratorGatewayCreator remote_configuration.ApiCreator = func(cbContainersCluster *operatorcontainerscarbonblackiov1.CBContainersAgent, accessToken string) (remote_configuration.ApiGateway, error) {
return gateway.NewDefaultGatewayCreator().CreateGateway(cbContainersCluster, accessToken)
}
applier := remote_configuration.NewConfigurator(
k8sClient,
configuratorGatewayCreator,
log,
operator.NewSecretAccessTokenProvider(k8sClient),
validatorCreator,
operatorNamespace,
clusterIdentifier,
)
applierController := controllers.NewRemoteConfigurationController(applier, log)
var wg sync.WaitGroup
wg.Add(2)
signalsContext := ctrl.SetupSignalHandler()
go func() {
defer wg.Done()
setupLog.Info("starting manager")
if err := mgr.Start(signalsContext); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}()
go func() {
defer wg.Done()
setupLog.Info("Starting remote configurator")
applierController.RunLoop(signalsContext)
}()
wg.Wait()
}
func extractConfigurationVariables(mgr manager.Manager) (clusterIdentifier string, k8sVersion string) {
setupLog.Info(fmt.Sprintf("Getting Cluster Identifier: %v uid", NamespaceIdentifier))
namespace := &coreV1.Namespace{}
apiReader := mgr.GetAPIReader()
if err := apiReader.Get(context.Background(), client.ObjectKey{Namespace: NamespaceIdentifier, Name: NamespaceIdentifier}, namespace); err != nil {
setupLog.Error(err, fmt.Sprintf("unable to get the %v namespace", NamespaceIdentifier))
os.Exit(1)
}
clusterIdentifier = string(namespace.UID)
setupLog.Info(fmt.Sprintf("Cluster Identifier: %v", clusterIdentifier))
setupLog.Info("Getting Nodes list")
nodesList := &coreV1.NodeList{}
if err := apiReader.List(context.Background(), nodesList); err != nil || nodesList.Items == nil || len(nodesList.Items) < 1 {
setupLog.Error(err, "couldn't get nodes list")
os.Exit(1)
}
k8sVersion = nodesList.Items[0].Status.NodeInfo.KubeletVersion
setupLog.Info(fmt.Sprintf("K8s version is: %v", k8sVersion))
return
}