Skip to content

Commit a655bd4

Browse files
committed
Skip tests modifying cluster/network.config when it is not permitted
In HyperShift a ValidatingAdmissionPolicy blocks the tests from modifying cluster config resources. Skip the tests that modify cluster/network.config.openshift.io if the admin client is not allowed to do so. Note that the ValidatingAdmissionPolicy failures return an Invalid(422) and not Forbidden(403) error: ``` I0527 19:02:21.445832 2048333 request.go:1154] Request Body: {"spec":{"networkType": ""}} I0527 19:02:21.445873 2048333 round_trippers.go:463] PATCH https://api.wveww-wjddj-6em.auu6.p3.openshiftapps.com:443/apis/config.openshift.io/v1/networks/cluster?dryRun=All&fieldManager=kubectl-patch I0527 19:02:21.445878 2048333 round_trippers.go:469] Request Headers: I0527 19:02:21.445886 2048333 round_trippers.go:473] Accept: application/json I0527 19:02:21.445892 2048333 round_trippers.go:473] Content-Type: application/merge-patch+json I0527 19:02:21.445897 2048333 round_trippers.go:473] User-Agent: oc/v4.2.0 (linux/amd64) kubernetes/5559085 I0527 19:02:21.445902 2048333 round_trippers.go:473] Authorization: Bearer <masked> I0527 19:02:21.577807 2048333 round_trippers.go:574] Response Status: 422 Unprocessable Entity in 131 milliseconds I0527 19:02:21.577830 2048333 round_trippers.go:577] Response Headers: I0527 19:02:21.577836 2048333 round_trippers.go:580] Cache-Control: no-cache, private I0527 19:02:21.577841 2048333 round_trippers.go:580] Content-Type: application/json I0527 19:02:21.577847 2048333 round_trippers.go:580] Strict-Transport-Security: max-age=31536000; includeSubDomains; preload I0527 19:02:21.577852 2048333 round_trippers.go:580] X-Kubernetes-Pf-Flowschema-Uid: 6e3b2d16-aa1b-42a1-bf05-eb06efacf90c I0527 19:02:21.577857 2048333 round_trippers.go:580] X-Kubernetes-Pf-Prioritylevel-Uid: 488a1e74-530c-4ca1-8d71-31360b7f84da I0527 19:02:21.577863 2048333 round_trippers.go:580] Content-Length: 702 I0527 19:02:21.577868 2048333 round_trippers.go:580] Date: Tue, 27 May 2025 17:02:21 GMT I0527 19:02:21.577874 2048333 round_trippers.go:580] Audit-Id: 0a1efc9d-4405-4c9a-bc6f-8563fd714f77 I0527 19:02:21.577903 2048333 request.go:1154] Response Body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"networks.config.openshift.io \"cluster\" is forbidden: ValidatingAdmissionPolicy 'config' with binding 'config-binding' denied request: This resource cannot be created, updated, or deleted. Please ask your administrator to modify the resource in the HostedCluster object.","reason":"Invalid","details":{"name":"cluster","group":"config.openshift.io","kind":"networks","causes":[{"message":"ValidatingAdmissionPolicy 'config' with binding 'config-binding' denied request: This resource cannot be created, updated, or deleted. Please ask your administrator to modify the resource in the HostedCluster object."}]},"code":422} The networks "cluster" is invalid: : ValidatingAdmissionPolicy 'config' with binding 'config-binding' denied request: This resource cannot be created, updated, or deleted. Please ask your administrator to modify the resource in the HostedCluster object. ``` Signed-off-by: Patryk Diak <[email protected]>
1 parent d30b7e1 commit a655bd4

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
@@ -29,6 +29,7 @@ import (
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/labels"
3131
"k8s.io/apimachinery/pkg/runtime/schema"
32+
"k8s.io/apimachinery/pkg/types"
3233
"k8s.io/apimachinery/pkg/util/intstr"
3334
"k8s.io/apimachinery/pkg/util/wait"
3435
"k8s.io/apiserver/pkg/storage/names"
@@ -976,3 +977,21 @@ func getMachineConfigPoolByLabel(oc *exutil.CLI, mcSelectorLabel labels.Set) ([]
976977
}
977978
return pools, nil
978979
}
980+
981+
// hasNetworkConfigWriteAccess determines if the admin client can patch the cluster/network.config.openshift.io object
982+
// by patching the resource in a dry-run mode(no changes are persisted).
983+
func hasNetworkConfigWriteAccess(oc *exutil.CLI) (bool, error) {
984+
_, err := oc.AdminConfigClient().ConfigV1().Networks().Patch(context.TODO(),
985+
clusterConfig,
986+
types.MergePatchType,
987+
[]byte(`{"spec":{"networkType": ""}}`),
988+
metav1.PatchOptions{FieldManager: oc.Namespace(), DryRun: []string{metav1.DryRunAll}})
989+
990+
if err != nil {
991+
if kapierrs.IsInvalid(err) || kapierrs.IsForbidden(err) {
992+
return false, nil
993+
}
994+
return false, err
995+
}
996+
return true, nil
997+
}

0 commit comments

Comments
 (0)