Skip to content

Commit feb3f92

Browse files
authored
Merge pull request kubernetes#128712 from kannon92/fix-stream-test-failures
fix PodLogsQuerySplitStream if feature is enabled and using defaults
2 parents 7fe41da + 3d08c10 commit feb3f92

File tree

2 files changed

+90
-9
lines changed

2 files changed

+90
-9
lines changed

pkg/registry/core/pod/rest/log.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/kubernetes/pkg/apis/core/validation"
3434
"k8s.io/kubernetes/pkg/kubelet/client"
3535
"k8s.io/kubernetes/pkg/registry/core/pod"
36+
"k8s.io/utils/ptr"
3637

3738
// ensure types are installed
3839
_ "k8s.io/kubernetes/pkg/apis/core/install"
@@ -88,8 +89,12 @@ func (r *LogREST) Get(ctx context.Context, name string, opts runtime.Object) (ru
8889

8990
countSkipTLSMetric(logOpts.InsecureSkipTLSVerifyBackend)
9091

91-
if !utilfeature.DefaultFeatureGate.Enabled(features.PodLogsQuerySplitStreams) {
92-
logOpts.Stream = nil
92+
if utilfeature.DefaultFeatureGate.Enabled(features.PodLogsQuerySplitStreams) {
93+
// Even with defaulters, logOpts.Stream can be nil if no arguments are provided at all.
94+
if logOpts.Stream == nil {
95+
// Default to "All" to maintain backward compatibility.
96+
logOpts.Stream = ptr.To(api.LogStreamAll)
97+
}
9398
}
9499
if errs := validation.ValidatePodLogOptions(logOpts, utilfeature.DefaultFeatureGate.Enabled(features.PodLogsQuerySplitStreams)); len(errs) > 0 {
95100
return nil, errors.NewInvalid(api.Kind("PodLogOptions"), name, errs)

pkg/registry/core/pod/rest/log_test.go

+83-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"fmt"
21+
"strings"
2022
"testing"
2123

2224
"k8s.io/apimachinery/pkg/api/errors"
2325
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
2426
"k8s.io/apiserver/pkg/registry/generic"
2527
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
28+
utilfeature "k8s.io/apiserver/pkg/util/feature"
29+
featuregatetesting "k8s.io/component-base/featuregate/testing"
2630
api "k8s.io/kubernetes/pkg/apis/core"
31+
"k8s.io/kubernetes/pkg/features"
2732
"k8s.io/kubernetes/pkg/registry/registrytest"
33+
"k8s.io/utils/ptr"
2834
)
2935

3036
func TestPodLogValidates(t *testing.T) {
@@ -40,16 +46,86 @@ func TestPodLogValidates(t *testing.T) {
4046
}
4147
logRest := &LogREST{Store: store, KubeletConn: nil}
4248

49+
// This test will panic if you don't have a validation failure.
4350
negativeOne := int64(-1)
44-
testCases := []*api.PodLogOptions{
45-
{SinceSeconds: &negativeOne},
46-
{TailLines: &negativeOne},
51+
testCases := []struct {
52+
name string
53+
podOptions api.PodLogOptions
54+
podQueryLogOptions bool
55+
invalidStreamMatch string
56+
}{
57+
{
58+
name: "SinceSeconds",
59+
podOptions: api.PodLogOptions{
60+
SinceSeconds: &negativeOne,
61+
},
62+
},
63+
{
64+
name: "TailLines",
65+
podOptions: api.PodLogOptions{
66+
TailLines: &negativeOne,
67+
},
68+
},
69+
{
70+
name: "StreamWithGateOff",
71+
podOptions: api.PodLogOptions{
72+
SinceSeconds: &negativeOne,
73+
},
74+
podQueryLogOptions: false,
75+
},
76+
{
77+
name: "StreamWithGateOnDefault",
78+
podOptions: api.PodLogOptions{
79+
SinceSeconds: &negativeOne,
80+
},
81+
podQueryLogOptions: true,
82+
},
83+
{
84+
name: "StreamWithGateOnAll",
85+
podOptions: api.PodLogOptions{
86+
SinceSeconds: &negativeOne,
87+
Stream: ptr.To(api.LogStreamAll),
88+
},
89+
podQueryLogOptions: true,
90+
},
91+
{
92+
name: "StreamWithGateOnStdErr",
93+
podOptions: api.PodLogOptions{
94+
SinceSeconds: &negativeOne,
95+
Stream: ptr.To(api.LogStreamStderr),
96+
},
97+
podQueryLogOptions: true,
98+
},
99+
{
100+
name: "StreamWithGateOnStdOut",
101+
podOptions: api.PodLogOptions{
102+
SinceSeconds: &negativeOne,
103+
Stream: ptr.To(api.LogStreamStdout),
104+
},
105+
podQueryLogOptions: true,
106+
},
107+
{
108+
name: "StreamWithGateOnAndBadValue",
109+
podOptions: api.PodLogOptions{
110+
Stream: ptr.To("nostream"),
111+
},
112+
podQueryLogOptions: true,
113+
invalidStreamMatch: "nostream",
114+
},
47115
}
48116

49117
for _, tc := range testCases {
50-
_, err := logRest.Get(genericapirequest.NewDefaultContext(), "test", tc)
51-
if !errors.IsInvalid(err) {
52-
t.Fatalf("Unexpected error: %v", err)
53-
}
118+
t.Run(tc.name, func(t *testing.T) {
119+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodLogsQuerySplitStreams, tc.podQueryLogOptions)
120+
_, err := logRest.Get(genericapirequest.NewDefaultContext(), "test", &tc.podOptions)
121+
if !errors.IsInvalid(err) {
122+
t.Fatalf("Unexpected error: %v", err)
123+
}
124+
if tc.invalidStreamMatch != "" {
125+
if !strings.Contains(err.Error(), "nostream") {
126+
t.Error(fmt.Printf("Expected %s got %s", err.Error(), "nostream"))
127+
}
128+
}
129+
})
54130
}
55131
}

0 commit comments

Comments
 (0)