@@ -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
+ config "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"
@@ -31,6 +31,7 @@ import (
31
31
"github.com/openshift/configuration-anomaly-detection/pkg/metrics"
32
32
ocm "github.com/openshift/configuration-anomaly-detection/pkg/ocm"
33
33
"github.com/openshift/configuration-anomaly-detection/pkg/pagerduty"
34
+ "go.uber.org/zap"
34
35
35
36
"github.com/spf13/cobra"
36
37
)
@@ -40,19 +41,40 @@ var InvestigateCmd = &cobra.Command{
40
41
Use : "investigate" ,
41
42
SilenceUsage : true ,
42
43
Short : "Filter for and investigate supported alerts" ,
43
- RunE : run ,
44
+ PreRunE : func (cmd * cobra.Command , args []string ) error {
45
+ var err error
46
+
47
+ if config .Config .ConfigFile != "" {
48
+ // loads the configuration unless config file is present
49
+ // config should be used only when developing
50
+ if _ , err = config .Config .GetCadOcmClientID (cmd ); err != nil {
51
+ return fmt .Errorf ("failed to get cadOcmClientID: %w" , err )
52
+ }
53
+ if _ , err = config .Config .GetCadOcmClientSecret (cmd ); err != nil {
54
+ return fmt .Errorf ("failed to get cadOcmClientSecret: %w" , err )
55
+ }
56
+ if _ , err = config .Config .GetCadOcmURL (cmd ); err != nil {
57
+ return fmt .Errorf ("failed to get cadOcmURL: %w" , err )
58
+ }
59
+ }
60
+
61
+ if _ , err = config .Config .GetExperimental (cmd ); err != nil {
62
+ return fmt .Errorf ("failed to get experimental flag: %w" , err )
63
+ }
64
+
65
+ return nil
66
+ },
67
+ RunE : run ,
44
68
}
45
69
46
- var (
47
- logLevelFlag = ""
48
- payloadPath = "./payload.json"
49
- )
70
+ var payloadPath = "./payload.json"
50
71
51
72
const pagerdutyTitlePrefix = "[CAD Investigated]"
52
73
53
74
func init () {
75
+ logging .RawLogger = logging .InitLogger (config .Config .LogLevel , "" )
76
+
54
77
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
78
57
79
err := InvestigateCmd .MarkFlagRequired ("payload-path" )
58
80
if err != nil {
@@ -62,10 +84,6 @@ func init() {
62
84
63
85
func run (cmd * cobra.Command , _ []string ) error {
64
86
// 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 , "" )
68
- }
69
87
payload , err := os .ReadFile (payloadPath )
70
88
if err != nil {
71
89
return fmt .Errorf ("failed to read webhook payload: %w" , err )
@@ -87,8 +105,7 @@ func run(cmd *cobra.Command, _ []string) error {
87
105
}
88
106
}()
89
107
90
- _ , cadExperimentalEnabled := os .LookupEnv ("CAD_EXPERIMENTAL_ENABLED" )
91
- alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), cadExperimentalEnabled )
108
+ alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), config .Config .Experimental )
92
109
93
110
// Escalate all unsupported alerts
94
111
if alertInvestigation == nil {
@@ -109,8 +126,17 @@ func run(cmd *cobra.Command, _ []string) error {
109
126
return err
110
127
}
111
128
112
- ocmClient , err := GetOCMClient ()
113
- if err != nil {
129
+ var ocmClient * ocm.SdkClient
130
+ var ocmErr error
131
+ // Initialize the OCM client
132
+ // If the config file is set, use it to initialize the OCM client
133
+ // use config file only when developing
134
+ if config .Config .ConfigFile != "" {
135
+ ocmClient , ocmErr = ocm .NewFromConfig (config .Config .ConfigFile )
136
+ } else {
137
+ ocmClient , ocmErr = ocm .NewFromClientKeyPair (config .Config .CadOcmURL , config .Config .CadOcmClientID , config .Config .CadOcmClientSecret )
138
+ }
139
+ if ocmErr != nil {
114
140
return fmt .Errorf ("could not initialize ocm client: %w" , err )
115
141
}
116
142
@@ -122,13 +148,8 @@ func run(cmd *cobra.Command, _ []string) error {
122
148
// For installing clusters, externalID can be empty.
123
149
internalClusterID := cluster .ID ()
124
150
125
- // re-initialize logger for the internal-cluster-id context
126
- // if log-level flag is set, take priority over env + default
127
- if cmd .Flags ().Changed ("log-level" ) {
128
- logging .RawLogger = logging .InitLogger (logLevelFlag , internalClusterID )
129
- } else {
130
- logging .RawLogger = logging .InitLogger (logging .LogLevelString , internalClusterID )
131
- }
151
+ // add the internal-cluster-id context to the logger
152
+ logging .RawLogger = logging .RawLogger .With (zap .String ("cluster_id" , internalClusterID ))
132
153
133
154
requiresInvestigation , err := clusterRequiresInvestigation (cluster , pdClient , ocmClient )
134
155
if err != nil || ! requiresInvestigation {
@@ -184,22 +205,6 @@ func handleCADFailure(err error, resources *investigation.Resources, pdClient *p
184
205
}
185
206
}
186
207
187
- // GetOCMClient will retrieve the OcmClient from the 'ocm' package
188
- func GetOCMClient () (* ocm.SdkClient , error ) {
189
- cadOcmFilePath := os .Getenv ("CAD_OCM_FILE_PATH" )
190
-
191
- _ , err := os .Stat (cadOcmFilePath )
192
- if os .IsNotExist (err ) {
193
- configDir , err := os .UserConfigDir ()
194
- if err != nil {
195
- return nil , err
196
- }
197
- cadOcmFilePath = filepath .Join (configDir , "/ocm/ocm.json" )
198
- }
199
-
200
- return ocm .New (cadOcmFilePath )
201
- }
202
-
203
208
// Checks pre-requisites for a cluster investigation:
204
209
// - the cluster's state is supported by CAD for an investigation (= not uninstalling)
205
210
// - the cloud provider is supported by CAD (cluster is AWS)
0 commit comments