Skip to content

Commit

Permalink
fix: Fixes #275, removed unneeded ownership rules from collection to …
Browse files Browse the repository at this point in the history
…component (#277)

Signed-off-by: Dustin Scott <sdustin@vmware.com>
  • Loading branch information
scottd018 authored Feb 23, 2022
1 parent e8a60a4 commit efef71a
Showing 1 changed file with 3 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ type Controller struct {
Builder workloadv1.WorkloadAPIBuilder

// template fields
BaseImports []string
OtherImports []string
InternalImports []string
CollectionImports []string
BaseImports []string
OtherImports []string
InternalImports []string
}

func (f *Controller) SetTemplateDefaults() error {
Expand All @@ -46,10 +45,6 @@ func (f *Controller) SetTemplateDefaults() error {
f.setOtherImports()
f.setInternalImports()

if f.Builder.IsCollection() {
f.setCollectionImports()
}

return nil
}

Expand Down Expand Up @@ -108,47 +103,6 @@ func (f *Controller) setInternalImports() {
}
}

func (f *Controller) setCollectionImports() {
for _, component := range f.Builder.GetComponents() {
if !f.importIsDefined(f.getAPITypesPath(component)) {
f.CollectionImports = append(f.CollectionImports, f.getAPITypesPath(component))
}
}

f.deduplicateCollectionImports()
}

func (f *Controller) importIsDefined(importCheck string) bool {
existingImports := []string{}
existingImports = append(existingImports, f.BaseImports...)
existingImports = append(existingImports, f.OtherImports...)
existingImports = append(existingImports, f.InternalImports...)

for _, existing := range existingImports {
if importCheck == existing {
return true
}
}

return false
}

func (f *Controller) deduplicateCollectionImports() {
keys := make(map[string]bool)

collectionImports := []string{}

for _, existing := range f.CollectionImports {
if _, value := keys[existing]; !value {
keys[existing] = true

collectionImports = append(collectionImports, existing)
}
}

f.CollectionImports = collectionImports
}

func (f *Controller) getAPITypesPath(builder workloadv1.WorkloadAPIBuilder) string {
return fmt.Sprintf(`%s%s "%s/apis/%s/%s"`,
builder.GetAPIGroup(),
Expand Down Expand Up @@ -176,12 +130,6 @@ import (
{{ range .InternalImports -}}
{{ . }}
{{ end }}
{{ if .Builder.IsCollection -}}
{{ range .CollectionImports -}}
{{ . }}
{{ end }}
{{ end }}
)
// {{ .Resource.Kind }}Reconciler reconciles a {{ .Resource.Kind }} object.
Expand Down Expand Up @@ -294,19 +242,6 @@ func (r *{{ .Resource.Kind }}Reconciler) SetCollection(component *{{ .Resource.I
req.Collection = collection
// set the owner reference so that we can reconcile this workload on changes to the collection
if err := ctrl.SetControllerReference(collection, req.Workload, r.Scheme()); err != nil {
req.Log.Error(
err, "unable to set collection owner reference on component workload",
"Name", req.Workload.GetName(),
"Namespace", req.Workload.GetNamespace(),
"collection.Name", collection.GetName(),
"collection.Namespace", collection.GetNamespace(),
)
return fmt.Errorf("unable to set owner reference on %s, %w", req.Workload.GetName(), err)
}
return r.EnqueueRequestOnCollectionChange(req)
}
Expand Down Expand Up @@ -494,11 +429,6 @@ func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) erro
baseController, err := ctrl.NewControllerManagedBy(mgr).
WithEventFilter(predicates.WorkloadPredicates()).
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
{{ if .Builder.IsCollection -}}
{{ range .Builder.GetComponents -}}
Owns(&{{ .Spec.API.Group }}{{ .Spec.API.Version }}.{{ .Spec.API.Kind }}{}).
{{ end -}}
{{ end -}}
Build(r)
if err != nil {
return fmt.Errorf("unable to setup controller, %w", err)
Expand Down

0 comments on commit efef71a

Please sign in to comment.