Skip to content

Commit

Permalink
chore: only log and stop propagating backstage errors (#232)
Browse files Browse the repository at this point in the history
* chore: only log and stop propagating backstage errors

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

* chore: check instead the length of backstage's label map

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>

---------

Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
Co-authored-by: Aris Boutselis <arisboutselis08@gmail.com>
  • Loading branch information
Aris Boutselis and arbreezy committed Oct 3, 2023
1 parent c076067 commit 7fe8884
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 8 additions & 4 deletions pkg/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -54,14 +56,16 @@ 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]
if !exists {
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}
}
9 changes: 4 additions & 5 deletions pkg/resources/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fe8884

Please sign in to comment.