Skip to content

Commit

Permalink
feat: add backstage support (#127)
Browse files Browse the repository at this point in the history
* feat: add backstage support

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

* chore: change default Local Mode port to prevent conflict with default serve port

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

* chore: bump Helm chart version and add updated CRD

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

* chore: move backstage to integrations

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

* chore: change backstage crd structure

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

* feat: add integrations constructor

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

* chore: Initialise once Integrations.

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

* docs: update README example

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 May 29, 2023
1 parent 51deb68 commit 1b267a6
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ spec:
enableAI: true
# filters:
# - Ingress
# extraOptions:
# backstage:
# enabled: true
secret:
name: k8sgpt-sample-secret
key: openai-api-key
Expand Down
23 changes: 16 additions & 7 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ import (
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

type Backstage struct {
Enabled bool `json:"enabled,omitempty"`
}

type SecretRef struct {
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
}

type ExtraOptionsRef struct {
Backstage *Backstage `json:"backstage,omitempty"`
}

// K8sGPTSpec defines the desired state of K8sGPT
type K8sGPTSpec struct {
// +kubebuilder:default:=openai
// +kubebuilder:validation:Enum=openai;localai;azureopenai
Backend `json:"backend"`
BaseUrl string `json:"baseUrl,omitempty"`
// +kubebuilder:default:=gpt-3.5-turbo
Model string `json:"model,omitempty"`
Engine string `json:"engine,omitempty"`
Secret *SecretRef `json:"secret,omitempty"`
Version string `json:"version,omitempty"`
EnableAI bool `json:"enableAI,omitempty"`
NoCache bool `json:"noCache,omitempty"`
Filters []string `json:"filters,omitempty"`
Model string `json:"model,omitempty"`
Engine string `json:"engine,omitempty"`
Secret *SecretRef `json:"secret,omitempty"`
Version string `json:"version,omitempty"`
EnableAI bool `json:"enableAI,omitempty"`
NoCache bool `json:"noCache,omitempty"`
Filters []string `json:"filters,omitempty"`
ExtraOptions *ExtraOptionsRef `json:"extraOptions,omitempty"`
}

type Backend string
Expand Down
35 changes: 29 additions & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions chart/operator/templates/k8sgpt-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ spec:
type: boolean
engine:
type: string
extraOptions:
properties:
backstage:
properties:
enabled:
type: boolean
type: object
type: object
model:
default: gpt-3.5-turbo
type: string
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ spec:
type: boolean
engine:
type: string
extraOptions:
properties:
backstage:
properties:
enabled:
type: boolean
type: object
type: object
filters:
items:
type: string
Expand Down
14 changes: 13 additions & 1 deletion controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"time"

corev1alpha1 "github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"

kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/integrations"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -68,6 +70,7 @@ var (
type K8sGPTReconciler struct {
client.Client
Scheme *runtime.Scheme
Integrations *integrations.Integrations
K8sGPTClient *kclient.Client
// This is a map of clients for each deployment
k8sGPTClients map[string]*kclient.Client
Expand Down Expand Up @@ -218,6 +221,14 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
Namespace: k8sgptConfig.Namespace,
},
}
if k8sgptConfig.Spec.ExtraOptions.Backstage.Enabled {
backstageLabel, err := r.Integrations.BackstageLabel(resultSpec)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}
result.ObjectMeta.Labels = backstageLabel
}
rawResults[name] = result
}

Expand Down Expand Up @@ -256,7 +267,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
err = r.Get(ctx, client.ObjectKey{Namespace: k8sgptConfig.Namespace,
Name: result.Name}, &existingResult)
if err != nil {
// if the result already exists, we will update it
// if the result doesn't exist, we will create it
if errors.IsNotFound(err) {
err = r.Create(ctx, &result)
if err != nil {
Expand All @@ -275,6 +286,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
} else {
// If the result already exists we will update it
existingResult.Spec = result.Spec
existingResult.Labels = result.Labels
err = r.Update(ctx, &existingResult)
if err != nil {
k8sgptReconcileErrorCount.Inc()
Expand Down
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
google.golang.org/grpc v1.55.0
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/cli-runtime v0.27.2
k8s.io/client-go v0.27.2
k8s.io/kubectl v0.27.2
sigs.k8s.io/controller-runtime v0.15.0
)

Expand All @@ -23,13 +25,18 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -38,23 +45,37 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/cobra v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.24.0 // indirect
Expand All @@ -77,6 +98,8 @@ require (
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 1b267a6

Please sign in to comment.