Skip to content

Commit

Permalink
✨ Allow heredoc resources and local save.
Browse files Browse the repository at this point in the history
Added some annotation management to add features:

- `config.kaweeze.com/inject-local` injects the function configuration
as is, as if it had been generated.
- `config.kaweezle.com/keep-local` allows saving a generated resource
along with the other configuration files.
- `config.kaweezle.com/(path|index)` allow specifying
the local file and position where to save the generated resource.

The astute observer have probably already seen that the specific
annotations domain has been changed from `config.kubernetes.io`
to `config.kaweezle.com`.
  • Loading branch information
antoinemartin committed Jan 25, 2023
1 parent 6044659 commit 1c07fc0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ func main() {

// 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.kubernetes.io/prune-local` present in a
// To avoid this, an annotation `config.kaweezle.com/prune-local` 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")
}
filter := &filters.IsLocalConfig{IncludeLocalConfig: false, ExcludeNonLocalConfig: false}
err = rl.Filter(filter)
if err != nil {
Expand All @@ -101,11 +105,11 @@ func main() {
}

for _, r := range rm.Resources() {
r.RemoveBuildAnnotations()
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.kubernetes.io/prune-local` to add on the last transformer.
// `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)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
// annotations
ConfigurationAnnotationDomain = "config.kubernetes.io"

LocalConfigurationAnnotationDomain = "config.kaweezle.com"

// Function configuration annotation
FunctionAnnotationFunction = ConfigurationAnnotationDomain + "/function"

Expand All @@ -25,8 +27,12 @@ const (

// Setting to true means we want this function configuration to be injected as a
// local configuration resource (local-config)
FunctionAnnotationInjectLocal = ConfigurationAnnotationDomain + "/inject-local"
FunctionAnnotationInjectLocal = LocalConfigurationAnnotationDomain + "/inject-local"

// if set, the transformation will remove all the resources marked as local-config
FunctionAnnotationPruneLocal = ConfigurationAnnotationDomain + "/prune-local"
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"
)
33 changes: 31 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down Expand Up @@ -40,10 +41,38 @@ func MakeResourceLocal(r *yaml.RNode) error {
annotations := r.GetAnnotations()

annotations[filters.LocalConfigAnnotation] = "true"
annotations[kioutil.PathAnnotation] = ".generated.yaml"
annotations[kioutil.LegacyPathAnnotation] = ".generated.yaml"
if _, ok := annotations[kioutil.PathAnnotation]; !ok {
annotations[kioutil.PathAnnotation] = ".generated.yaml"
}
if _, ok := annotations[kioutil.LegacyPathAnnotation]; !ok {
annotations[kioutil.LegacyPathAnnotation] = ".generated.yaml"
}
delete(annotations, FunctionAnnotationInjectLocal)
delete(annotations, FunctionAnnotationFunction)

return r.SetAnnotations(annotations)
}

func unLocal(list []*yaml.RNode) ([]*yaml.RNode, error) {
for _, r := range list {
annotations := r.GetAnnotations()
if _, ok := annotations[FunctionAnnotationKeepLocal]; ok {
delete(annotations, FunctionAnnotationKeepLocal)
delete(annotations, FunctionAnnotationLocalConfig)
if path, ok := annotations[FunctionAnnotationPath]; ok {
annotations[kioutil.LegacyPathAnnotation] = path
annotations[kioutil.PathAnnotation] = path
delete(annotations, FunctionAnnotationPath)
}
if index, ok := annotations[FunctionAnnotationIndex]; ok {
annotations[kioutil.LegacyIndexAnnotation] = index
annotations[kioutil.IndexAnnotation] = index
delete(annotations, FunctionAnnotationIndex)
}
r.SetAnnotations(annotations)
}
}
return list, nil
}

var UnLocal kio.FilterFunc = unLocal

0 comments on commit 1c07fc0

Please sign in to comment.