diff --git a/tests/e2e.sh b/tests/e2e.sh index 4cb89f6336..0daa08cb06 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -x set -e set -o pipefail @@ -97,24 +98,45 @@ function kube_state_metrics_up() { function test_daemonset() { sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/deployment.yaml sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/daemonset.yaml + sed -i "s|${KUBE_STATE_METRICS_CURRENT_IMAGE_NAME}:v.*|${KUBE_STATE_METRICS_IMAGE_NAME}:${KUBE_STATE_METRICS_IMAGE_TAG}|g" ./examples/daemonsetsharding/deployment-no-node-pods.yaml kubectl get deployment -n kube-system kubectl create -f ./examples/daemonsetsharding ls ./examples/daemonsetsharding kube_state_metrics_up - curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-shard:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/pod-metrics - curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/nonpod-metrics - m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/pod-metrics | grep "# TYPE kube_pod_info" || true)" - m2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/nonpod-metrics | grep "# TYPE kube_pod_info" || true)" - if [[ -z "${m1}" ]]; then - echo "can't found metric kube_pod_info from pod metrics service" + kubectl apply -f ./tests/e2e/testdata/pods.yaml + sleep 10 + kubectl get deployment -n default + curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-shard:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics + curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics + curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-no-node-pods:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics + + cat ${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics >> ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics + cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "kube_pod_info" + runningpod1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "runningpod1" | grep -c "kube_pod_info" )" + pendingpod2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "pendingpod2" | grep -c "kube_pod_info" )" + pendingpod2_in_deployment="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics | grep "pendingpod2" | grep -c "kube_pod_info" )" + expected_num_pod=1 + if [ "${runningpod1}" != "${expected_num_pod}" ]; then + echo "metric kube_pod_info for runningpod1 doesn't show up only once, got ${runningpod1} times" exit 1 fi - if [[ -n "${m2}" ]]; then - echo "shouldn't find metric kube_pod_info from non-pod metrics service" + cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-no-node-metrics | grep "kube_pod_info" + if [ "${pendingpod2_in_deployment}" != "${expected_num_pod}" ]; then + echo "metric kube_pod_info for pendingpod2 doesn't show up fonly once from service deployment-scraped-no-node-metrics" + exit 1 + fi + if [ "${pendingpod2}" != "${expected_num_pod}" ]; then + echo "metric kube_pod_info for pendingpod2 doesn't show up only once, got ${runningpod1} times" + exit 1 + fi + m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep -c "# TYPE kube_node_info" )" + if [ "${m1}" != "1" ]; then + echo "metric kube_node_info doesn't show up only once, got ${m1} times" exit 1 fi + kubectl delete -f ./tests/e2e/testdata/pods.yaml kubectl delete -f ./examples/daemonsetsharding sleep 20 } diff --git a/tests/e2e/testdata/pods.yaml b/tests/e2e/testdata/pods.yaml new file mode 100644 index 0000000000..523e7b3317 --- /dev/null +++ b/tests/e2e/testdata/pods.yaml @@ -0,0 +1,75 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: pendingpod2 + name: pendingpod2 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: pendingpod2 + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + labels: + app: pendingpod2 + spec: + nodeSelector: + debug-node: "non" + containers: + - command: + - /agnhost + - netexec + - --http-port=8080q + image: registry.k8s.io/e2e-test-images/agnhost:2.39 + imagePullPolicy: IfNotPresent + name: agnhost + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + terminationGracePeriodSeconds: 30 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: runningpod1 + name: runningpod1 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: runningpod1 + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + labels: + app: runningpod1 + spec: + containers: + - command: + - /agnhost + - netexec + - --http-port=8080 + image: registry.k8s.io/e2e-test-images/agnhost:2.39 + imagePullPolicy: IfNotPresent + name: agnhost + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + terminationGracePeriodSeconds: 30 \ No newline at end of file