forked from kubernetes-retired/cluster-api-provider-nested
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
152 lines (127 loc) · 4.22 KB
/
server.go
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
/*
Copyright 2019 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 (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
certutil "k8s.io/client-go/util/cert"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/cli/globalflag"
"k8s.io/component-base/term"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/healthz"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/cmd/vn-agent/app/options"
utilflag "sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/util/flag"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/version/verflag"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/certificate"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/config"
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/server"
)
func NewVnAgentCommand(stopChan <-chan struct{}) *cobra.Command {
s, err := options.NewVnAgentOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
}
cmd := &cobra.Command{
Use: "vn-agent",
Long: `The vn-agent is proxy between tenant apiserver and kubelet server on physical node.`,
Run: func(cmd *cobra.Command, args []string) {
verflag.PrintAndExitIfRequested()
utilflag.PrintFlags(cmd.Flags())
c, serverOptions, err := s.Config()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err := Run(c, serverOptions, stopChan); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
},
}
fs := cmd.Flags()
namedFlagSets := s.Flags()
verflag.AddFlags(namedFlagSets.FlagSet("global"))
globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name())
for _, f := range namedFlagSets.FlagSets {
fs.AddFlagSet(f)
}
usageFmt := "Usage:\n %s\n"
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStderr(), namedFlagSets, cols)
return nil
})
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
cliflag.PrintSections(cmd.OutOrStdout(), namedFlagSets, cols)
})
return cmd
}
// Run start the vn-agent server.
func Run(c *config.Config, serverOption *options.ServerOption, stopCh <-chan struct{}) error {
handler, err := server.NewServer(c, serverOption)
if err != nil {
return errors.Wrapf(err, "create server")
}
s := &http.Server{
Addr: fmt.Sprintf(":%d", serverOption.Port),
Handler: handler,
TLSConfig: &tls.Config{
ClientAuth: tls.RequestClientCert,
},
}
if serverOption.ClientCAFile != "" {
clientCAs, err := certutil.CertsFromFile(serverOption.ClientCAFile)
if err != nil {
return errors.Wrapf(err, "unable to load client CA file")
}
certPool := x509.NewCertPool()
for _, cert := range clientCAs {
certPool.AddCert(cert)
}
s.TLSConfig.ClientCAs = certPool
s.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
tlsConfig, err := certificate.InitializeTLS(serverOption.CertDirectory, serverOption.TLSCertFile, serverOption.TLSPrivateKeyFile, "vn")
if err != nil {
return errors.Wrapf(err, "failed to initial tls config")
}
klog.Infof("server listen on %s", s.Addr)
errCh := make(chan error)
go func() {
err := s.ListenAndServeTLS(tlsConfig.CertFile, tlsConfig.KeyFile)
errCh <- err
}()
go func() {
// start a health http server.
mux := http.NewServeMux()
healthz.InstallHandler(mux)
klog.Fatal(http.ListenAndServe(":8080", mux))
errCh <- err
}()
select {
case <-stopCh:
klog.Infof("closing server...")
s.Close()
case err := <-errCh:
klog.Errorf("server listen error %v", err)
}
return nil
}