-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
145 lines (131 loc) · 3.12 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"regexp"
"time"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/knight42/kt/pkg/controller"
)
type Options struct {
color string
selector string
sinceSeconds time.Duration
sinceTime string
previous bool
timestamps bool
exitWithPods bool
noPrefix bool
tail int64
container string
nodeName string
restClientGetter genericclioptions.RESTClientGetter
namespace string
podNamePattern *regexp.Regexp
containerNamePattern *regexp.Regexp
}
func (o *Options) Complete(getter genericclioptions.RESTClientGetter, args []string) error {
o.restClientGetter = getter
var err error
o.namespace, _, err = getter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
if len(o.container) > 0 {
o.containerNamePattern, err = regexp.Compile(o.container)
if err != nil {
return err
}
}
switch o.color {
case "auto", "always", "never":
default:
return fmt.Errorf("unknown value of flag `color`: %s", o.color)
}
switch len(args) {
case 0:
if len(o.selector) == 0 {
return fmt.Errorf("empty selector")
}
case 1:
if len(o.selector) > 0 {
return fmt.Errorf("label selector cannot be used here")
}
o.podNamePattern, err = regexp.Compile(args[0])
if err != nil {
return err
}
case 2:
if len(o.selector) > 0 {
return fmt.Errorf("label selector cannot be used here")
}
result := newBuilder(getter).
NamespaceParam(o.namespace).DefaultNamespace().
Latest().
ResourceNames(args[0], args[1]).SingleResourceType().
RequireObject(true).
Do()
if err := result.Err(); err != nil {
return err
}
obj, err := result.Object()
if err != nil {
return err
}
if isPod(obj) {
name, _ := meta.NewAccessor().Name(obj)
o.podNamePattern, err = regexp.Compile(name)
if err != nil {
return err
}
} else {
selector, err := getPodsSelector(obj, getter)
if err != nil {
return err
}
o.selector = labels.FormatLabels(selector)
}
}
return nil
}
func (o *Options) Run(cmd *cobra.Command) error {
logsOptions, err := o.toLogsOptions()
if err != nil {
return err
}
c := controller.New(o.restClientGetter, &logsOptions,
controller.WithColor(o.color),
controller.WithPodLabelsSelector(o.selector),
controller.WithPodNameRegexp(o.podNamePattern),
controller.WithContainerNameRegexp(o.containerNamePattern),
controller.EnableExitWithPods(o.exitWithPods),
controller.WithPrefix(!o.noPrefix),
controller.WithNodeName(o.nodeName),
)
return c.Run()
}
func (o *Options) toLogsOptions() (corev1.PodLogOptions, error) {
opt := corev1.PodLogOptions{
Follow: !o.previous,
Timestamps: o.timestamps,
Previous: o.previous,
}
if len(o.sinceTime) > 0 {
t, err := parseRFC3339(o.sinceTime)
if err != nil {
return corev1.PodLogOptions{}, err
}
opt.SinceTime = &t
}
if o.sinceSeconds > 0 {
sec := int64(o.sinceSeconds.Round(time.Second).Seconds())
opt.SinceSeconds = &sec
}
if o.tail > 0 {
opt.TailLines = &o.tail
}
return opt, nil
}