...

Source file src/sigs.k8s.io/kustomize/api/krusty/customconfig_test.go

Documentation: sigs.k8s.io/kustomize/api/krusty

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package krusty_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
    10  )
    11  
    12  func makeBaseReferencingCustomConfig(th kusttest_test.Harness) {
    13  	th.WriteK("base", `
    14  namePrefix: x-
    15  commonLabels:
    16    app: myApp
    17  vars:
    18  - name: APRIL_DIET
    19    objref:
    20      group: foo
    21      version: v1
    22      kind: Giraffe
    23      name: april
    24    fieldref:
    25      fieldpath: spec.diet
    26  - name: KOKO_DIET
    27    objref:
    28      group: foo
    29      version: v1
    30      kind: Gorilla
    31      name: koko
    32    fieldref:
    33      fieldpath: spec.diet
    34  resources:
    35  - animalPark.yaml
    36  - giraffes.yaml
    37  - gorilla.yaml
    38  configurations:
    39  - config/defaults.yaml
    40  - config/custom.yaml
    41  `)
    42  	th.WriteF("base/giraffes.yaml", `
    43  apiVersion: foo/v1
    44  kind: Giraffe
    45  metadata:
    46    name: april
    47  spec:
    48    diet: mimosa
    49    location: NE
    50  ---
    51  apiVersion: foo/v1
    52  kind: Giraffe
    53  metadata:
    54    name: may
    55  spec:
    56    diet: acacia
    57    location: SE
    58  `)
    59  	th.WriteF("base/gorilla.yaml", `
    60  apiVersion: foo/v1
    61  kind: Gorilla
    62  metadata:
    63    name: koko
    64  spec:
    65    diet: bambooshoots
    66    location: SW
    67  `)
    68  	th.WriteF("base/animalPark.yaml", `
    69  apiVersion: foo/v1
    70  kind: AnimalPark
    71  metadata:
    72    name: sandiego
    73  spec:
    74    gorillaRef:
    75      name: koko
    76    giraffeRef:
    77      name: april
    78    food:
    79    - "$(APRIL_DIET)"
    80    - "$(KOKO_DIET)"
    81  `)
    82  }
    83  
    84  func TestCustomConfig(t *testing.T) {
    85  	th := kusttest_test.MakeHarness(t)
    86  	makeBaseReferencingCustomConfig(th)
    87  	th.WriteLegacyConfigs("base/config/defaults.yaml")
    88  	th.WriteF("base/config/custom.yaml", `
    89  nameReference:
    90  - kind: Gorilla
    91    fieldSpecs:
    92    - kind: AnimalPark
    93      path: spec/gorillaRef/name
    94  - kind: Giraffe
    95    fieldSpecs:
    96    - kind: AnimalPark
    97      path: spec/giraffeRef/name
    98  varReference:
    99  - path: spec/food
   100    kind: AnimalPark
   101  `)
   102  	m := th.Run("base", th.MakeDefaultOptions())
   103  	th.AssertActualEqualsExpected(m, `
   104  apiVersion: foo/v1
   105  kind: AnimalPark
   106  metadata:
   107    labels:
   108      app: myApp
   109    name: x-sandiego
   110  spec:
   111    food:
   112    - mimosa
   113    - bambooshoots
   114    giraffeRef:
   115      name: x-april
   116    gorillaRef:
   117      name: x-koko
   118  ---
   119  apiVersion: foo/v1
   120  kind: Giraffe
   121  metadata:
   122    labels:
   123      app: myApp
   124    name: x-april
   125  spec:
   126    diet: mimosa
   127    location: NE
   128  ---
   129  apiVersion: foo/v1
   130  kind: Giraffe
   131  metadata:
   132    labels:
   133      app: myApp
   134    name: x-may
   135  spec:
   136    diet: acacia
   137    location: SE
   138  ---
   139  apiVersion: foo/v1
   140  kind: Gorilla
   141  metadata:
   142    labels:
   143      app: myApp
   144    name: x-koko
   145  spec:
   146    diet: bambooshoots
   147    location: SW
   148  `)
   149  }
   150  
   151  func TestCustomConfigWithDefaultOverspecification(t *testing.T) {
   152  	th := kusttest_test.MakeHarness(t)
   153  	makeBaseReferencingCustomConfig(th)
   154  	th.WriteLegacyConfigs("base/config/defaults.yaml")
   155  	// Specifying namePrefix here conflicts with (is the same as)
   156  	// the defaults written above.  This is intentional in the
   157  	// test to assure duplicate config doesn't cause problems.
   158  	th.WriteF("base/config/custom.yaml", `
   159  namePrefix:
   160  - path: metadata/name
   161  nameReference:
   162  - kind: Gorilla
   163    fieldSpecs:
   164    - kind: AnimalPark
   165      path: spec/gorillaRef/name
   166  - kind: Giraffe
   167    fieldSpecs:
   168    - kind: AnimalPark
   169      path: spec/giraffeRef/name
   170  varReference:
   171  - path: spec/food
   172    kind: AnimalPark
   173  `)
   174  	m := th.Run("base", th.MakeDefaultOptions())
   175  	th.AssertActualEqualsExpected(m, `
   176  apiVersion: foo/v1
   177  kind: AnimalPark
   178  metadata:
   179    labels:
   180      app: myApp
   181    name: x-sandiego
   182  spec:
   183    food:
   184    - mimosa
   185    - bambooshoots
   186    giraffeRef:
   187      name: x-april
   188    gorillaRef:
   189      name: x-koko
   190  ---
   191  apiVersion: foo/v1
   192  kind: Giraffe
   193  metadata:
   194    labels:
   195      app: myApp
   196    name: x-april
   197  spec:
   198    diet: mimosa
   199    location: NE
   200  ---
   201  apiVersion: foo/v1
   202  kind: Giraffe
   203  metadata:
   204    labels:
   205      app: myApp
   206    name: x-may
   207  spec:
   208    diet: acacia
   209    location: SE
   210  ---
   211  apiVersion: foo/v1
   212  kind: Gorilla
   213  metadata:
   214    labels:
   215      app: myApp
   216    name: x-koko
   217  spec:
   218    diet: bambooshoots
   219    location: SW
   220  `)
   221  }
   222  
   223  func TestFixedBug605_BaseCustomizationAvailableInOverlay(t *testing.T) {
   224  	th := kusttest_test.MakeHarness(t)
   225  	makeBaseReferencingCustomConfig(th)
   226  	th.WriteLegacyConfigs("base/config/defaults.yaml")
   227  	th.WriteF("base/config/custom.yaml", `
   228  nameReference:
   229  - kind: Gorilla
   230    fieldSpecs:
   231    - group: foo
   232      version: v1
   233      kind: AnimalPark
   234      path: spec/gorillaRef/name
   235  - kind: Giraffe
   236    fieldSpecs:
   237    - group: foo
   238      version: v1
   239      kind: AnimalPark
   240      path: spec/giraffeRef/name
   241  varReference:
   242  - path: spec/food
   243    group: foo
   244    version: v1
   245    kind: AnimalPark
   246  `)
   247  	th.WriteK("overlay", `
   248  namePrefix: o-
   249  commonLabels:
   250    movie: planetOfTheApes
   251  patchesStrategicMerge:
   252  - animalPark.yaml
   253  resources:
   254  - ../base
   255  - ursus.yaml
   256  `)
   257  	th.WriteF("overlay/ursus.yaml", `
   258  apiVersion: foo/v1
   259  kind: Gorilla
   260  metadata:
   261    name: ursus
   262  spec:
   263    diet: heston
   264    location: Arizona
   265  `)
   266  	// The following replaces the gorillaRef in the AnimalPark.
   267  	th.WriteF("overlay/animalPark.yaml", `
   268  apiVersion: foo/v1
   269  kind: AnimalPark
   270  metadata:
   271    name: sandiego
   272  spec:
   273    gorillaRef:
   274      name: ursus
   275  `)
   276  	m := th.Run("overlay", th.MakeDefaultOptions())
   277  	th.AssertActualEqualsExpected(m, `
   278  apiVersion: foo/v1
   279  kind: AnimalPark
   280  metadata:
   281    labels:
   282      app: myApp
   283      movie: planetOfTheApes
   284    name: o-x-sandiego
   285  spec:
   286    food:
   287    - mimosa
   288    - bambooshoots
   289    giraffeRef:
   290      name: o-x-april
   291    gorillaRef:
   292      name: o-ursus
   293  ---
   294  apiVersion: foo/v1
   295  kind: Giraffe
   296  metadata:
   297    labels:
   298      app: myApp
   299      movie: planetOfTheApes
   300    name: o-x-april
   301  spec:
   302    diet: mimosa
   303    location: NE
   304  ---
   305  apiVersion: foo/v1
   306  kind: Giraffe
   307  metadata:
   308    labels:
   309      app: myApp
   310      movie: planetOfTheApes
   311    name: o-x-may
   312  spec:
   313    diet: acacia
   314    location: SE
   315  ---
   316  apiVersion: foo/v1
   317  kind: Gorilla
   318  metadata:
   319    labels:
   320      app: myApp
   321      movie: planetOfTheApes
   322    name: o-x-koko
   323  spec:
   324    diet: bambooshoots
   325    location: SW
   326  ---
   327  apiVersion: foo/v1
   328  kind: Gorilla
   329  metadata:
   330    labels:
   331      movie: planetOfTheApes
   332    name: o-ursus
   333  spec:
   334    diet: heston
   335    location: Arizona
   336  `)
   337  }
   338  

View as plain text