File: main.go

package info (click to toggle)
golang-k8s-sigs-apiserver-network-proxy 0.33.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: makefile: 220; sh: 118
file content (218 lines) | stat: -rw-r--r-- 5,679 bytes parent folder | download
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"context"
	"flag"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"strconv"
	"syscall"
	"time"

	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
	"k8s.io/klog/v2"
	"sigs.k8s.io/apiserver-network-proxy/pkg/util"
)

func main() {
	testServer := &TestServer{}
	o := newTestServerRunOptions()
	command := newTestServerCommand(testServer, o)
	flags := command.Flags()
	flags.AddFlagSet(o.Flags())
	local := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
	klog.InitFlags(local)
	local.VisitAll(func(fl *flag.Flag) {
		fl.Name = util.Normalize(fl.Name)
		flags.AddGoFlag(fl)
	})
	if err := command.Execute(); err != nil {
		klog.Errorf("error: %v\n", err)
		klog.Flush()
		os.Exit(1)
	}
}

type TestServerRunOptions struct {
	// Port we listen for server connections on.
	serverPort uint
}

func (o *TestServerRunOptions) Flags() *pflag.FlagSet {
	flags := pflag.NewFlagSet("proxy-server", pflag.ContinueOnError)
	flags.UintVar(&o.serverPort, "server-port", o.serverPort, "Port we listen for server connections on. Set to 0 for UDS.")
	return flags
}

func (o *TestServerRunOptions) Print() {
	klog.Warningf("Server port set to %d.\n", o.serverPort)
}

func (o *TestServerRunOptions) Validate() error {
	if o.serverPort > 49151 {
		return fmt.Errorf("please do not try to use ephemeral port %d for the server port", o.serverPort)
	}
	if o.serverPort < 1024 {
		return fmt.Errorf("please do not try to use reserved port %d for the server port", o.serverPort)
	}

	return nil
}

func newTestServerRunOptions() *TestServerRunOptions {
	o := TestServerRunOptions{
		serverPort: 8000,
	}
	return &o
}

func newTestServerCommand(p *TestServer, o *TestServerRunOptions) *cobra.Command {
	cmd := &cobra.Command{
		Use:  "test http server",
		Long: `A test http server, url determines behavior for certain tests.`,
		RunE: func(_ *cobra.Command, _ []string) error {
			return p.run(o)
		},
	}

	return cmd
}

type TestServer struct {
}

type StopFunc func()

func (p *TestServer) run(o *TestServerRunOptions) error {
	o.Print()
	if err := o.Validate(); err != nil {
		return fmt.Errorf("failed to validate server options with %v", err)
	}
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	klog.Info("Starting test http server for client requests.")
	testStop, err := p.runTestServer(ctx, o)
	if err != nil {
		return fmt.Errorf("failed to run the test server: %v", err)
	}

	stopCh := SetupSignalHandler()
	<-stopCh
	klog.Info("Shutting down server.")

	if testStop != nil {
		testStop()
	}

	return nil
}

var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

func SetupSignalHandler() (stopCh <-chan struct{}) {
	stop := make(chan struct{})
	c := make(chan os.Signal, 2)
	signal.Notify(c, shutdownSignals...)
	go func() {
		<-c
		close(stop)
		<-c
		os.Exit(1) // second signal. Exit directly.
	}()

	return stop
}

func returnSuccess(w http.ResponseWriter, _ *http.Request) {
	fmt.Fprintf(w, "<!DOCTYPE html>\n<html>\n    <head>\n        <title>Success</title>\n    </head>\n    <body>\n        <p>The success test page!</p>\n    </body>\n</html>")
}

func returnError(w http.ResponseWriter, _ *http.Request) {
	http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

func closeNoResponse(w http.ResponseWriter, _ *http.Request) {
	hj, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
		return
	}
	conn, _, err := hj.Hijack()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	err = conn.Close()
	if err != nil {
		klog.ErrorS(err, "failed to close connection")
	}
}

func sleepReturnSuccess(w http.ResponseWriter, req *http.Request) {
	sleepArr := req.URL.Query()["time"]
	sleepInt := 10
	var err error
	if len(sleepArr) == 1 {
		sleepStr := sleepArr[0]
		sleepInt, err = strconv.Atoi(sleepStr)
		if err != nil {
			sleepInt = 10
			klog.Warningf("Failed to parse sleep time (%s), default to %d, got error %v.", sleepStr, sleepInt, err)
		}
	} else {
		klog.Warningf("Sleep time(%v) was length %d defaulting to sleep %d.", sleepArr, len(sleepArr), sleepInt)
	}
	sleep := time.Duration(sleepInt) * time.Second
	time.Sleep(sleep)
	returnSuccess(w, req)
}

func (p *TestServer) runTestServer(ctx context.Context, o *TestServerRunOptions) (StopFunc, error) {
	muxHandler := http.NewServeMux()
	muxHandler.HandleFunc("/success", returnSuccess)
	muxHandler.HandleFunc("/sleep", sleepReturnSuccess)
	muxHandler.HandleFunc("/error", returnError)
	muxHandler.HandleFunc("/close", closeNoResponse)
	server := &http.Server{
		Addr:              fmt.Sprintf("127.0.0.1:%d", o.serverPort),
		Handler:           muxHandler,
		MaxHeaderBytes:    1 << 20,
		ReadHeaderTimeout: 60 * time.Second,
	}

	go func() {
		err := server.ListenAndServe()
		if err != nil {
			klog.Warningf("HTTP test server received %v.\n", err)
		}
		klog.Warningf("HTTP test server stopped listening\n")
	}()

	stop := func() {
		err := server.Shutdown(ctx)
		if err != nil {
			klog.ErrorS(err, "failed to shutdown server")
		}
	}
	return stop, nil
}