@@ -9,8 +9,11 @@ import (
9
9
10
10
"github.com/openshift/library-go/pkg/controller/controllercmd"
11
11
"github.com/openshift/library-go/pkg/serviceability"
12
+
12
13
"github.com/spf13/cobra"
13
14
"k8s.io/client-go/pkg/version"
15
+ "k8s.io/client-go/rest"
16
+ "k8s.io/client-go/tools/clientcmd"
14
17
"k8s.io/klog/v2"
15
18
16
19
"github.com/openshift/insights-operator/pkg/config"
@@ -19,8 +22,9 @@ import (
19
22
20
23
const serviceCACertPath = "/var/run/configmaps/service-ca-bundle/service-ca.crt"
21
24
25
+ // Create the commad for running the Insights Operator.
22
26
func NewOperator () * cobra.Command {
23
- operator := & controller.Support {
27
+ operator := & controller.Operator {
24
28
Controller : config.Controller {
25
29
StoragePath : "/var/lib/insights-operator" ,
26
30
Interval : 10 * time .Minute ,
@@ -35,59 +39,128 @@ func NewOperator() *cobra.Command {
35
39
cmd := & cobra.Command {
36
40
Use : "start" ,
37
41
Short : "Start the operator" ,
38
- Run : func (cmd * cobra.Command , args []string ) {
39
- // boiler plate for the "normal" command
40
- rand .Seed (time .Now ().UTC ().UnixNano ())
41
- defer serviceability .BehaviorOnPanic (os .Getenv ("OPENSHIFT_ON_PANIC" ), version .Get ())()
42
- defer serviceability .Profile (os .Getenv ("OPENSHIFT_PROFILE" )).Stop ()
43
- serviceability .StartProfiler ()
44
-
45
- if config := cmd .Flags ().Lookup ("config" ).Value .String (); len (config ) == 0 {
46
- klog .Fatalf ("error: --config is required" )
47
- }
48
-
49
- unstructured , config , configBytes , err := cfg .Config ()
50
- if err != nil {
51
- klog .Fatal (err )
52
- }
42
+ Run : runOperator (operator , cfg ),
43
+ }
44
+ cmd .Flags ().AddFlagSet (cfg .NewCommand ().Flags ())
53
45
54
- startingFileContent , observedFiles , err := cfg .AddDefaultRotationToConfig (config , configBytes )
55
- if err != nil {
56
- klog .Fatal (err )
57
- }
46
+ return cmd
47
+ }
58
48
59
- // if the service CA is rotated, we want to restart
60
- if data , err := ioutil .ReadFile (serviceCACertPath ); err == nil {
61
- startingFileContent [serviceCACertPath ] = data
62
- } else {
63
- klog .V (4 ).Infof ("Unable to read service ca bundle: %v" , err )
64
- }
65
- observedFiles = append (observedFiles , serviceCACertPath )
66
-
67
- exitOnChangeReactorCh := make (chan struct {})
68
- ctx := context .Background ()
69
- ctx2 , cancel := context .WithCancel (ctx )
70
- go func () {
71
- select {
72
- case <- exitOnChangeReactorCh :
73
- cancel ()
74
- case <- ctx .Done ():
75
- cancel ()
76
- }
77
- }()
78
-
79
- builder := controllercmd .NewController ("openshift-insights-operator" , operator .Run ).
80
- WithKubeConfigFile (cmd .Flags ().Lookup ("kubeconfig" ).Value .String (), nil ).
81
- WithLeaderElection (config .LeaderElection , "" , "openshift-insights-operator-lock" ).
82
- WithServer (config .ServingInfo , config .Authentication , config .Authorization ).
83
- WithRestartOnChange (exitOnChangeReactorCh , startingFileContent , observedFiles ... )
84
-
85
- if err := builder .Run (ctx2 , unstructured ); err != nil {
86
- klog .Fatal (err )
87
- }
49
+ // Create the commad for running the a single gather.
50
+ func NewGather () * cobra.Command {
51
+ operator := & controller.GatherJob {
52
+ Controller : config.Controller {
53
+ StoragePath : "/var/lib/insights-operator" ,
54
+ Interval : 30 * time .Minute ,
88
55
},
89
56
}
57
+ cfg := controllercmd .NewControllerCommandConfig ("openshift-insights-operator" , version .Get (), nil )
58
+ cmd := & cobra.Command {
59
+ Use : "gather" ,
60
+ Short : "Does a single gather, without uploading it" ,
61
+ Run : runGather (* operator , cfg ),
62
+ }
90
63
cmd .Flags ().AddFlagSet (cfg .NewCommand ().Flags ())
91
64
92
65
return cmd
93
66
}
67
+
68
+ // Starts a single gather, main responsibility is loading in the necessary configs.
69
+ func runGather (operator controller.GatherJob , cfg * controllercmd.ControllerCommandConfig ) func (cmd * cobra.Command , args []string ) {
70
+ return func (cmd * cobra.Command , args []string ) {
71
+ if config := cmd .Flags ().Lookup ("config" ).Value .String (); len (config ) == 0 {
72
+ klog .Fatalf ("error: --config is required" )
73
+ }
74
+ unstructured , _ , _ , err := cfg .Config ()
75
+ if err != nil {
76
+ klog .Fatal (err )
77
+ }
78
+ cont , err := config .LoadConfig (operator .Controller , unstructured .Object , config .ToDisconnectedController )
79
+ if err != nil {
80
+ klog .Fatal (err )
81
+ }
82
+ operator .Controller = cont
83
+
84
+ kubeConfigPath := cmd .Flags ().Lookup ("kubeconfig" ).Value .String ()
85
+ if len (kubeConfigPath ) == 0 {
86
+ klog .Fatalf ("error: --kubeconfig is required" )
87
+ }
88
+ kubeConfigBytes , err := ioutil .ReadFile (kubeConfigPath )
89
+ if err != nil {
90
+ klog .Fatal (err )
91
+ }
92
+ kubeConfig , err := clientcmd .NewClientConfigFromBytes (kubeConfigBytes )
93
+ if err != nil {
94
+ klog .Fatal (err )
95
+ }
96
+ clientConfig , err := kubeConfig .ClientConfig ()
97
+ if err != nil {
98
+ klog .Fatal (err )
99
+ }
100
+ protoConfig := rest .CopyConfig (clientConfig )
101
+ protoConfig .AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
102
+ protoConfig .ContentType = "application/vnd.kubernetes.protobuf"
103
+
104
+ ctx , cancel := context .WithTimeout (context .Background (), operator .Interval )
105
+ err = operator .Gather (ctx , clientConfig , protoConfig )
106
+ if err != nil {
107
+ klog .Fatal (err )
108
+ }
109
+ cancel ()
110
+ os .Exit (0 )
111
+ }
112
+ }
113
+
114
+ // Boilerplate for running an operator and handling command line arguments.
115
+ func runOperator (operator * controller.Operator , cfg * controllercmd.ControllerCommandConfig ) func (cmd * cobra.Command , args []string ) {
116
+ return func (cmd * cobra.Command , args []string ) {
117
+ // boiler plate for the "normal" command
118
+ rand .Seed (time .Now ().UTC ().UnixNano ())
119
+ defer serviceability .BehaviorOnPanic (os .Getenv ("OPENSHIFT_ON_PANIC" ), version .Get ())()
120
+ defer serviceability .Profile (os .Getenv ("OPENSHIFT_PROFILE" )).Stop ()
121
+ serviceability .StartProfiler ()
122
+
123
+ if config := cmd .Flags ().Lookup ("config" ).Value .String (); len (config ) == 0 {
124
+ klog .Fatalf ("error: --config is required" )
125
+ }
126
+
127
+ unstructured , config , configBytes , err := cfg .Config ()
128
+ if err != nil {
129
+ klog .Fatal (err )
130
+ }
131
+
132
+ startingFileContent , observedFiles , err := cfg .AddDefaultRotationToConfig (config , configBytes )
133
+ if err != nil {
134
+ klog .Fatal (err )
135
+ }
136
+
137
+ // if the service CA is rotated, we want to restart
138
+ if data , err := ioutil .ReadFile (serviceCACertPath ); err == nil {
139
+ startingFileContent [serviceCACertPath ] = data
140
+ } else {
141
+ klog .V (4 ).Infof ("Unable to read service ca bundle: %v" , err )
142
+ }
143
+ observedFiles = append (observedFiles , serviceCACertPath )
144
+
145
+ exitOnChangeReactorCh := make (chan struct {})
146
+ ctx := context .Background ()
147
+ ctx2 , cancel := context .WithCancel (ctx )
148
+ go func () {
149
+ select {
150
+ case <- exitOnChangeReactorCh :
151
+ cancel ()
152
+ case <- ctx .Done ():
153
+ cancel ()
154
+ }
155
+ }()
156
+
157
+ builder := controllercmd .NewController ("openshift-insights-operator" , operator .Run ).
158
+ WithKubeConfigFile (cmd .Flags ().Lookup ("kubeconfig" ).Value .String (), nil ).
159
+ WithLeaderElection (config .LeaderElection , "" , "openshift-insights-operator-lock" ).
160
+ WithServer (config .ServingInfo , config .Authentication , config .Authorization ).
161
+ WithRestartOnChange (exitOnChangeReactorCh , startingFileContent , observedFiles ... )
162
+ if err := builder .Run (ctx2 , unstructured ); err != nil {
163
+ klog .Fatal (err )
164
+ }
165
+ }
166
+ }
0 commit comments