Skip to content

Commit

Permalink
fix: combine all exported services addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakknutsen committed Sep 4, 2024
1 parent 3c70e9d commit f3c1d97
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions controllers/routingctrl/reconcile_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,28 @@ func (r *Controller) createRoutingResources(ctx context.Context, target *unstruc
return fmt.Errorf("could not get domain: %w", errDomain)
}

targetPublicHosts := []string{}
targetExternalHosts := []string{}
var errSvcExport []error

Check failure on line 55 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

declarations should never be cuddled (wsl)

for i := range exportedServices {
if errExport := r.exportService(ctx, target, &exportedServices[i], domain); errExport != nil {
servicePublicHosts, serviceExternalHosts, errExport := r.exportService(ctx, target, &exportedServices[i], domain)
if errExport != nil {
errSvcExport = append(errSvcExport, errExport)
continue

Check failure on line 61 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

continue with no blank line before (nlreturn)
}
targetPublicHosts = append(targetPublicHosts, servicePublicHosts...)

Check failure on line 63 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

append only allowed to cuddle with appended value (wsl)
targetExternalHosts = append(targetExternalHosts, serviceExternalHosts...)
}

return errors.Join(errSvcExport...)
if errSvcExportCombined := errors.Join(errSvcExport...); errSvcExportCombined != nil {
return errSvcExportCombined
}

return r.propagateHostsToWatchedCR(ctx, target, targetPublicHosts, targetExternalHosts)
}

func (r *Controller) exportService(ctx context.Context, target *unstructured.Unstructured, exportedSvc *corev1.Service, domain string) error {
func (r *Controller) exportService(ctx context.Context, target *unstructured.Unstructured, exportedSvc *corev1.Service, domain string) ([]string, []string, error) {

Check failure on line 74 in controllers/routingctrl/reconcile_resources.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnamedResult: consider giving a name to these results (gocritic)
exportModes := r.extractExportModes(target)

externalHosts := []string{}
Expand All @@ -76,12 +86,12 @@ func (r *Controller) exportService(ctx context.Context, target *unstructured.Uns
for _, exportMode := range exportModes {
resources, err := r.templateLoader.Load(templateData, exportMode)
if err != nil {
return fmt.Errorf("could not load templates for type %s: %w", exportMode, err)
return nil, nil, fmt.Errorf("could not load templates for type %s: %w", exportMode, err)
}

ownershipLabels = append(ownershipLabels, labels.ExportType(exportMode))
if errApply := unstruct.Apply(ctx, r.Client, resources, ownershipLabels...); errApply != nil {
return fmt.Errorf("could not apply routing resources for type %s: %w", exportMode, errApply)
return nil, nil, fmt.Errorf("could not apply routing resources for type %s: %w", exportMode, errApply)
}

switch exportMode {
Expand All @@ -93,16 +103,25 @@ func (r *Controller) exportService(ctx context.Context, target *unstructured.Uns
}
}

return r.propagateHostsToWatchedCR(ctx, target, publicHosts, externalHosts)
return publicHosts, externalHosts, nil
}

func (r *Controller) propagateHostsToWatchedCR(ctx context.Context, target *unstructured.Unstructured, publicHosts, externalHosts []string) error {
err := unstruct.PatchWithRetry(ctx, r.Client, target, func() error {
// Always remove the annotations first
annotations.Remove(annotations.RoutingAddressesExternal(""))(target)
annotations.Remove(annotations.RoutingAddressesPublic(""))(target)
errPatch := unstruct.PatchWithRetry(ctx, r.Client, target, r.updateAddressAnnotations(target, publicHosts, externalHosts))
if errPatch != nil {
return fmt.Errorf("failed to propagate hosts to watched CR %s/%s: %w", target.GetNamespace(), target.GetName(), errPatch)
}

return nil
}

var metaOptions []metadata.Option
func (r *Controller) updateAddressAnnotations(target *unstructured.Unstructured, publicHosts, externalHosts []string) func() error {
return func() error {
// Remove all existing routing addresses
metaOptions := []metadata.Option{
annotations.Remove(annotations.RoutingAddressesExternal("")),
annotations.Remove(annotations.RoutingAddressesPublic("")),
}

if len(publicHosts) > 0 {
metaOptions = append(metaOptions, annotations.RoutingAddressesPublic(strings.Join(publicHosts, ";")))
Expand All @@ -115,13 +134,7 @@ func (r *Controller) propagateHostsToWatchedCR(ctx context.Context, target *unst
metadata.ApplyMetaOptions(target, metaOptions...)

return nil
})

if err != nil {
return fmt.Errorf("failed to propagate hosts to watched CR %s/%s: %w", target.GetNamespace(), target.GetName(), err)
}

return nil
}

func (r *Controller) ensureResourceHasFinalizer(ctx context.Context, target *unstructured.Unstructured) error {
Expand Down

0 comments on commit f3c1d97

Please sign in to comment.