1 /* 2 Copyright 2024 The Flux authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v2 18 19 import "github.com/fluxcd/pkg/apis/meta" 20 21 const ( 22 // ForceRequestAnnotation is the annotation used for triggering a one-off forced 23 // Helm release, even when there are no new changes in the HelmRelease. 24 // The value is interpreted as a token, and must equal the value of 25 // meta.ReconcileRequestAnnotation in order to trigger a release. 26 ForceRequestAnnotation string = "reconcile.fluxcd.io/forceAt" 27 28 // ResetRequestAnnotation is the annotation used for resetting the failure counts 29 // of a HelmRelease, so that it can be retried again. 30 // The value is interpreted as a token, and must equal the value of 31 // meta.ReconcileRequestAnnotation in order to reset the failure counts. 32 ResetRequestAnnotation string = "reconcile.fluxcd.io/resetAt" 33 ) 34 35 // ShouldHandleResetRequest returns true if the HelmRelease has a reset request 36 // annotation, and the value of the annotation matches the value of the 37 // meta.ReconcileRequestAnnotation annotation. 38 // 39 // To ensure that the reset request is handled only once, the value of 40 // HelmReleaseStatus.LastHandledResetAt is updated to match the value of the 41 // reset request annotation (even if the reset request is not handled because 42 // the value of the meta.ReconcileRequestAnnotation annotation does not match). 43 func ShouldHandleResetRequest(obj *HelmRelease) bool { 44 return handleRequest(obj, ResetRequestAnnotation, &obj.Status.LastHandledResetAt) 45 } 46 47 // ShouldHandleForceRequest returns true if the HelmRelease has a force request 48 // annotation, and the value of the annotation matches the value of the 49 // meta.ReconcileRequestAnnotation annotation. 50 // 51 // To ensure that the force request is handled only once, the value of 52 // HelmReleaseStatus.LastHandledForceAt is updated to match the value of the 53 // force request annotation (even if the force request is not handled because 54 // the value of the meta.ReconcileRequestAnnotation annotation does not match). 55 func ShouldHandleForceRequest(obj *HelmRelease) bool { 56 return handleRequest(obj, ForceRequestAnnotation, &obj.Status.LastHandledForceAt) 57 } 58 59 // handleRequest returns true if the HelmRelease has a request annotation, and 60 // the value of the annotation matches the value of the meta.ReconcileRequestAnnotation 61 // annotation. 62 // 63 // The lastHandled argument is used to ensure that the request is handled only 64 // once, and is updated to match the value of the request annotation (even if 65 // the request is not handled because the value of the meta.ReconcileRequestAnnotation 66 // annotation does not match). 67 func handleRequest(obj *HelmRelease, annotation string, lastHandled *string) bool { 68 requestAt, requestOk := obj.GetAnnotations()[annotation] 69 reconcileAt, reconcileOk := meta.ReconcileAnnotationValue(obj.GetAnnotations()) 70 71 var lastHandledRequest string 72 if requestOk { 73 lastHandledRequest = *lastHandled 74 *lastHandled = requestAt 75 } 76 77 if requestOk && reconcileOk && requestAt == reconcileAt { 78 lastHandledReconcile := obj.Status.GetLastHandledReconcileRequest() 79 if lastHandledReconcile != reconcileAt && lastHandledRequest != requestAt { 80 return true 81 } 82 } 83 return false 84 } 85