@@ -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,42 @@ 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
70
var (
47
- logLevelFlag = ""
48
- payloadPath = "./payload.json"
71
+ payloadPath = "./payload.json"
49
72
)
50
73
51
74
const pagerdutyTitlePrefix = "[CAD Investigated]"
52
75
53
76
func init () {
77
+ logging .RawLogger = logging .InitLogger (config .Config .LogLevel , "" )
78
+
54
79
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
80
57
81
err := InvestigateCmd .MarkFlagRequired ("payload-path" )
58
82
if err != nil {
@@ -62,10 +86,6 @@ func init() {
62
86
63
87
func run (cmd * cobra.Command , _ []string ) error {
64
88
// 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
89
payload , err := os .ReadFile (payloadPath )
70
90
if err != nil {
71
91
return fmt .Errorf ("failed to read webhook payload: %w" , err )
@@ -87,8 +107,7 @@ func run(cmd *cobra.Command, _ []string) error {
87
107
}
88
108
}()
89
109
90
- _ , cadExperimentalEnabled := os .LookupEnv ("CAD_EXPERIMENTAL_ENABLED" )
91
- alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), cadExperimentalEnabled )
110
+ alertInvestigation := investigations .GetInvestigation (pdClient .GetTitle (), config .Config .Experimental )
92
111
93
112
// Escalate all unsupported alerts
94
113
if alertInvestigation == nil {
@@ -109,8 +128,17 @@ func run(cmd *cobra.Command, _ []string) error {
109
128
return err
110
129
}
111
130
112
- ocmClient , err := GetOCMClient ()
113
- if err != nil {
131
+ var ocmClient * ocm.SdkClient
132
+ var ocmErr error
133
+ // Initialize the OCM client
134
+ // If the config file is set, use it to initialize the OCM client
135
+ // use config file only when developing
136
+ if config .Config .ConfigFile != "" {
137
+ ocmClient , ocmErr = ocm .NewFromConfig (config .Config .ConfigFile )
138
+ } else {
139
+ ocmClient , ocmErr = ocm .NewFromClientKeyPair (config .Config .CadOcmURL , config .Config .CadOcmClientID , config .Config .CadOcmClientSecret )
140
+ }
141
+ if ocmErr != nil {
114
142
return fmt .Errorf ("could not initialize ocm client: %w" , err )
115
143
}
116
144
@@ -122,13 +150,8 @@ func run(cmd *cobra.Command, _ []string) error {
122
150
// For installing clusters, externalID can be empty.
123
151
internalClusterID := cluster .ID ()
124
152
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
- }
153
+ // add the internal-cluster-id context to the logger
154
+ logging .RawLogger = logging .RawLogger .With (zap .String ("cluster_id" , internalClusterID ))
132
155
133
156
requiresInvestigation , err := clusterRequiresInvestigation (cluster , pdClient , ocmClient )
134
157
if err != nil || ! requiresInvestigation {
@@ -184,22 +207,6 @@ func handleCADFailure(err error, resources *investigation.Resources, pdClient *p
184
207
}
185
208
}
186
209
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
210
// Checks pre-requisites for a cluster investigation:
204
211
// - the cluster's state is supported by CAD for an investigation (= not uninstalling)
205
212
// - the cloud provider is supported by CAD (cluster is AWS)
0 commit comments