Skip to content

Commit 465de92

Browse files
committed
feat(cmd/attach): attach command in reorg
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent 4d8a9c1 commit 465de92

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

pkg/cmd/attach.go

+100-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56

6-
"github.com/davecgh/go-spew/spew"
7+
"github.com/fntlnz/kubectl-trace/pkg/attacher"
78
"github.com/fntlnz/kubectl-trace/pkg/factory"
9+
"github.com/fntlnz/kubectl-trace/pkg/meta"
10+
"github.com/fntlnz/kubectl-trace/pkg/signals"
11+
"github.com/fntlnz/kubectl-trace/pkg/tracejob"
812
"github.com/spf13/cobra"
13+
"k8s.io/apimachinery/pkg/types"
914
"k8s.io/cli-runtime/pkg/genericclioptions"
15+
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
16+
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
17+
"k8s.io/client-go/rest"
1018
)
1119

1220
var (
@@ -26,6 +34,10 @@ var (
2634
// AttachOptions ...
2735
type AttachOptions struct {
2836
genericclioptions.IOStreams
37+
traceID *types.UID
38+
traceName *string
39+
namespace string
40+
clientConfig *rest.Config
2941
}
3042

3143
// NewAttachOptions provides an instance of AttachOptions with default values.
@@ -40,16 +52,99 @@ func NewAttachCommand(factory factory.Factory, streams genericclioptions.IOStrea
4052
o := NewAttachOptions(streams)
4153

4254
cmd := &cobra.Command{
43-
Use: "attach TRACE_ID",
55+
Use: "attach (TRACE_ID | TRACE_NAME)",
4456
DisableFlagsInUseLine: true,
4557
Short: attachShort,
4658
Long: attachLong, // Wrap with templates.LongDesc()
4759
Example: fmt.Sprintf(attachExamples, "kubectl"), // Wrap with templates.Examples()
48-
Run: func(cmd *cobra.Command, args []string) {
49-
fmt.Println("attach")
50-
spew.Dump(o)
60+
PreRunE: func(c *cobra.Command, args []string) error {
61+
return o.Validate(c, args)
62+
},
63+
RunE: func(c *cobra.Command, args []string) error {
64+
if err := o.Complete(factory, c, args); err != nil {
65+
return err
66+
}
67+
if err := o.Run(); err != nil {
68+
fmt.Fprintln(o.ErrOut, err.Error())
69+
return nil
70+
}
71+
return nil
5172
},
5273
}
5374

5475
return cmd
5576
}
77+
78+
func (o *AttachOptions) Validate(cmd *cobra.Command, args []string) error {
79+
switch len(args) {
80+
case 1:
81+
if meta.IsObjectName(args[0]) {
82+
o.traceName = &args[0]
83+
} else {
84+
tid := types.UID(args[0])
85+
o.traceID = &tid
86+
}
87+
break
88+
default:
89+
return fmt.Errorf("(TRACE_ID | TRACE_NAME) is a required argument for the attach command")
90+
}
91+
92+
return nil
93+
}
94+
95+
func (o *AttachOptions) Complete(factory factory.Factory, cmd *cobra.Command, args []string) error {
96+
// Prepare namespace
97+
var err error
98+
o.namespace, _, err = factory.ToRawKubeConfigLoader().Namespace()
99+
if err != nil {
100+
return err
101+
}
102+
103+
//// Prepare client
104+
o.clientConfig, err = factory.ToRESTConfig()
105+
if err != nil {
106+
return err
107+
}
108+
109+
return nil
110+
}
111+
112+
func (o *AttachOptions) Run() error {
113+
jobsClient, err := batchv1client.NewForConfig(o.clientConfig)
114+
if err != nil {
115+
return err
116+
}
117+
118+
coreClient, err := corev1client.NewForConfig(o.clientConfig)
119+
if err != nil {
120+
return err
121+
}
122+
123+
tc := &tracejob.TraceJobClient{
124+
JobClient: jobsClient.Jobs(o.namespace),
125+
}
126+
127+
tf := tracejob.TraceJobFilter{
128+
Name: o.traceName,
129+
ID: o.traceID,
130+
}
131+
132+
jobs, err := tc.GetJob(tf)
133+
134+
if err != nil {
135+
return err
136+
}
137+
138+
if len(jobs) == 0 {
139+
return fmt.Errorf("no trace found with the provided criterias")
140+
}
141+
142+
job := jobs[0]
143+
144+
ctx := context.Background()
145+
ctx = signals.WithStandardSignals(ctx)
146+
a := attacher.NewAttacher(coreClient, o.clientConfig, o.IOStreams)
147+
a.WithContext(ctx)
148+
a.AttachJob(job.ID, job.Namespace)
149+
return nil
150+
}

pkg/cmd/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
%[1]s trace delete kubectl-trace-1bb3ae39-efe8-11e8-9f29-8c164500a77e
2828
2929
# Delete all bpftrace programs in a specific namespace
30-
%[1]s trace delete -n myns"`
30+
%[1]s trace delete -n myns --all`
3131
)
3232

3333
// DeleteOptions ...

0 commit comments

Comments
 (0)