...

Text file src/sigs.k8s.io/cli-utils/examples/alphaTestExamples/crds.md

Documentation: sigs.k8s.io/cli-utils/examples/alphaTestExamples

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: CRDs
     4
     5This demo shows how it is possible to apply both a CRD and a CR
     6using the CRD, in the same apply operation. This is not something
     7that is possible with kubectl.
     8
     9First define a place to work:
    10
    11<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
    12```
    13DEMO_HOME=$(mktemp -d)
    14```
    15
    16Alternatively, use
    17
    18> ```
    19> DEMO_HOME=~/hello
    20> ```
    21
    22## Establish the base
    23
    24Create the CRD and a CR.
    25
    26<!-- @createBase @testE2EAgainstLatestRelease -->
    27```
    28BASE=$DEMO_HOME/base
    29mkdir -p $BASE
    30OUTPUT=$DEMO_HOME/output
    31mkdir -p $OUTPUT
    32GREEN='\033[0;32m'
    33RED='\033[0;31m'
    34NC='\033[0m' # No Color
    35
    36function expectedOutputLine() {
    37  if ! grep -q "$@" "$OUTPUT/status"; then
    38    echo -e "${RED}Error: output line not found${NC}"
    39    echo -e "${RED}Expected: $@${NC}"
    40    exit 1
    41  else
    42    echo -e "${GREEN}Success: output line found${NC}"
    43  fi
    44}
    45```
    46
    47CRD
    48
    49<!-- @createCRD @testE2EAgainstLatestRelease-->
    50```
    51cat <<EOF >$BASE/crd.yaml
    52apiVersion: apiextensions.k8s.io/v1
    53kind: CustomResourceDefinition
    54metadata:
    55  name: foos.custom.io
    56spec:
    57  group: custom.io
    58  names:
    59    kind: Foo
    60    plural: foos
    61  scope: Namespaced
    62  versions:
    63  - name: v1alpha1
    64    schema:
    65      openAPIV3Schema:
    66        description: A sample CRD
    67        properties:
    68          apiVersion:
    69            description: 'APIVersion'
    70            type: string
    71          kind:
    72            description: 'Kind'
    73            type: string
    74          metadata:
    75            type: object
    76          spec:
    77            description: The spec for the CRD
    78            properties:
    79              name:
    80                description: Name
    81                type: string
    82            required:
    83            - name
    84            type: object
    85        type: object
    86    served: true
    87    storage: true
    88    subresources: {}
    89EOF
    90```
    91
    92CR
    93
    94<!-- @createCR @testE2EAgainstLatestRelease-->
    95```
    96cat <<EOF >$BASE/cr.yaml
    97apiVersion: custom.io/v1alpha1
    98kind: Foo
    99metadata:
   100  name: example-foo
   101spec:
   102  name: abc
   103EOF
   104```
   105
   106## Run end-to-end tests
   107
   108The following requires installation of [kind].
   109
   110Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind"
   111<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
   112```
   113kind delete cluster
   114kind create cluster
   115```
   116
   117We will install this in the default namespace.
   118
   119Use the kapply init command to generate the inventory template. This contains
   120the namespace and inventory id used by apply to create inventory objects. 
   121<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
   122```
   123kapply init $BASE
   124
   125ls -1 $BASE | tee $OUTPUT/status
   126expectedOutputLine "inventory-template.yaml"
   127```
   128
   129Use the `kapply` binary in `MYGOBIN` to apply both the CRD and the CR.
   130<!-- @runApply @testE2EAgainstLatestRelease -->
   131```
   132kapply apply $BASE --reconcile-timeout=1m --status-events | tee $OUTPUT/status
   133
   134expectedOutputLine "foo.custom.io/example-foo is Current: Resource is current"
   135
   136kubectl get crd --no-headers | awk '{print $1}' | tee $OUTPUT/status
   137expectedOutputLine "foos.custom.io"
   138
   139kubectl get foos.custom.io --no-headers | awk '{print $1}' | tee $OUTPUT/status
   140expectedOutputLine "example-foo"
   141
   142kind delete cluster
   143```

View as plain text