Skip to content

Commit

Permalink
[Feature]: Add a field for a secret to the Sink Type (#317)
Browse files Browse the repository at this point in the history
* add secret to sinks

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* generate deepcopy

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* add backward compat for mattermost sink and secret

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* sort imports

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* change variable name to make it clear

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* update readme

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>

* chore: minor fix in helm's crd

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

---------

Signed-off-by: Phillip Ahereza <pahereza@gmail.com>
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
Co-authored-by: Aris Boutselis <aris.boutselis@senseon.io>
Co-authored-by: Aris Boutselis <arisboutselis08@gmail.com>
  • Loading branch information
3 people committed Jan 15, 2024
1 parent cc6c14b commit efae4a9
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ spec:
# - Ingress
# sink:
# type: slack
# webhook: <webhook-url>
# webhook: <webhook-url> # use the sink secret if you want to keep your webhook url private
# secret:
# name: slack-webhook
# key: url
#extraOptions:
# backstage:
# enabled: true
Expand Down
11 changes: 6 additions & 5 deletions api/v1alpha1/k8sgpt_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ type GCSBackend struct {

type WebhookRef struct {
// +kubebuilder:validation:Enum=slack;mattermost
Type string `json:"type,omitempty"`
Endpoint string `json:"webhook,omitempty"`
Channel string `json:"channel,omitempty"`
UserName string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Type string `json:"type,omitempty"`
Endpoint string `json:"webhook,omitempty"`
Channel string `json:"channel,omitempty"`
UserName string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Secret *SecretRef `json:"secret,omitempty"`
}

type AISpec struct {
Expand Down
7 changes: 6 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

7 changes: 7 additions & 0 deletions chart/operator/templates/k8sgpt-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ spec:
type: string
icon_url:
type: string
secret:
properties:
key:
type: string
name:
type: string
type: object
type:
enum:
- slack
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/core.k8sgpt.ai_k8sgpts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ spec:
type: string
icon_url:
type: string
secret:
properties:
key:
type: string
name:
type: string
type: object
type:
enum:
- slack
Expand Down
22 changes: 20 additions & 2 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/apps/v1"
kcorev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -284,12 +286,28 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if len(latestResultList.Items) == 0 {
return r.finishReconcile(nil, false)
}
sinkEnabled := k8sgptConfig.Spec.Sink != nil && k8sgptConfig.Spec.Sink.Type != "" && k8sgptConfig.Spec.Sink.Endpoint != ""

sinkEnabled := k8sgptConfig.Spec.Sink != nil && k8sgptConfig.Spec.Sink.Type != "" && (k8sgptConfig.Spec.Sink.Endpoint != "" || k8sgptConfig.Spec.Sink.Secret != nil)

var sinkType sinks.ISink
if sinkEnabled {
var sinkSecretValue string

if k8sgptConfig.Spec.Sink.Secret != nil {
secret := &kcorev1.Secret{}
secretNamespacedName := types.NamespacedName{
Namespace: req.Namespace,
Name: k8sgptConfig.Spec.Sink.Secret.Name,
}
if err := r.Get(ctx, secretNamespacedName, secret); err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(fmt.Errorf("could not find sink secret: %w", err), false)
}

sinkSecretValue = string(secret.Data[k8sgptConfig.Spec.Sink.Secret.Key])
}
sinkType = sinks.NewSink(k8sgptConfig.Spec.Sink.Type)
sinkType.Configure(*k8sgptConfig, *r.SinkClient)
sinkType.Configure(*k8sgptConfig, *r.SinkClient, sinkSecretValue)
}

for _, result := range latestResultList.Items {
Expand Down
7 changes: 5 additions & 2 deletions pkg/sinks/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func buildMattermostMessage(kind, name, details, k8sgptCR, channel, username, ic
}
}

func (s *MattermostSink) Configure(config v1alpha1.K8sGPT, c Client) {
s.Endpoint = config.Spec.Sink.Endpoint
func (s *MattermostSink) Configure(config v1alpha1.K8sGPT, c Client, sinkSecretValue string) {
s.Endpoint = sinkSecretValue
if s.Endpoint == "" {
s.Endpoint = config.Spec.Sink.Endpoint
}
// If no value is given, the default value of the webhook is used
if config.Spec.Sink.Channel != "" {
s.Channel = config.Spec.Sink.Channel
Expand Down
2 changes: 1 addition & 1 deletion pkg/sinks/sinkreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type ISink interface {
Configure(config v1alpha1.K8sGPT, c Client)
Configure(config v1alpha1.K8sGPT, c Client, sinkSecretValue string)
Emit(results v1alpha1.ResultSpec) error
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/sinks/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ func buildSlackMessage(kind, name, details, k8sgptCR string) SlackMessage {
}
}

func (s *SlackSink) Configure(config v1alpha1.K8sGPT, c Client) {
s.Endpoint = config.Spec.Sink.Endpoint
func (s *SlackSink) Configure(config v1alpha1.K8sGPT, c Client, sinkSecretValue string) {
s.Endpoint = sinkSecretValue
// check if the webhook url is passed as a sinkSecretValue, if not use spec.sink.webhook
if s.Endpoint == "" {
s.Endpoint = config.Spec.Sink.Endpoint
}
s.Client = c
// take the name of the K8sGPT Custom Resource
s.K8sGPT = config.Name
Expand Down

0 comments on commit efae4a9

Please sign in to comment.