Skip to content

Commit 4977dcf

Browse files
Merge pull request #11 from ncdc/cleanups
Cleanups
2 parents 290f195 + aee263f commit 4977dcf

33 files changed

+392
-126
lines changed

.github/workflows/ci.yaml

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v3
1919
with:
20-
path: logicalcluster
20+
path: code-generator
2121
- uses: actions/checkout@v3
2222
with:
2323
repository: kubernetes/repo-infra
@@ -39,6 +39,17 @@ jobs:
3939
- name: Run golangci-lint
4040
run: make lint
4141

42+
test:
43+
name: test
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v3
47+
- uses: actions/setup-go@v3
48+
with:
49+
go-version: v1.17
50+
- name: Run go test
51+
run: make test
52+
4253
verify-codegen:
4354
name: verify-codegen
4455
runs-on: ubuntu-latest

Makefile

+43-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,49 @@ GOLANGCI_LINT_VER := v1.44.2
2828
GOLANGCI_LINT_BIN := golangci-lint
2929
GOLANGCI_LINT := $(GOBIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER)
3030

31+
KUBE_CLIENT_GEN_VER := v0.24.0
32+
KUBE_CLIENT_GEN_BIN := client-gen
33+
KUBE_CLIENT_GEN := $(GOBIN_DIR)/$(KUBE_CLIENT_GEN_BIN)-$(KUBE_CLIENT_GEN_VER)
34+
35+
$(KUBE_CLIENT_GEN):
36+
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) k8s.io/code-generator/cmd/client-gen $(KUBE_CLIENT_GEN_BIN) $(KUBE_CLIENT_GEN_VER)
37+
3138
$(CONTROLLER_GEN):
3239
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
3340

41+
.PHONY: build
42+
build: ## Build the project
43+
mkdir -p bin
44+
go build -o bin
45+
46+
.PHONY: install
47+
install:
48+
go install
49+
3450
.PHONY: codegen
35-
codegen: $(CONTROLLER_GEN)
36-
${CONTROLLER_GEN} object paths=./testdata/pkg/apis/...
51+
codegen: $(CONTROLLER_GEN) $(KUBE_CLIENT_GEN) build
52+
# Generate deepcopy functions
53+
${CONTROLLER_GEN} object paths=./examples/pkg/apis/...
54+
55+
# Generate standard clientset
56+
$(KUBE_CLIENT_GEN) \
57+
--clientset-name versioned \
58+
--go-header-file hack/boilerplate/boilerplate.generatego.txt \
59+
--input-base github.com/kcp-dev/code-generator/examples/pkg/apis \
60+
--input example/v1 \
61+
--output-base . \
62+
--output-package github.com/kcp-dev/code-generator/examples/pkg/generated/clientset \
63+
--trim-path-prefix github.com/kcp-dev/code-generator
64+
65+
# Generate cluster clientset
66+
bin/code-generator \
67+
client \
68+
--clientset-name clusterclient \
69+
--go-header-file hack/boilerplate/boilerplate.generatego.txt \
70+
--clientset-api-path github.com/kcp-dev/code-generator/examples/pkg/generated/clientset/versioned \
71+
--input-dir ./examples/pkg/apis \
72+
--output-dir ./examples/pkg \
73+
--group-versions example:v1
3774

3875
$(GOLANGCI_LINT):
3976
GOBIN=$(GOBIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER)
@@ -42,6 +79,10 @@ $(GOLANGCI_LINT):
4279
lint: $(GOLANGCI_LINT)
4380
$(GOLANGCI_LINT) run --timeout=10m ./...
4481

82+
.PHONY: test
83+
test:
84+
go test ./...
85+
4586
# Note, running this locally if you have any modified files, even those that are not generated,
4687
# will result in an error. This target is mostly for CI jobs.
4788
.PHONY: verify-codegen
File renamed without changes.

examples/pkg/apis/example/register.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2021 The KCP 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+
*/
16+
17+
package example
18+
19+
const (
20+
GroupName = "example.dev"
21+
)
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2021 The KCP 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+
*/
16+
17+
package v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
24+
"github.com/kcp-dev/code-generator/examples/pkg/apis/example"
25+
)
26+
27+
// SchemeGroupVersion is group version used to register these objects
28+
var SchemeGroupVersion = schema.GroupVersion{Group: example.GroupName, Version: "v1alpha1"}
29+
30+
// Kind takes an unqualified kind and returns back a Group qualified GroupKind
31+
func Kind(kind string) schema.GroupKind {
32+
return SchemeGroupVersion.WithKind(kind).GroupKind()
33+
}
34+
35+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
36+
func Resource(resource string) schema.GroupResource {
37+
return SchemeGroupVersion.WithResource(resource).GroupResource()
38+
}
39+
40+
var (
41+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
42+
AddToScheme = SchemeBuilder.AddToScheme
43+
)
44+
45+
// Adds the list of known types to Scheme.
46+
func addKnownTypes(scheme *runtime.Scheme) error {
47+
scheme.AddKnownTypes(SchemeGroupVersion,
48+
&TestType{},
49+
&TestTypeList{},
50+
&ClusterTestType{},
51+
&ClusterTestTypeList{},
52+
)
53+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
54+
return nil
55+
}

testdata/pkg/apis/example/v1/types.go renamed to examples/pkg/apis/example/v1/types.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@ import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020

2121
// +genclient
2222
// TestType is a top-level type. A client is created for it.
23+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
2324
type TestType struct {
25+
metav1.TypeMeta `json:",inline"`
26+
// +optional
27+
metav1.ObjectMeta `json:"metadata,omitempty"`
2428
// +optional
2529
APIGroups []string `json:"apiGroups,omitempty"`
2630
}
2731

2832
// TestTypeList is a top-level list type. The client methods for lists are automatically created.
2933
// You are not supposed to create a separated client for this one.
34+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
3035
type TestTypeList struct {
31-
Items []TestType `json:"items"`
36+
metav1.TypeMeta `json:",inline"`
37+
metav1.ListMeta `json:"metadata"`
38+
Items []TestType `json:"items"`
3239
}
3340

3441
// +genclient

testdata/pkg/apis/example/v1/zz_generated.deepcopy.go renamed to examples/pkg/apis/example/v1/zz_generated.deepcopy.go

+21-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/pkg/clusterclient/clientset.go renamed to examples/pkg/clusterclient/clientset.go

+16-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)