Skip to content

Commit d023e22

Browse files
committed
Allow test pods to access DNS metrics ports
New default network policies will limit access to DNS and DNS operator metrics ports to only pods in the openshift-monitoring namespace. Rather than creating test pods in the openshift-monitoring namespace, add a network policy that allows access to these metrics ports from the test namespace.
1 parent 2dfa4fd commit d023e22

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/extended/prometheus/prometheus.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
dto "github.com/prometheus/client_model/go"
2424
"github.com/prometheus/common/expfmt"
2525
v1 "k8s.io/api/core/v1"
26+
networkingv1 "k8s.io/api/networking/v1"
2627
kapierrs "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/util/intstr"
2830
"k8s.io/apimachinery/pkg/util/sets"
2931
"k8s.io/apimachinery/pkg/util/wait"
3032
"k8s.io/client-go/kubernetes"
@@ -99,6 +101,69 @@ var _ = g.Describe("[sig-instrumentation][Late] Platform Prometheus targets", fu
99101
expectedStatusCodes := sets.New(http.StatusUnauthorized, http.StatusForbidden)
100102

101103
g.By("checking that targets reject the requests with 401 or 403")
104+
TCP := v1.ProtocolTCP
105+
networkPolicies := []networkingv1.NetworkPolicy{
106+
{
107+
ObjectMeta: metav1.ObjectMeta{
108+
Name: "openshift-dns-test-pod-allow",
109+
Namespace: "openshift-dns",
110+
},
111+
Spec: networkingv1.NetworkPolicySpec{
112+
PodSelector: metav1.LabelSelector{},
113+
Ingress: []networkingv1.NetworkPolicyIngressRule{{
114+
Ports: []networkingv1.NetworkPolicyPort{
115+
{
116+
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 9154},
117+
Protocol: &TCP,
118+
},
119+
},
120+
From: []networkingv1.NetworkPolicyPeer{{
121+
NamespaceSelector: &metav1.LabelSelector{
122+
MatchLabels: map[string]string{
123+
"kubernetes.io/metadata.name": oc.Namespace(),
124+
},
125+
},
126+
}},
127+
}},
128+
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress},
129+
},
130+
},
131+
{
132+
ObjectMeta: metav1.ObjectMeta{
133+
Name: "openshift-dns-operator-test-pod-allow",
134+
Namespace: "openshift-dns-operator",
135+
},
136+
Spec: networkingv1.NetworkPolicySpec{
137+
PodSelector: metav1.LabelSelector{},
138+
Ingress: []networkingv1.NetworkPolicyIngressRule{{
139+
Ports: []networkingv1.NetworkPolicyPort{
140+
{
141+
Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 9393},
142+
Protocol: &TCP,
143+
},
144+
},
145+
From: []networkingv1.NetworkPolicyPeer{{
146+
NamespaceSelector: &metav1.LabelSelector{
147+
MatchLabels: map[string]string{
148+
"kubernetes.io/metadata.name": oc.Namespace(),
149+
},
150+
},
151+
}},
152+
}},
153+
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress},
154+
},
155+
},
156+
}
157+
for _, networkPolicy := range networkPolicies {
158+
_, err := oc.AdminKubeClient().NetworkingV1().NetworkPolicies(networkPolicy.Namespace).Create(context.Background(), &networkPolicy, metav1.CreateOptions{})
159+
o.Expect(err).NotTo(o.HaveOccurred(), fmt.Sprintf("Create networkpolicy %s/%s", networkPolicy.Namespace, networkPolicy.Name))
160+
}
161+
defer func() {
162+
for _, networkPolicy := range networkPolicies {
163+
err := oc.AdminKubeClient().NetworkingV1().NetworkPolicies(networkPolicy.Namespace).Delete(context.Background(), networkPolicy.Name, *metav1.NewDeleteOptions(1))
164+
o.Expect(err).NotTo(o.HaveOccurred(), fmt.Sprintf("Delete networkpolicy %s/%s", networkPolicy.Namespace, networkPolicy.Name))
165+
}
166+
}()
102167
execPod := exutil.CreateExecPodOrFail(oc.AdminKubeClient(), oc.Namespace(), "execpod-targets-authorization")
103168
defer func() {
104169
err := oc.AdminKubeClient().CoreV1().Pods(execPod.Namespace).Delete(context.Background(), execPod.Name, *metav1.NewDeleteOptions(1))

0 commit comments

Comments
 (0)