Skip to content

Commit

Permalink
feat(cmd/run): attach wait conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
fntlnz committed Nov 20, 2018
1 parent 50b28e6 commit e2cbf39
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
8 changes: 5 additions & 3 deletions cmd/kubectl-trace/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ func init() {
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kubectl-trace.yaml)")

// TODO(leodido): figure out this thing
// TODO(leodido): figure out how to use the flag from the main kubectl
// instead of having to recreate them like below
//parentConfigFlags = genericclioptions.ConfigFlags{}
//parentConfigFlags.AddFlags(rootCmd.PersistentFlags())

rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
viper.BindPFlag("kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
viper.BindEnv("kubeconfig", "KUBECONFIG")
rootCmd.AddCommand(runCmd)
}

Expand All @@ -59,8 +63,6 @@ func initConfig() {
viper.SetConfigName(".kubectl-trace")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
Expand Down
71 changes: 37 additions & 34 deletions cmd/kubectl-trace/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

"github.com/fntlnz/kubectl-trace/tracejob"
"github.com/spf13/cobra"
"github.com/spf13/viper"

apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
Expand All @@ -37,8 +39,10 @@ Examples:
}

func run(cmd *cobra.Command, args []string) {
// TODO(leodido): replace this with flags
kubeconfig := os.Getenv("KUBECONFIG")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

kubeconfig := viper.GetString("kubeconfig")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err)
Expand All @@ -54,40 +58,39 @@ func run(cmd *cobra.Command, args []string) {
panic(err)
}

time.Sleep(time.Second * 2)
pl, err := clientset.CoreV1().Pods(apiv1.NamespaceDefault).List(metav1.ListOptions{
LabelSelector: "job-name=test-renzo",
go wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) {
pl, err := clientset.CoreV1().Pods(apiv1.NamespaceDefault).List(metav1.ListOptions{
LabelSelector: "job-name=test-renzo",
})

if err != nil {
panic(err)
}
if len(pl.Items) == 0 {
panic("pod not found")
}
pod := &pl.Items[0]
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
panic(fmt.Errorf("cannot attach into a container in a completed pod; current phase is %s", pod.Status.Phase))
}

if len(pod.Spec.Containers) != 1 {
panic("unexpected number of containers in trace job pod")
}

restClient := clientset.CoreV1().RESTClient().(*restclient.RESTClient)
containerName := pod.Spec.Containers[0].Name

attfn := defaultAttachFunc(restClient, pod.Name, containerName, config)

err = attfn()
if err != nil {
return false, nil
}

return true, nil
})

if err != nil {
panic(err)
}
if len(pl.Items) == 0 {
panic("pod not found")
}
pod := &pl.Items[0]

if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
panic(fmt.Errorf("cannot attach into a container in a completed pod; current phase is %s", pod.Status.Phase))
}

if len(pod.Spec.Containers) != 1 {
panic("unexpected number of containers in trace job pod")
}

restClient := clientset.CoreV1().RESTClient().(*restclient.RESTClient)
containerName := pod.Spec.Containers[0].Name
attfn := defaultAttachFunc(restClient, pod.Name, containerName, config)

err = attfn()

if err != nil {
panic(err)
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

s := <-c
fmt.Println("signal:", s)
}
Expand Down
9 changes: 5 additions & 4 deletions tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func CreateJob(jobClient batchv1typed.JobInterface) (*batchv1.Job, error) {
},
},
Spec: batchv1.JobSpec{
Parallelism: int32Ptr(1),
Completions: int32Ptr(1),
ActiveDeadlineSeconds: int64Ptr(100), // TODO(fntlnz): allow canceling from kubectl and increase this
BackoffLimit: int32Ptr(1),
TTLSecondsAfterFinished: int32Ptr(5),
Parallelism: int32Ptr(1),
Completions: int32Ptr(1),
ActiveDeadlineSeconds: int64Ptr(100), // TODO(fntlnz): allow canceling from kubectl and increase this
BackoffLimit: int32Ptr(1),
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "test-renzo-pod",
Expand Down

0 comments on commit e2cbf39

Please sign in to comment.