Skip to content

Commit

Permalink
💥 Change how annotations are managed
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
antoinemartin committed Jan 26, 2023
1 parent 788be67 commit ce9701f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
21 changes: 7 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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")
}
}

Expand All @@ -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...)

}

Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
53 changes: 36 additions & 17 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/replacement/functions/01_configmap-generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ metadata:
name: configuration-map
namespace: argocd
annotations:
config.kaweezle.com/local-config: "true"
config.kubernetes.io/function: |
exec:
path: ../../krmfnbuiltin
Expand Down
3 changes: 1 addition & 2 deletions tests/test_krmfnbuiltin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ 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
rm -rf applications
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
Expand Down

0 comments on commit ce9701f

Please sign in to comment.