...

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

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

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Server Side Apply
     4
     5This demo shows how to invoke server-side apply,
     6instead of the default client-side apply.
     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```
    43
    44## Create the first "app"
    45
    46Create the config yaml for two config maps: (cm-a, cm-b).
    47
    48<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
    49```
    50cat <<EOF >$BASE/config-map-a.yaml
    51apiVersion: v1
    52kind: ConfigMap
    53metadata:
    54  name: cm-a
    55  labels:
    56    name: test-config-map-label
    57EOF
    58
    59cat <<EOF >$BASE/config-map-b.yaml
    60apiVersion: v1
    61kind: ConfigMap
    62metadata:
    63  name: cm-b
    64  labels:
    65    name: test-config-map-label
    66data:
    67  foo: sean
    68EOF
    69```
    70
    71## Run end-to-end tests
    72
    73The following requires installation of [kind].
    74
    75Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
    76
    77<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
    78```
    79kind delete cluster
    80kind create cluster
    81```
    82
    83Use the kapply init command to generate the inventory template. This contains
    84the namespace and inventory id used by apply to create inventory objects. 
    85<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
    86```
    87kapply init $BASE | tee $OUTPUT/status
    88expectedOutputLine "namespace: default is used for inventory object"
    89```
    90
    91Apply the "app" to the cluster. All the config maps should be created, and
    92no resources should be pruned.
    93<!-- @runServerSideApply @testE2EAgainstLatestRelease -->
    94```
    95kapply apply $BASE --server-side --reconcile-timeout=1m | tee $OUTPUT/status
    96expectedOutputLine "configmap/cm-a apply successful"
    97expectedOutputLine "configmap/cm-b 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 --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
   103expectedOutputLine "1"
   104# There should be two config maps that are not the inventory object
   105kubectl get cm --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
   106expectedOutputLine "2"
   107# ConfigMap cm-a had been created in the cluster
   108kubectl get configmap/cm-a --no-headers | wc -l | tee $OUTPUT/status
   109expectedOutputLine "1"
   110# ConfigMap cm-b had been created in the cluster
   111kubectl get configmap/cm-b --no-headers | wc -l | tee $OUTPUT/status
   112expectedOutputLine "1"
   113```
   114
   115Update a config map to update a field owned by the default field manager.
   116Update both config maps, using a different field-manager to create a
   117conflict, but the the --force-conflicts flag to overwrite successfully.
   118The conflicting field is "data.foo".
   119<!-- @runServerSideApplyWithForceConflicts @testE2EAgainstLatestRelease -->
   120```
   121cat <<EOF >$BASE/config-map-b.yaml
   122apiVersion: v1
   123kind: ConfigMap
   124metadata:
   125  name: cm-b
   126  labels:
   127    name: test-config-map-label
   128data:
   129  foo: baz
   130EOF
   131
   132kapply apply $BASE --server-side --field-manager=sean --force-conflicts --reconcile-timeout=1m | tee $OUTPUT/status
   133expectedOutputLine "configmap/cm-a apply successful"
   134expectedOutputLine "configmap/cm-b apply successful"
   135expectedOutputLine "apply result: 2 attempted, 2 successful, 0 skipped, 0 failed"
   136expectedOutputLine "reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out"
   137```

View as plain text