Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 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 #2500

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ var _ machinery.Template = &EnableCAInjectionPatch{}
// EnableCAInjectionPatch scaffolds a file that defines the patch that injects CA into the CRD
type EnableCAInjectionPatch struct {
machinery.TemplateMixin
machinery.MultiGroupMixin
machinery.ResourceMixin
}

// SetTemplateDefaults implements file.Template
func (f *EnableCAInjectionPatch) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[plural].yaml")
if f.MultiGroup {
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[group]_%[plural].yaml")
} else {
f.Path = filepath.Join("config", "crd", "patches", "cainjection_in_%[plural].yaml")
}
}
f.Path = f.Resource.Replacer().Replace(f.Path)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ var _ machinery.Template = &EnableWebhookPatch{}
// EnableWebhookPatch scaffolds a file that defines the patch that enables conversion webhook for the CRD
type EnableWebhookPatch struct {
machinery.TemplateMixin
machinery.MultiGroupMixin
machinery.ResourceMixin
}

// SetTemplateDefaults implements file.Template
func (f *EnableWebhookPatch) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[plural].yaml")
if f.MultiGroup {
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[group]_%[plural].yaml")
} else {
f.Path = filepath.Join("config", "crd", "patches", "webhook_in_%[plural].yaml")
}

}
f.Path = f.Resource.Replacer().Replace(f.Path)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ var _ machinery.Template = &CRDEditorRole{}
// CRDEditorRole scaffolds a file that defines the role that allows to edit plurals
type CRDEditorRole struct {
machinery.TemplateMixin
machinery.MultiGroupMixin
machinery.ResourceMixin
}

// SetTemplateDefaults implements file.Template
func (f *CRDEditorRole) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "rbac", "%[kind]_editor_role.yaml")
if f.MultiGroup {
f.Path = filepath.Join("config", "rbac", "%[group]_%[kind]_editor_role.yaml")
} else {
f.Path = filepath.Join("config", "rbac", "%[kind]_editor_role.yaml")
}

}
f.Path = f.Resource.Replacer().Replace(f.Path)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ var _ machinery.Template = &CRDViewerRole{}
// CRDViewerRole scaffolds a file that defines the role that allows to view plurals
type CRDViewerRole struct {
machinery.TemplateMixin
machinery.MultiGroupMixin
machinery.ResourceMixin
}

// SetTemplateDefaults implements file.Template
func (f *CRDViewerRole) SetTemplateDefaults() error {
if f.Path == "" {
f.Path = filepath.Join("config", "rbac", "%[kind]_viewer_role.yaml")
if f.MultiGroup {
f.Path = filepath.Join("config", "rbac", "%[group]_%[kind]_viewer_role.yaml")
} else {
f.Path = filepath.Join("config", "rbac", "%[kind]_viewer_role.yaml")
}

}
f.Path = f.Resource.Replacer().Replace(f.Path)

Expand Down
5 changes: 4 additions & 1 deletion test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function scaffold_test_project {
$kb create api --group foo.policy --version v1 --kind HealthCheckPolicy --controller=true --resource=true --make=false

$kb create api --group apps --version v1 --kind Deployment --controller=true --resource=false --make=false

$kb create api --group foo --version v1 --kind Bar --controller=true --resource=true --make=false
$kb create api --group fiz --version v1 --kind Bar --controller=true --resource=true --make=false

if [ $project == "project-v3-multigroup" ]; then
$kb create api --version v1 --kind Lakers --controller=true --resource=true --make=false
Expand Down Expand Up @@ -127,4 +130,4 @@ scaffold_test_project project-v3
scaffold_test_project project-v3-multigroup
scaffold_test_project project-v3-addon --plugins="go/v3,declarative"
scaffold_test_project project-v3-config --component-config
scaffold_test_project project-v3-v1beta1
scaffold_test_project project-v3-v1beta1
6 changes: 6 additions & 0 deletions testdata/project-v2-multigroup/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ resources:
- group: foo.policy
kind: HealthCheckPolicy
version: v1
- group: foo
kind: Bar
version: v1
- group: fiz
kind: Bar
version: v1
version: "2"
64 changes: 64 additions & 0 deletions testdata/project-v2-multigroup/apis/fiz/v1/bar_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2022 The Kubernetes authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// BarSpec defines the desired state of Bar
type BarSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Bar. Edit bar_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// BarStatus defines the observed state of Bar
type BarStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Bar is the Schema for the bars API
type Bar struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec BarSpec `json:"spec,omitempty"`
Status BarStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// BarList contains a list of Bar
type BarList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Bar `json:"items"`
}

func init() {
SchemeBuilder.Register(&Bar{}, &BarList{})
}
36 changes: 36 additions & 0 deletions testdata/project-v2-multigroup/apis/fiz/v1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2022 The Kubernetes authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1 contains API Schema definitions for the fiz v1 API group
//+kubebuilder:object:generate=true
//+groupName=fiz.testproject.org
package v1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "fiz.testproject.org", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
115 changes: 115 additions & 0 deletions testdata/project-v2-multigroup/apis/fiz/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions testdata/project-v2-multigroup/apis/foo/v1/bar_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2022 The Kubernetes authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// BarSpec defines the desired state of Bar
type BarSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Bar. Edit bar_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// BarStatus defines the observed state of Bar
type BarStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Bar is the Schema for the bars API
type Bar struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec BarSpec `json:"spec,omitempty"`
Status BarStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// BarList contains a list of Bar
type BarList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Bar `json:"items"`
}

func init() {
SchemeBuilder.Register(&Bar{}, &BarList{})
}
Loading