Skip to content

Commit 4c213d0

Browse files
committed
wire in generators
1 parent 90e42b7 commit 4c213d0

17 files changed

+660
-409
lines changed

examples/kcp/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11

2+
GO_INSTALL = ./hack/go-install.sh
3+
24
LOCALBIN ?= $(shell pwd)/bin
5+
ARTIFACT_DIR ?= .test
6+
7+
KCP_APIGEN_VER := v0.23.0
8+
KCP_APIGEN_BIN := apigen
9+
KCP_APIGEN_GEN := $(LOCALBIN)/$(KCP_APIGEN_BIN)-$(KCP_APIGEN_VER)
10+
export KCP_APIGEN_GEN # so hack scripts can use it
11+
12+
CONTROLLER_GEN_VER := v0.14.0
13+
CONTROLLER_GEN_BIN := controller-gen
14+
CONTROLLER_GEN := $(LOCALBIN)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
15+
export CONTROLLER_GEN # so hack scripts can use it
16+
317
KCP ?= $(LOCALBIN)/kcp
18+
419
KCP_VERSION ?= 0.23.0
520

621
OS ?= $(shell go env GOOS )
@@ -11,3 +26,30 @@ $(KCP): ## Download kcp locally if necessary.
1126
curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION)/kcp_$(KCP_VERSION)_$(OS)_$(ARCH).tar.gz | tar --directory $(LOCALBIN)/../ -xvzf - bin/kcp
1227
touch $(KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir
1328

29+
$(KCP_APIGEN_GEN):
30+
GOBIN=$(LOCALBIN) $(GO_INSTALL) github.com/kcp-dev/kcp/sdk/cmd/apigen $(KCP_APIGEN_BIN) $(KCP_APIGEN_VER)
31+
32+
$(CONTROLLER_GEN):
33+
GOBIN=$(LOCALBIN) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
34+
35+
build: $(KCP) $(KCP_APIGEN_GEN) $(CONTROLLER_GEN)
36+
37+
.PHONY: kcp-server
38+
kcp-server: $(KCP) $(ARTIFACT_DIR)/kcp ## Run the kcp server.
39+
@if [[ ! -s $(ARTIFACT_DIR)/kcp.log ]]; then ( $(KCP) start -v 5 --root-directory $(ARTIFACT_DIR)/kcp --kubeconfig-path $(ARTIFACT_DIR)/kcp.kubeconfig --audit-log-maxsize 1024 --audit-log-mode=batch --audit-log-batch-max-wait=1s --audit-log-batch-max-size=1000 --audit-log-batch-buffer-size=10000 --audit-log-batch-throttle-burst=15 --audit-log-batch-throttle-enable=true --audit-log-batch-throttle-qps=10 --audit-policy-file ./test/e2e/audit-policy.yaml --audit-log-path $(ARTIFACT_DIR)/audit.log >$(ARTIFACT_DIR)/kcp.log 2>&1 & ); fi
40+
@while true; do if [[ ! -s $(ARTIFACT_DIR)/kcp.kubeconfig ]]; then sleep 0.2; else break; fi; done
41+
@while true; do if ! kubectl --kubeconfig $(ARTIFACT_DIR)/kcp.kubeconfig get --raw /readyz >$(ARTIFACT_DIR)/kcp.probe.log 2>&1; then sleep 0.2; else break; fi; done
42+
43+
.PHONY: test-e2e-cleanup
44+
test-cleanup: ## Clean up processes and directories from an end-to-end test run.
45+
rm -rf $(ARTIFACT_DIR) || true
46+
pkill -sigterm kcp || true
47+
pkill -sigterm kubectl || true
48+
49+
ARTIFACT_DIR ?= .test
50+
51+
$(ARTIFACT_DIR)/kcp: ## Create a directory for the kcp server data.
52+
mkdir -p $(ARTIFACT_DIR)/kcp
53+
54+
generate: build
55+
./hack/update-codegen-crds.sh
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.14.0
7+
name: widgets.data.my.domain
8+
spec:
9+
group: data.my.domain
10+
names:
11+
kind: Widget
12+
listKind: WidgetList
13+
plural: widgets
14+
singular: widget
15+
scope: Namespaced
16+
versions:
17+
- name: v1alpha1
18+
schema:
19+
openAPIV3Schema:
20+
description: Widget is the Schema for the widgets API
21+
properties:
22+
apiVersion:
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
28+
type: string
29+
kind:
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
36+
type: string
37+
metadata:
38+
type: object
39+
spec:
40+
description: WidgetSpec defines the desired state of Widget
41+
properties:
42+
foo:
43+
type: string
44+
type: object
45+
status:
46+
description: WidgetStatus defines the observed state of Widget
47+
properties:
48+
total:
49+
type: integer
50+
type: object
51+
type: object
52+
served: true
53+
storage: true
54+
subresources:
55+
status: {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: apis.kcp.io/v1alpha1
2+
kind: APIExport
3+
metadata:
4+
creationTimestamp: null
5+
name: data.my.domain
6+
spec:
7+
latestResourceSchemas:
8+
- v240406-90e42b7b.widgets.data.my.domain
9+
status: {}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
apiVersion: apis.kcp.io/v1alpha1
2+
kind: APIResourceSchema
3+
metadata:
4+
creationTimestamp: null
5+
name: v240406-90e42b7b.widgets.data.my.domain
6+
spec:
7+
group: data.my.domain
8+
names:
9+
kind: Widget
10+
listKind: WidgetList
11+
plural: widgets
12+
singular: widget
13+
scope: Namespaced
14+
versions:
15+
- name: v1alpha1
16+
schema:
17+
description: Widget is the Schema for the widgets API
18+
properties:
19+
apiVersion:
20+
description: |-
21+
APIVersion defines the versioned schema of this representation of an object.
22+
Servers should convert recognized schemas to the latest internal value, and
23+
may reject unrecognized values.
24+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
25+
type: string
26+
kind:
27+
description: |-
28+
Kind is a string value representing the REST resource this object represents.
29+
Servers may infer this from the endpoint the client submits requests to.
30+
Cannot be updated.
31+
In CamelCase.
32+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
description: WidgetSpec defines the desired state of Widget
38+
properties:
39+
foo:
40+
type: string
41+
type: object
42+
status:
43+
description: WidgetStatus defines the observed state of Widget
44+
properties:
45+
total:
46+
type: integer
47+
type: object
48+
type: object
49+
served: true
50+
storage: true
51+
subresources:
52+
status: {}

examples/kcp/controllers/widget_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
"sigs.k8s.io/controller-runtime/pkg/log"
2828

29-
datav1alpha1 "github.com/kcp-dev/controller-runtime/examples/kcp/api/v1alpha1"
29+
datav1alpha1 "github.com/kcp-dev/controller-runtime/examples/kcp/apis/v1alpha1"
3030
)
3131

3232
// WidgetReconciler reconciles a Widget object

examples/kcp/hack/go-install.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2024 The KCP 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+
# Originally copied from
18+
# https://github.com/kubernetes-sigs/cluster-api-provider-gcp/blob/c26a68b23e9317323d5d37660fe9d29b3d2ff40c/scripts/go_install.sh
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
if [[ -z "${1:-}" ]]; then
25+
echo "must provide module as first parameter"
26+
exit 1
27+
fi
28+
29+
if [[ -z "${2:-}" ]]; then
30+
echo "must provide binary name as second parameter"
31+
exit 1
32+
fi
33+
34+
if [[ -z "${3:-}" ]]; then
35+
echo "must provide version as third parameter"
36+
exit 1
37+
fi
38+
39+
if [[ -z "${GOBIN:-}" ]]; then
40+
echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory."
41+
exit 1
42+
fi
43+
44+
mkdir -p "${GOBIN}"
45+
46+
tmp_dir=$(mktemp -d -t goinstall_XXXXXXXXXX)
47+
function clean {
48+
rm -rf "${tmp_dir}"
49+
}
50+
trap clean EXIT
51+
52+
rm "${GOBIN}/${2}"* > /dev/null 2>&1 || true
53+
54+
cd "${tmp_dir}"
55+
56+
# create a new module in the tmp directory
57+
go mod init fake/mod
58+
59+
# install the golang module specified as the first argument
60+
go install -tags kcptools "${1}@${3}"
61+
mv "${GOBIN}/${2}" "${GOBIN}/${2}-${3}"
62+
ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2024 The KCP 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+
set -o xtrace
21+
22+
if [[ -z "${CONTROLLER_GEN:-}" ]]; then
23+
echo "You must either set CONTROLLER_GEN to the path to controller-gen or invoke via make"
24+
exit 1
25+
fi
26+
27+
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
28+
29+
# Update generated CRD YAML
30+
(
31+
cd "${REPO_ROOT}/apis"
32+
"${CONTROLLER_GEN}" \
33+
crd \
34+
rbac:roleName=manager-role \
35+
webhook \
36+
paths="./..." \
37+
output:crd:artifacts:config="${REPO_ROOT}"/config/crds
38+
)
39+
40+
for CRD in "${REPO_ROOT}"/config/crds/*.yaml; do
41+
if [ -f "${CRD}-patch" ]; then
42+
echo "Applying ${CRD}"
43+
${YAML_PATCH} -o "${CRD}-patch" < "${CRD}" > "${CRD}.patched"
44+
mv "${CRD}.patched" "${CRD}"
45+
fi
46+
done
47+
48+
(
49+
${KCP_APIGEN_GEN} --input-dir "${REPO_ROOT}"/config/crds --output-dir "${REPO_ROOT}"/config/widgets/resources
50+
)

examples/kcp/test/e2e/apibinding.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apis.kcp.dev/v1alpha1
2+
kind: APIBinding
3+
metadata:
4+
name: data.my.domain
5+
spec:
6+
reference:
7+
workspace:
8+
path: WORKSPACE
9+
exportName: data.my.domain
10+
acceptedPermissionClaims:
11+
- resource: "secrets"
12+
- resource: "configmaps"
13+
- resource: "namespaces"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: audit.k8s.io/v1
2+
kind: Policy
3+
omitStages:
4+
- RequestReceived
5+
omitManagedFields: true
6+
rules:
7+
- level: None
8+
nonResourceURLs:
9+
- "/api*"
10+
- "/version"
11+
12+
- level: Metadata
13+
resources:
14+
- group: ""
15+
resources: ["secrets", "configmaps"]
16+
- group: "authorization.k8s.io"
17+
resources: ["subjectaccessreviews"]
18+
19+
- level: Metadata
20+
verbs: ["list", "watch"]
21+
22+
- level: Metadata
23+
verbs: ["get", "delete"]
24+
omitStages:
25+
- ResponseStarted
26+
27+
- level: RequestResponse
28+
verbs: ["create", "update", "patch"]
29+
omitStages:
30+
- ResponseStarted

0 commit comments

Comments
 (0)