|
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 | "os" |
10 | | - "regexp" |
11 | | - "strconv" |
12 | | - "strings" |
13 | 10 |
|
14 | 11 | "github.com/parnurzeal/gorequest" |
15 | 12 | "github.com/prometheus/client_golang/prometheus" |
@@ -264,122 +261,54 @@ func upgradeResource(clients kube.Clients, config util.Config, upgradeFuncs call |
264 | 261 | } |
265 | 262 | } |
266 | 263 |
|
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 |
282 | 267 | } |
283 | 268 |
|
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) |
296 | 270 |
|
297 | | - if isResourceExcluded { |
| 271 | + if strategyResult.Result != constants.Updated { |
298 | 272 | return nil |
299 | 273 | } |
300 | 274 |
|
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) |
306 | 279 | } |
307 | 280 |
|
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) |
321 | 284 |
|
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) |
326 | 289 | } |
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) |
349 | 294 |
|
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) |
351 | 296 |
|
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) |
364 | 302 | } |
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) |
379 | 308 | } |
380 | 309 | } |
381 | 310 |
|
382 | | - return false |
| 311 | + return nil |
383 | 312 | } |
384 | 313 |
|
385 | 314 | func getVolumeMountName(volumes []v1.Volume, mountType string, volumeName string) string { |
|
0 commit comments