Skip to content

Commit c15b3a0

Browse files
Merge pull request #29859 from kyrtapz/net_diag_perm_check
OCPBUGS-56698: Skip tests modifying cluster/network.config when it is not permitted
2 parents f87f59b + a655bd4 commit c15b3a0

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

test/extended/networking/network_diagnostics.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/kubernetes/test/e2e/framework"
1717

1818
exutil "github.com/openshift/origin/test/extended/util"
19+
"k8s.io/kubernetes/test/e2e/framework/skipper"
1920
)
2021

2122
const (
@@ -31,9 +32,16 @@ var _ = g.Describe("[sig-network][OCPFeatureGate:NetworkDiagnosticsConfig][Seria
3132
oc := exutil.NewCLIWithoutNamespace("network-diagnostics")
3233

3334
g.BeforeAll(func(ctx context.Context) {
35+
// Check if the test can write to cluster/network.config.openshift.io
36+
hasAccess, err := hasNetworkConfigWriteAccess(oc)
37+
o.Expect(err).NotTo(o.HaveOccurred())
38+
if !hasAccess {
39+
skipper.Skipf("The test is not permitted to modify the cluster/network.config.openshift.io resource")
40+
}
41+
3442
// Reset and take ownership of the network diagnostics config
3543
patch := []byte(`{"spec":{"networkDiagnostics":null}}`)
36-
_, err := oc.AdminConfigClient().ConfigV1().Networks().Patch(ctx, clusterConfig, types.MergePatchType, patch, metav1.PatchOptions{FieldManager: fieldManager})
44+
_, err = oc.AdminConfigClient().ConfigV1().Networks().Patch(ctx, clusterConfig, types.MergePatchType, patch, metav1.PatchOptions{FieldManager: fieldManager})
3745
o.Expect(err).NotTo(o.HaveOccurred())
3846
})
3947

test/extended/networking/services.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
admissionapi "k8s.io/pod-security-admission/api"
1616

1717
exutil "github.com/openshift/origin/test/extended/util"
18+
"k8s.io/kubernetes/test/e2e/framework/skipper"
1819
)
1920

2021
var _ = Describe("[sig-network] services", func() {
@@ -89,6 +90,13 @@ var _ = Describe("[sig-network] services", func() {
8990

9091
InIPv4ClusterContext(oc, func() {
9192
It("ensures external ip policy is configured correctly on the cluster [apigroup:config.openshift.io] [Serial]", func() {
93+
// Check if the test can write to cluster/network.config.openshift.io
94+
hasAccess, err := hasNetworkConfigWriteAccess(oc)
95+
Expect(err).NotTo(HaveOccurred())
96+
if !hasAccess {
97+
skipper.Skipf("The test is not permitted to modify the cluster/network.config.openshift.io resource")
98+
}
99+
92100
namespace := oc.Namespace()
93101
adminConfigClient := oc.AdminConfigClient()
94102
k8sClient := oc.KubeClient()
@@ -97,7 +105,7 @@ var _ = Describe("[sig-network] services", func() {
97105
By("create service of type load balancer with default cluster networks config")
98106
serviceName := names.SimpleNameGenerator.GenerateName("svc-without-ext-ip")
99107
By("check load balance service creation fails")
100-
err := createWebserverLBService(k8sClient, namespace, serviceName, "", []string{"192.168.132.10"}, nil)
108+
err = createWebserverLBService(k8sClient, namespace, serviceName, "", []string{"192.168.132.10"}, nil)
101109
Expect(kapierrs.IsForbidden(err)).Should(Equal(true))
102110

103111
// Test external ip policy configured with allowedCIDRs. Make sure service
@@ -166,6 +174,13 @@ var _ = Describe("[sig-network] services", func() {
166174

167175
InBareMetalIPv4ClusterContext(oc, func() {
168176
It("ensures external auto assign cidr is configured correctly on the cluster [apigroup:config.openshift.io] [Serial]", func() {
177+
// Check if the test can write to cluster/network.config.openshift.io
178+
hasAccess, err := hasNetworkConfigWriteAccess(oc)
179+
Expect(err).NotTo(HaveOccurred())
180+
if !hasAccess {
181+
skipper.Skipf("The test is not permitted to modify the cluster/network.config.openshift.io resource")
182+
}
183+
169184
namespace := oc.Namespace()
170185
adminConfigClient := oc.AdminConfigClient()
171186
k8sClient := oc.KubeClient()
@@ -175,7 +190,7 @@ var _ = Describe("[sig-network] services", func() {
175190
By("create service of type load balancer with default cluster networks config")
176191
serviceName := names.SimpleNameGenerator.GenerateName("svc-without-ext-ip-3")
177192
By("check load balance service creation fails")
178-
err := createWebserverLBService(k8sClient, namespace, serviceName, "", []string{"192.168.132.10"}, nil)
193+
err = createWebserverLBService(k8sClient, namespace, serviceName, "", []string{"192.168.132.10"}, nil)
179194
Expect(kapierrs.IsForbidden(err)).Should(Equal(true))
180195

181196
// Test external ip policy configured with both policy and auto assign cidr. Make sure service

test/extended/networking/util.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/labels"
3232
"k8s.io/apimachinery/pkg/runtime/schema"
33+
"k8s.io/apimachinery/pkg/types"
3334
"k8s.io/apimachinery/pkg/util/intstr"
3435
"k8s.io/apimachinery/pkg/util/wait"
3536
"k8s.io/apiserver/pkg/storage/names"
@@ -985,3 +986,21 @@ func getMachineConfigPoolByLabel(oc *exutil.CLI, mcSelectorLabel labels.Set) ([]
985986
}
986987
return pools, nil
987988
}
989+
990+
// hasNetworkConfigWriteAccess determines if the admin client can patch the cluster/network.config.openshift.io object
991+
// by patching the resource in a dry-run mode(no changes are persisted).
992+
func hasNetworkConfigWriteAccess(oc *exutil.CLI) (bool, error) {
993+
_, err := oc.AdminConfigClient().ConfigV1().Networks().Patch(context.TODO(),
994+
clusterConfig,
995+
types.MergePatchType,
996+
[]byte(`{"spec":{"networkType": ""}}`),
997+
metav1.PatchOptions{FieldManager: oc.Namespace(), DryRun: []string{metav1.DryRunAll}})
998+
999+
if err != nil {
1000+
if kapierrs.IsInvalid(err) || kapierrs.IsForbidden(err) {
1001+
return false, nil
1002+
}
1003+
return false, err
1004+
}
1005+
return true, nil
1006+
}

0 commit comments

Comments
 (0)