1
1
package cmd
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
6
- "github.com/davecgh/go-spew/spew "
7
+ "github.com/fntlnz/kubectl-trace/pkg/attacher "
7
8
"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"
8
12
"github.com/spf13/cobra"
13
+ "k8s.io/apimachinery/pkg/types"
9
14
"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"
10
18
)
11
19
12
20
var (
26
34
// AttachOptions ...
27
35
type AttachOptions struct {
28
36
genericclioptions.IOStreams
37
+ traceID * types.UID
38
+ traceName * string
39
+ namespace string
40
+ clientConfig * rest.Config
29
41
}
30
42
31
43
// NewAttachOptions provides an instance of AttachOptions with default values.
@@ -40,16 +52,99 @@ func NewAttachCommand(factory factory.Factory, streams genericclioptions.IOStrea
40
52
o := NewAttachOptions (streams )
41
53
42
54
cmd := & cobra.Command {
43
- Use : "attach TRACE_ID" ,
55
+ Use : "attach ( TRACE_ID | TRACE_NAME) " ,
44
56
DisableFlagsInUseLine : true ,
45
57
Short : attachShort ,
46
58
Long : attachLong , // Wrap with templates.LongDesc()
47
59
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
51
72
},
52
73
}
53
74
54
75
return cmd
55
76
}
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
+ }
0 commit comments