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

crd: add finalizers support in CRD generation #82

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/crd/generator/testData/config/crds/fun_v1alpha1_toy.yaml
Expand Up @@ -2,6 +2,9 @@ apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
finalizers:
- f1.example.com
- f2.example.com
labels:
controller-tools.k8s.io: "1.0"
name: toys.fun.myk8s.io
Expand Down
Expand Up @@ -67,6 +67,7 @@ type ToyStatus struct {
// +k8s:openapi-gen=true
// +kubebuilder:subresource:status
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=
// +kubebuilder:finalizers=f1.example.com, f2.example.com
type Toy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
22 changes: 20 additions & 2 deletions pkg/internal/codegen/parse/crd.go
Expand Up @@ -80,12 +80,17 @@ func (b *APIs) parseCRDs() {
}

if hasCategories(resource.Type) {
categoriesTag := getCategoriesTag(resource.Type)
categories := strings.Split(categoriesTag, ",")
categories := splitNTrim(getCategoriesTag(resource.Type), ",")
resource.CRD.Spec.Names.Categories = categories
resource.Categories = categories
}

finalizers := splitNTrim(getFinalizersTag(resource.Type), ",")
if len(finalizers) > 0 {
resource.CRD.ObjectMeta.Finalizers = finalizers
resource.Finalizers = finalizers
}

if hasStatusSubresource(resource.Type) {
if resource.CRD.Spec.Subresources == nil {
resource.CRD.Spec.Subresources = &v1beta1.CustomResourceSubresources{}
Expand Down Expand Up @@ -570,3 +575,16 @@ func (b *APIs) getMembers(t *types.Type, found sets.String) (map[string]v1beta1.
defer found.Delete(t.Name.String())
return members, result, required
}

// splitNTrim slices given string s in to all substrings separated by sep and
// returns a slice of substrings by trimming space and removing empty substrings.
func splitNTrim(s, sep string) (result []string) {
l := strings.Split(s, sep)
for _, elem := range l {
elem = strings.TrimSpace(elem)
if len(elem) > 0 {
result = append(result, elem)
}
}
return
}
9 changes: 6 additions & 3 deletions pkg/internal/codegen/parse/util.go
Expand Up @@ -295,6 +295,12 @@ func getCategoriesTag(c *types.Type) string {
return resource
}

// getFinalizersTag returns the value of the +kubebuilder:finalizers tags
func getFinalizersTag(c *types.Type) string {
comments := Comments(c.CommentLines)
return comments.getTag("kubebuilder:finalizers", "=")
}

// getDocAnnotation parse annotations of "+kubebuilder:doc:" with tags of "warning" or "doc" for control generating doc config.
// E.g. +kubebuilder:doc:warning=foo +kubebuilder:doc:note=bar
func getDocAnnotation(t *types.Type, tags ...string) map[string]string {
Expand Down Expand Up @@ -370,9 +376,6 @@ func parseScaleParams(t *types.Type) (map[string]string, error) {
if len(path) < 2 {
return nil, fmt.Errorf(jsonPathError)
}
for _, s := range path {
fmt.Printf("\n[debug] %s", s)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @droot . this is going to be deleted in my separate PR.

for _, s := range path {
kv := strings.Split(s, "=")
if kv[0] == specReplicasPath || kv[0] == statusReplicasPath || kv[0] == labelSelectorPath {
Expand Down
4 changes: 4 additions & 0 deletions pkg/internal/codegen/types.go
Expand Up @@ -173,6 +173,10 @@ type APIResource struct {
DocAnnotation map[string]string
// Categories is a list of categories the resource is part of.
Categories []string

// Finalizers is a list of finalizers (pre-delete hooks) to be added to the
// resource
Finalizers []string
}

// APISubresource contains information of an API subresource.
Expand Down