Skip to content

Commit

Permalink
Add dry-run flag
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed authored Aug 25, 2020
2 parents b3b217a + 0e07213 commit 2964cf6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions cli/cmd/data/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type TargetDetails struct {
Id string
FileName string
Alpine bool
DryRun bool
}
26 changes: 26 additions & 0 deletions cli/cmd/kubernetes/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ package kubernetes
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"os"

"github.com/VerizonMedia/kubectl-flame/cli/cmd/data"
batchv1 "k8s.io/api/batch/v1"
apiv1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/uuid"
)

Expand All @@ -30,6 +33,10 @@ func LaunchFlameJob(targetPod *v1.Pod, targetDetails *data.TargetDetails, ctx co
}

job := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
Kind: "Job",
APIVersion: "batch/v1",
},
ObjectMeta: commonMeta,
Spec: batchv1.JobSpec{
Parallelism: int32Ptr(1),
Expand Down Expand Up @@ -93,6 +100,11 @@ func LaunchFlameJob(targetPod *v1.Pod, targetDetails *data.TargetDetails, ctx co
},
}

if targetDetails.DryRun {
err := printJob(job)
return "", nil, err
}

createJob, err := clientSet.
BatchV1().
Jobs(targetDetails.Namespace).
Expand All @@ -105,6 +117,20 @@ func LaunchFlameJob(targetPod *v1.Pod, targetDetails *data.TargetDetails, ctx co
return id, createJob, nil
}

func printJob(job *batchv1.Job) error {
scheme := runtime.NewScheme()
err := metav1.AddMetaToScheme(scheme)
if err != nil {
return err
}

encoder := json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, json.SerializerOptions{
Yaml: true,
})

return encoder.Encode(job, os.Stdout)
}

func DeleteProfilingJob(job *batchv1.Job, targetDetails *data.TargetDetails, ctx context.Context) error {
deleteStrategy := metav1.DeletePropagationForeground
return clientSet.
Expand Down
25 changes: 15 additions & 10 deletions cli/cmd/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,67 @@ import (

func Flame(target *data.TargetDetails, configFlags *genericclioptions.ConfigFlags) {
ns, err := kubernetes.Connect(configFlags)
p := NewPrinter(target.DryRun)
if err != nil {
fmt.Printf("Failed connecting to kubernetes cluster: %v\n", err)
os.Exit(1)
}

target.Namespace = ns
ctx := context.Background()
fmt.Print("Verifying target pod ... ")
p.Print("Verifying target pod ... ")
pod, err := kubernetes.GetPodDetails(target.PodName, target.Namespace, ctx)
if err != nil {
PrintError()
p.PrintError()
fmt.Println(err.Error())
os.Exit(1)
}

containerName, err := validatePod(pod, target.ContainerName)
if err != nil {
PrintError()
p.PrintError()
fmt.Println(err.Error())
os.Exit(1)
}

containerId, err := kubernetes.GetContainerId(containerName, pod)
if err != nil {
PrintError()
p.PrintError()
fmt.Println(err.Error())
os.Exit(1)
}

PrintSuccess()
p.PrintSuccess()
target.ContainerName = containerName
target.ContainerId = containerId
fmt.Print("Launching profiler ... ")
p.Print("Launching profiler ... ")
profileId, job, err := kubernetes.LaunchFlameJob(pod, target, ctx)
if err != nil {
PrintError()
p.PrintError()
fmt.Print(err.Error())
os.Exit(1)
}

if target.DryRun {
return
}

target.Id = profileId
profilerPod, err := kubernetes.WaitForPodStart(target, ctx)
if err != nil {
PrintError()
p.PrintError()
fmt.Println(err.Error())
os.Exit(1)
}

PrintSuccess()
p.PrintSuccess()
apiHandler := &handler.ApiEventsHandler{
Job: job,
Target: target,
}
done, err := kubernetes.GetLogsFromPod(profilerPod, apiHandler, ctx)
if err != nil {
PrintError()
p.PrintError()
fmt.Println(err.Error())
}

Expand Down
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewFlameCommand(streams genericclioptions.IOStreams) *cobra.Command {
cmd.Flags().DurationVarP(&targetDetails.Duration, "time", "t", defaultDuration, "Enter max scan Duration")
cmd.Flags().StringVarP(&targetDetails.FileName, "file", "f", "flamegraph.svg", "Optional file location")
cmd.Flags().BoolVar(&targetDetails.Alpine, "alpine", false, "Target image is based on Alpine")
cmd.Flags().BoolVar(&targetDetails.DryRun, "dry-run", false, "simulate profiling")
options.configFlags.AddFlags(cmd.Flags())

return cmd
Expand Down
30 changes: 27 additions & 3 deletions cli/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@ package cmd

import "fmt"

func PrintSuccess() {
fmt.Printf("✔\n")
type Printer interface {
Print(str string)
PrintSuccess()
PrintError()
}

func PrintError() {
type dryRunPrinter struct {
dryRun bool
}

func (p *dryRunPrinter) Print(str string) {
if !p.dryRun {
fmt.Print(str)
}
}

func (p *dryRunPrinter) PrintSuccess() {
if !p.dryRun {
fmt.Printf("✔\n")
}
}

func (p *dryRunPrinter) PrintError() {
fmt.Printf("❌\n")
}

func NewPrinter(dryRun bool) Printer {
return &dryRunPrinter{
dryRun: dryRun,
}
}

0 comments on commit 2964cf6

Please sign in to comment.