...

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

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

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Basic Prune
     4
     5This demo shows basic pruning behavior by creating an
     6"app" with three config maps. After the initial apply of the
     7"app", pruning is demonstrated by locally deleting one
     8of the config maps, and applying again.
     9
    10First define a place to work:
    11
    12<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
    13```
    14DEMO_HOME=$(mktemp -d)
    15```
    16
    17Alternatively, use
    18
    19> ```
    20> DEMO_HOME=~/hello
    21> ```
    22
    23## Establish the base
    24
    25<!-- @createBase @testE2EAgainstLatestRelease -->
    26```
    27BASE=$DEMO_HOME/base
    28mkdir -p $BASE
    29OUTPUT=$DEMO_HOME/output
    30mkdir -p $OUTPUT
    31GREEN='\033[0;32m'
    32RED='\033[0;31m'
    33NC='\033[0m' # No Color
    34
    35function expectedOutputLine() {
    36  if ! grep -q "$@" "$OUTPUT/status"; then
    37    echo -e "${RED}Error: output line not found${NC}"
    38    echo -e "${RED}Expected: $@${NC}"
    39    exit 1
    40  else
    41    echo -e "${GREEN}Success: output line found${NC}"
    42  fi
    43}
    44```
    45
    46## Create the first "app"
    47
    48Create the config yaml for three config maps: (cm-a, cm-b, cm-c).
    49
    50<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
    51```
    52cat <<EOF >$BASE/config-map-a.yaml
    53apiVersion: v1
    54kind: ConfigMap
    55metadata:
    56  name: cm-a
    57  labels:
    58    name: test-config-map-label
    59EOF
    60
    61cat <<EOF >$BASE/config-map-b.yaml
    62apiVersion: v1
    63kind: ConfigMap
    64metadata:
    65  name: cm-b
    66  labels:
    67    name: test-config-map-label
    68EOF
    69
    70cat <<EOF >$BASE/config-map-c.yaml
    71apiVersion: v1
    72kind: ConfigMap
    73metadata:
    74  name: cm-c
    75  labels:
    76    name: test-config-map-label
    77EOF
    78```
    79
    80## Run end-to-end tests
    81
    82The following requires installation of [kind].
    83
    84Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
    85
    86<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
    87```
    88kind delete cluster
    89kind create cluster
    90```
    91
    92Use the kapply init command to generate the inventory template. This contains
    93the namespace and inventory id used by apply to create inventory objects. 
    94<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
    95```
    96kapply init $BASE | tee $OUTPUT/status
    97expectedOutputLine "namespace: default is used for inventory object"
    98```
    99
   100Apply the "app" to the cluster. All the config maps should be created, and
   101no resources should be pruned.
   102<!-- @runApply @testE2EAgainstLatestRelease -->
   103```
   104kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
   105expectedOutputLine "configmap/cm-a apply successful"
   106expectedOutputLine "configmap/cm-b apply successful"
   107expectedOutputLine "configmap/cm-c apply successful"
   108expectedOutputLine "apply result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   109expectedOutputLine "reconcile result: 3 attempted, 3 successful, 0 skipped, 0 failed, 0 timed out"
   110
   111# There should be only one inventory object
   112kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
   113expectedOutputLine "1"
   114# Capture the inventory object name for later testing
   115invName=$(kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | awk '{print $1}')
   116# There should be three config maps
   117kubectl get cm --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
   118expectedOutputLine "3"
   119# ConfigMap cm-a had been created in the cluster
   120kubectl get configmap/cm-a --no-headers | wc -l | tee $OUTPUT/status
   121expectedOutputLine "1"
   122# ConfigMap cm-b had been created in the cluster
   123kubectl get configmap/cm-b --no-headers | wc -l | tee $OUTPUT/status
   124expectedOutputLine "1"
   125# ConfigMap cm-c had been created in the cluster
   126kubectl get configmap/cm-c --no-headers | wc -l | tee $OUTPUT/status
   127expectedOutputLine "1"
   128```
   129
   130## Update the "app" to remove a config map, and add another config map.
   131
   132Remove cm-a.
   133
   134Create a fourth config map--cm-d.
   135<!-- @createAnotherConfigMap @testE2EAgainstLatestRelease -->
   136```
   137rm -f $BASE/config-map-a.yaml
   138
   139cat <<EOF >$BASE/config-map-d.yaml
   140apiVersion: v1
   141kind: ConfigMap
   142metadata:
   143  name: cm-d
   144  labels:
   145    name: test-config-map-label
   146EOF
   147```
   148
   149## Apply the updated "app"
   150
   151cm-a should be pruned (since it has been deleted locally).
   152
   153cm-b, cm-c should be unchanged.
   154
   155cm-d should be created.
   156<!-- @applySecondTime @testE2EAgainstLatestRelease -->
   157```
   158kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
   159expectedOutputLine "configmap/cm-a prune successful"
   160expectedOutputLine "configmap/cm-b apply successful"
   161expectedOutputLine "configmap/cm-c apply successful"
   162expectedOutputLine "configmap/cm-d apply successful"
   163expectedOutputLine "apply result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   164expectedOutputLine "prune result: 1 attempted, 1 successful, 0 skipped, 0 failed"
   165expectedOutputLine "reconcile result: 4 attempted, 4 successful, 0 skipped, 0 failed, 0 timed out"
   166
   167# There should be only one inventory object
   168kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
   169expectedOutputLine "1"
   170# The inventory object should have the same name
   171kubectl get configmap/${invName} --no-headers | tee $OUTPUT/status
   172expectedOutputLine "${invName}"
   173# There should be three config maps
   174kubectl get cm --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
   175expectedOutputLine "3"
   176# ConfigMap cm-b had been created in the cluster
   177kubectl get configmap/cm-b --no-headers | wc -l | tee $OUTPUT/status
   178expectedOutputLine "1"
   179# ConfigMap cm-c had been created in the cluster
   180kubectl get configmap/cm-c --no-headers | wc -l | tee $OUTPUT/status
   181expectedOutputLine "1"
   182# ConfigMap cm-d had been created in the cluster
   183kubectl get configmap/cm-d --no-headers | wc -l | tee $OUTPUT/status
   184expectedOutputLine "1"
   185```

View as plain text