@@ -19,10 +19,10 @@ package investigate
19
19
import (
20
20
"fmt"
21
21
"os"
22
- "path/filepath"
23
22
"strings"
24
23
25
24
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
25
+ "github.com/openshift/configuration-anomaly-detection/cadctl/config"
26
26
investigations "github.com/openshift/configuration-anomaly-detection/pkg/investigations"
27
27
"github.com/openshift/configuration-anomaly-detection/pkg/investigations/ccam"
28
28
investigation "github.com/openshift/configuration-anomaly-detection/pkg/investigations/investigation"
@@ -40,39 +40,79 @@ var InvestigateCmd = &cobra.Command{
40
40
Use : "investigate" ,
41
41
SilenceUsage : true ,
42
42
Short : "Filter for and investigate supported alerts" ,
43
- RunE : run ,
43
+ PreRunE : func (cmd * cobra.Command , _ []string ) error {
44
+ var err error
45
+ var c config.Config
46
+
47
+ c , err = config .BuildConfig (cmd )
48
+ if err != nil {
49
+ return fmt .Errorf ("failed to build config: %w" , err )
50
+ }
51
+
52
+ // Initialize the logger here because it depends on user input
53
+ logging .RawLogger = logging .InitLogger (c .LogLevel )
54
+ logging .RawLogger = logging .WithPipelineName (logging .RawLogger , c .PipelineName )
55
+
56
+ // configure the RunE command using a wrapper to
57
+ // append the configuration into the command
58
+ cmd .RunE = func (cmd * cobra.Command , args []string ) error {
59
+ if err := run (c , cmd , args ); err != nil {
60
+ logging .Error (err )
61
+ }
62
+ return err
63
+ }
64
+
65
+ return nil
66
+ },
67
+ RunE : func (_ * cobra.Command , _ []string ) error {
68
+ // DO NOT REMOVE
69
+ // this ensures RunE work as expected
70
+ // RunE is set in the PreRunE function
71
+ // this works because the RunE is always called after PreRunE
72
+ return nil
73
+ },
44
74
}
45
75
46
- var (
47
- logLevelFlag = ""
48
- payloadPath = "./payload.json"
49
- )
76
+ var payloadPath = "./payload.json"
50
77
51
78
const pagerdutyTitlePrefix = "[CAD Investigated]"
52
79
53
80
func init () {
54
81
InvestigateCmd .Flags ().StringVarP (& payloadPath , "payload-path" , "p" , payloadPath , "the path to the payload" )
55
- InvestigateCmd .Flags ().StringVarP (& logging .LogLevelString , "log-level" , "l" , "" , "the log level [debug,info,warn,error,fatal], default = info" )
56
82
57
83
err := InvestigateCmd .MarkFlagRequired ("payload-path" )
58
84
if err != nil {
59
85
logging .Warn ("Could not mark flag 'payload-path' as required" )
60
86
}
61
87
}
62
88
63
- func run (cmd * cobra.Command , _ []string ) error {
64
- // early init of logger for logs before clusterID is known
65
- if cmd .Flags ().Changed ("log-level" ) {
66
- flagValue , _ := cmd .Flags ().GetString ("log-level" )
67
- logging .RawLogger = logging .InitLogger (flagValue , "" )
89
+ func run (c config.Config , _ * cobra.Command , _ []string ) error {
90
+ var err error
91
+
92
+ if c .PrometheusPushGateway == "" {
93
+ logging .Warn ("metrics disabled, set env 'CAD_PROMETHEUS_PUSHGATEWAY' to push metrics" )
94
+ }
95
+
96
+ err = metrics .Push (c .PrometheusPushGateway )
97
+ if err != nil {
98
+ logging .Warnf ("failed to push metrics: %v" , err )
68
99
}
100
+
101
+ // early init of logger for logs before clusterID is known
69
102
payload , err := os .ReadFile (payloadPath )
70
103
if err != nil {
71
104
return fmt .Errorf ("failed to read webhook payload: %w" , err )
72
105
}
73
106
logging .Info ("Running CAD with webhook payload:" , string (payload ))
74
107
75
- pdClient , err := pagerduty .GetPDClient (payload )
108
+ if c .PagerDutyToken == "" {
109
+ return fmt .Errorf ("missing PagerDuty token" )
110
+ }
111
+ if c .PagerDutySilentPolicy == "" {
112
+ return fmt .Errorf ("missing PagerDuty silent policy" )
113
+ }
114
+
115
+ pdClient , err := pagerduty .GetPDClient (payload , c .PagerDutyToken , c .PagerDutySilentPolicy )
76
116
if err != nil {
77
117
return fmt .Errorf ("could not initialize pagerduty client: %w" , err )
78
118
}
@@ -87,8 +127,7 @@ func run(cmd *cobra.Command, _ []string) error {
87
127
}
88
128
}()
89
129
90
- _ , cadExperimentalEnabled := os .LookupEnv ("CAD_EXPERIMENTAL_ENABLED" )
91
- alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), cadExperimentalEnabled )
130
+ alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), c .Experimental )
92
131
93
132
// Escalate all unsupported alerts
94
133
if alertInvestigation == nil {
@@ -109,8 +148,11 @@ func run(cmd *cobra.Command, _ []string) error {
109
148
return err
110
149
}
111
150
112
- ocmClient , err := GetOCMClient ()
113
- if err != nil {
151
+ var ocmClient * ocm.SdkClient
152
+ var ocmErr error
153
+
154
+ ocmClient , ocmErr = ocm .NewFromClientKeyPair (c .CadOcmURL , c .CadOcmClientID , c .CadOcmClientSecret )
155
+ if ocmErr != nil {
114
156
return fmt .Errorf ("could not initialize ocm client: %w" , err )
115
157
}
116
158
@@ -127,13 +169,8 @@ func run(cmd *cobra.Command, _ []string) error {
127
169
// For installing clusters, externalID can be empty.
128
170
internalClusterID := cluster .ID ()
129
171
130
- // re-initialize logger for the internal-cluster-id context
131
- // if log-level flag is set, take priority over env + default
132
- if cmd .Flags ().Changed ("log-level" ) {
133
- logging .RawLogger = logging .InitLogger (logLevelFlag , internalClusterID )
134
- } else {
135
- logging .RawLogger = logging .InitLogger (logging .LogLevelString , internalClusterID )
136
- }
172
+ // add the internal-cluster-id context to the logger
173
+ logging .RawLogger = logging .WithClusterID (logging .RawLogger , internalClusterID )
137
174
138
175
requiresInvestigation , err := clusterRequiresInvestigation (cluster , pdClient , ocmClient )
139
176
if err != nil || ! requiresInvestigation {
@@ -145,7 +182,22 @@ func run(cmd *cobra.Command, _ []string) error {
145
182
return fmt .Errorf ("could not retrieve Cluster Deployment for %s: %w" , internalClusterID , err )
146
183
}
147
184
148
- customerAwsClient , err := managedcloud .CreateCustomerAWSClient (cluster , ocmClient )
185
+ if c .BackplaneURL == "" {
186
+ return fmt .Errorf ("missing backplane URL" )
187
+ }
188
+ if c .BackplaneProxyURL == "" {
189
+ logging .Warn ("missing backplane proxy URL, using default" )
190
+ }
191
+ if c .BackplaneInitialARN == "" {
192
+ return fmt .Errorf ("missing backplane initial ARN" )
193
+ }
194
+ customerAwsClient , err := managedcloud .CreateCustomerAWSClient (
195
+ cluster ,
196
+ ocmClient ,
197
+ c .BackplaneURL ,
198
+ c .BackplaneProxyURL ,
199
+ c .BackplaneInitialARN ,
200
+ )
149
201
if err != nil {
150
202
ccamResources := & investigation.Resources {Name : "ccam" , Cluster : cluster , ClusterDeployment : clusterDeployment , AwsClient : customerAwsClient , OcmClient : ocmClient , PdClient : pdClient , Notes : nil , AdditionalResources : map [string ]interface {}{"error" : err }}
151
203
inv := ccam.Investigation {}
@@ -154,7 +206,16 @@ func run(cmd *cobra.Command, _ []string) error {
154
206
return err
155
207
}
156
208
157
- investigationResources = & investigation.Resources {Name : alertInvestigation .Name (), Cluster : cluster , ClusterDeployment : clusterDeployment , AwsClient : customerAwsClient , OcmClient : ocmClient , PdClient : pdClient , Notes : nil }
209
+ investigationResources = & investigation.Resources {
210
+ Name : alertInvestigation .Name (),
211
+ BackplaneURL : c .BackplaneURL ,
212
+ Cluster : cluster ,
213
+ ClusterDeployment : clusterDeployment ,
214
+ AwsClient : customerAwsClient ,
215
+ OcmClient : ocmClient ,
216
+ PdClient : pdClient ,
217
+ Notes : nil ,
218
+ }
158
219
159
220
logging .Infof ("Starting investigation for %s" , alertInvestigation .Name ())
160
221
result , err := alertInvestigation .Run (investigationResources )
@@ -189,22 +250,6 @@ func handleCADFailure(err error, resources *investigation.Resources, pdClient *p
189
250
}
190
251
}
191
252
192
- // GetOCMClient will retrieve the OcmClient from the 'ocm' package
193
- func GetOCMClient () (* ocm.SdkClient , error ) {
194
- cadOcmFilePath := os .Getenv ("CAD_OCM_FILE_PATH" )
195
-
196
- _ , err := os .Stat (cadOcmFilePath )
197
- if os .IsNotExist (err ) {
198
- configDir , err := os .UserConfigDir ()
199
- if err != nil {
200
- return nil , err
201
- }
202
- cadOcmFilePath = filepath .Join (configDir , "/ocm/ocm.json" )
203
- }
204
-
205
- return ocm .New (cadOcmFilePath )
206
- }
207
-
208
253
// Checks pre-requisites for a cluster investigation:
209
254
// - the cluster's state is supported by CAD for an investigation (= not uninstalling)
210
255
// - the cloud provider is supported by CAD (cluster is AWS)
0 commit comments