Skip to content

Commit a836d6c

Browse files
author
Ricardo Lüders
authored
NO-JIRA: (refactor) reduce cognitive and adding unit tests (#889)
* refactor: reduce configobserver config cognitive * refactor: conditions checks and tests * test: testing conditions * style: adjust code style
1 parent 363c3b1 commit a836d6c

File tree

3 files changed

+274
-92
lines changed

3 files changed

+274
-92
lines changed

pkg/config/configobserver/config.go

+51-46
Original file line numberDiff line numberDiff line change
@@ -76,52 +76,12 @@ func (c *Config) loadHTTP(data map[string][]byte) {
7676
}
7777

7878
func (c *Config) loadReport(data map[string][]byte) {
79-
if enableGlobalObfuscation, ok := data["enableGlobalObfuscation"]; ok {
80-
c.EnableGlobalObfuscation = strings.EqualFold(string(enableGlobalObfuscation), "true")
81-
}
82-
83-
if reportEndpoint, ok := data["reportEndpoint"]; ok {
84-
c.ReportEndpoint = string(reportEndpoint)
85-
}
86-
if reportPullingDelay, ok := data["reportPullingDelay"]; ok {
87-
if v, err := time.ParseDuration(string(reportPullingDelay)); err == nil {
88-
c.ReportPullingDelay = v
89-
} else {
90-
klog.Warningf(
91-
"reportPullingDelay secret contains an invalid value (%s). Using previous value",
92-
reportPullingDelay,
93-
)
94-
}
95-
} else {
96-
c.ReportPullingDelay = time.Duration(-1)
97-
}
98-
99-
if reportPullingTimeout, ok := data["reportPullingTimeout"]; ok {
100-
if v, err := time.ParseDuration(string(reportPullingTimeout)); err == nil {
101-
c.ReportPullingTimeout = v
102-
} else {
103-
klog.Warningf(
104-
"reportPullingTimeout secret contains an invalid value (%s). Using previous value",
105-
reportPullingTimeout,
106-
)
107-
}
108-
}
109-
110-
if reportMinRetryTime, ok := data["reportMinRetryTime"]; ok {
111-
if v, err := time.ParseDuration(string(reportMinRetryTime)); err == nil {
112-
c.ReportMinRetryTime = v
113-
} else {
114-
klog.Warningf(
115-
"reportMinRetryTime secret contains an invalid value (%s). Using previous value",
116-
reportMinRetryTime,
117-
)
118-
}
119-
}
120-
121-
c.Report = len(c.Endpoint) > 0
122-
if disableInsightsAlerts, ok := data["disableInsightsAlerts"]; ok {
123-
c.DisableInsightsAlerts = strings.EqualFold(string(disableInsightsAlerts), "true")
124-
}
79+
c.loadEnableGlobalObfuscation(data)
80+
c.loadReportEndpoint(data)
81+
c.loadReportPullingDelay(data)
82+
c.loadReportPullingTimeout(data)
83+
c.loadReportMinRetryTime(data)
84+
c.loadReportSettings(data)
12585
}
12686

12787
func (c *Config) loadOCM(data map[string][]byte) {
@@ -168,3 +128,48 @@ func (c *Config) loadReportEndpointTechPreview(data map[string][]byte) {
168128
c.ReportEndpointTechPreview = string(endpoint)
169129
}
170130
}
131+
132+
func (c *Config) loadEnableGlobalObfuscation(data map[string][]byte) {
133+
if enableGlobalObfuscation, ok := data["enableGlobalObfuscation"]; ok {
134+
c.EnableGlobalObfuscation = strings.EqualFold(string(enableGlobalObfuscation), "true")
135+
}
136+
}
137+
138+
func (c *Config) loadReportEndpoint(data map[string][]byte) {
139+
if reportEndpoint, ok := data["reportEndpoint"]; ok {
140+
c.ReportEndpoint = string(reportEndpoint)
141+
}
142+
}
143+
144+
func (c *Config) loadReportPullingDelay(data map[string][]byte) {
145+
c.loadReportDurationSetting(data, "reportPullingDelay", &c.ReportPullingDelay)
146+
}
147+
148+
func (c *Config) loadReportPullingTimeout(data map[string][]byte) {
149+
c.loadReportDurationSetting(data, "reportPullingTimeout", &c.ReportPullingTimeout)
150+
}
151+
152+
func (c *Config) loadReportMinRetryTime(data map[string][]byte) {
153+
c.loadReportDurationSetting(data, "reportMinRetryTime", &c.ReportMinRetryTime)
154+
}
155+
156+
func (c *Config) loadReportDurationSetting(data map[string][]byte, key string, target *time.Duration) {
157+
if value, ok := data[key]; ok {
158+
if v, err := time.ParseDuration(string(value)); err == nil {
159+
*target = v
160+
} else {
161+
klog.Warningf("%s secret contains an invalid value (%s). Using previous value", key, value)
162+
}
163+
} else {
164+
if key == "reportPullingDelay" {
165+
*target = time.Duration(-1)
166+
}
167+
}
168+
}
169+
170+
func (c *Config) loadReportSettings(data map[string][]byte) {
171+
c.Report = len(c.Endpoint) > 0
172+
if disableInsightsAlerts, ok := data["disableInsightsAlerts"]; ok {
173+
c.DisableInsightsAlerts = strings.EqualFold(string(disableInsightsAlerts), "true")
174+
}
175+
}

pkg/gatherers/conditional/conditions.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,15 @@ type ClusterVersionMatchesConditionParams struct {
5353
// areAllConditionsSatisfied returns true if all the conditions are satisfied, for example if the condition is
5454
// to check if a metric is firing, it will look at that metric and return the result according to that
5555
func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (bool, error) {
56-
for _, condition := range conditions {
56+
for k := range conditions {
57+
condition := conditions[k]
5758
switch condition.Type {
5859
case AlertIsFiring:
59-
if condition.Alert == nil {
60-
return false, fmt.Errorf("alert field should not be nil")
61-
}
62-
63-
if firing, err := g.isAlertFiring(condition.Alert.Name); !firing || err != nil {
60+
if ok, err := g.checkAlertIsFiring(condition.Alert); !ok || err != nil {
6461
return false, err
6562
}
6663
case ClusterVersionMatches:
67-
if condition.ClusterVersionMatches == nil {
68-
return false, fmt.Errorf("cluster_version_matches field should not be nil")
69-
}
70-
71-
if doesMatch, err := g.doesClusterVersionMatch(condition.ClusterVersionMatches.Version); !doesMatch || err != nil {
64+
if ok, err := g.checkClusterVersionMatches(condition.ClusterVersionMatches); !ok || err != nil {
7265
return false, err
7366
}
7467
default:
@@ -79,6 +72,26 @@ func (g *Gatherer) areAllConditionsSatisfied(conditions []ConditionWithParams) (
7972
return true, nil
8073
}
8174

75+
// checkAlertIsFiring verifies whether an alert is currently in a firing state. This function is used to
76+
// evaluate the AlertIsFiring condition.
77+
func (g *Gatherer) checkAlertIsFiring(alert *AlertConditionParams) (bool, error) {
78+
if alert == nil {
79+
return false, fmt.Errorf("alert field should not be nil")
80+
}
81+
82+
return g.isAlertFiring(alert.Name)
83+
}
84+
85+
// checkClusterVersionMatches verifies if the OpenShift cluster version matches the specified criteria. This function
86+
// is employed to evaluate the ClusterVersionMatches condition.
87+
func (g *Gatherer) checkClusterVersionMatches(clusterVersion *ClusterVersionMatchesConditionParams) (bool, error) {
88+
if clusterVersion == nil {
89+
return false, fmt.Errorf("cluster_version_matches field should not be nil")
90+
}
91+
92+
return g.doesClusterVersionMatch(clusterVersion.Version)
93+
}
94+
8295
// isAlertFiring using the cache it returns true if the alert is firing
8396
func (g *Gatherer) isAlertFiring(alertName string) (bool, error) {
8497
if g.firingAlerts == nil {

0 commit comments

Comments
 (0)