Skip to content

Commit b6763f2

Browse files
committed
Update Make targets add Release tooling
Update make targets removing heavy lifting in the Makefile in favor of doing it in scripts. Add a script to collapse our CRDs into one yaml that can be applied. Add tooling for cutting releases. Signed-off-by: Andrew Stoycos <[email protected]>
1 parent 7d1e131 commit b6763f2

File tree

8 files changed

+285
-60
lines changed

8 files changed

+285
-60
lines changed

Makefile

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ help: ## Display this help.
2626

2727
##@ Development
2828

29-
CLIENTSET_NAME ?= versioned
30-
CLIENTSET_PKG_NAME ?= clientset
31-
API_PKG ?= sigs.k8s.io/network-policy-api
32-
API_GROUP_NAME ?= policy.networking.k8s.io
33-
API_DIR ?= ${API_PKG}/apis/v1alpha1
34-
OUTPUT_PKG ?= sigs.k8s.io/network-policy-api/pkg/client
35-
COMMON_FLAGS ?= --go-header-file $(shell pwd)/hack/boilerplate.go.txt
36-
37-
.PHONY: manifests
38-
manifests: ## Generate ClusterRole and CustomResourceDefinition objects.
39-
go run sigs.k8s.io/controller-tools/cmd/controller-gen rbac:roleName=manager-role crd paths=./apis/... output:crd:dir=./config/crd/bases output:stdout
40-
4129
.PHONY: fmt
4230
fmt: ## Run go fmt against code.
4331
go fmt ./...
@@ -47,50 +35,10 @@ vet: ## Run go vet against code.
4735
go vet ./...
4836

4937
.PHONY: generate
50-
generate: generate-deepcopy generate-typed-clients generate-typed-listers generate-typed-informers
51-
52-
.PHONY: generate-setup
53-
generate-setup:
54-
# Even when modules are enabled, the code-generator tools always write to
55-
# a traditional GOPATH directory, so fake on up to point to the current
56-
# workspace.
57-
mkdir -p "${GOPATH}/src/sigs.k8s.io"
58-
ln -sf "${ROOT_DIR}" "${GOPATH}/src/sigs.k8s.io/network-policy-api"
59-
60-
.PHONY: generate-cleanup
61-
generate-cleanup:
62-
rm -rf "${GOPATH}/src/sigs.k8s.io"
63-
64-
.PHONY: generate-deepcopy
65-
generate-deepcopy: ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
66-
go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
38+
generate:
39+
./hack/update-codegen.sh
6740

68-
.PHONY: generate-typed-clients
69-
generate-typed-clients: ## Generate typed client code
70-
go run k8s.io/code-generator/cmd/client-gen \
71-
--clientset-name "${CLIENTSET_NAME}" \
72-
--input-base "" \
73-
--input "${API_DIR}" \
74-
--output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
75-
${COMMON_FLAGS}
76-
77-
.PHONY: generate-typed-listers
78-
generate-typed-listers: ## Generate typed listers code
79-
go run k8s.io/code-generator/cmd/lister-gen \
80-
--input-dirs "${API_DIR}" \
81-
--output-package "${OUTPUT_PKG}/listers" \
82-
${COMMON_FLAGS}
83-
84-
.PHONY: generate-typed-informers
85-
generate-typed-informers: ## Generate typed informers code
86-
go run k8s.io/code-generator/cmd/informer-gen \
87-
--input-dirs "${API_DIR}" \
88-
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
89-
--listers-package "${OUTPUT_PKG}/listers" \
90-
--output-package "${OUTPUT_PKG}/informers" \
91-
${COMMON_FLAGS}
92-
93-
all: generate manifests fmt vet ## Runs all the development targets
41+
all: generate fmt vet ## Runs all the development targets
9442

9543
.PHONY: verify
9644
verify:
@@ -100,10 +48,10 @@ crd-e2e:
10048
hack/crd-e2e.sh -v
10149

10250
##@ Deployment
103-
install: manifests ## Install CRDs into the K8s cluster specified in ~/.kube/config.
51+
install: generate ## Install CRDs into the K8s cluster specified in ~/.kube/config.
10452
kubectl kustomize config/crd | kubectl apply -f -
10553

106-
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
54+
uninstall: generate kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
10755
kubectl kustomize config/crd | kubectl delete -f -
10856

10957
.PHONY: docs ## Build the documentation website
@@ -112,4 +60,8 @@ docs:
11260

11361
.PHONY: local-docs ## Deploy the docs locally
11462
local-docs:
115-
mkdocs serve
63+
mkdocs serve
64+
65+
.PHONY: build-install-yaml
66+
build-install-yaml:
67+
./hack/build-install-yaml.sh

hack/boilerplate.generatego.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/

hack/boilerplate.go.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
1313
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
16-
*/
16+
*/

hack/boilerplate.sh.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright YEAR The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

hack/build-install-yaml.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2022 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+
thisyear=`date +"%Y"`
22+
23+
mkdir -p release/
24+
25+
# Make clean files with boilerplate
26+
cat hack/boilerplate.sh.txt > release/install.yaml
27+
sed -i "s/YEAR/$thisyear/g" release/install.yaml
28+
cat << EOF >> release/install.yaml
29+
#
30+
# NetworkPolicy API install
31+
#
32+
EOF
33+
34+
for file in `ls config/crd/policy*.yaml`
35+
do
36+
echo "---" >> release/install.yaml
37+
echo "#" >> release/install.yaml
38+
echo "# $file" >> release/install.yaml
39+
echo "#" >> release/install.yaml
40+
cat $file >> release/install.yaml
41+
done
42+
43+
echo "Generated:" release/install.yaml

hack/update-codegen.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 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+
readonly SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")"/.. && pwd)"
22+
23+
# Keep outer module cache so we don't need to redownload them each time.
24+
# The build cache already is persisted.
25+
readonly GOMODCACHE="$(go env GOMODCACHE)"
26+
readonly GO111MODULE="on"
27+
readonly GOFLAGS="-mod=readonly"
28+
readonly GOPATH="$(mktemp -d)"
29+
readonly MIN_REQUIRED_GO_VER="1.19"
30+
31+
function go_version_matches {
32+
go version | perl -ne "exit 1 unless m{go version go([0-9]+.[0-9]+)}; exit 1 if (\$1 < ${MIN_REQUIRED_GO_VER})"
33+
return $?
34+
}
35+
36+
if ! go_version_matches; then
37+
echo "Go v${MIN_REQUIRED_GO_VER} or later is required to run code generation"
38+
exit 1
39+
fi
40+
41+
export GOMODCACHE GO111MODULE GOFLAGS GOPATH
42+
43+
# Even when modules are enabled, the code-generator tools always write to
44+
# a traditional GOPATH directory, so fake on up to point to the current
45+
# workspace.
46+
mkdir -p "$GOPATH/src/sigs.k8s.io"
47+
ln -sf "${SCRIPT_ROOT}" "$GOPATH/src/sigs.k8s.io/network-policy-api"
48+
49+
readonly OUTPUT_PKG=sigs.k8s.io/network-policy-api/pkg/client
50+
readonly APIS_PKG=sigs.k8s.io/network-policy-api
51+
readonly API_DIR=${APIS_PKG}/apis/v1alpha1
52+
readonly CLIENTSET_NAME=versioned
53+
readonly CLIENTSET_PKG_NAME=clientset
54+
55+
readonly COMMON_FLAGS="${VERIFY_FLAG:-} --go-header-file ${SCRIPT_ROOT}/hack/boilerplate.generatego.txt"
56+
57+
echo "Generating CRDs"
58+
go run ./pkg/generator
59+
60+
echo "Generating clientset at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}"
61+
go run k8s.io/code-generator/cmd/client-gen \
62+
--clientset-name "${CLIENTSET_NAME}" \
63+
--input-base "" \
64+
--input "${API_DIR}" \
65+
--output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}" \
66+
${COMMON_FLAGS}
67+
68+
echo "Generating listers at ${OUTPUT_PKG}/listers"
69+
go run k8s.io/code-generator/cmd/lister-gen \
70+
--input-dirs "${API_DIR}" \
71+
--output-package "${OUTPUT_PKG}/listers" \
72+
${COMMON_FLAGS}
73+
74+
echo "Generating informers at ${OUTPUT_PKG}/informers"
75+
go run k8s.io/code-generator/cmd/informer-gen \
76+
--input-dirs "${API_DIR}" \
77+
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME}/${CLIENTSET_NAME}" \
78+
--listers-package "${OUTPUT_PKG}/listers" \
79+
--output-package "${OUTPUT_PKG}/informers" \
80+
${COMMON_FLAGS}
81+
82+
for VERSION in v1alpha1 ## Add more versions eventually
83+
do
84+
echo "Generating ${VERSION} register at ${APIS_PKG}/apis/${VERSION}"
85+
go run k8s.io/code-generator/cmd/register-gen \
86+
--input-dirs "${APIS_PKG}/apis/${VERSION}" \
87+
--output-package "${APIS_PKG}/apis/${VERSION}" \
88+
${COMMON_FLAGS}
89+
90+
echo "Generating ${VERSION} deepcopy at ${APIS_PKG}/apis/${VERSION}"
91+
go run sigs.k8s.io/controller-tools/cmd/controller-gen \
92+
object:headerFile=${SCRIPT_ROOT}/hack/boilerplate.generatego.txt \
93+
paths="${APIS_PKG}/apis/${VERSION}"
94+
done

hack/verify-codegen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -o pipefail
2020

2121
SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/..
2222

23-
make -C "$SCRIPT_ROOT" generate-deepcopy generate-typed-clients generate-typed-listers generate-typed-informers
23+
make -C "$SCRIPT_ROOT" generate
2424

2525
if git status -s 2>&1 | grep -E -q '^\s+[MADRCU]'
2626
then

pkg/generator/main.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package main
15+
16+
import (
17+
"fmt"
18+
"log"
19+
"os"
20+
21+
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
22+
"sigs.k8s.io/controller-tools/pkg/crd"
23+
"sigs.k8s.io/controller-tools/pkg/loader"
24+
"sigs.k8s.io/controller-tools/pkg/markers"
25+
"sigs.k8s.io/yaml"
26+
)
27+
28+
const (
29+
bundleVersionAnnotation = "policy.networking.k8s.io/bundle-version"
30+
31+
// These values must be updated during the release process
32+
bundleVersion = ""
33+
approvalLink = ""
34+
)
35+
36+
// This generation code is largely copied from
37+
// github.com/kubernetes-sigs/controller-tools/blob/ab52f76cc7d167925b2d5942f24bf22e30f49a02/pkg/crd/gen.go
38+
func main() {
39+
roots, err := loader.LoadRoots(
40+
"k8s.io/apimachinery/pkg/runtime/schema", // Needed to parse generated register functions.
41+
"sigs.k8s.io/network-policy-api/apis/v1alpha1",
42+
)
43+
if err != nil {
44+
log.Fatalf("failed to load package roots: %s", err)
45+
}
46+
47+
generator := &crd.Generator{}
48+
49+
parser := &crd.Parser{
50+
Collector: &markers.Collector{Registry: &markers.Registry{}},
51+
Checker: &loader.TypeChecker{
52+
NodeFilters: []loader.NodeFilter{generator.CheckFilter()},
53+
},
54+
}
55+
56+
err = generator.RegisterMarkers(parser.Collector.Registry)
57+
if err != nil {
58+
log.Fatalf("failed to register markers: %s", err)
59+
}
60+
61+
crd.AddKnownTypes(parser)
62+
for _, r := range roots {
63+
parser.NeedPackage(r)
64+
}
65+
66+
metav1Pkg := crd.FindMetav1(roots)
67+
if metav1Pkg == nil {
68+
log.Fatalf("no objects in the roots, since nothing imported metav1")
69+
}
70+
71+
kubeKinds := crd.FindKubeKinds(parser, metav1Pkg)
72+
if len(kubeKinds) == 0 {
73+
log.Fatalf("no objects in the roots")
74+
}
75+
76+
for _, groupKind := range kubeKinds {
77+
log.Printf("generating CRD for %v\n", groupKind)
78+
79+
parser.NeedCRDFor(groupKind, nil)
80+
crdRaw := parser.CustomResourceDefinitions[groupKind]
81+
82+
// Inline version of "addAttribution(&crdRaw)" ...
83+
if crdRaw.ObjectMeta.Annotations == nil {
84+
crdRaw.ObjectMeta.Annotations = map[string]string{}
85+
}
86+
crdRaw.ObjectMeta.Annotations[bundleVersionAnnotation] = bundleVersion
87+
crdRaw.ObjectMeta.Annotations[apiext.KubeAPIApprovedAnnotation] = approvalLink
88+
89+
// Prevent the top level metadata for the CRD to be generated regardless of the intention in the arguments
90+
crd.FixTopLevelMetadata(crdRaw)
91+
92+
conv, err := crd.AsVersion(crdRaw, apiext.SchemeGroupVersion)
93+
if err != nil {
94+
log.Fatalf("failed to convert CRD: %s", err)
95+
}
96+
97+
out, err := yaml.Marshal(conv)
98+
if err != nil {
99+
log.Fatalf("failed to marshal CRD: %s", err)
100+
}
101+
102+
fileName := fmt.Sprintf("config/crd/%s_%s.yaml", crdRaw.Spec.Group, crdRaw.Spec.Names.Plural)
103+
err = os.WriteFile(fileName, out, 0o600)
104+
if err != nil {
105+
log.Fatalf("failed to write CRD: %s", err)
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)