Skip to content

Commit bcba425

Browse files
chewongkfswain
authored andcommitted
test: kubectl-validate manifests in presubmit (kubernetes-sigs#1083)
* test: kubectl-validate manifests in presubmit Signed-off-by: Ernest Wong <[email protected]> * Address PR comments Signed-off-by: Ernest Wong <[email protected]> --------- Signed-off-by: Ernest Wong <[email protected]>
1 parent 8bcef51 commit bcba425

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ vet: ## Run go vet against code.
132132
go vet ./...
133133

134134
.PHONY: test
135-
test: manifests generate fmt vet envtest image-build ## Run tests.
135+
test: manifests generate fmt vet envtest image-build verify-crds ## Run tests.
136136
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e | grep -v /conformance) -race -coverprofile cover.out
137137

138138
.PHONY: test-unit
@@ -163,6 +163,10 @@ ci-lint: golangci-lint
163163
verify: vet fmt-verify manifests generate ci-lint verify-all
164164
git --no-pager diff --exit-code config api client-go
165165

166+
.PHONY: verify-crds
167+
verify-crds: kubectl-validate
168+
hack/verify-manifests.sh
169+
166170
# Run static analysis.
167171
.PHONY: verify-all
168172
verify-all:
@@ -354,13 +358,15 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest
354358
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
355359
HELM = $(PROJECT_DIR)/bin/helm
356360
YQ = $(PROJECT_DIR)/bin/yq
361+
KUBECTL_VALIDATE = $(PROJECT_DIR)/bin/kubectl-validate
357362

358363
## Tool Versions
359364
KUSTOMIZE_VERSION ?= v5.4.3
360365
CONTROLLER_TOOLS_VERSION ?= v0.16.1
361366
ENVTEST_VERSION ?= release-0.19
362367
GOLANGCI_LINT_VERSION ?= v1.62.2
363368
HELM_VERSION ?= v3.17.1
369+
KUBECTL_VALIDATE_VERSION ?= v0.0.4
364370

365371
.PHONY: kustomize
366372
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -390,6 +396,11 @@ yq: ## Download yq locally if necessary.
390396
helm: ## Download helm locally if necessary.
391397
GOBIN=$(PROJECT_DIR)/bin GO111MODULE=on go install helm.sh/helm/v3/cmd/helm@$(HELM_VERSION)
392398

399+
.PHONY: kubectl-validate
400+
kubectl-validate: $(KUBECTL_VALIDATE) ## Download kubectl-validate locally if necessary.
401+
$(KUBECTL_VALIDATE): $(LOCALBIN)
402+
$(call go-install-tool,$(KUBECTL_VALIDATE),sigs.k8s.io/kubectl-validate,$(KUBECTL_VALIDATE_VERSION))
403+
393404
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
394405
# $1 - target path with name of binary
395406
# $2 - package url which can be installed

hack/verify-manifests.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Copyright 2025 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
GATEWAY_API_VERSION="${GATEWAY_API_VERSION:-v1.3.0}"
23+
GKE_GATEWAY_API_VERSION="${GKE_GATEWAY_API_VERSION:-v1.4.0}"
24+
ISTIO_VERSION="${ISTIO_VERSION:-1.26.2}"
25+
TEMP_DIR=$(mktemp -d)
26+
27+
cleanup() {
28+
rm -rf "${TEMP_DIR}" || true
29+
}
30+
trap cleanup EXIT
31+
32+
fetch_crds() {
33+
local url="$1"
34+
curl -sL "${url}" -o "${TEMP_DIR}/$(basename "${url}")"
35+
}
36+
37+
main() {
38+
cp ${SCRIPT_ROOT}/config/crd/bases/* "${TEMP_DIR}/"
39+
40+
# Download external CRDs for validation
41+
fetch_crds "https://raw.githubusercontent.com/kubernetes-sigs/gateway-api/refs/tags/${GATEWAY_API_VERSION}/config/crd/standard/gateway.networking.k8s.io_gateways.yaml"
42+
fetch_crds "https://raw.githubusercontent.com/kubernetes-sigs/gateway-api/refs/tags/${GATEWAY_API_VERSION}/config/crd/standard/gateway.networking.k8s.io_httproutes.yaml"
43+
fetch_crds "https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/refs/tags/${GKE_GATEWAY_API_VERSION}/config/crd/networking.gke.io_gcpbackendpolicies.yaml"
44+
fetch_crds "https://raw.githubusercontent.com/GoogleCloudPlatform/gke-gateway-api/refs/tags/${GKE_GATEWAY_API_VERSION}/config/crd/networking.gke.io_healthcheckpolicies.yaml"
45+
fetch_crds "https://raw.githubusercontent.com/istio/istio/refs/tags/${ISTIO_VERSION}/manifests/charts/base/files/crd-all.gen.yaml"
46+
47+
make kubectl-validate
48+
49+
${SCRIPT_ROOT}/bin/kubectl-validate "${TEMP_DIR}"
50+
${SCRIPT_ROOT}/bin/kubectl-validate "${SCRIPT_ROOT}/config/manifests" --local-crds "${TEMP_DIR}"
51+
}
52+
53+
main

0 commit comments

Comments
 (0)