Skip to content

Commit 958ac27

Browse files
authored
Merge pull request kubernetes-sigs#2500 from rmb938/groupmanifestnames
🐛 fix issue to scaffold same Kind for different groups with multi-group support by using group name in crd/patches and rbac editor/viewer manifests
2 parents da74a51 + 02afb69 commit 958ac27

File tree

85 files changed

+2082
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2082
-5
lines changed

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches/enablecainjection_patch.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ var _ machinery.Template = &EnableCAInjectionPatch{}
2727
// EnableCAInjectionPatch scaffolds a file that defines the patch that injects CA into the CRD
2828
type EnableCAInjectionPatch struct {
2929
machinery.TemplateMixin
30+
machinery.MultiGroupMixin
3031
machinery.ResourceMixin
3132
}
3233

3334
// SetTemplateDefaults implements file.Template
3435
func (f *EnableCAInjectionPatch) SetTemplateDefaults() error {
3536
if f.Path == "" {
36-
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[plural].yaml")
37+
if f.MultiGroup {
38+
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[group]_%[plural].yaml")
39+
} else {
40+
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[plural].yaml")
41+
}
3742
}
3843
f.Path = f.Resource.Replacer().Replace(f.Path)
3944

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches/enablewebhook_patch.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ var _ machinery.Template = &EnableWebhookPatch{}
2727
// EnableWebhookPatch scaffolds a file that defines the patch that enables conversion webhook for the CRD
2828
type EnableWebhookPatch struct {
2929
machinery.TemplateMixin
30+
machinery.MultiGroupMixin
3031
machinery.ResourceMixin
3132
}
3233

3334
// SetTemplateDefaults implements file.Template
3435
func (f *EnableWebhookPatch) SetTemplateDefaults() error {
3536
if f.Path == "" {
36-
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[plural].yaml")
37+
if f.MultiGroup {
38+
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[group]_%[plural].yaml")
39+
} else {
40+
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[plural].yaml")
41+
}
42+
3743
}
3844
f.Path = f.Resource.Replacer().Replace(f.Path)
3945

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac/crd_editor_role.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ var _ machinery.Template = &CRDEditorRole{}
2727
// CRDEditorRole scaffolds a file that defines the role that allows to edit plurals
2828
type CRDEditorRole struct {
2929
machinery.TemplateMixin
30+
machinery.MultiGroupMixin
3031
machinery.ResourceMixin
3132
}
3233

3334
// SetTemplateDefaults implements file.Template
3435
func (f *CRDEditorRole) SetTemplateDefaults() error {
3536
if f.Path == "" {
36-
f.Path = filepath.Join("config", "rbac", "%[kind]_editor_role.yaml")
37+
if f.MultiGroup {
38+
f.Path = filepath.Join("config", "rbac", "%[group]_%[kind]_editor_role.yaml")
39+
} else {
40+
f.Path = filepath.Join("config", "rbac", "%[kind]_editor_role.yaml")
41+
}
42+
3743
}
3844
f.Path = f.Resource.Replacer().Replace(f.Path)
3945

pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac/crd_viewer_role.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ var _ machinery.Template = &CRDViewerRole{}
2727
// CRDViewerRole scaffolds a file that defines the role that allows to view plurals
2828
type CRDViewerRole struct {
2929
machinery.TemplateMixin
30+
machinery.MultiGroupMixin
3031
machinery.ResourceMixin
3132
}
3233

3334
// SetTemplateDefaults implements file.Template
3435
func (f *CRDViewerRole) SetTemplateDefaults() error {
3536
if f.Path == "" {
36-
f.Path = filepath.Join("config", "rbac", "%[kind]_viewer_role.yaml")
37+
if f.MultiGroup {
38+
f.Path = filepath.Join("config", "rbac", "%[group]_%[kind]_viewer_role.yaml")
39+
} else {
40+
f.Path = filepath.Join("config", "rbac", "%[kind]_viewer_role.yaml")
41+
}
42+
3743
}
3844
f.Path = f.Resource.Replacer().Replace(f.Path)
3945

test/testdata/generate.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ function scaffold_test_project {
9494
$kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false
9595

9696
$kb create api --group apps --version v1 --kind Deployment --controller=true --resource=false --make=false
97+
98+
$kb create api --group foo --version v1 --kind Bar --controller=true --resource=true --make=false
99+
$kb create api --group fiz --version v1 --kind Bar --controller=true --resource=true --make=false
97100

98101
if [ $project == "project-v3-multigroup" ]; then
99102
$kb create api --version v1 --kind Lakers --controller=true --resource=true --make=false
@@ -127,4 +130,4 @@ scaffold_test_project project-v3
127130
scaffold_test_project project-v3-multigroup
128131
scaffold_test_project project-v3-addon --plugins="go/v3,declarative"
129132
scaffold_test_project project-v3-config --component-config
130-
scaffold_test_project project-v3-v1beta1
133+
scaffold_test_project project-v3-v1beta1

testdata/project-v2-multigroup/PROJECT

+6
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ resources:
2323
- group: foo.policy
2424
kind: HealthCheckPolicy
2525
version: v1
26+
- group: foo
27+
kind: Bar
28+
version: v1
29+
- group: fiz
30+
kind: Bar
31+
version: v1
2632
version: "2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2022 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+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
// BarSpec defines the desired state of Bar
27+
type BarSpec struct {
28+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29+
// Important: Run "make" to regenerate code after modifying this file
30+
31+
// Foo is an example field of Bar. Edit bar_types.go to remove/update
32+
Foo string `json:"foo,omitempty"`
33+
}
34+
35+
// BarStatus defines the observed state of Bar
36+
type BarStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
// Important: Run "make" to regenerate code after modifying this file
39+
}
40+
41+
//+kubebuilder:object:root=true
42+
//+kubebuilder:subresource:status
43+
44+
// Bar is the Schema for the bars API
45+
type Bar struct {
46+
metav1.TypeMeta `json:",inline"`
47+
metav1.ObjectMeta `json:"metadata,omitempty"`
48+
49+
Spec BarSpec `json:"spec,omitempty"`
50+
Status BarStatus `json:"status,omitempty"`
51+
}
52+
53+
//+kubebuilder:object:root=true
54+
55+
// BarList contains a list of Bar
56+
type BarList struct {
57+
metav1.TypeMeta `json:",inline"`
58+
metav1.ListMeta `json:"metadata,omitempty"`
59+
Items []Bar `json:"items"`
60+
}
61+
62+
func init() {
63+
SchemeBuilder.Register(&Bar{}, &BarList{})
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022 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 contains API Schema definitions for the fiz v1 API group
18+
//+kubebuilder:object:generate=true
19+
//+groupName=fiz.testproject.org
20+
package v1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "fiz.testproject.org", Version: "v1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

testdata/project-v2-multigroup/apis/fiz/v1/zz_generated.deepcopy.go

+115
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2022 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+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
// BarSpec defines the desired state of Bar
27+
type BarSpec struct {
28+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29+
// Important: Run "make" to regenerate code after modifying this file
30+
31+
// Foo is an example field of Bar. Edit bar_types.go to remove/update
32+
Foo string `json:"foo,omitempty"`
33+
}
34+
35+
// BarStatus defines the observed state of Bar
36+
type BarStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
// Important: Run "make" to regenerate code after modifying this file
39+
}
40+
41+
//+kubebuilder:object:root=true
42+
//+kubebuilder:subresource:status
43+
44+
// Bar is the Schema for the bars API
45+
type Bar struct {
46+
metav1.TypeMeta `json:",inline"`
47+
metav1.ObjectMeta `json:"metadata,omitempty"`
48+
49+
Spec BarSpec `json:"spec,omitempty"`
50+
Status BarStatus `json:"status,omitempty"`
51+
}
52+
53+
//+kubebuilder:object:root=true
54+
55+
// BarList contains a list of Bar
56+
type BarList struct {
57+
metav1.TypeMeta `json:",inline"`
58+
metav1.ListMeta `json:"metadata,omitempty"`
59+
Items []Bar `json:"items"`
60+
}
61+
62+
func init() {
63+
SchemeBuilder.Register(&Bar{}, &BarList{})
64+
}

0 commit comments

Comments
 (0)