File: server.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 (303 lines) | stat: -rw-r--r-- 9,183 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright 2022 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 app

import (
	"bytes"
	"context"
	"crypto/tls"
	"fmt"
	"net"
	"net/http"
	"net/http/pprof"
	"os"
	"os/signal"
	"runtime"
	runpprof "runtime/pprof"
	"strconv"
	"strings"
	"syscall"
	"time"

	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/spf13/cobra"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	"google.golang.org/grpc/keepalive"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/client-go/kubernetes"
	coordinationv1lister "k8s.io/client-go/listers/coordination/v1"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/klog/v2"
	"k8s.io/utils/clock"
	"sigs.k8s.io/apiserver-network-proxy/cmd/agent/app/options"
	"sigs.k8s.io/apiserver-network-proxy/pkg/agent"
	"sigs.k8s.io/apiserver-network-proxy/pkg/util"
)

const (
	ReadHeaderTimeout   = 60 * time.Second
	LeaseInformerResync = time.Second * 10
)

func NewAgentCommand(a *Agent, o *options.GrpcProxyAgentOptions) *cobra.Command {
	cmd := &cobra.Command{
		Use:  "agent",
		Long: `A gRPC agent, Connects to the proxy and then allows traffic to be forwarded to it.`,
		RunE: func(_ *cobra.Command, _ []string) error {
			drainCh, stopCh := SetupSignalHandler()
			return a.Run(o, drainCh, stopCh)
		},
	}

	return cmd
}

type Agent struct {
	adminServer  *http.Server
	healthServer *http.Server

	cs *agent.ClientSet
}

func (a *Agent) Run(o *options.GrpcProxyAgentOptions, drainCh, stopCh <-chan struct{}) error {
	o.Print()
	if err := o.Validate(); err != nil {
		return fmt.Errorf("failed to validate agent options with %v", err)
	}

	cs, err := a.runProxyConnection(o, drainCh, stopCh)
	if err != nil {
		return fmt.Errorf("failed to run proxy connection with %v", err)
	}
	a.cs = cs

	if err := a.runHealthServer(o, cs); err != nil {
		return fmt.Errorf("failed to run health server with %v", err)
	}
	defer a.healthServer.Close()

	if err := a.runAdminServer(o); err != nil {
		return fmt.Errorf("failed to run admin server with %v", err)
	}
	defer a.adminServer.Close()

	<-stopCh
	klog.V(1).Infoln("Shutting down agent.")

	return nil
}

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

func SetupSignalHandler() (drainCh, stopCh <-chan struct{}) {
	drain := make(chan struct{})
	stop := make(chan struct{})
	c := make(chan os.Signal, 2)
	signal.Notify(c, shutdownSignals...)
	labels := runpprof.Labels(
		"core", "signalHandler",
	)
	go runpprof.Do(context.Background(), labels, func(context.Context) { handleSignals(c, drain, stop) })

	return drain, stop
}

func handleSignals(signalCh chan os.Signal, drainCh, stopCh chan struct{}) {
	s := <-signalCh
	klog.V(2).InfoS("Received first signal", "signal", s)
	close(drainCh)
	s = <-signalCh
	klog.V(2).InfoS("Received second signal", "signal", s)
	close(stopCh)
}

func (a *Agent) runProxyConnection(o *options.GrpcProxyAgentOptions, drainCh, stopCh <-chan struct{}) (*agent.ClientSet, error) {
	var tlsConfig *tls.Config
	var err error
	if tlsConfig, err = util.GetClientTLSConfig(o.CaCert, o.AgentCert, o.AgentKey, o.ProxyServerHost, o.AlpnProtos); err != nil {
		return nil, err
	}
	dialOptions := []grpc.DialOption{
		grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
		grpc.WithKeepaliveParams(keepalive.ClientParameters{
			Time:                o.KeepaliveTime,
			PermitWithoutStream: true,
		}),
	}
	cc := o.ClientSetConfig(dialOptions...)

	if o.CountServerLeases {
		var config *rest.Config
		if o.KubeconfigPath != "" {
			config, err = clientcmd.BuildConfigFromFlags("", o.KubeconfigPath)
			if err != nil {
				return nil, fmt.Errorf("failed to load kubernetes client config: %v", err)
			}
		} else {
			config, err = rest.InClusterConfig()
			if err != nil {
				return nil, fmt.Errorf("failed to load in cluster kubernetes client config: %w", err)
			}
		}
		config.ContentType = o.APIContentType

		k8sClient, err := kubernetes.NewForConfig(config)
		if err != nil {
			return nil, fmt.Errorf("failed to create kubernetes clientset: %v", err)
		}
		leaseInformer := agent.NewLeaseInformerWithMetrics(k8sClient, o.LeaseNamespace, LeaseInformerResync)
		go leaseInformer.Run(stopCh)
		cache.WaitForCacheSync(stopCh, leaseInformer.HasSynced)
		leaseLister := coordinationv1lister.NewLeaseLister(leaseInformer.GetIndexer())
		serverLeaseSelector, _ := labels.Parse(o.LeaseLabel)
		serverLeaseCounter := agent.NewServerLeaseCounter(
			clock.RealClock{},
			leaseLister,
			serverLeaseSelector,
		)
		cc.ServerLeaseCounter = serverLeaseCounter
	}

	cs := cc.NewAgentClientSet(drainCh, stopCh)
	cs.Serve()

	return cs, nil
}

func (a *Agent) runHealthServer(o *options.GrpcProxyAgentOptions, cs agent.ReadinessManager) error {
	livenessHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprintf(w, "ok")
	})

	checks := []agent.HealthChecker{agent.Ping, agent.NewServerConnected(cs)}
	readinessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var failedChecks []string
		var individualCheckOutput bytes.Buffer
		for _, check := range checks {
			if err := check.Check(r); err != nil {
				fmt.Fprintf(&individualCheckOutput, "[-]%s failed: %v\n", check.Name(), err)
				failedChecks = append(failedChecks, check.Name())
			} else {
				fmt.Fprintf(&individualCheckOutput, "[+]%s ok\n", check.Name())
			}
		}

		// Always be verbose if the check has failed
		if len(failedChecks) > 0 {
			klog.V(0).Infof("%s check failed: \n%v", strings.Join(failedChecks, ","), individualCheckOutput.String())
			w.WriteHeader(http.StatusServiceUnavailable)
			fmt.Fprint(w, individualCheckOutput.String())
			return
		}

		if _, found := r.URL.Query()["verbose"]; !found {
			w.WriteHeader(http.StatusOK)
			fmt.Fprint(w, "ok")
			return
		}

		fmt.Fprintf(&individualCheckOutput, "check passed\n")

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, individualCheckOutput.String())
	})

	muxHandler := http.NewServeMux()
	muxHandler.Handle("/metrics", promhttp.Handler())
	muxHandler.HandleFunc("/healthz", livenessHandler)
	// "/ready" is deprecated but being maintained for backward compatibility
	muxHandler.HandleFunc("/ready", readinessHandler)
	muxHandler.HandleFunc("/readyz", readinessHandler)
	a.healthServer = &http.Server{
		Addr:              net.JoinHostPort(o.HealthServerHost, strconv.Itoa(o.HealthServerPort)),
		Handler:           muxHandler,
		MaxHeaderBytes:    1 << 20,
		ReadHeaderTimeout: ReadHeaderTimeout,
	}

	labels := runpprof.Labels(
		"core", "healthListener",
		"port", strconv.Itoa(o.HealthServerPort),
	)
	go runpprof.Do(context.Background(), labels, func(context.Context) { a.serveHealth(a.healthServer) })

	return nil
}

func (a *Agent) serveHealth(healthServer *http.Server) {
	err := healthServer.ListenAndServe()
	if err != nil {
		klog.ErrorS(err, "health server could not listen")
	}
	klog.V(0).Infoln("Health server stopped listening")
}

func (a *Agent) runAdminServer(o *options.GrpcProxyAgentOptions) error {
	muxHandler := http.NewServeMux()
	muxHandler.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		host, _, err := net.SplitHostPort(r.Host)
		// The port number may be omitted if the admin server is running on port
		// 80, the default port for HTTP
		if err != nil {
			host = r.Host
		}
		dest := *r.URL
		dest.Host = net.JoinHostPort(host, strconv.Itoa(o.HealthServerPort))
		http.Redirect(w, r, dest.String(), http.StatusMovedPermanently)
	}))
	if o.EnableProfiling {
		muxHandler.HandleFunc("/debug/pprof", util.RedirectTo("/debug/pprof/"))
		muxHandler.HandleFunc("/debug/pprof/", pprof.Index)
		muxHandler.HandleFunc("/debug/pprof/profile", pprof.Profile)
		muxHandler.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
		muxHandler.HandleFunc("/debug/pprof/trace", pprof.Trace)
		if o.EnableContentionProfiling {
			runtime.SetBlockProfileRate(1)
		}
	}

	a.adminServer = &http.Server{
		Addr:              net.JoinHostPort(o.AdminBindAddress, strconv.Itoa(o.AdminServerPort)),
		Handler:           muxHandler,
		MaxHeaderBytes:    1 << 20,
		ReadHeaderTimeout: ReadHeaderTimeout,
	}

	labels := runpprof.Labels(
		"core", "adminListener",
		"port", strconv.Itoa(o.AdminServerPort),
	)
	go runpprof.Do(context.Background(), labels, func(context.Context) { a.serveAdmin(a.adminServer) })

	return nil
}

func (a *Agent) serveAdmin(adminServer *http.Server) {
	err := adminServer.ListenAndServe()
	if err != nil {
		klog.ErrorS(err, "admin server could not listen")
	}
	klog.V(0).Infoln("Admin server stopped listening")
}

// ClientSet exposes internal state for testing.
func (a *Agent) ClientSet() *agent.ClientSet {
	return a.cs
}