...

Source file src/sigs.k8s.io/kustomize/api/krusty/crd_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 writeBaseWithCrd(th kusttest_test.Harness) {
    13  	th.WriteK("base", `
    14  crds:
    15  - mycrd.json
    16  
    17  resources:
    18  - secret.yaml
    19  - mykind.yaml
    20  - bee.yaml
    21  
    22  namePrefix: x-
    23  `)
    24  	th.WriteF("base/bee.yaml", `
    25  apiVersion: v1beta1
    26  kind: Bee
    27  metadata:
    28    name: bee
    29  spec:
    30    action: fly
    31  `)
    32  	th.WriteF("base/mykind.yaml", `
    33  apiVersion: jingfang.example.com/v1
    34  kind: MyKind
    35  metadata:
    36    name: mykind
    37  spec:
    38    secretRef:
    39      name: crdsecret
    40    beeRef:
    41      name: bee
    42  `)
    43  	th.WriteF("base/secret.yaml", `
    44  apiVersion: v1
    45  kind: Secret
    46  metadata:
    47    name: crdsecret
    48  data:
    49    PATH: yellowBrickRoad
    50  `)
    51  	th.WriteF("base/mycrd.json", `
    52  {
    53    "github.com/example/pkg/apis/jingfang/v1beta1.Bee": {
    54      "Schema": {
    55        "description": "Bee",
    56        "properties": {
    57          "apiVersion": {
    58            "description": "APIVersion defines the versioned schema of this representation of an object.",
    59            "type": "string"
    60          },
    61          "kind": {
    62            "description": "Kind is a string value representing the REST resource this object represents.",
    63            "type": "string"
    64          },
    65          "metadata": {
    66            "$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
    67          },
    68          "spec": {
    69            "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec"
    70          },
    71          "status": {
    72            "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus"
    73          }
    74        }
    75      },
    76      "Dependencies": [
    77        "github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec",
    78        "github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus",
    79        "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
    80      ]
    81    },
    82    "github.com/example/pkg/apis/jingfang/v1beta1.BeeList": {
    83      "Schema": {
    84        "required": [
    85          "items"
    86        ],
    87        "properties": {
    88          "apiVersion": {
    89            "description": "APIVersion defines the versioned schema of this representation of an object.",
    90            "type": "string"
    91          },
    92          "items": {
    93            "type": "array",
    94            "items": {
    95              "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.Bee"
    96            }
    97          },
    98          "kind": {
    99            "description": "Kind is a string value representing the REST resource this object represents.",
   100            "type": "string"
   101          },
   102          "metadata": {
   103            "$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"
   104          }
   105        }
   106      },
   107      "Dependencies": [
   108        "github.com/example/pkg/apis/jingfang/v1beta1.Bee",
   109        "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"
   110      ]
   111    },
   112    "github.com/example/pkg/apis/jingfang/v1beta1.BeeObjectReference": {
   113      "Schema": {
   114        "properties": {
   115          "name": {
   116            "type": "string"
   117          }
   118        }
   119      },
   120      "Dependencies": []
   121    },
   122    "github.com/example/pkg/apis/jingfang/v1beta1.BeeSpec": {
   123      "Schema": {
   124        "description": "BeeSpec defines the desired state of Bee"
   125      },
   126      "Dependencies": []
   127    },
   128    "github.com/example/pkg/apis/jingfang/v1beta1.BeeStatus": {
   129      "Schema": {
   130        "description": "BeeStatus defines the observed state of Bee"
   131      },
   132      "Dependencies": []
   133    },
   134    "github.com/example/pkg/apis/jingfang/v1beta1.MyKind": {
   135      "Schema": {
   136        "description": "MyKind",
   137        "properties": {
   138          "apiVersion": {
   139            "description": "APIVersion defines the versioned schema of this representation of an object.",
   140            "type": "string"
   141          },
   142          "kind": {
   143            "description": "Kind is a string value representing the REST resource this object represents.",
   144            "type": "string"
   145          },
   146          "metadata": {
   147            "$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
   148          },
   149          "spec": {
   150            "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec"
   151          },
   152          "status": {
   153            "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus"
   154          }
   155        }
   156      },
   157      "Dependencies": [
   158        "github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec",
   159        "github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus",
   160        "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"
   161      ]
   162    },
   163    "github.com/example/pkg/apis/jingfang/v1beta1.MyKindList": {
   164      "Schema": {
   165        "required": [
   166          "items"
   167        ],
   168        "properties": {
   169          "apiVersion": {
   170            "description": "APIVersion defines the versioned schema of this representation of an object.",
   171            "type": "string"
   172          },
   173          "items": {
   174            "type": "array",
   175            "items": {
   176              "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.MyKind"
   177            }
   178          },
   179          "kind": {
   180            "description": "Kind is a string value representing the REST resource this object represents.",
   181            "type": "string"
   182          },
   183          "metadata": {
   184            "$ref": "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"
   185          }
   186        }
   187      },
   188      "Dependencies": [
   189        "github.com/example/pkg/apis/jingfang/v1beta1.MyKind",
   190        "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta"
   191      ]
   192    },
   193    "github.com/example/pkg/apis/jingfang/v1beta1.MyKindSpec": {
   194      "Schema": {
   195        "description": "MyKindSpec defines the desired state of MyKind",
   196        "properties": {
   197          "beeRef": {
   198            "x-kubernetes-object-ref-api-version": "v1beta1",
   199            "x-kubernetes-object-ref-kind": "Bee",
   200            "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeObjectReference"
   201          },
   202          "secretRef": {
   203            "description": "If defined, use this secret for configuring the MYSQL_ROOT_PASSWORD",
   204            "x-kubernetes-object-ref-api-version": "v1",
   205            "x-kubernetes-object-ref-kind": "Secret",
   206            "$ref": "k8s.io/api/core/v1.LocalObjectReference"
   207          }
   208        }
   209      },
   210      "Dependencies": [
   211        "github.com/example/pkg/apis/jingfang/v1beta1.BeeObjectReference",
   212        "k8s.io/api/core/v1.LocalObjectReference"
   213      ]
   214    },
   215    "github.com/example/pkg/apis/jingfang/v1beta1.MyKindStatus": {
   216      "Schema": {
   217        "description": "MyKindStatus defines the observed state of MyKind"
   218      },
   219      "Dependencies": []
   220    }
   221  }
   222  `)
   223  }
   224  
   225  func TestCrdBase(t *testing.T) {
   226  	th := kusttest_test.MakeHarness(t)
   227  	writeBaseWithCrd(th)
   228  	m := th.Run("base", th.MakeDefaultOptions())
   229  	th.AssertActualEqualsExpected(m, `
   230  apiVersion: v1
   231  data:
   232    PATH: yellowBrickRoad
   233  kind: Secret
   234  metadata:
   235    name: x-crdsecret
   236  ---
   237  apiVersion: jingfang.example.com/v1
   238  kind: MyKind
   239  metadata:
   240    name: x-mykind
   241  spec:
   242    beeRef:
   243      name: x-bee
   244    secretRef:
   245      name: x-crdsecret
   246  ---
   247  apiVersion: v1beta1
   248  kind: Bee
   249  metadata:
   250    name: x-bee
   251  spec:
   252    action: fly
   253  `)
   254  }
   255  
   256  func TestCrdWithOverlay(t *testing.T) {
   257  	th := kusttest_test.MakeHarness(t)
   258  	writeBaseWithCrd(th)
   259  	th.WriteK("overlay", `
   260  namePrefix: prod-
   261  resources:
   262  - ../base
   263  patchesStrategicMerge:
   264  - bee.yaml
   265  `)
   266  	th.WriteF("overlay/bee.yaml", `
   267  apiVersion: v1beta1
   268  kind: Bee
   269  metadata:
   270    name: bee
   271  spec:
   272    action: makehoney
   273  `)
   274  	m := th.Run("overlay", th.MakeDefaultOptions())
   275  
   276  	th.AssertActualEqualsExpected(m, `
   277  apiVersion: v1
   278  data:
   279    PATH: yellowBrickRoad
   280  kind: Secret
   281  metadata:
   282    name: prod-x-crdsecret
   283  ---
   284  apiVersion: jingfang.example.com/v1
   285  kind: MyKind
   286  metadata:
   287    name: prod-x-mykind
   288  spec:
   289    beeRef:
   290      name: prod-x-bee
   291    secretRef:
   292      name: prod-x-crdsecret
   293  ---
   294  apiVersion: v1beta1
   295  kind: Bee
   296  metadata:
   297    name: prod-x-bee
   298  spec:
   299    action: makehoney
   300  `)
   301  }
   302  
   303  func TestCrdWithContainers(t *testing.T) {
   304  	th := kusttest_test.MakeHarness(t)
   305  	th.WriteK("crd/containers", `
   306  resources:
   307    - crd.yaml
   308  images:
   309    - name: test/test
   310      newName: registry.gitlab.com/test
   311      newTag: latest
   312  `)
   313  	th.WriteF("crd/containers/crd.yaml", `
   314  apiVersion: apiextensions.k8s.io/v1beta1
   315  kind: CustomResourceDefinition
   316  metadata:
   317    name: crontabs.stable.example.com
   318  spec:
   319    group: stable.example.com
   320    scope: Namespaced
   321    names:
   322      plural: crontabs
   323      singular: crontab
   324      kind: CronTab
   325      shortNames:
   326      - ct
   327    validation:
   328      openAPIV3Schema:
   329        properties:
   330          spec:
   331            containers:
   332              description: Containers allows injecting additional containers
   333    `)
   334  	m := th.Run("crd/containers", th.MakeDefaultOptions())
   335  	th.AssertActualEqualsExpected(m, `
   336  apiVersion: apiextensions.k8s.io/v1beta1
   337  kind: CustomResourceDefinition
   338  metadata:
   339    name: crontabs.stable.example.com
   340  spec:
   341    group: stable.example.com
   342    names:
   343      kind: CronTab
   344      plural: crontabs
   345      shortNames:
   346      - ct
   347      singular: crontab
   348    scope: Namespaced
   349    validation:
   350      openAPIV3Schema:
   351        properties:
   352          spec:
   353            containers:
   354              description: Containers allows injecting additional containers
   355  `)
   356  }
   357  

View as plain text