From 7fe88841ac8e74298cdcc58f3e98763b459d6c23 Mon Sep 17 00:00:00 2001 From: Aris Boutselis Date: Tue, 3 Oct 2023 07:47:38 +0100 Subject: [PATCH] chore: only log and stop propagating backstage errors (#232) * chore: only log and stop propagating backstage errors Signed-off-by: Aris Boutselis * chore: check instead the length of backstage's label map Signed-off-by: Aris Boutselis --------- Signed-off-by: Aris Boutselis Co-authored-by: Aris Boutselis --- pkg/integrations/integrations.go | 12 ++++++++---- pkg/resources/results.go | 9 ++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/integrations/integrations.go b/pkg/integrations/integrations.go index dfa99ceb..caa077b4 100644 --- a/pkg/integrations/integrations.go +++ b/pkg/integrations/integrations.go @@ -36,13 +36,15 @@ func NewIntegrations(client client.Client, ctx context.Context) (*Integrations, }, nil } -func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) (map[string]string, error) { +func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) map[string]string { namespace, resourceName, _ := strings.Cut(result.Name, "/") + // Log and don't propagate errors so we won't trigger a new reconciliation gvr, err := i.restMapper.ResourceFor(schema.GroupVersionResource{ Resource: result.Kind, }) if err != nil { - return nil, err + fmt.Printf("Unable to find Kind '%s'\n", result.Kind) + return map[string]string{} } obj := &unstructured.Unstructured{} @@ -54,8 +56,10 @@ func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) (map[string]st obj.SetGroupVersionKind(gvk) // Retrieve the resource using the client err = i.client.Get(i.ctx, client.ObjectKey{Name: resourceName, Namespace: namespace}, obj) + // if we don't find the K8s object we won't trigger a new reconciliation and just log a message if err != nil { - return nil, err + fmt.Printf("Fail to retrieve resource %s for namespace %s\n", resourceName, namespace) + return map[string]string{} } labels := obj.GetLabels() value, exists := labels[backstageLabelKey] @@ -63,5 +67,5 @@ func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) (map[string]st fmt.Printf("Label key '%s' does not exist in %s resource: %s\n", backstageLabelKey, result.Kind, resourceName) } // Assign the same label key/value to result CR - return map[string]string{backstageLabelKey: value}, nil + return map[string]string{backstageLabelKey: value} } diff --git a/pkg/resources/results.go b/pkg/resources/results.go index 21e8be09..68f02ce8 100644 --- a/pkg/resources/results.go +++ b/pkg/resources/results.go @@ -31,12 +31,11 @@ func MapResults(i integrations.Integrations, resultsSpec []v1alpha1.ResultSpec, name = strings.ReplaceAll(name, "/", "") result := GetResult(resultSpec, name, namespace, backend) if backstageEnabled { - backstageLabel, err := i.BackstageLabel(resultSpec) - if err != nil { - return nil, err + backstageLabel := i.BackstageLabel(resultSpec) + if len(backstageLabel) != 0 { + // add Backstage label + result.ObjectMeta.Labels = backstageLabel } - // add Backstage label - result.ObjectMeta.Labels = backstageLabel } rawResults[name] = result