...

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

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

     1[hello]: https://github.com/monopole/hello
     2[kind]: https://github.com/kubernetes-sigs/kind
     3
     4# Demo: hello app
     5
     6This demo helps you to deploy an example hello app end-to-end using `kapply`.
     7
     8Steps:
     91. Create the resources files.
    102. Spin-up kubernetes cluster on local using [kind].
    113. Deploy, modify and delete the app using `kapply` and verify the status.
    12
    13First define a place to work:
    14
    15<!-- @makeWorkplace @testE2EAgainstLatestRelease-->
    16```
    17DEMO_HOME=$(mktemp -d)
    18```
    19
    20Alternatively, use
    21
    22> ```
    23> DEMO_HOME=~/hello
    24> ```
    25
    26## Establish the base
    27
    28Let's run the [hello] service.
    29
    30<!-- @createBase @testE2EAgainstLatestRelease-->
    31```
    32BASE=$DEMO_HOME/base
    33mkdir -p $BASE
    34OUTPUT=$DEMO_HOME/output
    35mkdir -p $OUTPUT
    36GREEN='\033[0;32m'
    37RED='\033[0;31m'
    38NC='\033[0m' # No Color
    39
    40function expectedOutputLine() {
    41  if ! grep -q "$@" "$OUTPUT/status"; then
    42    echo -e "${RED}Error: output line not found${NC}"
    43    echo -e "${RED}Expected: $@${NC}"
    44    exit 1
    45  else
    46    echo -e "${GREEN}Success: output line found${NC}"
    47  fi
    48}
    49
    50function expectedNotFound() {
    51  if grep -q "$@" $OUTPUT/status; then
    52    echo -e "${RED}Error: output line found:${NC}"
    53    echo -e "${RED}Found: $@${NC}"
    54    exit 1
    55  else
    56    echo -e "${GREEN}Success: output line not found found${NC}"
    57  fi
    58}
    59```
    60
    61Let's add a simple config map resource in `base`
    62
    63<!-- @createConfigMapYaml @testE2EAgainstLatestRelease-->
    64```
    65cat <<EOF >$BASE/configMap.yaml
    66apiVersion: v1
    67kind: ConfigMap
    68metadata:
    69  name: the-map1
    70  namespace: hellospace
    71data:
    72  altGreeting: "Good Morning!"
    73  enableRisky: "false"
    74EOF
    75```
    76
    77Create a deployment file with the following example configuration
    78
    79<!-- @createDeploymentYaml @testE2EAgainstLatestRelease-->
    80```
    81cat <<EOF >$BASE/deployment.yaml
    82apiVersion: apps/v1
    83kind: Deployment
    84metadata:
    85  labels:
    86    app: hello
    87  name: the-deployment
    88  namespace: hellospace
    89spec:
    90  replicas: 3
    91  selector:
    92    matchLabels:
    93      app: hello
    94  template:
    95    metadata:
    96      labels:
    97        app: hello
    98        deployment: hello
    99    spec:
   100      containers:
   101      - command:
   102        - /hello
   103        - --port=8080
   104        - --enableRiskyFeature=\$(ENABLE_RISKY)
   105        env:
   106        - name: ALT_GREETING
   107          valueFrom:
   108            configMapKeyRef:
   109              key: altGreeting
   110              name: the-map1
   111        - name: ENABLE_RISKY
   112          valueFrom:
   113            configMapKeyRef:
   114              key: enableRisky
   115              name: the-map1
   116        image: monopole/hello:1
   117        name: the-container
   118        ports:
   119        - containerPort: 8080
   120          protocol: TCP
   121EOF
   122```
   123
   124Create `service.yaml` pointing to the deployment created above
   125
   126<!-- @createServiceYaml @testE2EAgainstLatestRelease-->
   127```
   128cat <<EOF >$BASE/service.yaml
   129kind: Service
   130apiVersion: v1
   131metadata:
   132  name: the-service
   133  namespace: hellospace
   134spec:
   135  selector:
   136    deployment: hello
   137  type: LoadBalancer
   138  ports:
   139  - protocol: TCP
   140    port: 8666
   141    targetPort: 8080
   142EOF
   143```
   144
   145## Run end-to-end tests
   146
   147The following requires installation of [kind].
   148
   149Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind"
   150<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
   151```
   152kind delete cluster
   153kind create cluster
   154```
   155
   156Create the `hellospace` namespace where we will install the resources.
   157<!-- @createNamespace @testE2EAgainstLatestRelease -->
   158```
   159kubectl create namespace hellospace
   160```
   161
   162Use the kapply init command to generate the inventory template. This contains
   163the namespace and inventory id used by apply to create inventory objects. 
   164<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
   165```
   166kapply init $BASE
   167
   168ls -1 $BASE | tee $OUTPUT/status
   169expectedOutputLine "inventory-template.yaml"
   170```
   171
   172Run preview to check which commands will be executed
   173<!-- @previewHelloApp @testE2EAgainstLatestRelease -->
   174```
   175kapply preview $BASE | tee $OUTPUT/status
   176
   177expectedOutputLine "apply result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   178
   179kapply preview $BASE --server-side | tee $OUTPUT/status
   180
   181expectedOutputLine "apply result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   182
   183# Verify that preview didn't create any resources.
   184kubectl get all -n hellospace 2>&1 | tee $OUTPUT/status
   185expectedOutputLine "No resources found in hellospace namespace."
   186```
   187
   188Use the `kapply` binary in `MYGOBIN` to apply a deployment and verify it is successful.
   189<!-- @runHelloApp @testE2EAgainstLatestRelease -->
   190```
   191kapply apply $BASE --reconcile-timeout=1m --status-events | tee $OUTPUT/status
   192
   193expectedOutputLine "deployment.apps/the-deployment is Current: Deployment is available. Replicas: 3"
   194
   195expectedOutputLine "service/the-service is Current: Service is ready"
   196
   197expectedOutputLine "configmap/the-map1 is Current: Resource is always ready"
   198
   199# Verify that we have the pods running in the cluster
   200kubectl get --no-headers pod -n hellospace | wc -l | xargs | tee $OUTPUT/status
   201expectedOutputLine "3"
   202```
   203
   204Now let's replace the configMap with configMap2 apply the config, fetch and verify the status.
   205This should delete the-map1 from deployment and add the-map2.
   206<!-- @replaceConfigMapInHello @testE2EAgainstLatestRelease -->
   207```
   208cat <<EOF >$BASE/configMap2.yaml
   209apiVersion: v1
   210kind: ConfigMap
   211metadata:
   212  name: the-map2
   213  namespace: hellospace
   214data:
   215  altGreeting: "Good Evening!"
   216  enableRisky: "false"
   217EOF
   218
   219rm $BASE/configMap.yaml
   220
   221kapply apply $BASE --reconcile-timeout=120s --status-events | tee $OUTPUT/status
   222
   223expectedOutputLine "configmap/the-map2 is Current: Resource is always ready"
   224
   225expectedOutputLine "configmap/the-map1 prune successful"
   226
   227# Verify that the new configmap has been created and the old one pruned.
   228kubectl get cm -n hellospace --no-headers | awk '{print $1}' | tee $OUTPUT/status
   229expectedOutputLine "the-map2"
   230expectedNotFound "the-map1"
   231```
   232
   233Clean-up the cluster 
   234<!-- @deleteKindCluster @testE2EAgainstLatestRelease -->
   235```
   236kapply preview $BASE --destroy | tee $OUTPUT/status
   237
   238expectedOutputLine "deployment.apps/the-deployment delete successful"
   239expectedOutputLine "configmap/the-map2 delete successful"
   240expectedOutputLine "service/the-service delete successful"
   241expectedOutputLine "delete result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   242
   243kapply preview $BASE --destroy --server-side | tee $OUTPUT/status
   244
   245expectedOutputLine "deployment.apps/the-deployment delete successful"
   246expectedOutputLine "configmap/the-map2 delete successful"
   247expectedOutputLine "service/the-service delete successful"
   248expectedOutputLine "delete result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   249
   250# Verify that preview all resources are still there after running preview.
   251kubectl get --no-headers all -n hellospace | wc -l | xargs | tee $OUTPUT/status
   252expectedOutputLine "6"
   253
   254kapply destroy $BASE | tee $OUTPUT/status;
   255
   256expectedOutputLine "deployment.apps/the-deployment delete successful"
   257expectedOutputLine "configmap/the-map2 delete successful"
   258expectedOutputLine "service/the-service delete successful"
   259expectedOutputLine "delete result: 3 attempted, 3 successful, 0 skipped, 0 failed"
   260expectedOutputLine "reconcile result: 3 attempted, 3 successful, 0 skipped, 0 failed, 0 timed out"
   261expectedNotFound "prune result"
   262
   263kind delete cluster;
   264```

View as plain text