...

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

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

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Init Command
     4
     5This demo shows how the kapply init command works.
     6
     7First define a place to work:
     8
     9<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
    10```
    11DEMO_HOME=$(mktemp -d)
    12```
    13
    14Alternatively, use
    15
    16> ```
    17> DEMO_HOME=~/demo
    18> ```
    19
    20## Establish the base
    21
    22<!-- @createBase @testE2EAgainstLatestRelease -->
    23```
    24BASE=$DEMO_HOME/base
    25mkdir -p $BASE
    26OUTPUT=$DEMO_HOME/output
    27mkdir -p $OUTPUT
    28GREEN='\033[0;32m'
    29RED='\033[0;31m'
    30NC='\033[0m' # No Color
    31
    32function expectedOutputLine() {
    33  if ! grep -q "$@" "$OUTPUT/status"; then
    34    echo -e "${RED}Error: output line not found${NC}"
    35    echo -e "${RED}Expected: $@${NC}"
    36    exit 1
    37  else
    38    echo -e "${GREEN}Success: output line found${NC}"
    39  fi
    40}
    41```
    42
    43## Create the first "app"
    44
    45Create the config yaml for three config maps: (cm-a, cm-b, cm-c).
    46
    47<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
    48```
    49cat <<EOF >$BASE/namespace.yaml
    50apiVersion: v1
    51kind: Namespace
    52metadata:
    53  name: test-namespace
    54EOF
    55
    56cat <<EOF >$BASE/config-map-a.yaml
    57apiVersion: v1
    58kind: ConfigMap
    59metadata:
    60  name: cm-a
    61  namespace: test-namespace
    62  labels:
    63    name: test-config-map-label
    64EOF
    65
    66cat <<EOF >$BASE/config-map-b.yaml
    67apiVersion: v1
    68kind: ConfigMap
    69metadata:
    70  name: cm-b
    71  namespace: test-namespace
    72  labels:
    73    name: test-config-map-label
    74EOF
    75
    76cat <<EOF >$BASE/config-map-c.yaml
    77apiVersion: v1
    78kind: ConfigMap
    79metadata:
    80  name: cm-c
    81  namespace: test-namespace
    82  labels:
    83    name: test-config-map-label
    84EOF
    85```
    86
    87## Run end-to-end tests
    88
    89The following requires installation of [kind].
    90
    91Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
    92
    93<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
    94```
    95kind delete cluster
    96kind create cluster
    97```
    98
    99Use the kapply init command to generate the inventory template. This contains
   100the namespace and inventory id used by apply to create inventory objects. 
   101<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
   102```
   103kapply init $BASE | tee $OUTPUT/status
   104expectedOutputLine "namespace: test-namespace is used for inventory object"
   105```
   106
   107Add another ConfigMap (cm-d) which is in the default namespace. The init
   108command should calculate the namespace to be default, since not all
   109objects are in the test-namespace.
   110
   111<!-- @updateApp @testE2EAgainstLatestRelease -->
   112```
   113
   114cat <<EOF >$BASE/config-map-d.yaml
   115apiVersion: v1
   116kind: ConfigMap
   117metadata:
   118  name: cm-d
   119  labels:
   120    name: test-config-map-label
   121EOF
   122
   123# Remove the initial inventory template.
   124rm -f $BASE/inventory-template.yaml
   125
   126kapply init $BASE | tee $OUTPUT/status
   127expectedOutputLine "namespace: default is used for inventory object"
   128```
   129
   130Remove the ConfigMap (cm-d) which is in the default namespace, and
   131add a cluster-scoped object. This cluster-scoped object should not
   132be used in the init namespace calculations, so we should calculate the
   133namespace as test-namespace.
   134
   135<!-- @updateAppAgain @testE2EAgainstLatestRelease -->
   136```
   137
   138# Remove the initial inventory template.
   139rm -f $BASE/inventory-template.yaml
   140
   141# Remove the ConfigMap in the default namespace.
   142rm -f $BASE/config-map-d.yaml
   143
   144# Add cluster-scoped resource--cluster-role
   145cat <<EOF >$BASE/cluster-role.yaml
   146apiVersion: rbac.authorization.k8s.io/v1
   147kind: ClusterRole
   148metadata:
   149  # "namespace" omitted since ClusterRoles are not namespaced
   150  name: secret-reader
   151rules:
   152- apiGroups: [""]
   153  #
   154  # at the HTTP level, the name of the resource for accessing Secret
   155  # objects is "secrets"
   156  resources: ["secrets"]
   157  verbs: ["get", "watch", "list"]
   158EOF
   159
   160kapply init $BASE | tee $OUTPUT/status
   161expectedOutputLine "namespace: test-namespace is used for inventory object"
   162```
   163

View as plain text