From ce9701fee855d99daf816269e2a1ffdf114a4beb Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Thu, 26 Jan 2023 14:48:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Change=20how=20annotations=20are?= =?UTF-8?q?=20managed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Now annotations are explicit. To make a resource local to the transformation, you need to add the annotation `config.kaweezle.com/local-config`. - The previous means that without annotation, generated resources will be saved. However, they will be saved in a file named `.krmfnbuiltin.yaml` - There is no more `keep-local` as it is not needed anymore. - Documentation needs to be updated. --- main.go | 21 +++----- pkg/utils/constants.go | 8 +-- pkg/utils/utils.go | 53 +++++++++++++------ .../functions/01_multi-transformation.yaml | 1 + .../functions/01_configmap-generator.yaml | 1 + tests/test_krmfnbuiltin.sh | 3 +- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index fddddbb..776dd3c 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ func main() { if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; ok { injected := config.Copy() - err := utils.MakeResourceLocal(injected) + err := utils.TransferAnnotations([]*yaml.RNode{injected}, config) if err != nil { return errors.WrapPrefixf( err, "Error while mangling annotations on %s fails configuration", res.OrgId()) @@ -75,14 +75,12 @@ func main() { rl.Items = rm.ToRNodeSlice() - // kustomize fn don't remove config.kubernetes.io/local-config resources upon completion. - // As it always add a filename by default, the local resources keep saved. - // To avoid this, an annotation `config.kaweezle.com/prune-local` present in a + // If the annotation `config.kaweezle.com/prune-local` is present in a // transformer makes all the local resources disappear. if _, ok := config.GetAnnotations()[utils.FunctionAnnotationPruneLocal]; ok { err = rl.Filter(utils.UnLocal) if err != nil { - return errors.WrapPrefixf(err, "Removing local from keep-local resources") + return errors.WrapPrefixf(err, "while pruning `config.kaweezle.com/local-config` resources") } } @@ -98,17 +96,12 @@ func main() { return errors.WrapPrefixf(err, "generating resource(s)") } - for _, r := range rm.Resources() { - utils.RemoveBuildAnnotations(r) - // We add the annotation config.kubernetes.io/local-config to be able to delete - // The generated resource at the end of the process. Unfortunately, kustomize doesn't - // do that on functions. So we have added a special annotation - // `config.kaweezle.com/prune-local` to add on the last transformer. - // We set the filename of the generated resource in case it is forgotten. - utils.MakeResourceLocal(&r.RNode) + rrl := rm.ToRNodeSlice() + if err := utils.TransferAnnotations(rrl, config); err != nil { + return errors.WrapPrefixf(err, "While transferring annotations") } - rl.Items = append(rl.Items, rm.ToRNodeSlice()...) + rl.Items = append(rl.Items, rrl...) } diff --git a/pkg/utils/constants.go b/pkg/utils/constants.go index 8ce7eec..32dc405 100644 --- a/pkg/utils/constants.go +++ b/pkg/utils/constants.go @@ -31,8 +31,8 @@ const ( // if set, the transformation will remove all the resources marked as local-config FunctionAnnotationPruneLocal = LocalConfigurationAnnotationDomain + "/prune-local" - // if set on a Generated resource, it won't be pruned - FunctionAnnotationKeepLocal = LocalConfigurationAnnotationDomain + "/keep-local" - FunctionAnnotationPath = LocalConfigurationAnnotationDomain + "/path" - FunctionAnnotationIndex = LocalConfigurationAnnotationDomain + "/index" + // Saving path for injected resource + FunctionAnnotationPath = LocalConfigurationAnnotationDomain + "/path" + // Saving index for injected resource + FunctionAnnotationIndex = LocalConfigurationAnnotationDomain + "/index" ) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index f02dd12..c8d551e 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -1,6 +1,8 @@ package utils import ( + "strconv" + "sigs.k8s.io/kustomize/api/resource" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -36,29 +38,50 @@ func RemoveBuildAnnotations(r *resource.Resource) { } } -func MakeResourceLocal(r *yaml.RNode) error { - annotations := r.GetAnnotations() +func TransferAnnotations(list []*yaml.RNode, config *yaml.RNode) (err error) { + path := ".krmfnbuiltin.yaml" + startIndex := 0 + + configAnnotations := config.GetAnnotations() + _, local := configAnnotations[FunctionAnnotationLocalConfig] - annotations[FunctionAnnotationLocalConfig] = "true" - if _, ok := annotations[kioutil.PathAnnotation]; !ok { - annotations[kioutil.PathAnnotation] = ".generated.yaml" + if annoPath, ok := configAnnotations[FunctionAnnotationPath]; ok { + path = annoPath } - if _, ok := annotations[kioutil.LegacyPathAnnotation]; !ok { - annotations[kioutil.LegacyPathAnnotation] = ".generated.yaml" + + if annoIndex, ok := configAnnotations[FunctionAnnotationIndex]; ok { + startIndex, err = strconv.Atoi(annoIndex) + if err != nil { + return + } } - delete(annotations, FunctionAnnotationInjectLocal) - delete(annotations, FunctionAnnotationFunction) - return r.SetAnnotations(annotations) + for index, r := range list { + annotations := r.GetAnnotations() + if local { + annotations[FunctionAnnotationLocalConfig] = "true" + } + annotations[kioutil.LegacyPathAnnotation] = path + annotations[kioutil.PathAnnotation] = path + + curIndex := strconv.Itoa(startIndex + index) + annotations[kioutil.LegacyIndexAnnotation] = curIndex + annotations[kioutil.IndexAnnotation] = curIndex + delete(annotations, FunctionAnnotationInjectLocal) + delete(annotations, FunctionAnnotationFunction) + r.SetAnnotations(annotations) + } + return } func unLocal(list []*yaml.RNode) ([]*yaml.RNode, error) { output := []*yaml.RNode{} for _, r := range list { annotations := r.GetAnnotations() - if _, ok := annotations[FunctionAnnotationKeepLocal]; ok { - delete(annotations, FunctionAnnotationKeepLocal) - delete(annotations, FunctionAnnotationLocalConfig) + // We don't append resources with config.kaweezle.com/local-config resources + if _, ok := annotations[FunctionAnnotationLocalConfig]; !ok { + // For the remaining resources, if a path and/or index was specified + // we copy it. if path, ok := annotations[FunctionAnnotationPath]; ok { annotations[kioutil.LegacyPathAnnotation] = path annotations[kioutil.PathAnnotation] = path @@ -71,10 +94,6 @@ func unLocal(list []*yaml.RNode) ([]*yaml.RNode, error) { } r.SetAnnotations(annotations) output = append(output, r) - } else { - if _, ok := annotations[FunctionAnnotationLocalConfig]; !ok { - output = append(output, r) - } } } return output, nil diff --git a/tests/multi-replacement/functions/01_multi-transformation.yaml b/tests/multi-replacement/functions/01_multi-transformation.yaml index 52e85b0..659a24d 100644 --- a/tests/multi-replacement/functions/01_multi-transformation.yaml +++ b/tests/multi-replacement/functions/01_multi-transformation.yaml @@ -4,6 +4,7 @@ metadata: name: configuration-map annotations: config.kaweezle.com/inject-local: "true" + config.kaweezle.com/local-config: "true" config.kubernetes.io/function: | exec: path: ../../krmfnbuiltin diff --git a/tests/replacement/functions/01_configmap-generator.yaml b/tests/replacement/functions/01_configmap-generator.yaml index 0ac9370..cbe64c1 100644 --- a/tests/replacement/functions/01_configmap-generator.yaml +++ b/tests/replacement/functions/01_configmap-generator.yaml @@ -6,6 +6,7 @@ metadata: name: configuration-map namespace: argocd annotations: + config.kaweezle.com/local-config: "true" config.kubernetes.io/function: | exec: path: ../../krmfnbuiltin diff --git a/tests/test_krmfnbuiltin.sh b/tests/test_krmfnbuiltin.sh index cf90fd7..aa206a0 100755 --- a/tests/test_krmfnbuiltin.sh +++ b/tests/test_krmfnbuiltin.sh @@ -9,7 +9,6 @@ set -e pipefail trap "find . -type d -name 'applications' -exec rm -rf {} +" EXIT - for d in $(ls -d */); do echo "Running Test in $d..." cd $d @@ -17,7 +16,7 @@ for d in $(ls -d */); do cp -r original applications echo " > Performing kustomizations..." kustomize fn run --enable-exec --fn-path functions applications - for f in $(ls -1 expected); do + for f in $(ls -1 applications); do echo " > Checking $f..." diff <(yq eval -P expected/$f) <(yq eval -P applications/$f) done