-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve mutate efficiency and logic (#48)
* feat: added improvements to child resource mutation This adds inline functionality to mutate resources. Prior to this change, the mutation functionality would loop through all resources for each child resource to perform mutation. This resulted in a lot of if/then statements and really ugly code. This new change mutates the resources inline and also passes the request with context, and the reconciler so that cluster lookups and storage of data on the context may happen between resources. Also Fixes #17, which now makes the header comments more legible and strips the ugly start/end tags. Signed-off-by: Dustin Scott <[email protected]> * feat: move names into its own package This will prevent import cycle conflicts when attempting to retrieve names from packages. Signed-off-by: Dustin Scott <[email protected]> * feat: Fixes #29, create source file names with intelligence This commit addresses an issue that creates very long file names for each manifest. This instead prefers the file name, and works its way back through the directory structure until a unique name is found. If the names cannot be deduplicated, then a count is appended to the end. Signed-off-by: Dustin Scott <[email protected]> * feat: decrease mutate file name length Signed-off-by: Dustin Scott <[email protected]> * refactor: change from names to constants package Signed-off-by: Dustin Scott <[email protected]> * chore: fix linter errors Signed-off-by: Dustin Scott <[email protected]> * fix: backwards logic for checking out of range file names Signed-off-by: Dustin Scott <[email protected]> * chore: rename constants.go to names.go to make file more specific Signed-off-by: Dustin Scott <[email protected]> * chore: add pod disruption budget alias Signed-off-by: Dustin Scott <[email protected]> * chore: add webhook aliases Signed-off-by: Dustin Scott <[email protected]>
- Loading branch information
Showing
13 changed files
with
678 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
internal/plugins/workload/v1/scaffolds/templates/api/resources/constants.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 Nukleros | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/machinery" | ||
|
||
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds" | ||
) | ||
|
||
var _ machinery.Template = &Constants{} | ||
|
||
// Types scaffolds the child resource Constants files. | ||
type Constants struct { | ||
machinery.TemplateMixin | ||
machinery.BoilerplateMixin | ||
machinery.RepositoryMixin | ||
machinery.ResourceMixin | ||
|
||
// input fields | ||
Builder kinds.WorkloadBuilder | ||
|
||
// template fields | ||
ConstantStrings []string | ||
} | ||
|
||
func (f *Constants) SetTemplateDefaults() error { | ||
f.Path = filepath.Join( | ||
"apis", | ||
f.Resource.Group, | ||
f.Resource.Version, | ||
f.Builder.GetPackageName(), | ||
"constants", | ||
"names.go", | ||
) | ||
|
||
children := kinds.GetWorkloadChildren(f.Builder) | ||
|
||
for i := range children { | ||
child := children[i] | ||
|
||
if child.NameConstant() != "" { | ||
f.ConstantStrings = append( | ||
f.ConstantStrings, | ||
fmt.Sprintf("%s = %q", child.UniqueName, child.NameConstant()), | ||
) | ||
} | ||
} | ||
|
||
f.TemplateBody = NamesTemplate | ||
f.IfExistsAction = machinery.OverwriteFile | ||
|
||
return nil | ||
} | ||
|
||
const NamesTemplate = `{{ .Boilerplate }} | ||
package constants | ||
{{ if .Builder.HasChildResources }} | ||
// this package includes the constants which include the resource names. it is a standalone | ||
// package to prevent import cycle errors when attempting to reference the names from other | ||
// packages (e.g. mutate). | ||
const ( | ||
{{ range .ConstantStrings }} | ||
{{- . }} | ||
{{ end }} | ||
) | ||
{{ end }} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
internal/plugins/workload/v1/scaffolds/templates/api/resources/mutate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2022 Nukleros | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resources | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/machinery" | ||
|
||
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds" | ||
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/manifests" | ||
) | ||
|
||
var ( | ||
_ machinery.Template = &Mutate{} | ||
) | ||
|
||
// Mutate scaffolds the root command file for the companion CLI. | ||
type Mutate struct { | ||
machinery.TemplateMixin | ||
machinery.BoilerplateMixin | ||
machinery.RepositoryMixin | ||
machinery.ResourceMixin | ||
|
||
// input variables | ||
Builder kinds.WorkloadBuilder | ||
ChildResource manifests.ChildResource | ||
} | ||
|
||
func (f *Mutate) SetTemplateDefaults() error { | ||
// set interface variables | ||
f.Path = filepath.Join( | ||
"apis", | ||
f.Resource.Group, | ||
f.Resource.Version, | ||
f.Builder.GetPackageName(), | ||
"mutate", | ||
f.ChildResource.MutateFileName(), | ||
) | ||
|
||
f.TemplateBody = MutateTemplate | ||
|
||
return nil | ||
} | ||
|
||
// GetIfExistsAction implements file.Builder interface. | ||
func (*Mutate) GetIfExistsAction() machinery.IfExistsAction { | ||
return machinery.SkipFile | ||
} | ||
|
||
//nolint:lll | ||
const MutateTemplate = `{{ .Boilerplate }} | ||
package mutate | ||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"github.com/nukleros/operator-builder-tools/pkg/controller/workload" | ||
{{ .Resource.ImportAlias }} "{{ .Resource.Path }}" | ||
{{- if .Builder.IsComponent }} | ||
{{ .Builder.GetCollection.Spec.API.Group }}{{ .Builder.GetCollection.Spec.API.Version }} "{{ .Repo }}/apis/{{ .Builder.GetCollection.Spec.API.Group }}/{{ .Builder.GetCollection.Spec.API.Version }}" | ||
{{ end -}} | ||
) | ||
// {{ .ChildResource.MutateFuncName }} mutates the {{ .ChildResource.Kind }} resource with name {{ .ChildResource.NameComment }}. | ||
func {{ .ChildResource.MutateFuncName }} ( | ||
original client.Object, | ||
parent *{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}, {{ if .Builder.IsComponent }}collection *{{ .Builder.GetCollection.Spec.API.Group }}{{ .Builder.GetCollection.Spec.API.Version }}.{{ .Builder.GetCollection.Spec.API.Kind }},{{ end }} | ||
reconciler workload.Reconciler, req *workload.Request, | ||
) ([]client.Object, error) { | ||
// if either the reconciler or request are found to be nil, return the base object. | ||
if reconciler == nil || req == nil { | ||
return []client.Object{original}, nil | ||
} | ||
// mutation logic goes here | ||
return []client.Object{original}, nil | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.