Skip to content

Commit be2cf7e

Browse files
committed
code-generator/client-gen: add example with single package api/v1
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent 6b2f779 commit be2cf7e

36 files changed

+3231
-0
lines changed

staging/src/k8s.io/code-generator/examples/hack/update-codegen.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ kube::codegen::gen_client \
6060
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
6161
"${SCRIPT_ROOT}/crd/apis"
6262

63+
kube::codegen::gen_client \
64+
--with-watch \
65+
--with-applyconfig \
66+
--output-dir "${SCRIPT_ROOT}/single" \
67+
--output-pkg "${THIS_PKG}/single" \
68+
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
69+
--one-input-api "api" \
70+
"${SCRIPT_ROOT}/single"
71+
6372
kube::codegen::gen_client \
6473
--with-watch \
6574
--with-applyconfig \

staging/src/k8s.io/code-generator/examples/hack/verify-codegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fi
4747
echo "Smoke testing examples by compiling..."
4848
pushd "${SCRIPT_ROOT}"
4949
go build "k8s.io/code-generator/examples/crd/..."
50+
go build "k8s.io/code-generator/examples/single/..."
5051
go build "k8s.io/code-generator/examples/apiserver/..."
5152
go build "k8s.io/code-generator/examples/MixedCase/..."
5253
go build "k8s.io/code-generator/examples/HyphenGroup/..."
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2016 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+
*/
16+
17+
// +k8s:deepcopy-gen=package
18+
// +k8s:defaulter-gen=TypeMeta
19+
// +groupName=example.crd.code-generator.k8s.io
20+
21+
package v1 // import "k8s.io/code-generator/examples/crd/apis/example/v1"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2015 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+
*/
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+
25+
var SchemeGroupVersion = schema.GroupVersion{Group: "example.crd.code-generator.k8s.io", Version: "v1"}
26+
27+
var (
28+
// TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api.
29+
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
30+
SchemeBuilder runtime.SchemeBuilder
31+
localSchemeBuilder = &SchemeBuilder
32+
AddToScheme = localSchemeBuilder.AddToScheme
33+
)
34+
35+
func init() {
36+
// We only register manually written functions here. The registration of the
37+
// generated functions takes place in the generated files. The separation
38+
// makes the code compile even when the generated files are missing.
39+
localSchemeBuilder.Register(addKnownTypes)
40+
}
41+
42+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
43+
func Resource(resource string) schema.GroupResource {
44+
return SchemeGroupVersion.WithResource(resource).GroupResource()
45+
}
46+
47+
// Adds the list of known types to the given scheme.
48+
func addKnownTypes(scheme *runtime.Scheme) error {
49+
scheme.AddKnownTypes(SchemeGroupVersion,
50+
&TestType{},
51+
&TestTypeList{},
52+
)
53+
54+
scheme.AddKnownTypes(SchemeGroupVersion,
55+
&metav1.Status{},
56+
)
57+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
58+
return nil
59+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2015 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+
*/
16+
17+
package v1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
// +genclient
22+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
23+
24+
// TestType is a top-level type. A client is created for it.
25+
type TestType struct {
26+
metav1.TypeMeta `json:",inline"`
27+
// +optional
28+
metav1.ObjectMeta `json:"metadata,omitempty"`
29+
// +optional
30+
Status TestTypeStatus `json:"status,omitempty"`
31+
}
32+
33+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
34+
35+
// TestTypeList is a top-level list type. The client methods for lists are automatically created.
36+
// You are not supposed to create a separated client for this one.
37+
type TestTypeList struct {
38+
metav1.TypeMeta `json:",inline"`
39+
// +optional
40+
metav1.ListMeta `json:"metadata,omitempty"`
41+
42+
Items []TestType `json:"items"`
43+
}
44+
45+
type TestTypeStatus struct {
46+
Blah string
47+
}
48+
49+
// +genclient:nonNamespaced
50+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
51+
52+
type ClusterTestTypeList struct {
53+
metav1.TypeMeta
54+
metav1.ListMeta
55+
Items []ClusterTestType
56+
}
57+
58+
// +genclient
59+
// +genclient:nonNamespaced
60+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
61+
// +genclient:method=GetScale,verb=get,subresource=scale,result=k8s.io/api/autoscaling/v1.Scale
62+
// +genclient:method=UpdateScale,verb=update,subresource=scale,input=k8s.io/api/autoscaling/v1.Scale,result=k8s.io/api/autoscaling/v1.Scale
63+
64+
type ClusterTestType struct {
65+
metav1.TypeMeta `json:",inline"`
66+
// +optional
67+
metav1.ObjectMeta `json:"metadata,omitempty"`
68+
// +optional
69+
Status ClusterTestTypeStatus `json:"status,omitempty"`
70+
}
71+
72+
type ClusterTestTypeStatus struct {
73+
Blah string
74+
}

staging/src/k8s.io/code-generator/examples/single/api/v1/zz_generated.deepcopy.go

Lines changed: 178 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/code-generator/examples/single/api/v1/zz_generated.defaults.go

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)