Skip to content

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 12, 2023
1 parent 5ca5cc7 commit 10ab3bc
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 141 deletions.
8 changes: 0 additions & 8 deletions api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/flanksource/duty"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/flanksource/kommons"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -39,7 +38,6 @@ type Context interface {

Namespace() string
Kubernetes() kubernetes.Interface
Kommons() *kommons.Client

WithDB(db *gorm.DB) Context
WithEchoContext(ctx EchoContext) Context
Expand Down Expand Up @@ -70,7 +68,6 @@ type context struct {
db *gorm.DB
pool *pgxpool.Pool

kommons *kommons.Client
kubernetes kubernetes.Interface
namespace string
}
Expand All @@ -81,17 +78,12 @@ func NewContext(db *gorm.DB, pool *pgxpool.Pool) Context {
db: db,
pool: pool,
kubernetes: Kubernetes,
kommons: KommonsClient,
namespace: Namespace,
}

return c
}

func (c *context) Kommons() *kommons.Client {
return c.kommons
}

func (c *context) Kubernetes() kubernetes.Interface {
return c.kubernetes
}
Expand Down
2 changes: 0 additions & 2 deletions api/global.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"github.com/flanksource/kommons"
"github.com/google/uuid"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -13,7 +12,6 @@ var (
CanaryCheckerPath string
ApmHubPath string
Kubernetes kubernetes.Interface
KommonsClient *kommons.Client
Namespace string

// Full URL of the mission control web UI.
Expand Down
8 changes: 2 additions & 6 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 @@ -31,11 +32,6 @@ func PreRun(cmd *cobra.Command, args []string) {
api.Kubernetes = fake.NewSimpleClientset()
}

api.KommonsClient, _, err = k8s.NewKommonsClient()
if err != nil {
logger.Warnf("Failed to get kubernetes client: %v", err)
}

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

if otelcollectorURL != "" {
Expand All @@ -59,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
16 changes: 4 additions & 12 deletions config/crds/mission-control.flanksource.com_playbooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,6 @@ spec:
type: object
type: object
type: array
maxSSLExpiry:
description: Maximum number of days until the SSL Certificate
expires.
type: integer
method:
description: Method to use - defaults to GET
type: string
Expand Down Expand Up @@ -346,10 +342,6 @@ spec:
description: TemplateBody controls whether the body of the
request needs to be templated
type: boolean
thresholdMillis:
description: Maximum duration in milliseconds for the HTTP
request. It will fail the check if it takes longer.
type: integer
url:
description: Connection url, interpolated with username,password
type: string
Expand Down Expand Up @@ -386,6 +378,10 @@ spec:
type: string
pod:
properties:
maxLength:
description: 'MaxLength is the maximum length of the logs
to show Default: 3000 characters'
type: integer
name:
description: Name is name of the pod that'll be created
type: string
Expand All @@ -406,9 +402,6 @@ spec:
connection:
description: Connection identifier e.g. connection://Postgres/flanksource
type: string
database:
description: Database is the name of the database
type: string
driver:
description: 'Driver is the name of the underlying database
to connect to. Example: postgres, mysql, ...'
Expand All @@ -420,7 +413,6 @@ spec:
description: URL is the database connection url
type: string
required:
- database
- driver
- query
type: object
Expand Down
2 changes: 1 addition & 1 deletion config/schemas/playbook.schema.json

Large diffs are not rendered by default.

94 changes: 46 additions & 48 deletions k8s/kommons.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
package k8s

import (
"context"
"os"
"path/filepath"
"bytes"
"fmt"
"io"
"time"

"github.com/flanksource/commons/files"
"github.com/flanksource/commons/logger"
"github.com/flanksource/kommons"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/flanksource/incident-commander/api"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

func NewKommonsClient() (*kommons.Client, kubernetes.Interface, error) {
kubeConfig := GetKubeconfig()
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, fake.NewSimpleClientset(), errors.Wrap(err, "Failed to generate rest config")
}
Client := kommons.NewClient(config, logger.StandardLogger())
if Client == nil {
return nil, fake.NewSimpleClientset(), errors.New("could not create kommons client")
}
// WaitForPod waits for a pod to be in the specified phase, or returns an
// error if the timeout is exceeded
func WaitForPod(ctx api.Context, name string, timeout time.Duration, phases ...v1.PodPhase) error {
pods := ctx.Kubernetes().CoreV1().Pods(ctx.Namespace())
start := time.Now()
for {
pod, err := pods.Get(ctx, name, metav1.GetOptions{})
if start.Add(timeout).Before(time.Now()) {
return fmt.Errorf("timeout exceeded waiting for %s is %s, error: %v", name, pod.Status.Phase, err)
}

if pod == nil || pod.Status.Phase == v1.PodPending {
time.Sleep(5 * time.Second)
continue
}
if pod.Status.Phase == v1.PodFailed {
return nil
}

k8s, err := Client.GetClientset()
if err == nil {
return Client, k8s, nil
for _, phase := range phases {
if pod.Status.Phase == phase {
return nil
}
}
}
return nil, fake.NewSimpleClientset(), errors.Wrap(err, "failed to create k8s client")
}

func GetClusterName(config *rest.Config) string {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return ""
func GetPodLogs(ctx api.Context, podName, container string) (string, error) {
podLogOptions := v1.PodLogOptions{}
if container != "" {
podLogOptions.Container = container
}
kubeadmConfig, err := clientset.CoreV1().ConfigMaps("kube-system").Get(context.TODO(), "kubeadm-config", metav1.GetOptions{})

req := ctx.Kubernetes().CoreV1().Pods(ctx.Namespace()).GetLogs(podName, &podLogOptions)
podLogs, err := req.Stream(ctx)
if err != nil {
return ""
return "", err
}
clusterConfiguration := make(map[string]interface{})
defer podLogs.Close()

if err := yaml.Unmarshal([]byte(kubeadmConfig.Data["ClusterConfiguration"]), &clusterConfiguration); err != nil {
return ""
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", err
}
return clusterConfiguration["clusterName"].(string)

return buf.String(), nil
}

func GetKubeconfig() string {
var kubeConfig string
if os.Getenv("KUBECONFIG") != "" {
kubeConfig = os.Getenv("KUBECONFIG")
} else if home := homedir.HomeDir(); home != "" {
kubeConfig = filepath.Join(home, ".kube", "config")
if !files.Exists(kubeConfig) {
kubeConfig = ""
}
}
return kubeConfig
func DeletePod(ctx api.Context, name string) error {
return ctx.Kubernetes().CoreV1().Pods(ctx.Namespace()).Delete(ctx, name, metav1.DeleteOptions{})
}
29 changes: 13 additions & 16 deletions playbook/actions/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
netHTTP "net/http"
"net/url"
"time"

"github.com/flanksource/commons/http"
"github.com/flanksource/duty/models"
Expand All @@ -17,40 +16,35 @@ type HTTPResult struct {
Code string
Headers netHTTP.Header
Body string
SslAge *time.Duration
}

type HTTP struct {
}

func (c *HTTP) Run(ctx api.Context, action v1.HTTPAction, env TemplateEnv) (*HTTPResult, error) {
if action.URL == "" {
return nil, fmt.Errorf("must specify URL")
}

connection, err := ctx.HydrateConnection(action.HTTPConnection.Connection)
if err != nil {
return nil, fmt.Errorf("must specify URL")
return nil, fmt.Errorf("failed to hydrate connection: %w", err)
} else if connection != nil {
if connection.URL == "" {
return nil, fmt.Errorf("no url or connection specified")
}

if ntlm, ok := connection.Properties["ntlm"]; ok {
action.NTLM = ntlm == "true"
} else if ntlm, ok := connection.Properties["ntlmv2"]; ok {
action.NTLMv2 = ntlm == "true"
}

if _, err := url.Parse(connection.URL); err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
return nil, fmt.Errorf("failed to parse url(%q): %w", connection.URL, err)
}
} else if connection == nil {
connection = &models.Connection{
URL: action.URL,
}
}

if connection.URL == "" {
return nil, fmt.Errorf("must specify a URL")
}

if action.TemplateBody {
templated, err := gomplate.RunTemplate(env.AsMap(), gomplate.Template{Template: action.Body})
if err != nil {
Expand All @@ -62,15 +56,18 @@ func (c *HTTP) Run(ctx api.Context, action v1.HTTPAction, env TemplateEnv) (*HTT

resp, err := c.makeRequest(ctx, action, connection)
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
return nil, fmt.Errorf("failed to make HTTP request: %w", err)
}

body, err := resp.AsString()
if err != nil {
return nil, fmt.Errorf("failed to get response body: %w", err)
}

body, _ := resp.AsString()
result := &HTTPResult{
Code: resp.Status,
Headers: resp.Header,
Body: body,
SslAge: resp.GetSSLAge(),
}

return result, nil
Expand Down Expand Up @@ -110,7 +107,7 @@ func (c *HTTP) makeRequest(ctx api.Context, action v1.HTTPAction, connection *mo

response, err := req.Do(action.Method, connection.URL)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to make HTTP request: %w", err)
}

return response, nil
Expand Down
Loading

0 comments on commit 10ab3bc

Please sign in to comment.