Skip to content

Commit 98074ee

Browse files
committed
separate methods
1 parent 404c370 commit 98074ee

2 files changed

Lines changed: 128 additions & 103 deletions

File tree

internal/pkg/handler/upgrade.go

Lines changed: 32 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"fmt"
88
"io"
99
"os"
10-
"regexp"
11-
"strconv"
12-
"strings"
1310

1411
"github.com/parnurzeal/gorequest"
1512
"github.com/prometheus/client_golang/prometheus"
@@ -264,122 +261,54 @@ func upgradeResource(clients kube.Clients, config util.Config, upgradeFuncs call
264261
}
265262
}
266263

267-
// find correct annotation and update the resource
268-
annotations := upgradeFuncs.AnnotationsFunc(resource)
269-
annotationValue, found := annotations[config.Annotation]
270-
searchAnnotationValue, foundSearchAnn := annotations[options.AutoSearchAnnotation]
271-
reloaderEnabledValue, foundAuto := annotations[options.ReloaderAutoAnnotation]
272-
typedAutoAnnotationEnabledValue, foundTypedAuto := annotations[config.TypedAutoAnnotation]
273-
excludeConfigmapAnnotationValue, foundExcludeConfigmap := annotations[options.ConfigmapExcludeReloaderAnnotation]
274-
excludeSecretAnnotationValue, foundExcludeSecret := annotations[options.SecretExcludeReloaderAnnotation]
275-
276-
if !found && !foundAuto && !foundTypedAuto && !foundSearchAnn {
277-
annotations = upgradeFuncs.PodAnnotationsFunc(resource)
278-
annotationValue = annotations[config.Annotation]
279-
searchAnnotationValue = annotations[options.AutoSearchAnnotation]
280-
reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation]
281-
typedAutoAnnotationEnabledValue = annotations[config.TypedAutoAnnotation]
264+
reloadCheckResult := util.ShouldReload(config, upgradeFuncs.AnnotationsFunc(resource), upgradeFuncs.PodAnnotationsFunc(resource))
265+
if !reloadCheckResult.ShouldReload {
266+
return nil
282267
}
283268

284-
isResourceExcluded := false
285-
286-
switch config.Type {
287-
case constants.ConfigmapEnvVarPostfix:
288-
if foundExcludeConfigmap {
289-
isResourceExcluded = checkIfResourceIsExcluded(config.ResourceName, excludeConfigmapAnnotationValue)
290-
}
291-
case constants.SecretEnvVarPostfix:
292-
if foundExcludeSecret {
293-
isResourceExcluded = checkIfResourceIsExcluded(config.ResourceName, excludeSecretAnnotationValue)
294-
}
295-
}
269+
strategyResult := strategy(upgradeFuncs, resource, config, reloadCheckResult.AutoReload)
296270

297-
if isResourceExcluded {
271+
if strategyResult.Result != constants.Updated {
298272
return nil
299273
}
300274

301-
strategyResult := InvokeStrategyResult{constants.NotUpdated, nil}
302-
reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue)
303-
typedAutoAnnotationEnabled, _ := strconv.ParseBool(typedAutoAnnotationEnabledValue)
304-
if reloaderEnabled || typedAutoAnnotationEnabled || reloaderEnabledValue == "" && typedAutoAnnotationEnabledValue == "" && options.AutoReloadAll {
305-
strategyResult = strategy(upgradeFuncs, resource, config, true)
275+
if upgradeFuncs.SupportsPatch && strategyResult.Patch != nil {
276+
err = upgradeFuncs.PatchFunc(clients, config.Namespace, resource, strategyResult.Patch.Type, strategyResult.Patch.Bytes)
277+
} else {
278+
err = upgradeFuncs.UpdateFunc(clients, config.Namespace, resource)
306279
}
307280

308-
if strategyResult.Result != constants.Updated && annotationValue != "" {
309-
values := strings.Split(annotationValue, ",")
310-
for _, value := range values {
311-
value = strings.TrimSpace(value)
312-
re := regexp.MustCompile("^" + value + "$")
313-
if re.Match([]byte(config.ResourceName)) {
314-
strategyResult = strategy(upgradeFuncs, resource, config, false)
315-
if strategyResult.Result == constants.Updated {
316-
break
317-
}
318-
}
319-
}
320-
}
281+
if err != nil {
282+
message := fmt.Sprintf("Update for '%s' of type '%s' in namespace '%s' failed with error %v", resourceName, upgradeFuncs.ResourceType, config.Namespace, err)
283+
logrus.Errorf("Update for '%s' of type '%s' in namespace '%s' failed with error %v", resourceName, upgradeFuncs.ResourceType, config.Namespace, err)
321284

322-
if strategyResult.Result != constants.Updated && searchAnnotationValue == "true" {
323-
matchAnnotationValue := config.ResourceAnnotations[options.SearchMatchAnnotation]
324-
if matchAnnotationValue == "true" {
325-
strategyResult = strategy(upgradeFuncs, resource, config, true)
285+
collectors.Reloaded.With(prometheus.Labels{"success": "false"}).Inc()
286+
collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "false", "namespace": config.Namespace}).Inc()
287+
if recorder != nil {
288+
recorder.Event(resource, v1.EventTypeWarning, "ReloadFail", message)
326289
}
327-
}
328-
if strategyResult.Result == constants.Updated {
329-
var err error
330-
if upgradeFuncs.SupportsPatch && strategyResult.Patch != nil {
331-
err = upgradeFuncs.PatchFunc(clients, config.Namespace, resource, strategyResult.Patch.Type, strategyResult.Patch.Bytes)
332-
} else {
333-
err = upgradeFuncs.UpdateFunc(clients, config.Namespace, resource)
334-
}
335-
336-
if err != nil {
337-
message := fmt.Sprintf("Update for '%s' of type '%s' in namespace '%s' failed with error %v", resourceName, upgradeFuncs.ResourceType, config.Namespace, err)
338-
logrus.Errorf("Update for '%s' of type '%s' in namespace '%s' failed with error %v", resourceName, upgradeFuncs.ResourceType, config.Namespace, err)
339-
340-
collectors.Reloaded.With(prometheus.Labels{"success": "false"}).Inc()
341-
collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "false", "namespace": config.Namespace}).Inc()
342-
if recorder != nil {
343-
recorder.Event(resource, v1.EventTypeWarning, "ReloadFail", message)
344-
}
345-
return err
346-
} else {
347-
message := fmt.Sprintf("Changes detected in '%s' of type '%s' in namespace '%s'", config.ResourceName, config.Type, config.Namespace)
348-
message += fmt.Sprintf(", Updated '%s' of type '%s' in namespace '%s'", resourceName, upgradeFuncs.ResourceType, config.Namespace)
290+
return err
291+
} else {
292+
message := fmt.Sprintf("Changes detected in '%s' of type '%s' in namespace '%s'", config.ResourceName, config.Type, config.Namespace)
293+
message += fmt.Sprintf(", Updated '%s' of type '%s' in namespace '%s'", resourceName, upgradeFuncs.ResourceType, config.Namespace)
349294

350-
logrus.Infof("Changes detected in '%s' of type '%s' in namespace '%s'; updated '%s' of type '%s' in namespace '%s'", config.ResourceName, config.Type, config.Namespace, resourceName, upgradeFuncs.ResourceType, config.Namespace)
295+
logrus.Infof("Changes detected in '%s' of type '%s' in namespace '%s'; updated '%s' of type '%s' in namespace '%s'", config.ResourceName, config.Type, config.Namespace, resourceName, upgradeFuncs.ResourceType, config.Namespace)
351296

352-
collectors.Reloaded.With(prometheus.Labels{"success": "true"}).Inc()
353-
collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "true", "namespace": config.Namespace}).Inc()
354-
alert_on_reload, ok := os.LookupEnv("ALERT_ON_RELOAD")
355-
if recorder != nil {
356-
recorder.Event(resource, v1.EventTypeNormal, "Reloaded", message)
357-
}
358-
if ok && alert_on_reload == "true" {
359-
msg := fmt.Sprintf(
360-
"Reloader detected changes in *%s* of type *%s* in namespace *%s*. Hence reloaded *%s* of type *%s* in namespace *%s*",
361-
config.ResourceName, config.Type, config.Namespace, resourceName, upgradeFuncs.ResourceType, config.Namespace)
362-
alert.SendWebhookAlert(msg)
363-
}
297+
collectors.Reloaded.With(prometheus.Labels{"success": "true"}).Inc()
298+
collectors.ReloadedByNamespace.With(prometheus.Labels{"success": "true", "namespace": config.Namespace}).Inc()
299+
alert_on_reload, ok := os.LookupEnv("ALERT_ON_RELOAD")
300+
if recorder != nil {
301+
recorder.Event(resource, v1.EventTypeNormal, "Reloaded", message)
364302
}
365-
}
366-
367-
return nil
368-
}
369-
370-
func checkIfResourceIsExcluded(resourceName, excludedResources string) bool {
371-
if excludedResources == "" {
372-
return false
373-
}
374-
375-
excludedResourcesList := strings.Split(excludedResources, ",")
376-
for _, excludedResource := range excludedResourcesList {
377-
if strings.TrimSpace(excludedResource) == resourceName {
378-
return true
303+
if ok && alert_on_reload == "true" {
304+
msg := fmt.Sprintf(
305+
"Reloader detected changes in *%s* of type *%s* in namespace *%s*. Hence reloaded *%s* of type *%s* in namespace *%s*",
306+
config.ResourceName, config.Type, config.Namespace, resourceName, upgradeFuncs.ResourceType, config.Namespace)
307+
alert.SendWebhookAlert(msg)
379308
}
380309
}
381310

382-
return false
311+
return nil
383312
}
384313

385314
func getVolumeMountName(volumes []v1.Volume, mountType string, volumeName string) string {

internal/pkg/util/util.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package util
33
import (
44
"bytes"
55
"encoding/base64"
6+
"regexp"
67
"sort"
8+
"strconv"
79
"strings"
810

11+
"github.com/stakater/Reloader/internal/pkg/constants"
912
"github.com/stakater/Reloader/internal/pkg/crypto"
13+
"github.com/stakater/Reloader/internal/pkg/options"
1014
v1 "k8s.io/api/core/v1"
1115
)
1216

@@ -64,3 +68,95 @@ func (l *List) Contains(s string) bool {
6468
}
6569
return false
6670
}
71+
72+
type ReloadCheckResult struct {
73+
ShouldReload bool
74+
AutoReload bool
75+
}
76+
77+
func ShouldReload(config Config, annotations Map, podAnnotations Map) ReloadCheckResult {
78+
79+
annotationValue, found := annotations[config.Annotation]
80+
searchAnnotationValue, foundSearchAnn := annotations[options.AutoSearchAnnotation]
81+
reloaderEnabledValue, foundAuto := annotations[options.ReloaderAutoAnnotation]
82+
typedAutoAnnotationEnabledValue, foundTypedAuto := annotations[config.TypedAutoAnnotation]
83+
excludeConfigmapAnnotationValue, foundExcludeConfigmap := annotations[options.ConfigmapExcludeReloaderAnnotation]
84+
excludeSecretAnnotationValue, foundExcludeSecret := annotations[options.SecretExcludeReloaderAnnotation]
85+
86+
if !found && !foundAuto && !foundTypedAuto && !foundSearchAnn {
87+
annotations = podAnnotations
88+
annotationValue = annotations[config.Annotation]
89+
searchAnnotationValue = annotations[options.AutoSearchAnnotation]
90+
reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation]
91+
typedAutoAnnotationEnabledValue = annotations[config.TypedAutoAnnotation]
92+
}
93+
94+
isResourceExcluded := false
95+
96+
switch config.Type {
97+
case constants.ConfigmapEnvVarPostfix:
98+
if foundExcludeConfigmap {
99+
isResourceExcluded = checkIfResourceIsExcluded(config.ResourceName, excludeConfigmapAnnotationValue)
100+
}
101+
case constants.SecretEnvVarPostfix:
102+
if foundExcludeSecret {
103+
isResourceExcluded = checkIfResourceIsExcluded(config.ResourceName, excludeSecretAnnotationValue)
104+
}
105+
}
106+
107+
if isResourceExcluded {
108+
return ReloadCheckResult{
109+
ShouldReload: false,
110+
}
111+
}
112+
113+
reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue)
114+
typedAutoAnnotationEnabled, _ := strconv.ParseBool(typedAutoAnnotationEnabledValue)
115+
if reloaderEnabled || typedAutoAnnotationEnabled || reloaderEnabledValue == "" && typedAutoAnnotationEnabledValue == "" && options.AutoReloadAll {
116+
return ReloadCheckResult{
117+
ShouldReload: true,
118+
AutoReload: true,
119+
}
120+
}
121+
122+
values := strings.Split(annotationValue, ",")
123+
for _, value := range values {
124+
value = strings.TrimSpace(value)
125+
re := regexp.MustCompile("^" + value + "$")
126+
if re.Match([]byte(config.ResourceName)) {
127+
return ReloadCheckResult{
128+
ShouldReload: true,
129+
AutoReload: false,
130+
}
131+
}
132+
}
133+
134+
if searchAnnotationValue == "true" {
135+
matchAnnotationValue := config.ResourceAnnotations[options.SearchMatchAnnotation]
136+
if matchAnnotationValue == "true" {
137+
return ReloadCheckResult{
138+
ShouldReload: true,
139+
AutoReload: true,
140+
}
141+
}
142+
}
143+
144+
return ReloadCheckResult{
145+
ShouldReload: false,
146+
}
147+
}
148+
149+
func checkIfResourceIsExcluded(resourceName, excludedResources string) bool {
150+
if excludedResources == "" {
151+
return false
152+
}
153+
154+
excludedResourcesList := strings.Split(excludedResources, ",")
155+
for _, excludedResource := range excludedResourcesList {
156+
if strings.TrimSpace(excludedResource) == resourceName {
157+
return true
158+
}
159+
}
160+
161+
return false
162+
}

0 commit comments

Comments
 (0)