forked from openshift/insights-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.go
89 lines (76 loc) · 2.69 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
package start
import (
"context"
"io/ioutil"
"math/rand"
"os"
"time"
"github.com/openshift/library-go/pkg/controller/controllercmd"
"github.com/openshift/library-go/pkg/serviceability"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/version"
"k8s.io/klog"
"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"
func NewOperator() *cobra.Command {
operator := &controller.Support{
Controller: config.Controller{
StoragePath: "/var/lib/insights-operator",
Interval: 10 * time.Minute,
Endpoint: "https://cloud.redhat.com/api/ingress/v1/upload",
},
}
cfg := controllercmd.NewControllerCommandConfig("openshift-insights-operator", version.Get(), operator.Run)
cmd := &cobra.Command{
Use: "start",
Short: "Start the operator",
Run: func(cmd *cobra.Command, args []string) {
// boiler plate for the "normal" command
rand.Seed(time.Now().UTC().UnixNano())
defer serviceability.BehaviorOnPanic(os.Getenv("OPENSHIFT_ON_PANIC"), version.Get())()
defer serviceability.Profile(os.Getenv("OPENSHIFT_PROFILE")).Stop()
serviceability.StartProfiler()
if config := cmd.Flags().Lookup("config").Value.String(); len(config) == 0 {
klog.Fatalf("error: --config is required")
}
unstructured, config, configBytes, err := cfg.Config()
if err != nil {
klog.Fatal(err)
}
startingFileContent, observedFiles, err := cfg.AddDefaultRotationToConfig(config, configBytes)
if err != nil {
klog.Fatal(err)
}
// if the service CA is rotated, we want to restart
if data, err := ioutil.ReadFile(serviceCACertPath); err == nil {
startingFileContent[serviceCACertPath] = data
} else {
klog.V(4).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).
WithKubeConfigFile(cmd.Flags().Lookup("kubeconfig").Value.String(), nil).
WithLeaderElection(config.LeaderElection, "", "openshift-insights-operator-lock").
WithServer(config.ServingInfo, config.Authentication, config.Authorization).
WithRestartOnChange(exitOnChangeReactorCh, startingFileContent, observedFiles...)
if err := builder.Run(ctx2, unstructured); err != nil {
klog.Fatal(err)
}
},
}
cmd.Flags().AddFlagSet(cfg.NewCommand().Flags())
return cmd
}