...

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

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

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Lifecycle directives
     4
     5This demo shows how it is possible to use a lifecycle directive to 
     6change the behavior of prune and delete for specific resources.
     7
     8First define a place to work:
     9
    10<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
    11```
    12DEMO_HOME=$(mktemp -d)
    13```
    14
    15Alternatively, use
    16
    17> ```
    18> DEMO_HOME=~/hello
    19> ```
    20
    21## Establish the base
    22
    23<!-- @createBase @testE2EAgainstLatestRelease -->
    24```
    25BASE=$DEMO_HOME/base
    26mkdir -p $BASE
    27OUTPUT=$DEMO_HOME/output
    28mkdir -p $OUTPUT
    29GREEN='\033[0;32m'
    30RED='\033[0;31m'
    31NC='\033[0m' # No Color
    32
    33function expectedOutputLine() {
    34  if ! grep -q "$@" "$OUTPUT/status"; then
    35    echo -e "${RED}Error: output line not found${NC}"
    36    echo -e "${RED}Expected: $@${NC}"
    37    exit 1
    38  else
    39    echo -e "${GREEN}Success: output line found${NC}"
    40  fi
    41}
    42
    43function expectedNotFound() {
    44  if grep -q "$@" $OUTPUT/status; then
    45    echo -e "${RED}Error: output line found:${NC}"
    46    echo -e "${RED}Found: $@${NC}"
    47    exit 1
    48  else
    49    echo -e "${GREEN}Success: output line not found found${NC}"
    50  fi
    51}
    52```
    53
    54In this example we will just use three ConfigMap resources for simplicity, but
    55of course any type of resource can be used.
    56
    57- the first ConfigMap resource does not have any annotations;
    58- the second ConfigMap resource has the **cli-utils.sigs.k8s.io/on-remove** annotation with the value of **keep**;
    59- the third ConfigMap resource has the **client.lifecycle.config.k8s.io/deletion** annotation with the value of **detach**.
    60
    61These two annotations tell the kapply tool that a resource should not be deleted, even
    62if it would otherwise be pruned or deleted with the destroy command.
    63
    64<!-- @createFirstCM @testE2EAgainstLatestRelease-->
    65```
    66cat <<EOF >$BASE/configMap1.yaml
    67apiVersion: v1
    68kind: ConfigMap
    69metadata:
    70  name: firstmap
    71data:
    72  artist: Ornette Coleman
    73  album: The shape of jazz to come
    74EOF
    75```
    76
    77This ConfigMap includes the **cli-utils.sigs.k8s.io/on-remove** annotation
    78
    79<!-- @createSecondCM @testE2EAgainstLatestRelease-->
    80```
    81cat <<EOF >$BASE/configMap2.yaml
    82apiVersion: v1
    83kind: ConfigMap
    84metadata:
    85  name: secondmap
    86  annotations:
    87    cli-utils.sigs.k8s.io/on-remove: keep
    88data:
    89  artist: Husker Du
    90  album: New Day Rising
    91EOF
    92```
    93
    94
    95This ConfigMap includes the **client.lifecycle.config.k8s.io/deletion** annotation
    96
    97<!-- @createSecondCM @testE2EAgainstLatestRelease-->
    98```
    99cat <<EOF >$BASE/configMap3.yaml
   100apiVersion: v1
   101kind: ConfigMap
   102metadata:
   103  name: thirdmap
   104  annotations:
   105    client.lifecycle.config.k8s.io/deletion: detach
   106data:
   107  artist: Husker Du
   108  album: New Day Rising
   109EOF
   110```
   111
   112## Run end-to-end tests
   113
   114The following requires installation of [kind].
   115
   116Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind"
   117<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
   118```
   119kind delete cluster
   120kind create cluster
   121```
   122
   123Use the kapply init command to generate the inventory template. This contains
   124the namespace and inventory id used by apply to create inventory objects. 
   125<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
   126```
   127kapply init $BASE | tee $OUTPUT/status
   128expectedOutputLine "namespace: default is used for inventory object"
   129
   130```
   131
   132Apply the three resources to the cluster.
   133<!-- @runApply @testE2EAgainstLatestRelease -->
   134```
   135kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
   136
   137expectedOutputLine "apply result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   138expectedOutputLine "reconcile result: 3 attempted, 3 successful, 0 skipped, 0 failed, 0 timed out"
   139```
   140
   141Use the preview command to show what will happen if we run destroy. This should
   142show that secondmap and thirdmap will not be deleted even when using the destroy
   143command.
   144<!-- @runDestroyPreview @testE2EAgainstLatestRelease -->
   145```
   146kapply preview --destroy $BASE | tee $OUTPUT/status
   147
   148expectedOutputLine "configmap/firstmap delete successful"
   149expectedOutputLine 'configmap/secondmap delete skipped: annotation prevents deletion ("cli-utils.sigs.k8s.io/on-remove": "keep")'
   150expectedOutputLine 'configmap/thirdmap delete skipped: annotation prevents deletion ("client.lifecycle.config.k8s.io/deletion": "detach")'
   151expectedOutputLine "delete result: 3 attempted, 1 successful, 2 skipped, 0 failed"
   152```
   153
   154We run the destroy command and see that the resource without the annotations (firstmap)
   155has been deleted, while the resources with the annotations (secondmap and thirdmap)  are still in the
   156cluster.
   157<!-- @runDestroy @testE2EAgainstLatestRelease -->
   158```
   159kapply destroy $BASE | tee $OUTPUT/status
   160
   161expectedOutputLine "configmap/firstmap delete successful"
   162expectedOutputLine 'configmap/secondmap delete skipped: annotation prevents deletion ("cli-utils.sigs.k8s.io/on-remove": "keep")'
   163expectedOutputLine 'configmap/thirdmap delete skipped: annotation prevents deletion ("client.lifecycle.config.k8s.io/deletion": "detach")'
   164expectedOutputLine "configmap/firstmap reconcile successful"
   165expectedOutputLine "configmap/secondmap reconcile skipped"
   166expectedOutputLine "configmap/thirdmap reconcile skipped"
   167expectedOutputLine "delete result: 3 attempted, 1 successful, 2 skipped, 0 failed"
   168expectedOutputLine "reconcile result: 3 attempted, 1 successful, 2 skipped, 0 failed, 0 timed out"
   169expectedNotFound "prune result"
   170
   171kubectl get cm --no-headers | awk '{print $1}' | tee $OUTPUT/status
   172expectedOutputLine "secondmap"
   173
   174kubectl get cm --no-headers | awk '{print $1}' | tee $OUTPUT/status
   175expectedOutputLine "thirdmap"
   176```
   177
   178Apply the resources back to the cluster so we can demonstrate the lifecycle
   179directive with pruning.
   180<!-- @runApplyAgain @testE2EAgainstLatestRelease -->
   181```
   182kapply apply $BASE --inventory-policy=adopt --reconcile-timeout=1m | tee $OUTPUT/status
   183```
   184
   185Delete the manifest for secondmap and thirdmap
   186<!-- @runDeleteManifest @testE2EAgainstLatestRelease -->
   187```
   188rm $BASE/configMap2.yaml
   189
   190rm $BASE/configMap3.yaml
   191```
   192
   193Run preview to see that while secondmap and thirdmap would normally be pruned, they
   194will instead be skipped due to the lifecycle directive.
   195<!-- @runPreviewForPrune @testE2EAgainstLatestRelease -->
   196```
   197kapply preview $BASE | tee $OUTPUT/status
   198
   199expectedOutputLine "configmap/secondmap prune skipped"
   200expectedOutputLine "configmap/thirdmap prune skipped"
   201```
   202
   203Run apply and verify that secondmap and thirdmap are still in the cluster.
   204<!-- @runApplyToPrune @testE2EAgainstLatestRelease -->
   205```
   206kapply apply $BASE | tee $OUTPUT/status
   207
   208expectedOutputLine "configmap/secondmap prune skipped"
   209expectedOutputLine "configmap/thirdmap prune skipped"
   210
   211kubectl get cm --no-headers | awk '{print $1}' | tee $OUTPUT/status
   212expectedOutputLine "secondmap"
   213
   214kubectl get cm --no-headers | awk '{print $1}' | tee $OUTPUT/status
   215expectedOutputLine "thirdmap"
   216
   217kind delete cluster;
   218```

View as plain text