...

Source file src/sigs.k8s.io/kustomize/api/krusty/diamonds_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  // Here's a structure of two kustomizations,
    13  // `dev` and `prod`, individually deployable,
    14  // that depend on a diamond that combines
    15  // multiple tenants (kirk, spock and bones),
    16  // each sharing a common base.
    17  //
    18  // The objects used are contrived to avoid
    19  // clouding the example with authentic
    20  // but verbose Deployment boilerplate.
    21  //
    22  // Patches are applied at various levels,
    23  // requiring more specificity as needed.
    24  //
    25  //         dev      prod
    26  //             \   /
    27  //            tenants
    28  //          /    |    \
    29  //      kirk   spock  bones
    30  //          \    |    /
    31  //             base
    32  //
    33  func writeDiamondBase(th kusttest_test.Harness) {
    34  	th.WriteK("base", `
    35  resources:
    36  - deploy.yaml
    37  `)
    38  	th.WriteF("base/deploy.yaml", `
    39  apiVersion: v1
    40  kind: Deployment
    41  metadata:
    42    name: storefront
    43  spec:
    44    numReplicas: 1
    45  `)
    46  }
    47  
    48  func writeKirk(th kusttest_test.Harness) {
    49  	th.WriteK("kirk", `
    50  namePrefix: kirk-
    51  resources:
    52  - ../base
    53  - configmap.yaml
    54  patchesStrategicMerge:
    55  - dep-patch.yaml
    56  `)
    57  	th.WriteF("kirk/dep-patch.yaml", `
    58  apiVersion: v1
    59  kind: Deployment
    60  metadata:
    61    name: storefront
    62  spec:
    63    type: Confident
    64  `)
    65  	th.WriteF("kirk/configmap.yaml", `
    66  apiVersion: v1
    67  kind: ConfigMap
    68  metadata:
    69    name: settings
    70  data:
    71    phaser: caress
    72  `)
    73  }
    74  
    75  func writeSpock(th kusttest_test.Harness) {
    76  	th.WriteK("spock", `
    77  namePrefix: spock-
    78  resources:
    79  - ../base
    80  patchesStrategicMerge:
    81  - dep-patch.yaml
    82  `)
    83  	th.WriteF("spock/dep-patch.yaml", `
    84  apiVersion: v1
    85  kind: Deployment
    86  metadata:
    87    name: storefront
    88  spec:
    89    type: Logical
    90  `)
    91  }
    92  
    93  func writeBones(th kusttest_test.Harness) {
    94  	th.WriteK("bones", `
    95  namePrefix: bones-
    96  resources:
    97  - ../base
    98  patchesStrategicMerge:
    99  - dep-patch.yaml
   100  `)
   101  	th.WriteF("bones/dep-patch.yaml", `
   102  apiVersion: v1
   103  kind: Deployment
   104  metadata:
   105    name: storefront
   106  spec:
   107    type: Concerned
   108  `)
   109  }
   110  
   111  func writeTenants(th kusttest_test.Harness) {
   112  	th.WriteK("tenants", `
   113  namePrefix: t-
   114  resources:
   115  - ../kirk
   116  - ../spock
   117  - ../bones
   118  - configMap.yaml
   119  patchesStrategicMerge:
   120  - bones-patch.yaml
   121  `)
   122  	th.WriteF("tenants/bones-patch.yaml", `
   123  apiVersion: v1
   124  kind: Deployment
   125  metadata:
   126    name: bones-storefront
   127  spec:
   128    mood: Cantankerous
   129  `)
   130  	th.WriteF("tenants/configMap.yaml", `
   131  apiVersion: v1
   132  kind: ConfigMap
   133  metadata:
   134    name: federation
   135  data:
   136    zone: neutral
   137    guardian: forever
   138  `)
   139  }
   140  
   141  func TestBasicDiamond(t *testing.T) {
   142  	th := kusttest_test.MakeHarness(t)
   143  	writeDiamondBase(th)
   144  	writeKirk(th)
   145  	writeSpock(th)
   146  	writeBones(th)
   147  	writeTenants(th)
   148  	th.WriteK("prod", `
   149  namePrefix: prod-
   150  resources:
   151  - ../tenants
   152  patchesStrategicMerge:
   153  - patches.yaml
   154  `)
   155  	// The patch only has to be specific enough
   156  	// to match the item.
   157  	th.WriteF("prod/patches.yaml", `
   158  apiVersion: v1
   159  kind: Deployment
   160  metadata:
   161    name: t-kirk-storefront
   162  spec:
   163    numReplicas: 10000
   164  ---
   165  apiVersion: v1
   166  kind: ConfigMap
   167  metadata:
   168    name: federation
   169  data:
   170    guardian: ofTheGalaxy
   171  ---
   172  apiVersion: v1
   173  kind: ConfigMap
   174  metadata:
   175    name: t-federation
   176  data:
   177    zone: twilight
   178  `)
   179  
   180  	m := th.Run("prod", th.MakeDefaultOptions())
   181  	th.AssertActualEqualsExpected(m, `
   182  apiVersion: v1
   183  kind: Deployment
   184  metadata:
   185    name: prod-t-kirk-storefront
   186  spec:
   187    numReplicas: 10000
   188    type: Confident
   189  ---
   190  apiVersion: v1
   191  data:
   192    phaser: caress
   193  kind: ConfigMap
   194  metadata:
   195    name: prod-t-kirk-settings
   196  ---
   197  apiVersion: v1
   198  kind: Deployment
   199  metadata:
   200    name: prod-t-spock-storefront
   201  spec:
   202    numReplicas: 1
   203    type: Logical
   204  ---
   205  apiVersion: v1
   206  kind: Deployment
   207  metadata:
   208    name: prod-t-bones-storefront
   209  spec:
   210    mood: Cantankerous
   211    numReplicas: 1
   212    type: Concerned
   213  ---
   214  apiVersion: v1
   215  data:
   216    guardian: ofTheGalaxy
   217    zone: twilight
   218  kind: ConfigMap
   219  metadata:
   220    name: prod-t-federation
   221  `)
   222  }
   223  

View as plain text