Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit def6d3e

Browse files
committed
Updates e2e upgrade tests to use only client-go
Signed-off-by: JoshVanL <[email protected]>
1 parent eacd61a commit def6d3e

File tree

2 files changed

+69
-60
lines changed

2 files changed

+69
-60
lines changed

cmd/run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func NewRunCommand(stopCh <-chan struct{}) *cobra.Command {
4343
},
4444
}
4545
ssoptionsWithLB := ssoptions.WithLoopback()
46-
//secureServingOptions = secureServingOptions.WithLoopback()
4746

4847
clientConfigFlags := genericclioptions.NewConfigFlags(true)
4948

pkg/e2e/upgrade_test.go

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import (
1414
corev1 "k8s.io/api/core/v1"
1515
rbacv1 "k8s.io/api/rbac/v1"
1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17-
"k8s.io/cli-runtime/pkg/genericclioptions"
1817
"k8s.io/client-go/kubernetes"
18+
"k8s.io/client-go/kubernetes/scheme"
1919
"k8s.io/client-go/rest"
2020
"k8s.io/client-go/tools/portforward"
21+
"k8s.io/client-go/tools/remotecommand"
2122
"k8s.io/client-go/transport/spdy"
22-
"k8s.io/kubernetes/pkg/kubectl/cmd/exec"
23-
cmdportforward "k8s.io/kubernetes/pkg/kubectl/cmd/portforward"
2423

2524
"github.com/jetstack/kube-oidc-proxy/pkg/utils"
2625
)
@@ -119,97 +118,108 @@ func Test_Upgrade(t *testing.T) {
119118
t.Fatal(err)
120119
}
121120

121+
// wait for our echo server to become ready
122122
err = utils.WaitForPodReady(e2eSuite.kubeclient,
123123
"echoserver", namespaceUpgradeTest)
124124
if err != nil {
125125
t.Fatal(err)
126126
}
127127

128-
var execOut bytes.Buffer
129-
var execErr bytes.Buffer
128+
RESTClient, err := rest.RESTClientFor(restConfig)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
130132

131133
// curl echo server from within pod
132-
ioStreams := genericclioptions.IOStreams{In: nil, Out: &execOut, ErrOut: &execErr}
133-
execOptions := &exec.ExecOptions{
134-
StreamOptions: exec.StreamOptions{
135-
IOStreams: ioStreams,
136-
PodName: pod.Name,
137-
Namespace: pod.Namespace,
138-
},
139-
Command: []string{
140-
"curl", "127.0.0.1:8080", "-s", "-d", "hello world",
141-
},
142-
PodClient: kubeclient.CoreV1(),
143-
Config: restConfig,
144-
Executor: &exec.DefaultRemoteExecutor{},
134+
req := RESTClient.Post().
135+
Resource("pods").
136+
Name(pod.Name).
137+
Namespace(pod.Namespace).
138+
SubResource("exec").
139+
VersionedParams(&corev1.PodExecOptions{
140+
Container: "echoserver",
141+
Command: []string{
142+
"curl", "127.0.0.1:8080", "-s", "-d", "hello world",
143+
},
144+
Stdin: false,
145+
Stdout: true,
146+
Stderr: true,
147+
TTY: false,
148+
}, scheme.ParameterCodec)
149+
150+
exec, err := remotecommand.NewSPDYExecutor(restConfig, "POST", req.URL())
151+
if err != nil {
152+
t.Fatalf("failed to create SPDY executor: %s", err)
145153
}
154+
execOut := &bytes.Buffer{}
155+
execErr := &bytes.Buffer{}
146156

147-
if err := execOptions.Validate(); err != nil {
148-
t.Fatal(err)
157+
sopt := remotecommand.StreamOptions{
158+
Stdout: execOut,
159+
Stderr: execErr,
160+
Tty: false,
149161
}
150162

151-
if err := execOptions.Run(); err != nil {
152-
t.Fatal(err)
163+
err = exec.Stream(sopt)
164+
if err != nil {
165+
t.Fatalf("failed to execute stream command: %s", err)
153166
}
154167

155168
// should have no stderr output
156169
if execErr.String() != "" {
157170
t.Errorf("got curl error: %s", execErr.String())
158171
}
159172

173+
t.Logf("%s/%s: %s", pod.Namespace, pod.Name, execOut.String())
174+
160175
// should have correct stdout output from echo server
161176
if !strings.HasSuffix(execOut.String(), "BODY:\nhello world") {
162177
t.Errorf("got unexpected echoserver response: exp=...hello world got=%s",
163178
execOut.String())
164179
}
165180

166181
// test we can port forward to the echo server and curl on localhost to it
167-
168-
var portOut bytes.Buffer
169-
var portErr bytes.Buffer
182+
portOut := &bytes.Buffer{}
183+
portErr := &bytes.Buffer{}
170184

171185
freePort, err := utils.FreePort()
172186
if err != nil {
173187
t.Fatal(err)
174188
}
175189

176-
RESTClient, err := rest.RESTClientFor(restConfig)
177-
if err != nil {
178-
t.Fatal(err)
179-
}
180-
181-
ioStreams = genericclioptions.IOStreams{In: nil, Out: &portOut, ErrOut: &portErr}
182-
183-
portForwardOptions := &cmdportforward.PortForwardOptions{
184-
Namespace: pod.Namespace,
185-
PodName: pod.Name,
186-
RESTClient: RESTClient,
187-
Config: restConfig,
188-
PodClient: kubeclient.CoreV1(),
189-
Address: []string{"127.0.0.1"},
190-
Ports: []string{freePort + ":8080"},
191-
PortForwarder: &defaultPortForwarder{
192-
IOStreams: ioStreams,
193-
},
194-
StopChannel: make(chan struct{}, 1),
195-
ReadyChannel: make(chan struct{}),
190+
req = RESTClient.Post().
191+
Resource("pods").
192+
Namespace(pod.Namespace).
193+
Name(pod.Name).
194+
SubResource("portforward")
195+
196+
pfopts := &portForwardOptions{
197+
address: []string{"127.0.0.1"},
198+
ports: []string{freePort + ":8080"},
199+
stopCh: make(chan struct{}, 1),
200+
readyCh: make(chan struct{}),
201+
outBuf: portOut,
202+
errBuf: portErr,
203+
restConfig: restConfig,
196204
}
197205

198206
go func() {
199-
defer close(portForwardOptions.StopChannel)
207+
defer close(pfopts.stopCh)
200208

201209
// give a chance to establish a connection
202210
time.Sleep(time.Second * 2)
203211

204212
portInR := bytes.NewReader([]byte("hello world"))
205213

214+
// send message through port forward
206215
resp, err := http.Post(
207216
fmt.Sprintf("http://127.0.0.1:%s", freePort), "", portInR)
208217
if err != nil {
209218
t.Errorf("failed to request echoserver from port forward: %s", err)
210219
return
211220
}
212221

222+
// expect 200 resp and correct body
213223
if resp.StatusCode != 200 {
214224
t.Errorf("got unexpected response code from server, exp=200 got=%d",
215225
resp.StatusCode)
@@ -228,31 +238,31 @@ func Test_Upgrade(t *testing.T) {
228238
}
229239
}()
230240

231-
if err := portForwardOptions.Validate(); err != nil {
232-
t.Fatal(err)
241+
if err := forwardPorts("POST", req.URL(), pfopts); err != nil {
242+
t.Error(err)
243+
return
233244
}
234245

235-
if err := portForwardOptions.RunPortForward(); err != nil {
236-
t.Fatal(err)
237-
}
238246
}
239247

240-
// taken from k8s.io/kubernetes/pkg/kubectl/cmd/portforward
241-
type defaultPortForwarder struct {
242-
genericclioptions.IOStreams
248+
type portForwardOptions struct {
249+
address, ports []string
250+
readyCh, stopCh chan struct{}
251+
outBuf, errBuf *bytes.Buffer
252+
restConfig *rest.Config
243253
}
244254

245-
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts cmdportforward.PortForwardOptions) error {
246-
transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
255+
func forwardPorts(method string, url *url.URL, opts *portForwardOptions) error {
256+
transport, upgrader, err := spdy.RoundTripperFor(opts.restConfig)
247257
if err != nil {
248258
return err
249259
}
250260
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, url)
251-
fw, err := portforward.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, opts.ReadyChannel, f.Out, f.ErrOut)
261+
fw, err := portforward.NewOnAddresses(dialer, opts.address,
262+
opts.ports, opts.stopCh, opts.readyCh, opts.outBuf, opts.errBuf)
252263
if err != nil {
253264
return err
254265
}
266+
255267
return fw.ForwardPorts()
256268
}
257-
258-
//

0 commit comments

Comments
 (0)