-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
- Loading branch information
Showing
11 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package sinks | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1" | ||
) | ||
|
||
type ISink interface { | ||
Configure(config v1alpha1.K8sGPT, c Client) | ||
Emit(results v1alpha1.ResultSpec) error | ||
} | ||
|
||
func NewSink(sinkType string) ISink { | ||
switch sinkType { | ||
case "slack": | ||
return &SlackSink{} | ||
//Introduce more Sink Providers | ||
default: | ||
return &SlackSink{} | ||
} | ||
} | ||
|
||
type Client struct { | ||
hclient *http.Client | ||
} | ||
|
||
func NewClient(timeout time.Duration) *Client { | ||
client := &http.Client{ | ||
Timeout: timeout, | ||
} | ||
return &Client{ | ||
hclient: client, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package sinks | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1" | ||
) | ||
|
||
var _ ISink = (*SlackSink)(nil) | ||
|
||
type SlackSink struct { | ||
Endpoint string | ||
Client Client | ||
} | ||
|
||
type SlackMessage struct { | ||
Text string `json:"text"` | ||
Attachments []Attachment `json:"attachments"` | ||
} | ||
|
||
type Attachment struct { | ||
Type string `json:"type"` | ||
Text string `json:"text"` | ||
Color string `json:"color"` | ||
Title string `json:"title"` | ||
} | ||
|
||
func buildSlackMessage(kind, name, details, backend string) SlackMessage { | ||
return SlackMessage{ | ||
Text: fmt.Sprintf("`Analysis from %s of the %s %s`", backend, kind, name), | ||
Attachments: []Attachment{ | ||
Attachment{ | ||
Type: "mrkdwn", | ||
Text: details, | ||
Color: "danger", | ||
Title: "Report", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (s *SlackSink) Configure(config v1alpha1.K8sGPT, c Client) { | ||
if config.Spec.Sink == nil { | ||
s.Endpoint = "" | ||
} | ||
s.Endpoint = config.Spec.Sink.Endpoint | ||
s.Client = c | ||
} | ||
|
||
func (s *SlackSink) Emit(results v1alpha1.ResultSpec) error { | ||
if s.Endpoint == "" { | ||
// emit nothing | ||
return nil | ||
} | ||
|
||
message := buildSlackMessage(results.Kind, results.Name, results.Details, results.Backend) | ||
payload, err := json.Marshal(message) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest(http.MethodPost, s.Endpoint, bytes.NewBuffer(payload)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := s.Client.hclient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to send report: %s", resp.Status) | ||
} | ||
|
||
return nil | ||
} |