...

Text file src/k8s.io/kubernetes/cluster/addons/addon-manager/kube-addons-test.sh

Documentation: k8s.io/kubernetes/cluster/addons/addon-manager

     1#!/usr/bin/env bash
     2
     3# Copyright 2020 The Kubernetes Authors.
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8#
     9#     http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License.
    16
    17# These tests enforce behavior of kube-addon-manager functions against a real
    18# cluster. A working Kubernetes cluster must be set up with kubectl configured.
    19# To run with the released version of kubectl, use `make test`.
    20
    21set -o errexit
    22set -o pipefail
    23set -o nounset
    24
    25# Default kubectl to the test users installation if needed.
    26KUBECTL_BIN="${KUBECTL_BIN:-kubectl}"
    27
    28# Disabling shellcheck following files as the full path would be required.
    29# shellcheck disable=SC1091
    30source "kube-addons.sh"
    31
    32TEST_NS="kube-addon-manager-test"
    33
    34function retry() {
    35  local tries=10
    36  while [ "${tries}" -gt 0 ]; do
    37    "$@" && return 0;
    38    (( tries-- ))
    39    sleep 1
    40  done
    41}
    42
    43function setup(){
    44  retry kubectl create namespace "${TEST_NS}"
    45}
    46
    47function teardown() {
    48  retry kubectl delete namespace "${TEST_NS}"
    49}
    50
    51function error() {
    52  echo -e "\e[31m$*\e[0m"
    53}
    54
    55function echo_green() {
    56  echo -e "\e[32m$*\e[0m"
    57}
    58
    59function echo_blue() {
    60  echo -e "\e[34m$*\e[0m"
    61}
    62
    63function test_create_resource_reconcile() {
    64  local limitrange
    65  read -r -d '' limitrange << EOF
    66apiVersion: "v1"
    67kind: "LimitRange"
    68metadata:
    69  name: "limits"
    70  namespace: "${TEST_NS}"
    71  labels:
    72    addonmanager.kubernetes.io/mode: Reconcile
    73spec:
    74  limits:
    75    - type: "Container"
    76      defaultRequest:
    77        cpu: "100m"
    78EOF
    79
    80  # arguments are yaml text, number of tries, delay, name of file, and namespace
    81  echo_blue "Creating initial resource to test Reconcile mode"
    82  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
    83  if ! (kubectl get limits/limits -n "${TEST_NS}"); then
    84    error "failed to create limits w/ reconcile"
    85    return 1
    86  elif ! (kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"); then
    87    error "limits does not match applied config"
    88    return 1
    89  fi
    90
    91  # Changes to addons with mode reconcile should be reflected.
    92  echo_blue "Changes to manifest should be reflected in the cluster"
    93  limitrange="${limitrange//100m/50m}"
    94  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
    95  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"; then
    96    error "failed to update resource, still has 100m"
    97    return 1
    98  elif ! (kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "50m"); then
    99    error "failed to update resource, 50m limit was not reflected"
   100    return 1
   101  fi
   102
   103  # Finally, the users configuration will not be respected.
   104  echo_blue "Changes the user makes should be overwritten by kube-addon-manager"
   105  EDITOR="sed -i 's/50m/600m/'" kubectl edit limits/limits -n ${TEST_NS}
   106  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "50m"; then
   107    error "failed to edit resource with sed -- test is broken"
   108    return 1
   109  fi
   110  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   111  if ! ( kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "50m"); then
   112    error "failed to update resource, user config was respected when it should have been rewritten"
   113    return 1
   114  fi
   115}
   116
   117function test_create_resource_ensureexists() {
   118  local limitrange
   119  read -r -d '' limitrange << EOF
   120apiVersion: "v1"
   121kind: "LimitRange"
   122metadata:
   123  name: "limits"
   124  namespace: "${TEST_NS}"
   125  labels:
   126    addonmanager.kubernetes.io/mode: EnsureExists
   127spec:
   128  limits:
   129    - type: "Container"
   130      defaultRequest:
   131        cpu: "100m"
   132EOF
   133
   134  # arguments are yaml text, number of tries, delay, name of file, and namespace
   135  echo_blue "Creating initial resource to test mode EnsureExists"
   136  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   137  if ! (kubectl get limits/limits -n "${TEST_NS}"); then
   138    error "failed to create limits w/ EnsureExists"
   139    return 1
   140  elif ! (kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"); then
   141    error "limits does not match applied config"
   142    return 1
   143  fi
   144
   145  # Changes to addons with mode EnsureExists should NOT be reflected.
   146  echo_blue "Changes to the manifest should not be reconciled with the cluster"
   147  limitrange="${limitrange//100m/50m}"
   148  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   149  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "50m"; then
   150    error "failed to respect existing resource, was overwritten despite EnsureExists"
   151    return 1
   152  fi
   153
   154  # the users configuration must be respected
   155  echo_blue "User configuration will be persisted for EnsureExists"
   156  EDITOR="sed -i 's/100m/600m/'" kubectl edit limits/limits -n ${TEST_NS}
   157  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"; then
   158    error "failed to edit resource with sed -- test is broken"
   159    return 1
   160  fi
   161  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   162  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"; then
   163    error "failed to respect user changes to EnsureExists object"
   164    return 1
   165  fi
   166
   167  # unless they delete the object, in which case it should return
   168  echo_blue "Missing EnsureExists resources will be re-created"
   169  kubectl delete limits/limits -n ${TEST_NS}
   170  if kubectl get limits/limits -n ${TEST_NS}; then
   171    error "failed to delete limitrange"
   172    return 1
   173  fi
   174  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   175  if ! kubectl get limits/limits -n ${TEST_NS}; then
   176    error "failed to recreate deleted EnsureExists resource"
   177    return 1
   178  fi
   179}
   180
   181function test_create_multiresource() {
   182  local limitrange
   183  read -r -d '' limitrange << EOF
   184apiVersion: "v1"
   185kind: "LimitRange"
   186metadata:
   187  name: "limits"
   188  namespace: "${TEST_NS}"
   189  labels:
   190    addonmanager.kubernetes.io/mode: EnsureExists
   191spec:
   192  limits:
   193    - type: "Container"
   194      defaultRequest:
   195        cpu: "100m"
   196---
   197apiVersion: "v1"
   198kind: "LimitRange"
   199metadata:
   200  name: "limits2"
   201  namespace: "${TEST_NS}"
   202  labels:
   203    addonmanager.kubernetes.io/mode: Reconcile
   204spec:
   205  limits:
   206    - type: "Container"
   207      defaultRequest:
   208        cpu: "100m"
   209EOF
   210
   211  # arguments are yaml text, number of tries, delay, name of file, and namespace
   212  echo_blue "Creating initial resources from multi-resource manifest"
   213  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   214  if ! (kubectl get limits/limits -n "${TEST_NS}"); then
   215    error "failed to create limits w/ EnsureExists"
   216    return 1
   217  elif ! (kubectl get limits/limits2 -n "${TEST_NS}"); then
   218    error "failed to create limits2 w/ Reconcile"
   219    return 1
   220  fi
   221
   222  # Changes to addons with mode EnsureExists should NOT be reflected.
   223  # However, the mode=Reconcile addon should be changed.
   224  echo_blue "Multi-resource manifest changes should apply to EnsureExists, not Reconcile"
   225  limitrange="${limitrange//100m/50m}"
   226  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   227  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "50m"; then
   228    error "failed to respect existing resource, was overwritten despite EnsureExists"
   229    return 1
   230  elif kubectl get limits/limits2 -n ${TEST_NS} | grep --silent "100m"; then
   231    error "failed to update resource with mode Reconcile"
   232    return 1
   233  fi
   234
   235  # the users configuration must be respected for EnsureExists
   236  echo_blue "Multi-resource manifest should not overwrite user config in EnsureExists"
   237  EDITOR="sed -i 's/100m/600m/'" kubectl edit limits/limits -n ${TEST_NS}
   238  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"; then
   239    error "failed to edit resource with sed -- test is broken"
   240    return 1
   241  fi
   242  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   243  if kubectl get limits/limits -n ${TEST_NS} -o yaml | grep --silent "100m"; then
   244    error "failed to respect user changes to EnsureExists object"
   245    return 1
   246  fi
   247
   248  # But not for Reconcile.
   249  echo_blue "Multi-resource manifest should overwrite user config in EnsureExists"
   250  EDITOR="sed -i 's/50m/600m/'" kubectl edit limits/limits2 -n ${TEST_NS}
   251  if kubectl get limits/limits2 -n ${TEST_NS} -o yaml | grep --silent "50m"; then
   252    error "failed to edit resource with sed -- test is broken"
   253    return 1
   254  fi
   255  create_resource_from_string "${limitrange}" "10" "1" "limitrange.yaml" "${TEST_NS}"
   256  if ! ( kubectl get limits/limits2 -n ${TEST_NS} -o yaml | grep --silent "50m"); then
   257    error "failed to update resource, user config was respected when it should have been rewritten"
   258    return 1
   259  fi
   260}
   261
   262function test_func() {
   263  local -r name="${1}"
   264
   265  echo_blue "=== TEST ${name}"
   266  setup
   267  if ! "${name}"; then
   268    failures=$((failures+1))
   269    error "=== FAIL"
   270  else
   271    echo_green "=== PASS"
   272  fi
   273  teardown
   274}
   275
   276failures=0
   277test_func test_create_resource_reconcile
   278test_func test_create_resource_ensureexists
   279test_func test_create_multiresource
   280if [ "${failures}" -gt 0 ]; then
   281  error "no. failed tests: ${failures}"
   282  error "FAIL"
   283  exit 1
   284else
   285  echo_green "PASS"
   286fi

View as plain text