Skip to content

Commit

Permalink
feat(cmd): add local-config annotation on create
Browse files Browse the repository at this point in the history
this adds a DefaultMetadata object in konfig that can be used
when creating a new kustomization yaml that conforms with the
best practice recommendations in the kustomize docs.

> The `Kustomization` config in a `kustomization.yaml`
> **SHOULD** contain this annotation so that tools know it is not intended to be sent to
> the Kubernetes api server.
  • Loading branch information
bt-macole committed Nov 8, 2024
1 parent bb7a280 commit e9965a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/konfig/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

package konfig

import (
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
)

// RecognizedKustomizationFileNames is a list of file names
// that kustomize recognizes.
// To avoid ambiguity, a kustomization directory may not
Expand All @@ -19,6 +24,17 @@ func DefaultKustomizationFileName() string {
return RecognizedKustomizationFileNames()[0]
}

func DefaultKustomizationMetadata() *types.ObjectMeta {
configAnnotation := map[string]string{
filters.LocalConfigAnnotation: "true",
}
defaultMetadata := &types.ObjectMeta{
Annotations: configAnnotation,
}

return defaultMetadata
}

const (
// An environment variable to consult for kustomization
// configuration data. See:
Expand Down
1 change: 1 addition & 0 deletions kustomize/commands/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory)
if err != nil {
return err
}
m.MetaData = konfig.DefaultKustomizationMetadata()
m.Resources = resources
m.Namespace = opts.namespace
m.NamePrefix = opts.prefix
Expand Down
17 changes: 17 additions & 0 deletions kustomize/commands/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/provider"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
Expand Down Expand Up @@ -134,6 +135,22 @@ func TestCreateWithNameSuffix(t *testing.T) {
}
}

func TestCreateHasDefaultMetadata(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
fSys.WriteFile("foo.yaml", []byte(""))
fSys.WriteFile("bar.yaml", []byte(""))
opts := createFlags{resources: "foo.yaml,bar.yaml"}
err := runCreate(opts, fSys, factory)
if err != nil {
t.Errorf("unexpected cmd error: %v", err)
}
m := readKustomizationFS(t, fSys)
expected := konfig.DefaultKustomizationMetadata()
if !reflect.DeepEqual(m.MetaData, expected) {
t.Fatalf("expected %+v but got %+v", expected, m.Resources)
}
}

func writeDetectContent(fSys filesys.FileSystem) {
fSys.WriteFile("/test.yaml", []byte(`
apiVersion: v1
Expand Down

0 comments on commit e9965a9

Please sign in to comment.