8000 Allow pods without labels to be selected in the resource query (#223) · stern/stern@fc51906 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit fc51906

Browse files
author
Takashi Kusumi
authored
Allow pods without labels to be selected in the resource query (#223)
1 parent 6e0d5fc commit fc51906

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

stern/stern.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func Run(ctx context.Context, config *Config) error {
9393
}
9494
resource.kind, resource.name = parts[0], parts[1]
9595
if PodMatcher.Matches(resource.kind) {
96-
// Pods can share the same labels, so we also set the pod query.
96+
// Pods might have no labels or share the same labels,
97+
// so we use an exact match instead.
9798
podName, err := regexp.Compile("^" + resource.name + "$")
9899
if err != nil {
99100
return errors.Wrap(err, "failed to compile regular expression for pod")
@@ -244,6 +245,10 @@ func chooseSelector(ctx context.Context, client clientset.Interface, namespace,
244245
if kind == "" {
245246
return selector, nil
246247
}
248+
if PodMatcher.Matches(kind) {
249+
// We use an exact match for pods instead of a label to select pods without labels.
250+
return labels.Everything(), nil
251+
}
247252
labelMap, err := retrieveLabelsFromResource(ctx, client, namespace, kind, name)
248253
if err != nil {
249254
return nil, err
@@ -258,12 +263,6 @@ func retrieveLabelsFromResource(ctx context.Context, client clientset.Interface,
258263
opt := metav1.GetOptions{}
259264
switch {
260265
// core
261-
case PodMatcher.Matches(kind):
262-
o, err := client.CoreV1().Pods(namespace).Get(ctx, name, opt)
263-
if err != nil {
264-
return nil, err
265-
}
266-
return o.Labels, nil
267266
case ReplicationControllerMatcher.Matches(kind):
268267
o, err := client.CoreV1().ReplicationControllers(namespace).Get(ctx, name, opt)
269268
if err != nil {

stern/stern_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ func TestRetrieveLabelsFromResource(t *testing.T) {
3333
}
3434
objs := []runtime.Object{
3535
// core
36-
&corev1.Pod{
37-
ObjectMeta: metav1.ObjectMeta{
38-
Name: "pod1",
39-
Namespace: "ns1",
40-
Labels: map[string]string{
41-
"app": "pod-label",
42-
},
43-
},
44-
},
4536
&corev1.ReplicationController{
4637
ObjectMeta: genMeta("rc1"),
4738
Spec: corev1.ReplicationControllerSpec{
@@ -114,12 +105,6 @@ func TestRetrieveLabelsFromResource(t *testing.T) {
114105
wantError bool
115106
}{
116107
// core
117-
{
118-
desc: "pods",
119-
kinds: []string{"po", "pods", "pod"},
120-
name: "pod1",
121-
label: "pod-label",
122-
},
123108
{
124109
desc: "replicationcontrollers",
125110
kinds: []string{"rc", "replicationcontrollers", "replicationcontroller"},

0 commit comments

Comments
 (0)
0