Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineF-dev committed Jun 5, 2024
1 parent 775af91 commit 3756fc7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 10 deletions.
20 changes: 20 additions & 0 deletions examples/daemonsetsharding/deployment-no-node-pods-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: exporter
app.kubernetes.io/name: kube-state-metrics-no-node-pods
app.kubernetes.io/version: 2.12.0
name: kube-state-metrics-no-node-pods
namespace: kube-system
spec:
clusterIP: None
ports:
- name: http-metrics
port: 8080
targetPort: http-metrics
- name: telemetry
port: 8081
targetPort: telemetry
selector:
app.kubernetes.io/name: kube-state-metrics-no-node-pods
2 changes: 2 additions & 0 deletions examples/daemonsetsharding/deployment-no-node-pods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:
app.kubernetes.io/name: kube-state-metrics-pods
app.kubernetes.io/version: 2.12.0
name: kube-state-metrics-pods
app.kubernetes.io/name: kube-state-metrics-no-node-pods
name: kube-state-metrics-no-node-pods
namespace: kube-system
spec:
replicas: 1
Expand Down
2 changes: 1 addition & 1 deletion jsonnet/kube-state-metrics/kube-state-metrics.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
'--node=',
],
};
local shardksmname = ksm.name + "-pods";
local shardksmname = ksm.name + "-no-node-pods";
std.mergePatch(ksm.deployment,
{
metadata: {
Expand Down
35 changes: 26 additions & 9 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -102,19 +103,35 @@ function test_daemonset() {
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
cat ${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics
cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics
runningpod1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics | grep "kube_pod_info" | grep "runningpod1" | wc -l)"
pendingpod2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics | grep "kube_pod_info" | grep "pendingpod2" | wc -l)"
expected_num_pod=1
if [ "${runningpod1}" == "${expected_num_pod}" ]; then
echo "runningpod1 doesn't show up in kube_pod_info metric only once"
exit 1
fi
if [[ -n "${m2}" ]]; then
echo "shouldn't find metric kube_pod_info from non-pod metrics service"
if [ "${pendingpod2}" -ne "${expected_num_pod}" ]; then
echo "pendingpod2 doesn't show up in kube_pod_info metric only once"
exit 1
fi

m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/daemonset-scraped-metrics | grep "# TYPE kube_node_info" | wc -l)"
m2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/deployment-scraped-metrics | grep "# TYPE kube_node_info" | wc -l)"
if [[ -z "${m2}" ]]; then
echo "can't found metric kube_node_info from deployment metrics service"
exit 1
fi
if [[ -n "${m1}" ]]; then
echo "shouldn't find metric kube_node_info from daemonset metrics service"
exit 1
fi
kubectl delete -f ./tests/e2e/testdata/pods.yaml
kubectl delete -f ./examples/daemonsetsharding
sleep 20
}
Expand Down
75 changes: 75 additions & 0 deletions tests/e2e/testdata/pods.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3756fc7

Please sign in to comment.