...

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

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

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Inventory with Namespace
     4
     5This demo shows that the namespace the inventory object
     6is applied into will get applied first, so the inventory
     7object will always have a namespace to be applied into.
     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
    24<!-- @createBase @testE2EAgainstLatestRelease -->
    25```
    26BASE=$DEMO_HOME/base
    27mkdir -p $BASE
    28OUTPUT=$DEMO_HOME/output
    29mkdir -p $OUTPUT
    30GREEN='\033[0;32m'
    31RED='\033[0;31m'
    32NC='\033[0m' # No Color
    33
    34function expectedOutputLine() {
    35  if ! grep -q "$@" "$OUTPUT/status"; then
    36    echo -e "${RED}Error: output line not found${NC}"
    37    echo -e "${RED}Expected: $@${NC}"
    38    exit 1
    39  else
    40    echo -e "${GREEN}Success: output line found${NC}"
    41  fi
    42}
    43```
    44
    45## Create the "app"
    46
    47Create the config yaml for a config map and a namespace: (cm-a, test-namespace).
    48
    49<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
    50```
    51cat <<EOF >$BASE/config-map-a.yaml
    52apiVersion: v1
    53kind: ConfigMap
    54metadata:
    55  name: cm-a
    56  namespace: test-namespace
    57  labels:
    58    name: test-config-map-label
    59EOF
    60
    61cat <<EOF >$BASE/test-namespace.yaml
    62apiVersion: v1
    63kind: Namespace
    64metadata:
    65  name: test-namespace
    66EOF
    67```
    68
    69## Run end-to-end tests
    70
    71The following requires installation of [kind].
    72
    73Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
    74
    75<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
    76```
    77kind delete cluster
    78kind create cluster
    79```
    80
    81Use the kapply init command to generate the inventory template. This contains
    82the namespace and inventory id used by apply to create inventory objects. 
    83<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
    84```
    85kapply init --namespace=test-namespace $BASE | tee $OUTPUT/status
    86expectedOutputLine "namespace: test-namespace is used for inventory object"
    87```
    88
    89Apply the "app" to the cluster. The test-namespace should be configured, and
    90the config map should be created, and no resources should be pruned. The
    91test-namespace is created first, so the following resources within the namespace
    92(including the inventory object) will not fail.
    93<!-- @runApply @testE2EAgainstLatestRelease -->
    94```
    95kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
    96expectedOutputLine "namespace/test-namespace apply successful"
    97expectedOutputLine "configmap/cm-a apply successful"
    98expectedOutputLine "apply result: 2 attempted, 2 successful, 0 skipped, 0 failed"
    99expectedOutputLine "reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out"
   100
   101# There should be only one inventory object
   102kubectl get cm -n test-namespace --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
   103expectedOutputLine "1"
   104
   105# Capture the inventory object name for later testing
   106invName=$(kubectl get cm -n test-namespace --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | awk '{print $1}')
   107
   108# There should be one config map that is not the inventory object
   109kubectl get cm -n test-namespace --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
   110expectedOutputLine "1"
   111
   112# ConfigMap cm-a had been created in the cluster
   113kubectl get configmap/cm-a -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
   114expectedOutputLine "1"
   115```
   116
   117Now delete the inventory namespace from the local config. Ensure
   118that the subsequent apply does not prune this omitted namespace.
   119<!-- @noPruneInventoryNamespace @testE2EAgainstLatestRelease -->
   120```
   121rm -f $BASE/test-namespace.yaml
   122kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
   123expectedOutputLine "prune result: 1 attempted, 0 successful, 1 skipped, 0 failed"
   124expectedOutputLine "reconcile result: 1 attempted, 0 successful, 1 skipped, 0 failed, 0 timed out"
   125
   126# Inventory namespace should still exist
   127kubectl get ns test-namespace --no-headers | wc -l | tee $OUTPUT/status
   128
   129# Inventory object should still exist
   130kubectl get cm/${invName} -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
   131
   132# ConfigMap cm-a should still exist
   133kubectl get configmap/cm-a -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
   134expectedOutputLine "1"

View as plain text