Skip to content

Commit

Permalink
✨ Added the no-op function that just injects its content
Browse files Browse the repository at this point in the history
Setting the config.kubernetes.io/inject-config annotation will  add the
functionConfig to the set or resources with the
config.kubernetes.io/local-config annotation.

This is meant to inject whatever resource we want for replacement
instead of relying on flat configmaps.
  • Loading branch information
antoinemartin committed Jan 25, 2023
1 parent a03948e commit 6044659
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
Expand All @@ -28,7 +27,20 @@ func main() {

plugin, err := plugins.MakeBuiltinPlugin(resid.GvkFromNode(config))
if err != nil {
return errors.WrapPrefixf(err, "creating plugin")
// Check if config asks us to inject it
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationInjectLocal]; ok {
injected := config.Copy()

err := utils.MakeResourceLocal(injected)
if err != nil {
return errors.WrapPrefixf(
err, "Error while mangling annotations on %s fails configuration", res.OrgId())
}
rl.Items = append(rl.Items, injected)
return nil
} else {
return errors.WrapPrefixf(err, "creating plugin")
}
}

yamlNode := config.YNode()
Expand Down Expand Up @@ -68,7 +80,7 @@ func main() {
// 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
// transformer makes all the local resources disappear.
if _, ok := config.GetAnnotations()["config.kubernetes.io/prune-local"]; ok {
if _, ok := config.GetAnnotations()[utils.FunctionAnnotationPruneLocal]; ok {
filter := &filters.IsLocalConfig{IncludeLocalConfig: false, ExcludeNonLocalConfig: false}
err = rl.Filter(filter)
if err != nil {
Expand All @@ -95,9 +107,7 @@ func main() {
// do that on functions. So we have added a special annotation
// `config.kubernetes.io/prune-local` to add on the last transformer.
// We set the filename of the generated resource in case it is forgotten.
r.Pipe(yaml.SetAnnotation(filters.LocalConfigAnnotation, "true"))
r.Pipe(yaml.SetAnnotation(kioutil.PathAnnotation, ".generated.yaml"))
r.Pipe(yaml.SetAnnotation(kioutil.LegacyPathAnnotation, ".generated.yaml"))
utils.MakeResourceLocal(&r.RNode)
}

rl.Items = append(rl.Items, rm.ToRNodeSlice()...)
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@ const (
BuildAnnotationsRefBy = konfig.ConfigAnnoDomain + "/refBy"
BuildAnnotationsGenBehavior = konfig.ConfigAnnoDomain + "/generatorBehavior"
BuildAnnotationsGenAddHashSuffix = konfig.ConfigAnnoDomain + "/needsHashSuffix"

// ConfigurationAnnotationDomain is the domain of function configuration
// annotations
ConfigurationAnnotationDomain = "config.kubernetes.io"

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

// true when the resource is part of the local configuration
FunctionAnnotationLocalConfig = ConfigurationAnnotationDomain + "/local-config"

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

// if set, the transformation will remove all the resources marked as local-config
FunctionAnnotationPruneLocal = ConfigurationAnnotationDomain + "/prune-local"
)
19 changes: 18 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package utils

import "sigs.k8s.io/kustomize/api/resource"
import (
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

var buildAnnotations = []string{
BuildAnnotationPreviousKinds,
Expand Down Expand Up @@ -30,3 +35,15 @@ func RemoveBuildAnnotations(r *resource.Resource) {
panic(err)
}
}

func MakeResourceLocal(r *yaml.RNode) error {
annotations := r.GetAnnotations()

annotations[filters.LocalConfigAnnotation] = "true"
annotations[kioutil.PathAnnotation] = ".generated.yaml"
annotations[kioutil.LegacyPathAnnotation] = ".generated.yaml"
delete(annotations, FunctionAnnotationInjectLocal)
delete(annotations, FunctionAnnotationFunction)

return r.SetAnnotations(annotations)
}

0 comments on commit 6044659

Please sign in to comment.