Skip to content

Commit 8f3c5c4

Browse files
Add e2e test
1 parent b45913d commit 8f3c5c4

File tree

3 files changed

+145
-11
lines changed

3 files changed

+145
-11
lines changed

pkg/options/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func (n NodeType) AsSlice() []string {
130130
}
131131

132132
func (n NodeType) String() string {
133+
klog.InfoS("n.AsSlice()", n.AsSlice())
133134
return strings.Join(n.AsSlice(), ",")
134135
}
135136

@@ -151,6 +152,7 @@ func (n *NodeType) GetNodeFieldSelector() string {
151152
klog.InfoS("Using spec.nodeName= to select unscheduable pods without node")
152153
return "spec.nodeName="
153154
}
155+
klog.InfoS("Using spec.nodeName=", nodeName)
154156
return fields.OneTermEqualSelector("spec.nodeName", nodeName).String()
155157

156158
}

tests/e2e.sh

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
set -x
1718
set -e
1819
set -o pipefail
1920

@@ -75,10 +76,14 @@ set +e
7576
kubectl proxy &
7677

7778
function kube_state_metrics_up() {
79+
serviceName="kube-state-metrics"
80+
if [[ -n "$1" ]]; then
81+
serviceName="$1"
82+
fi
7883
is_kube_state_metrics_running="false"
7984
# this for loop waits until kube-state-metrics is running by accessing the healthz endpoint
8085
for _ in {1..30}; do # timeout for 1 minutes
81-
KUBE_STATE_METRICS_STATUS=$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/healthz")
86+
KUBE_STATE_METRICS_STATUS=$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/${serviceName}:http-metrics/proxy/healthz")
8287
if [[ "${KUBE_STATE_METRICS_STATUS}" == "OK" ]]; then
8388
is_kube_state_metrics_running="true"
8489
break
@@ -94,27 +99,76 @@ function kube_state_metrics_up() {
9499
exit 1
95100
fi
96101
}
102+
function kube_pod_up() {
103+
is_pod_running="false"
104+
105+
for _ in {1..90}; do # timeout for 3 minutes
106+
kubectl get pods -A | grep "$1" 1>/dev/null 2>&1
107+
if [[ $? -ne 1 ]]; then
108+
is_pod_running="true"
109+
break
110+
fi
111+
112+
echo "waiting for pod $1 to come up"
113+
sleep 2
114+
done
115+
116+
if [[ ${is_pod_running} == "false" ]]; then
117+
echo "Pod does not show up within 3 minutes"
118+
exit 1
119+
fi
120+
}
121+
97122
function test_daemonset() {
98123
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
99124
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
125+
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
100126

127+
cat ./examples/daemonsetsharding/deployment-no-node-pods.yaml
128+
sleep 3
101129
kubectl get deployment -n kube-system
102-
kubectl create -f ./examples/daemonsetsharding
103130
ls ./examples/daemonsetsharding
104-
kube_state_metrics_up
105-
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
106-
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
107-
m1="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/pod-metrics | grep "# TYPE kube_pod_info" || true)"
108-
m2="$(cat ${KUBE_STATE_METRICS_LOG_DIR}/nonpod-metrics | grep "# TYPE kube_pod_info" || true)"
109-
if [[ -z "${m1}" ]]; then
110-
echo "can't found metric kube_pod_info from pod metrics service"
131+
kubectl create -f ./examples/daemonsetsharding
132+
kube_state_metrics_up kube-state-metrics-no-node-pods
133+
kube_state_metrics_up kube-state-metrics
134+
kube_state_metrics_up kube-state-metrics-shard
135+
kubectl apply -f ./tests/e2e/testdata/pods.yaml
136+
kube_pod_up runningpod1
137+
kube_pod_up pendingpod2
138+
139+
kubectl get deployment -n default
140+
# 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
141+
# 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
142+
# 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
143+
144+
# 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
145+
# cat ${KUBE_STATE_METRICS_LOG_DIR}/all-metrics | grep "kube_pod_info"
146+
runningpod1="$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-shard:http-metrics/proxy/metrics" | grep "runningpod1" | grep -c "kube_pod_info" )"
147+
node1="$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" | grep -c "# TYPE kube_node_info" )"
148+
expected_num_pod=1
149+
if [ "${runningpod1}" != "${expected_num_pod}" ]; then
150+
echo "metric kube_pod_info for runningpod1 doesn't show up only once, got ${runningpod1} times"
111151
exit 1
112152
fi
113-
if [[ -n "${m2}" ]]; then
114-
echo "shouldn't find metric kube_pod_info from non-pod metrics service"
153+
154+
if [ "${node1}" != "1" ]; then
155+
echo "metric kube_node_info doesn't show up only once, got ${node1} times"
115156
exit 1
116157
fi
117158

159+
kubectl logs deployment/kube-state-metrics-no-node-pods -n kube-system
160+
sleep 3
161+
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-no-node-pods:http-metrics/proxy/metrics"
162+
sleep 3
163+
kubectl get pods -A --field-selector spec.nodeName=""
164+
sleep 3
165+
pendingpod2="$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics-no-node-pods:http-metrics/proxy/metrics" | grep "pendingpod2" | grep -c "kube_pod_info" )"
166+
if [ "${pendingpod2}" != "${expected_num_pod}" ]; then
167+
echo "metric kube_pod_info for pendingpod2 doesn't show up only once, got ${runningpod1} times"
168+
exit 1
169+
fi
170+
171+
kubectl delete -f ./tests/e2e/testdata/pods.yaml
118172
kubectl delete -f ./examples/daemonsetsharding
119173
sleep 20
120174
}
@@ -144,6 +198,9 @@ set -e
144198
kubectl version
145199

146200
# query kube-state-metrics image tag
201+
cat pkg/options/types.go
202+
sleep 3
203+
147204
REGISTRY="registry.k8s.io/kube-state-metrics" make container
148205
docker images -a
149206
KUBE_STATE_METRICS_IMAGE_TAG=$(docker images -a|grep "${KUBE_STATE_METRICS_IMAGE_NAME}" |grep -v 'latest'|awk '{print $2}'|sort -u)

tests/e2e/testdata/pods.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
labels:
5+
app: pendingpod2
6+
name: pendingpod2
7+
namespace: default
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
app: pendingpod2
13+
strategy:
14+
rollingUpdate:
15+
maxSurge: 25%
16+
maxUnavailable: 25%
17+
type: RollingUpdate
18+
template:
19+
metadata:
20+
labels:
21+
app: pendingpod2
22+
spec:
23+
nodeSelector:
24+
debug-node: "non"
25+
containers:
26+
- command:
27+
- /agnhost
28+
- netexec
29+
- --http-port=8080q
30+
image: registry.k8s.io/e2e-test-images/agnhost:2.39
31+
imagePullPolicy: IfNotPresent
32+
name: agnhost
33+
terminationMessagePath: /dev/termination-log
34+
terminationMessagePolicy: File
35+
dnsPolicy: ClusterFirst
36+
restartPolicy: Always
37+
schedulerName: default-scheduler
38+
terminationGracePeriodSeconds: 30
39+
---
40+
apiVersion: apps/v1
41+
kind: Deployment
42+
metadata:
43+
labels:
44+
app: runningpod1
45+
name: runningpod1
46+
namespace: default
47+
spec:
48+
replicas: 1
49+
selector:
50+
matchLabels:
51+
app: runningpod1
52+
strategy:
53+
rollingUpdate:
54+
maxSurge: 25%
55+
maxUnavailable: 25%
56+
type: RollingUpdate
57+
template:
58+
metadata:
59+
labels:
60+
app: runningpod1
61+
spec:
62+
containers:
63+
- command:
64+
- /agnhost
65+
- netexec
66+
- --http-port=8080
67+
image: registry.k8s.io/e2e-test-images/agnhost:2.39
68+
imagePullPolicy: IfNotPresent
69+
name: agnhost
70+
terminationMessagePath: /dev/termination-log
71+
terminationMessagePolicy: File
72+
dnsPolicy: ClusterFirst
73+
restartPolicy: Always
74+
schedulerName: default-scheduler
75+
terminationGracePeriodSeconds: 30

0 commit comments

Comments
 (0)