Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip kustomize transformers for paths #1491

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
841ec16
Makefile needs to be updated after change in kustomize organization
jbrette Sep 25, 2019
a8ebeb6
TransfomerConfig: Add Behavior field to FieldSpec (1/6)
jbrette Aug 30, 2019
2869b3c
TransfomerConfig: Add Skip field to FieldSpec (2/6)
jbrette Aug 30, 2019
c94fbcf
TransformerConfig: Use the Skip field to NamespaceTransformer (3/6)
jbrette Aug 30, 2019
4700ad8
feat: skip name prefix/suffix by kind
sethp-nr Aug 28, 2019
b889edc
fix: configure plugin for test
sethp-nr Aug 29, 2019
64c8d2d
refactor: remove gvkSlice
sethp-nr Aug 29, 2019
bf7eedc
TransformerConfig: Use the Skip field in PrefixSuffixTransformer (4/6)
jbrette Aug 30, 2019
4d69c15
TransformerConfig: Update unit testing (5/6)
jbrette Aug 30, 2019
b85a1f0
TransformerConfig: Update label and annotation transformer (6/6)
jbrette Sep 2, 2019
5378e06
TransformerConfig: Adapt fix to recent hardcoding of APIVersion
jbrette Sep 13, 2019
70acfe7
Sort name suffixes in transformer configurations
jgustie Sep 26, 2019
fbd5f94
Fix possible copy/paste issue in transformerconfig test
jgustie Sep 26, 2019
9e80f7d
Do not create commonLabel selectors on ReplicationController
jgustie Sep 26, 2019
7c43857
Do not create commonLabel selectors on legacy resources
jgustie Sep 26, 2019
b10b25f
Change the GVK sort order
jgustie Sep 26, 2019
523616e
Allow transformer config to have multiple field specs differing by ve…
jgustie Sep 26, 2019
b5376d3
FieldSpec: Reuse improved sorting
jbrette Sep 28, 2019
f964ecb
Fixes Namespace not set on APIService .spec.service.namespace
jbrette Oct 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BIN_NAME=kustomize
KUSTOMIZE_NAME=kustomize
PLUGINATOR_NAME=pluginator

COVER_FILE=coverage.out

Expand All @@ -18,10 +19,12 @@ generate-code:
./plugin/generateBuiltins.sh $(GOPATH)

build:
go build -o $(BIN_NAME) cmd/kustomize/main.go
cd pluginator && go build -o $(PLUGINATOR_NAME) .
cd kustomize && go build -o $(KUSTOMIZE_NAME) ./main.go

install:
go install $(PWD)/cmd/kustomize
cd pluginator && go install $(PWD)/pluginator
cd kustomize && go install $(PWD)/kustomize

cover:
# The plugin directory eludes coverage, and is therefore omitted
Expand All @@ -30,8 +33,7 @@ cover:


clean:
go clean
rm -f $(BIN_NAME)
cd kustomize && go clean && rm -f $(KUSTOMIZE_NAME)
rm -f $(COVER_FILE)

.PHONY: test build install clean generate-code test-go test-lint cover
19 changes: 19 additions & 0 deletions pkg/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ func (x Gvk) IsLessThan(o Gvk) bool {
return x.String() < o.String()
}

// IsLessThan2 returns true if self is less than the argument.
func (x Gvk) IsLessThan2(o Gvk) bool {
if x.Kind != o.Kind {
indexI := typeOrders[x.Kind]
indexJ := typeOrders[o.Kind]
if indexI != indexJ {
return indexI < indexJ
}
return x.Kind < o.Kind
}
if x.Group != o.Group {
return x.Group != "" && (o.Group == "" || x.Group < o.Group)
}
if x.Version != o.Version {
return x.Version != "" && (o.Version == "" || x.Version < o.Version)
}
return false
}

// IsSelected returns true if `selector` selects `x`; otherwise, false.
// If `selector` and `x` are the same, return true.
// If `selector` is nil, it is considered a wildcard match, returning true.
Expand Down
15 changes: 15 additions & 0 deletions pkg/gvk/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ValidatingWebhookConfiguration"}},
{Gvk{Group: "a", Version: "b", Kind: "Service"},
Gvk{Group: "a", Version: "b", Kind: "APIService"}},
{Gvk{Group: "a", Version: "b", Kind: "c"},
Gvk{Group: "", Version: "b", Kind: "c"}},
{Gvk{Group: "a", Version: "b", Kind: "c"},
Gvk{Group: "a", Version: "", Kind: "c"}},
}

func TestIsLessThan1(t *testing.T) {
Expand All @@ -85,6 +89,17 @@ func TestIsLessThan1(t *testing.T) {
}
}

func TestIsLessThan2(t *testing.T) {
for _, hey := range lessThanTests {
if !hey.x1.IsLessThan2(hey.x2) {
t.Fatalf("%v should be less than %v", hey.x1, hey.x2)
}
if hey.x2.IsLessThan2(hey.x1) {
t.Fatalf("%v should not be less than %v", hey.x2, hey.x1)
}
}
}

var stringTests = []struct {
x Gvk
s string
Expand Down
70 changes: 70 additions & 0 deletions pkg/target/skip_nameprefix_for_kind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package target_test

import (
"testing"

kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
)

// TestNameTransformation_skippingSecrets validates that
// PrefixSuffixTransformer can skip based on resource kind, and that CustomResourceDefinitions are always skipped.
// TODO: also the nameref stuff?
func TestNameTransformation_skippingSecrets(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/nameandconfig")

th.WriteK("/nameandconfig", `
namePrefix: p1-
nameSuffix: -s1
resources:
- resources.yaml
configurations:
- skip-secrets-prefix.yaml
`)

th.WriteF("/nameandconfig/resources.yaml", `
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1
---
apiVersion: v1
kind: Secret
metadata:
name: secret
---
kind: CustomResourceDefinition
metadata:
name: my-crd
`)

th.WriteF("/nameandconfig/skip-secrets-prefix.yaml", `
namePrefix:
- path: metadata/name
apiVersion: v1
kind: Secret
skip: true
`)

m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: ConfigMap
metadata:
name: p1-cm1-s1
---
apiVersion: v1
kind: Secret
metadata:
name: secret
---
kind: CustomResourceDefinition
metadata:
name: my-crd
`)
}
18 changes: 18 additions & 0 deletions pkg/transformers/config/defaultconfig/commonlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ commonLabels:
group: apps
kind: Deployment

- path: spec/selector/matchLabels
create: false
group: extensions
version: v1beta1
kind: ReplicaSet

- path: spec/selector/matchLabels
create: true
kind: ReplicaSet
Expand All @@ -72,6 +78,12 @@ commonLabels:
create: true
kind: ReplicaSet

- path: spec/selector/matchLabels
create: false
group: extensions
version: v1beta1
kind: DaemonSet

- path: spec/selector/matchLabels
create: true
kind: DaemonSet
Expand All @@ -80,6 +92,12 @@ commonLabels:
create: true
kind: DaemonSet

- path: spec/selector/matchLabels
create: false
group: apps
version: v1beta1
kind: StatefulSet

- path: spec/selector/matchLabels
create: true
group: apps
Expand Down
22 changes: 22 additions & 0 deletions pkg/transformers/config/defaultconfig/nameprefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,27 @@ const (
namePrefixFieldSpecs = `
namePrefix:
- path: metadata/name
- path: metadata/name
kind: CustomResourceDefinition
skip: true

# Following merge PR broke backward compatility
# https://github.com/kubernetes-sigs/kustomize/pull/1526
- path: metadata/name
kind: APIService
group: apiregistration.k8s.io
skip: true

# Would make sense to skip those
# by default but would break backward
# compatility
#
# - path: metadata/name
# kind: Namespace
# skip: true
# - path: metadata/name
# group: storage.k8s.io
# kind: StorageClass
# skip: true
`
)
2 changes: 1 addition & 1 deletion pkg/transformers/config/defaultconfig/namereference.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ nameReference:
kind: Ingress
- path: spec/backend/serviceName
kind: Ingress
- path: spec/service/name
- path: spec/service
kind: APIService
group: apiregistration.k8s.io
- path: webhooks/clientConfig/service
Expand Down
79 changes: 79 additions & 0 deletions pkg/transformers/config/defaultconfig/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,90 @@ package defaultconfig
const (
namespaceFieldSpecs = `
namespace:
# replace or add namespace field
# on all entities by default
- path: metadata/namespace
create: true

# Update namespace if necessary
# in the subjects fields
- path: subjects
kind: RoleBinding
- path: subjects
kind: ClusterRoleBinding

# Would make sense to skip those
# by default but would break backward
# compatility.
# - path: metadata/name
# kind: Namespace

# skip those ClusterWide entities
- path: metadata/namespace
kind: APIService
skip: true
- path: metadata/namespace
kind: CSIDriver
skip: true
- path: metadata/namespace
kind: CSINode
skip: true
- path: metadata/namespace
kind: CertificateSigningRequest
skip: true
- path: metadata/namespace
kind: ClusterRole
skip: true
- path: metadata/namespace
kind: ClusterRoleBinding
skip: true
- path: metadata/namespace
kind: ComponentStatus
skip: true
- path: metadata/namespace
kind: CustomResourceDefinition
skip: true
- path: metadata/namespace
kind: MutatingWebhookConfiguration
skip: true
- path: metadata/namespace
kind: Namespace
skip: true
- path: metadata/namespace
kind: Node
skip: true
- path: metadata/namespace
kind: PersistentVolume
skip: true
- path: metadata/namespace
kind: PodSecurityPolicy
skip: true
- path: metadata/namespace
kind: PriorityClass
skip: true
- path: metadata/namespace
kind: RuntimeClass
skip: true
- path: metadata/namespace
kind: SelfSubjectAccessReview
skip: true
- path: metadata/namespace
kind: SelfSubjectRulesReview
skip: true
- path: metadata/namespace
kind: StorageClass
skip: true
- path: metadata/namespace
kind: SubjectAccessReview
skip: true
- path: metadata/namespace
kind: TokenReview
skip: true
- path: metadata/namespace
kind: ValidatingWebhookConfiguration
skip: true
- path: metadata/namespace
kind: VolumeAttachment
skip: true
`
)
21 changes: 16 additions & 5 deletions pkg/transformers/config/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,26 @@ type Factory struct {
// if any, with default config.
func MakeTransformerConfig(
ldr ifc.Loader, paths []string) (*TransformerConfig, error) {
t1 := MakeDefaultConfig()

result := &TransformerConfig{}
var err error

// merge and normalize Default Config
defaultcfg := MakeDefaultConfig()
result, err = result.Merge(defaultcfg)
if err != nil {
return nil, err
}
if len(paths) == 0 {
return t1, nil
return defaultcfg, nil
}
t2, err := NewFactory(ldr).FromFiles(paths)

// merge and normalize user provided configurations
result, err = NewFactory(ldr).FromFiles(result, paths)
if err != nil {
return nil, err
}
return t1.Merge(t2)
return result, nil
}

func NewFactory(l ifc.Loader) *Factory {
Expand All @@ -56,8 +67,8 @@ func (tf *Factory) loader() ifc.Loader {

// FromFiles returns a TranformerConfig object from a list of files
func (tf *Factory) FromFiles(
result *TransformerConfig,
paths []string) (*TransformerConfig, error) {
result := &TransformerConfig{}
for _, path := range paths {
data, err := tf.loader().Load(path)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/transformers/config/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namePrefix:
- path: nameprefix/path
kind: SomeKind
`))
tcfg, err := NewFactory(ldr).FromFiles([]string{"/app/config.yaml"})
emptycfg := &TransformerConfig{}
tcfg, err := NewFactory(ldr).FromFiles(emptycfg, []string{"/app/config.yaml"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
Loading