Skip to content

Commit 7e52d34

Browse files
committed
code-generator/client-gen: add example with core group
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent 29defc1 commit 7e52d34

31 files changed

+1646
-2
lines changed

api/api-rules/codegen_violation_exceptions.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,StatusCause
77
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,Time,Time
88
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentEncoding
99
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentType
10+
API rule violation: names_match,k8s.io/code-generator/examples/apiserver/apis/core/v1,TestTypeStatus,Blah
1011
API rule violation: names_match,k8s.io/code-generator/examples/apiserver/apis/example/v1,TestTypeStatus,Blah
1112
API rule violation: names_match,k8s.io/code-generator/examples/apiserver/apis/example2/v1,TestTypeStatus,Blah
1213
API rule violation: names_match,k8s.io/code-generator/examples/apiserver/apis/example3.io/v1,TestTypeStatus,Blah
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2024 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+
19+
package core // import "k8s.io/code-generator/examples/apiserver/apis/core"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2024 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 install installs the experimental API group, making it available as
18+
// an option to all of the API encoding/decoding machinery.
19+
package install
20+
21+
import (
22+
"k8s.io/apimachinery/pkg/runtime"
23+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
24+
"k8s.io/code-generator/examples/apiserver/apis/core"
25+
"k8s.io/code-generator/examples/apiserver/apis/core/v1"
26+
)
27+
28+
// Install registers the API group and adds types to a scheme
29+
func Install(scheme *runtime.Scheme) {
30+
utilruntime.Must(core.AddToScheme(scheme))
31+
utilruntime.Must(v1.AddToScheme(scheme))
32+
utilruntime.Must(scheme.SetVersionPriority(v1.SchemeGroupVersion))
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2024 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 core
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
"k8s.io/apimachinery/pkg/runtime/schema"
22+
)
23+
24+
var SchemeGroupVersion = schema.GroupVersion{Version: runtime.APIVersionInternal}
25+
26+
var (
27+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
28+
AddToScheme = SchemeBuilder.AddToScheme
29+
)
30+
31+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
32+
func Resource(resource string) schema.GroupResource {
33+
return SchemeGroupVersion.WithResource(resource).GroupResource()
34+
}
35+
36+
// Adds the list of known types to the given scheme.
37+
func addKnownTypes(scheme *runtime.Scheme) error {
38+
scheme.AddKnownTypes(SchemeGroupVersion,
39+
&TestType{},
40+
&TestTypeList{},
41+
)
42+
43+
scheme.AddKnownTypes(SchemeGroupVersion)
44+
return nil
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2024 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 core
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
27+
metav1.ObjectMeta
28+
Status TestTypeStatus
29+
}
30+
31+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
32+
33+
// TestTypeList is a top-level list type. The client methods for lists are automatically created.
34+
// You are not supposed to create a separated client for this one.
35+
type TestTypeList struct {
36+
metav1.TypeMeta
37+
metav1.ListMeta
38+
39+
Items []TestType
40+
}
41+
42+
type TestTypeStatus struct {
43+
Blah string
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2024 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:openapi-gen=true
18+
// +k8s:deepcopy-gen=package
19+
// +k8s:defaulter-gen=TypeMeta
20+
// +k8s:conversion-gen=k8s.io/code-generator/examples/apiserver/apis/core
21+
22+
package v1 // import "k8s.io/code-generator/examples/apiserver/apis/core/v1"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2024 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.apiserver.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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2024 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+
}

0 commit comments

Comments
 (0)