Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add backstage support #127

Merged
merged 10 commits into from
May 29, 2023
8 changes: 7 additions & 1 deletion controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
// Parse the k8sgpt-deployment response into a list of results
k8sgptNumberOfResults.Set(float64(len(response.Results)))
rawResults := make(map[string]corev1alpha1.Result)
integration, err := integrations.NewIntegrations(r.Client, ctx)
arbreezy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}

for _, resultSpec := range response.Results {
resultSpec.Backend = k8sgptConfig.Spec.Backend
name := strings.ReplaceAll(resultSpec.Name, "-", "")
Expand All @@ -221,7 +227,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
},
}
if k8sgptConfig.Spec.ExtraOptions.Backstage.Enabled {
backstageLabel, err := integrations.BackstageLabel(ctx, r.Client, resultSpec)
backstageLabel, err := integration.BackstageLabel(resultSpec)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
Expand Down
26 changes: 20 additions & 6 deletions pkg/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -17,14 +18,27 @@ const (
backstageLabelKey = "backstage.io/kubernetes-id"
arbreezy marked this conversation as resolved.
Show resolved Hide resolved
)

func BackstageLabel(ctx context.Context, c client.Client, result v1alpha1.ResultSpec) (map[string]string, error) {
type Integrations struct {
restMapper meta.RESTMapper
client client.Client
ctx context.Context
}

namespace, resourceName, _ := strings.Cut(result.Name, "/")
m, err := cmdutil.NewFactory(genericclioptions.NewConfigFlags(true)).ToRESTMapper()
func NewIntegrations(client client.Client, ctx context.Context) (*Integrations, error) {
restMapper, err := cmdutil.NewFactory(genericclioptions.NewConfigFlags(true)).ToRESTMapper()
if err != nil {
return nil, err
return &Integrations{}, err
}
gvr, err := m.ResourceFor(schema.GroupVersionResource{
return &Integrations{
restMapper: restMapper,
client: client,
ctx: ctx,
}, nil
}

func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) (map[string]string, error) {
namespace, resourceName, _ := strings.Cut(result.Name, "/")
gvr, err := i.restMapper.ResourceFor(schema.GroupVersionResource{
Resource: result.Kind,
})
if err != nil {
Expand All @@ -39,7 +53,7 @@ func BackstageLabel(ctx context.Context, c client.Client, result v1alpha1.Result
}
obj.SetGroupVersionKind(gvk)
// Retrieve the resource using the client
err = c.Get(ctx, client.ObjectKey{Name: resourceName, Namespace: namespace}, obj)
err = i.client.Get(i.ctx, client.ObjectKey{Name: resourceName, Namespace: namespace}, obj)
if err != nil {
return nil, err
}
Expand Down