-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathstart.go
290 lines (250 loc) · 10.4 KB
/
start.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package start
import (
"context"
"fmt"
"os"
"time"
"github.com/openshift/api/features"
configv1client "github.com/openshift/client-go/config/clientset/versioned"
configv1informers "github.com/openshift/client-go/config/informers/externalversions"
"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/serviceability"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/version"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"k8s.io/utils/clock"
"github.com/openshift/insights-operator/pkg/config"
"github.com/openshift/insights-operator/pkg/controller"
)
const (
serviceCACertPath = "/var/run/configmaps/service-ca-bundle/service-ca.crt"
pbContentType = "application/vnd.kubernetes.protobuf"
pbAcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
missingVersion = "0.0.1-snapshot"
)
// NewOperator create the command for running the Insights Operator.
func NewOperator() *cobra.Command {
operator := &controller.Operator{
Controller: config.Controller{
StoragePath: "/var/lib/insights-operator",
Interval: 10 * time.Minute,
Endpoint: "https://console.redhat.com/api/ingress/v1/upload",
ReportEndpoint: "https://console.redhat.com/api/insights-results-aggregator/v2/cluster/%s/reports",
ConditionalGathererEndpoint: "https://console.redhat.com/api/gathering/gathering_rules",
ReportPullingDelay: 60 * time.Second,
ReportMinRetryTime: 10 * time.Second,
ReportPullingTimeout: 30 * time.Minute,
OCMConfig: config.OCMConfig{
SCAInterval: 8 * time.Hour,
SCAEndpoint: "https://api.openshift.com/api/accounts_mgmt/v1/certificates",
ClusterTransferEndpoint: "https://api.openshift.com/api/accounts_mgmt/v1/cluster_transfers",
ClusterTransferInterval: 12 * time.Hour,
},
},
}
cfg := controllercmd.NewControllerCommandConfig("openshift-insights-operator", version.Get(), operator.Run, clock.RealClock{})
cmd := &cobra.Command{
Use: "start",
Short: "Start the operator",
Run: runOperator(operator, cfg),
}
cmd.Flags().AddFlagSet(cfg.NewCommandWithContext(context.Background()).Flags())
return cmd
}
// NewGather create the command for running a single gather.
func NewGather() *cobra.Command {
operator := &controller.GatherJob{
Controller: config.Controller{
ConditionalGathererEndpoint: "https://console.redhat.com/api/gathering/gathering_rules",
StoragePath: "/var/lib/insights-operator",
Interval: 30 * time.Minute,
},
}
cfg := controllercmd.NewControllerCommandConfig("openshift-insights-operator", version.Get(), nil, clock.RealClock{})
cmd := &cobra.Command{
Use: "gather",
Short: "Does a single gather, without uploading it",
Run: runGather(operator, cfg),
}
cmd.Flags().AddFlagSet(cfg.NewCommandWithContext(context.Background()).Flags())
return cmd
}
func NewGatherAndUpload() *cobra.Command {
operator := &controller.GatherJob{
Controller: config.Controller{
ConditionalGathererEndpoint: "https://console.redhat.com/api/gathering/gathering_rules",
StoragePath: "/var/lib/insights-operator",
Interval: 2 * time.Hour,
Endpoint: "https://console.redhat.com/api/ingress/v1/upload",
ReportEndpoint: "https://console.redhat.com/api/insights-results-aggregator/v2/cluster/%s/reports",
ReportPullingDelay: 60 * time.Second,
ReportMinRetryTime: 10 * time.Second,
ReportPullingTimeout: 30 * time.Minute,
ProcessingStatusEndpoint: "https://console.redhat.com/api/insights-results-aggregator/v2/cluster/%s/request/%s/status",
ReportEndpointTechPreview: "https://console.redhat.com/api/insights-results-aggregator/v2/cluster/%s/request/%s/report",
},
}
cfg := controllercmd.NewControllerCommandConfig("openshift-insights-operator", version.Get(), nil, clock.RealClock{})
cmd := &cobra.Command{
Use: "gather-and-upload",
Short: "Runs the data gathering as job, uploads the data, waits for Insights analysis report and ends",
Run: runGatherAndUpload(operator, cfg),
}
cmd.Flags().AddFlagSet(cfg.NewCommand().Flags())
return cmd
}
// Boilerplate for running an operator and handling command line arguments.
func runOperator(operator *controller.Operator, cfg *controllercmd.ControllerCommandConfig) func(cmd *cobra.Command, _ []string) {
return func(cmd *cobra.Command, _ []string) {
// boilerplate for the "normal" command
defer serviceability.BehaviorOnPanic(os.Getenv("OPENSHIFT_ON_PANIC"), version.Get())()
defer serviceability.Profile(os.Getenv("OPENSHIFT_PROFILE")).Stop()
serviceability.StartProfiler()
if configArg := cmd.Flags().Lookup("config").Value.String(); len(configArg) == 0 {
klog.Exit("error: --config is required")
}
unstructured, operatorConfig, configBytes, err := cfg.Config()
if err != nil {
klog.Exit(err)
}
startingFileContent, observedFiles, err := cfg.AddDefaultRotationToConfig(operatorConfig, configBytes)
if err != nil {
klog.Exit(err)
}
// if the service CA is rotated, we want to restart
if data, err := os.ReadFile(serviceCACertPath); err == nil {
startingFileContent[serviceCACertPath] = data
} else {
klog.Infof("Unable to read service ca bundle: %v", err)
}
observedFiles = append(observedFiles, serviceCACertPath)
exitOnChangeReactorCh := make(chan struct{})
ctx := context.Background()
ctx2, cancel := context.WithCancel(ctx)
go func() {
select {
case <-exitOnChangeReactorCh:
cancel()
case <-ctx.Done():
cancel()
}
}()
builder := controllercmd.NewController("openshift-insights-operator", operator.Run, clock.RealClock{}).
WithKubeConfigFile(cmd.Flags().Lookup("kubeconfig").Value.String(), nil).
WithLeaderElection(operatorConfig.LeaderElection, "", "openshift-insights-operator-lock").
WithServer(operatorConfig.ServingInfo, operatorConfig.Authentication, operatorConfig.Authorization).
WithRestartOnChange(exitOnChangeReactorCh, startingFileContent, observedFiles...)
if err := builder.Run(ctx2, unstructured); err != nil {
klog.Error(err)
}
}
}
// Starts a single gather, main responsibility is loading in the necessary configs.
func runGather(operator *controller.GatherJob, cfg *controllercmd.ControllerCommandConfig) func(cmd *cobra.Command, _ []string) {
return func(cmd *cobra.Command, _ []string) {
clientConfig, protoConfig := createClientConfig(cmd, operator, cfg)
// Before the start of gathering we need to check if featureGates are enabled
ctx, cancel := context.WithTimeout(context.Background(), operator.Interval)
checkFeatureGates(ctx, clientConfig, operator)
// Run gatherer
if err := operator.Gather(ctx, clientConfig, protoConfig); err != nil {
klog.Error(err)
}
cancel()
os.Exit(0)
}
}
// Starts a single gather, main responsibility is loading in the necessary configs.
func runGatherAndUpload(
operator *controller.GatherJob, cfg *controllercmd.ControllerCommandConfig,
) func(cmd *cobra.Command, _ []string) {
return func(cmd *cobra.Command, _ []string) {
clientConfig, protoConfig := createClientConfig(cmd, operator, cfg)
// Before the start of gathering we need to check if featureGates are enabled
ctx, cancel := context.WithTimeout(context.Background(), operator.Interval)
checkFeatureGates(ctx, clientConfig, operator)
// Run gatherer
if err := operator.GatherAndUpload(clientConfig, protoConfig); err != nil {
klog.Exit(err)
}
cancel()
os.Exit(0)
}
}
func checkFeatureGates(ctx context.Context, clientConfig *rest.Config, operator *controller.GatherJob) {
configClientSet, err := configv1client.NewForConfig(clientConfig)
if err != nil {
klog.Exitf("error: building client: %v", err)
}
configInformers := configv1informers.NewSharedInformerFactory(configClientSet, 10*time.Minute)
desiredVersion := missingVersion
if envVersion, exists := os.LookupEnv("RELEASE_VERSION"); exists {
desiredVersion = envVersion
}
// By default, this will exit(0) the process if the featuregates ever change to a different set of values.
featureGateAccessor := featuregates.NewFeatureGateAccess(
desiredVersion, missingVersion,
configInformers.Config().V1().ClusterVersions(), configInformers.Config().V1().FeatureGates(),
events.NewLoggingEventRecorder("insights-gather", clock.RealClock{}),
)
go featureGateAccessor.Run(ctx)
go configInformers.Start(ctx.Done())
select {
case <-featureGateAccessor.InitialFeatureGatesObserved():
featureGates, _ := featureGateAccessor.CurrentFeatureGates()
klog.Infof("FeatureGates initialized: knownFeatureGates=%v", featureGates.KnownFeatures())
case <-time.After(1 * time.Minute):
klog.Errorf("timed out waiting for FeatureGate detection")
klog.Exit(fmt.Errorf("timed out waiting for FeatureGate detection"))
}
featureGates, err := featureGateAccessor.CurrentFeatureGates()
if err != nil {
klog.Exit(err)
}
operator.InsightsConfigAPIEnabled = featureGates.Enabled(features.FeatureGateInsightsConfigAPI)
operator.RuntimeExtractorEnabled = featureGates.Enabled(features.FeatureGateInsightsRuntimeExtractor)
}
func createClientConfig(
cmd *cobra.Command, operator *controller.GatherJob, cfg *controllercmd.ControllerCommandConfig,
) (clientConfig, protoConfig *rest.Config) {
if configArg := cmd.Flags().Lookup("config").Value.String(); len(configArg) == 0 {
klog.Exit("error: --config is required")
}
unstructured, _, _, err := cfg.Config()
if err != nil {
klog.Exit(err)
}
cont, err := config.LoadConfig(operator.Controller, unstructured.Object, config.ToDisconnectedController)
if err != nil {
klog.Exit(err)
}
operator.Controller = cont
var clientCfg *rest.Config
if kubeConfigPath := cmd.Flags().Lookup("kubeconfig").Value.String(); len(kubeConfigPath) > 0 {
kubeConfigBytes, err := os.ReadFile(kubeConfigPath) //nolint: govet
if err != nil {
klog.Exit(err)
}
kubeConfig, err := clientcmd.NewClientConfigFromBytes(kubeConfigBytes)
if err != nil {
klog.Exit(err)
}
clientCfg, err = kubeConfig.ClientConfig()
if err != nil {
klog.Exit(err)
}
} else {
clientCfg, err = rest.InClusterConfig()
if err != nil {
klog.Exit(err)
}
}
protoCfg := rest.CopyConfig(clientCfg)
protoCfg.AcceptContentTypes = pbAcceptContentTypes
protoCfg.ContentType = pbContentType
return clientCfg, protoCfg
}