...

Source file src/sigs.k8s.io/kustomize/api/krusty/basereusenameprefix_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 is a structure of a kustomization of two components, component1
    13  // and component2, that both use a shared postgres definition, which
    14  // they would individually adjust. This test case checks that the name
    15  // prefix does not cause a name reference conflict.
    16  //
    17  //                   root
    18  //              /            \
    19  //  component1/overlay  component2/overlay
    20  //             |              |
    21  //    component1/base    component2/base
    22  //              \            /
    23  //                   base
    24  //
    25  // This is the directory layout:
    26  //
    27  // ├── component1
    28  // │   ├── base
    29  // │   │   └── kustomization.yaml
    30  // │   └── overlay
    31  // │       └── kustomization.yaml
    32  // ├── component2
    33  // │   ├── base
    34  // │   │   └── kustomization.yaml
    35  // │   └── overlay
    36  // │       └── kustomization.yaml
    37  // ├── shared
    38  // │   ├── kustomization.yaml
    39  // │   └── resources.yaml
    40  // ├── kustomization.yaml
    41  
    42  func TestBaseReuseNameConflict(t *testing.T) {
    43  	th := kusttest_test.MakeHarness(t)
    44  	th.WriteK("component1/base", `
    45  resources:
    46    - ../../shared
    47  
    48  namePrefix: component1-
    49  `)
    50  	th.WriteK("component1/overlay", `
    51  resources:
    52    - ../base
    53  
    54  namePrefix: overlay-
    55  `)
    56  
    57  	th.WriteK("component2/base", `
    58  resources:
    59    - ../../shared
    60  
    61  namePrefix: component2-
    62  `)
    63  	th.WriteK("component2/overlay", `
    64  resources:
    65    - ../base
    66  
    67  namePrefix: overlay-
    68  `)
    69  
    70  	th.WriteK("shared", `
    71  resources:
    72    - resources.yaml
    73  `)
    74  	th.WriteF("shared/resources.yaml", `
    75  ---
    76  kind: PersistentVolumeClaim
    77  apiVersion: v1
    78  metadata:
    79    name: postgres
    80  spec:
    81    resources:
    82      requests:
    83        storage: 1Gi
    84    accessModes:
    85      - ReadWriteOnce
    86  ---
    87  apiVersion: apps/v1
    88  kind: Deployment
    89  metadata:
    90    name: postgres
    91  spec:
    92    selector:
    93      matchLabels: {}
    94    strategy:
    95      type: Recreate
    96    template:
    97      spec:
    98        containers:
    99          - name: postgres
   100            image: postgres
   101            imagePullPolicy: IfNotPresent
   102            volumeMounts:
   103              - mountPath: /var/lib/postgresql
   104                name: data
   105            ports:
   106              - name: postgres
   107                containerPort: 5432
   108        volumes:
   109          - name: data
   110            persistentVolumeClaim:
   111              claimName: postgres
   112  `)
   113  
   114  	th.WriteK(".", `
   115  resources:
   116    - component1/overlay
   117    - component2/overlay
   118  `)
   119  
   120  	m := th.Run(".", th.MakeDefaultOptions())
   121  	th.AssertActualEqualsExpected(m, `
   122  apiVersion: v1
   123  kind: PersistentVolumeClaim
   124  metadata:
   125    name: overlay-component1-postgres
   126  spec:
   127    accessModes:
   128    - ReadWriteOnce
   129    resources:
   130      requests:
   131        storage: 1Gi
   132  ---
   133  apiVersion: apps/v1
   134  kind: Deployment
   135  metadata:
   136    name: overlay-component1-postgres
   137  spec:
   138    selector:
   139      matchLabels: {}
   140    strategy:
   141      type: Recreate
   142    template:
   143      spec:
   144        containers:
   145        - image: postgres
   146          imagePullPolicy: IfNotPresent
   147          name: postgres
   148          ports:
   149          - containerPort: 5432
   150            name: postgres
   151          volumeMounts:
   152          - mountPath: /var/lib/postgresql
   153            name: data
   154        volumes:
   155        - name: data
   156          persistentVolumeClaim:
   157            claimName: overlay-component1-postgres
   158  ---
   159  apiVersion: v1
   160  kind: PersistentVolumeClaim
   161  metadata:
   162    name: overlay-component2-postgres
   163  spec:
   164    accessModes:
   165    - ReadWriteOnce
   166    resources:
   167      requests:
   168        storage: 1Gi
   169  ---
   170  apiVersion: apps/v1
   171  kind: Deployment
   172  metadata:
   173    name: overlay-component2-postgres
   174  spec:
   175    selector:
   176      matchLabels: {}
   177    strategy:
   178      type: Recreate
   179    template:
   180      spec:
   181        containers:
   182        - image: postgres
   183          imagePullPolicy: IfNotPresent
   184          name: postgres
   185          ports:
   186          - containerPort: 5432
   187            name: postgres
   188          volumeMounts:
   189          - mountPath: /var/lib/postgresql
   190            name: data
   191        volumes:
   192        - name: data
   193          persistentVolumeClaim:
   194            claimName: overlay-component2-postgres
   195  `)
   196  }
   197  

View as plain text