Skip to content

Commit 2298e65

Browse files
committed
OCPBUGS-48481 annotation validation policy
Adding new assets to payload Unit tests updated
1 parent f3755d7 commit 2298e65

5 files changed

+34
-3
lines changed

pkg/cloud/cloud_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,26 @@ func TestGetResources(t *testing.T) {
129129
}, {
130130
name: "GCP resources returned as expected",
131131
testPlatform: platformsMap[string(configv1.GCPPlatformType)],
132-
expectedResourceCount: 4,
132+
expectedResourceCount: 6,
133133
expectedResourcesKindName: []string{
134134
"Deployment/gcp-cloud-controller-manager",
135135
"PodDisruptionBudget/gcp-cloud-controller-manager",
136136
"ClusterRole/gcp-cloud-controller-manager",
137137
"ClusterRoleBinding/gcp-cloud-controller-manager:cloud-provider",
138+
"ValidatingAdmissionPolicyBinding/network-tier-annotation-binding",
139+
"ValidatingAdmissionPolicy/network-tier-annotation-validation-policy",
138140
},
139141
}, {
140142
name: "GCP resources returned as expected with single node cluster",
141143
testPlatform: platformsMap[string(configv1.GCPPlatformType)],
142-
expectedResourceCount: 3,
144+
expectedResourceCount: 5,
143145
singleReplica: true,
144146
expectedResourcesKindName: []string{
145147
"Deployment/gcp-cloud-controller-manager",
146148
"ClusterRole/gcp-cloud-controller-manager",
147149
"ClusterRoleBinding/gcp-cloud-controller-manager:cloud-provider",
150+
"ValidatingAdmissionPolicyBinding/network-tier-annotation-binding",
151+
"ValidatingAdmissionPolicy/network-tier-annotation-validation-policy",
148152
},
149153
}, {
150154
name: "Azure resources returned as expected",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: admissionregistration.k8s.io/v1
2+
kind: ValidatingAdmissionPolicyBinding
3+
metadata:
4+
name: network-tier-annotation-binding
5+
spec:
6+
policyName: network-tier-annotation-validation-policy
7+
validationActions: ["Deny"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: admissionregistration.k8s.io/v1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: network-tier-annotation-validation-policy
5+
spec:
6+
matchConstraints:
7+
resourceRules:
8+
- apiGroups: [""]
9+
apiVersions: ["v1"]
10+
operations: ["CREATE", "UPDATE"]
11+
resources: ["services"]
12+
validations:
13+
- expression: "(!has(object.metadata.annotations) || !('cloud.google.com/network-tier' in object.metadata.annotations) ||
14+
(object.metadata.annotations['cloud.google.com/network-tier'] == 'Standard' || object.metadata.annotations['cloud.google.com/network-tier'] == 'Premium') ||
15+
(has(oldObject.metadata.annotations) && oldObject.metadata.annotations['cloud.google.com/network-tier'] == object.metadata.annotations['cloud.google.com/network-tier']))"
16+
message: "The annotation 'cloud.google.com/network-tier', if specified, must be either 'Standard' or 'Premium'."
17+

pkg/cloud/gcp/gcp.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/asaskevich/govalidator"
8+
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
89
appsv1 "k8s.io/api/apps/v1"
910
rbacv1 "k8s.io/api/rbac/v1"
1011
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -22,6 +23,8 @@ var (
2223
{ReferenceObject: &appsv1.Deployment{}, EmbedFsPath: "assets/cloud-controller-manager.yaml"},
2324
{ReferenceObject: &rbacv1.ClusterRole{}, EmbedFsPath: "assets/gcp-cloud-controller-manager-clusterrole.yaml"},
2425
{ReferenceObject: &rbacv1.ClusterRoleBinding{}, EmbedFsPath: "assets/gcp-cloud-controller-manager-clusterrolebinding.yaml"},
26+
{ReferenceObject: &admissionregistrationv1.ValidatingAdmissionPolicyBinding{}, EmbedFsPath: "assets/validating-admission-policy-binding.yaml"},
27+
{ReferenceObject: &admissionregistrationv1.ValidatingAdmissionPolicy{}, EmbedFsPath: "assets/validating-admission-policy.yaml"},
2528
}
2629
)
2730

pkg/cloud/gcp/gcp_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestResourcesRenderingSmoke(t *testing.T) {
5555
}
5656

5757
resources := assets.GetRenderedResources()
58-
assert.Len(t, resources, 3)
58+
assert.Len(t, resources, 5)
5959
})
6060
}
6161
}

0 commit comments

Comments
 (0)