Skip to content

Commit

Permalink
🐛 ✨ Introduce new annotation to cleanup annotations
Browse files Browse the repository at this point in the history
After transformation, we were deleting all internal annotations.
When using krmfnbuiltin in the context of a kustomize build, we
don't want those annotation to disappear. For instance, this would
prevent hash generation for secrets and configmaps.

But in the context of a single function execution, we want to keep
it. This is the reason we have added a specific annotation for that.
  • Loading branch information
antoinemartin committed Feb 7, 2023
1 parent 3b1e706 commit 33d8cdf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ transformation in your kustomize projects.
<summary>Table of Contents</summary>
<ol>
<li><a href="#rationale">Rationale</a></li>
<li><a href="#usage-example">Usage Example</a></li>
<li><a href="#usage-example">Usage Example</a>
<ul><li><a href="#internal-annotations-cleanup">Internal annotations cleanup</a></li></ul>
</li>
<li><a href="#use-of-generators">Use of generators</a></li>
<li><a href="#keeping-or-deleting-generated-resources">Keeping or deleting generated resources</a></li>
<li><a href="#extensions">Extensions</a>
Expand Down Expand Up @@ -133,12 +135,14 @@ kind: PatchTransformer
metadata:
name: fn-change-repo-and-branch
annotations:
# This will remove the internal annotations the transformer adds.
config.kaweezle.com/cleanup: "true"
config.kubernetes.io/function: |
exec:
path: krmfnbuiltin
# Can also be:
# container:
# image: ghcr.io/kaweezle/krmfnbuiltin:v0.4.0
# image: ghcr.io/kaweezle/krmfnbuiltin:v0.4.1
patch: |-
- op: replace
path: /spec/source/repoURL
Expand Down Expand Up @@ -174,6 +178,40 @@ source:
You now can commit the 10 modified manifests in your branch and deploy the
applications.

### Internal annotations cleanup

Some kustomize transformers add annotations to enable for instance reference
reconciliation at the end of the build. These annotations have the prefix
`internal.config.kubernetes.io`. When performing a `kustomize build`, kustomize
removes them at the end of the process. When we are using the transformations in
the context of a KRM function with `kustomize fn run`, the build cleanup is not
performed. In consequence, the annotations would be added to the resource file
touched by the transformation. For instance:

```diff
--- original.argocd.yaml
+++ transformed.argocd.yaml
@@ -5,6 +5,9 @@
namespace: argocd
annotations:
autocloud/local: "true"
+ internal.config.kubernetes.io/previousKinds: Application
+ internal.config.kubernetes.io/previousNames: argo-cd
+ internal.config.kubernetes.io/previousNamespaces: argocd
spec:
destination:
namespace: argocd
```

To avoid that, you can insert the following annotation:

```yaml
config.kaweezle.com/cleanup: "true"
```

It will inform krmfnbuiltin that you are not using the transformer in the
context of a bulid and that the internal annotations need to be removed.

## Use of generators

`krmfnbuiltin` provides all the Kustomize
Expand Down Expand Up @@ -1092,7 +1130,7 @@ curl -sLS https://raw.githubusercontent.com/kaweezle/krmfnbuiltin/main/get.sh |
If you don't want to pipe into shell, you can do:

```console
> KRMFNBUILTIN_VERSION="v0.4.0"
> KRMFNBUILTIN_VERSION="v0.4.1"
> curl -sLo /usr/local/bin/krmfnbuiltin https://github.com/kaweezle/krmfnbuiltin/releases/download/${KRMFNBUILTIN_VERSION}/krmfnbuiltin_${KRMFNBUILTIN_VERSION}_linux_amd64
```

Expand Down Expand Up @@ -1121,7 +1159,7 @@ summarize:
```Dockerfile
FROM argoproj/argocd:latest

ARG KRMFNBUILTIN_VERSION=v0.4.0
ARG KRMFNBUILTIN_VERSION=v0.4.1

# Switch to root for the ability to perform install
USER root
Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ func main() {
return errors.WrapPrefixf(err, "Transforming resources")
}

for _, r := range rm.Resources() {
utils.RemoveBuildAnnotations(r)
configAnnotations := config.GetAnnotations()

if _, ok := configAnnotations[utils.FunctionAnnotationCleanup]; ok {
for _, r := range rm.Resources() {
utils.RemoveBuildAnnotations(r)
}
}

rl.Items = rm.ToRNodeSlice()

// 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 {
if _, ok := configAnnotations[utils.FunctionAnnotationPruneLocal]; ok {
err = rl.Filter(utils.UnLocal)
if err != nil {
return errors.WrapPrefixf(err, "while pruning `config.kaweezle.com/local-config` resources")
Expand Down Expand Up @@ -110,7 +114,7 @@ func main() {

cmd := command.Build(processor, command.StandaloneDisabled, false)
command.AddGenerateDockerfile(cmd)
cmd.Version = "v0.4.0" // <---VERSION--->
cmd.Version = "v0.4.1" // <---VERSION--->

if err := cmd.Execute(); err != nil {
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const (
// local configuration resource (local-config)
FunctionAnnotationInjectLocal = LocalConfigurationAnnotationDomain + "/inject-local"

// if set, Remove any transformation leftover annotations
FunctionAnnotationCleanup = LocalConfigurationAnnotationDomain + "/cleanup"

// if set, the transformation will remove all the resources marked as local-config
FunctionAnnotationPruneLocal = LocalConfigurationAnnotationDomain + "/prune-local"
// Saving path for injected resource
Expand Down
1 change: 1 addition & 0 deletions tests/patch/functions/patch-transformer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: PatchTransformer
metadata:
name: not-important-to-example
annotations:
config.kaweezle.com/cleanup: "true"
config.kubernetes.io/function: |
exec:
path: ../../krmfnbuiltin
Expand Down

0 comments on commit 33d8cdf

Please sign in to comment.