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 more playbook actions #587

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions api/v1/playbook_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
gocontext "context"
"encoding/json"
"fmt"

"github.com/flanksource/duty"
Expand All @@ -10,6 +11,62 @@ import (
"k8s.io/client-go/kubernetes"
)

type PodAction struct {
// Name is name of the pod that'll be created
Name string `yaml:"name" json:"name"`
// MaxLength is the maximum length of the logs to show
// Default: 3000 characters
MaxLength int `yaml:"maxLength,omitempty" json:"maxLength,omitempty"`
// Timeout in minutes to wait for specified container to finish its job. Defaults to 5 minutes
Timeout int `yaml:"timeout,omitempty" json:"timeout,omitempty"`
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
// Spec is the container spec
Spec json.RawMessage `yaml:"spec" json:"spec"`
}

type SQLAction struct {
// Connection identifier e.g. connection://Postgres/flanksource
Connection string `yaml:"connection,omitempty" json:"connection,omitempty"`
// URL is the database connection url
URL string `yaml:"url,omitempty" json:"url,omitempty"`
// Query is the sql query to run
Query string `yaml:"query" json:"query" template:"true"`
// Driver is the name of the underlying database to connect to.
// Example: postgres, mysql, ...
Driver string `yaml:"driver" json:"driver"`
}

type HTTPConnection struct {
// Connection name e.g. connection://http/google
Connection string `yaml:"connection,omitempty" json:"connection,omitempty"`
// Connection url, interpolated with username,password
URL string `yaml:"url,omitempty" json:"url,omitempty" template:"true"`
Authentication `yaml:",inline" json:",inline"`
}

type Authentication struct {
Username types.EnvVar `yaml:"username,omitempty" json:"username,omitempty"`
Password types.EnvVar `yaml:"password,omitempty" json:"password,omitempty"`
}

type HTTPAction struct {
HTTPConnection `yaml:",inline" json:",inline"`
// Method to use - defaults to GET
Method string `yaml:"method,omitempty" json:"method,omitempty"`
// NTLM when set to true will do authentication using NTLM v1 protocol
NTLM bool `yaml:"ntlm,omitempty" json:"ntlm,omitempty"`
// NTLM when set to true will do authentication using NTLM v2 protocol
NTLMv2 bool `yaml:"ntlmv2,omitempty" json:"ntlmv2,omitempty"`
// Header fields to be used in the query
Headers []types.EnvVar `yaml:"headers,omitempty" json:"headers,omitempty"`
// Request Body Contents
Body string `yaml:"body,omitempty" json:"body,omitempty" template:"true"`
// TemplateBody controls whether the body of the request needs to be templated
TemplateBody bool `yaml:"templateBody,omitempty" json:"templateBody,omitempty"`
}

type ExecAction struct {
// Script can be a inline script or a path to a script that needs to be executed
// On windows executed via powershell and in darwin and linux executed using bash
Expand Down Expand Up @@ -139,4 +196,7 @@ func (t *AWSConnection) Populate(ctx connectionContext, k8s kubernetes.Interface
type PlaybookAction struct {
Name string `yaml:"name" json:"name"`
Exec *ExecAction `json:"exec,omitempty" yaml:"exec,omitempty"`
HTTP *HTTPAction `json:"http,omitempty" yaml:"http,omitempty"`
SQL *SQLAction `json:"sql,omitempty" yaml:"sql,omitempty"`
Pod *PodAction `json:"pod,omitempty" yaml:"pod,omitempty"`
}
107 changes: 107 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/utils"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/jobs"
Expand All @@ -24,15 +25,15 @@ func PreRun(cmd *cobra.Command, args []string) {
logger.Fatalf("Failed to initialize the db: %v", err)
}

api.DefaultContext = api.NewContext(db.Gorm, db.Pool)

var err error
api.Kubernetes, err = k8s.NewClient()
if err != nil {
logger.Infof("Kubernetes client not available: %v", err)
api.Kubernetes = fake.NewSimpleClientset()
}

api.DefaultContext = api.NewContext(db.Gorm, db.Pool)

if otelcollectorURL != "" {
telemetry.InitTracer("mission-control", otelcollectorURL, true)
}
Expand All @@ -54,7 +55,7 @@ var otelcollectorURL string

func ServerFlags(flags *pflag.FlagSet) {
flags.IntVar(&httpPort, "httpPort", 8080, "Port to expose a health dashboard")
flags.StringVar(&api.Namespace, "namespace", os.Getenv("NAMESPACE"), "Namespace to use for config/secret lookups")
flags.StringVar(&api.Namespace, "namespace", utils.Coalesce(os.Getenv("NAMESPACE"), "default"), "Namespace to use for config/secret lookups")
flags.IntVar(&devGuiPort, "devGuiPort", 3004, "Port used by a local npm server in development mode")
flags.IntVar(&metricsPort, "metricsPort", 8081, "Port to expose a health dashboard ")
flags.BoolVar(&dev, "dev", false, "Run in development mode")
Expand Down
5 changes: 2 additions & 3 deletions components/logs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package components

import (
"context"
"encoding/json"
"io"
"net/url"
"strings"

"github.com/flanksource/commons/http"

Expand Down Expand Up @@ -47,8 +47,7 @@ func GetLogsByComponent(componentID, start, end string) (api.ComponentLogs, erro
return api.ComponentLogs{}, err
}

client := http.NewClient(&http.Config{})
resp, err := client.Post(endpoint, "application/json", io.NopCloser(strings.NewReader(string(payloadBytes))))
resp, err := http.NewClient().R(context.Background()).Header("Content-Type", "application/json").Post(endpoint, payloadBytes)
if err != nil {
return api.ComponentLogs{}, err
}
Expand Down
Loading
Loading