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

Commit e898f9b

Browse files
adding vn-agent healthcheck & ability to boot from kconfig
Signed-off-by: Chris Hein <[email protected]>
1 parent 4826e22 commit e898f9b

File tree

7 files changed

+38
-9
lines changed

7 files changed

+38
-9
lines changed

virtualcluster/cmd/vn-agent/app/options/options.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type ServerOption struct {
5151
// Port is the vn-agent server listening on.
5252
Port uint
5353

54+
// Kubeconfig is the supercluster Kubeconfig to connect to
55+
Kubeconfig string
56+
5457
// FeatureGates enabled by the user.
5558
FeatureGates map[string]bool
5659
}
@@ -84,6 +87,7 @@ func (o *Options) Flags() cliflag.NamedFlagSets {
8487
serverFS.StringVar(&o.CertDirectory, "cert-dir", o.CertDirectory, "CertDirectory is the directory where the TLS certs are located")
8588
serverFS.StringVar(&o.TLSCertFile, "tls-cert-file", o.TLSCertFile, "TLSCertFile is the file containing x509 Certificate for HTTPS")
8689
serverFS.StringVar(&o.TLSPrivateKeyFile, "tls-private-key-file", o.TLSPrivateKeyFile, "TLSPrivateKeyFile is the file containing x509 private key matching tlsCertFile")
90+
serverFS.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path to kubeconfig file with authorization and master location information.")
8791
serverFS.UintVar(&o.Port, "port", 10550, "Port is the server listening on")
8892
serverFS.Var(cliflag.NewMapStringBool(&o.ServerOption.FeatureGates), "feature-gates", "A set of key=value pairs that describe featuregate gates for various features.")
8993

virtualcluster/cmd/vn-agent/app/server.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/component-base/cli/globalflag"
3131
"k8s.io/component-base/term"
3232
"k8s.io/klog"
33+
"k8s.io/kubernetes/pkg/healthz"
3334

3435
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/cmd/vn-agent/app/options"
3536
utilflag "sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/util/flag"
@@ -90,7 +91,7 @@ func NewVnAgentCommand(stopChan <-chan struct{}) *cobra.Command {
9091

9192
// Run start the vn-agent server.
9293
func Run(c *config.Config, serverOption *options.ServerOption, stopCh <-chan struct{}) error {
93-
handler, err := server.NewServer(c)
94+
handler, err := server.NewServer(c, serverOption)
9495
if err != nil {
9596
return errors.Wrapf(err, "create server")
9697
}
@@ -131,6 +132,14 @@ func Run(c *config.Config, serverOption *options.ServerOption, stopCh <-chan str
131132
errCh <- err
132133
}()
133134

135+
go func() {
136+
// start a health http server.
137+
mux := http.NewServeMux()
138+
healthz.InstallHandler(mux)
139+
klog.Fatal(http.ListenAndServe(":8080", mux))
140+
errCh <- err
141+
}()
142+
134143
select {
135144
case <-stopCh:
136145
klog.Infof("closing server...")

virtualcluster/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd h1:sOHNzJIkytDF6qadMNKhhD
10821082
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
10831083
k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7 h1:vEx13qjvaZ4yfObSSXW7BrMc/KQBBT/Jyee8XtLf4x0=
10841084
k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7/go.mod h1:wXW5VT87nVfh/iLV8FpR2uDvrFyomxbtb1KivDbvPTE=
1085+
k8s.io/kubernetes v0.20.2 h1:KxQftTwyg3DRlwKDcI6wla6eLygmMJ21FXlE4o7QcvU=
10851086
k8s.io/kubernetes v0.20.2/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
10861087
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
10871088
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 h1:0T5IaWHO3sJTEmCP6mUlBvMukxPKUQWqiI/YuiBNMiQ=

virtualcluster/pkg/controller/controllers/suite_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var cfg *rest.Config
4444
var cli client.Client
4545
var testEnv *envtest.Environment
4646

47-
const timeout = time.Second * 10
48-
const longTimeout = time.Second * 20
47+
const timeout = time.Second * 20
48+
const longTimeout = time.Second * 60
4949
const interval = time.Millisecond * 250
5050

5151
func TestAPIs(t *testing.T) {

virtualcluster/pkg/vn-agent/server/server.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ package server
1818

1919
import (
2020
"crypto/tls"
21+
"crypto/x509"
2122
"net/http"
2223
"net/url"
2324

2425
"github.com/emicklei/go-restful"
2526
"github.com/pkg/errors"
2627
"k8s.io/client-go/rest"
28+
"k8s.io/client-go/tools/clientcmd"
2729
certutil "k8s.io/client-go/util/cert"
2830

31+
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/cmd/vn-agent/app/options"
2932
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/config"
3033
)
3134

@@ -44,7 +47,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
4447
}
4548

4649
// NewServer initializes and configures a vn-agent.Server object to handle HTTP requests.
47-
func NewServer(cfg *config.Config) (*Server, error) {
50+
func NewServer(cfg *config.Config, serverOption *options.ServerOption) (*Server, error) {
4851
u, err := url.Parse(cfg.KubeletServerHost)
4952
if err != nil {
5053
return nil, errors.Wrap(err, "parse kubelet server url")
@@ -66,17 +69,27 @@ func NewServer(cfg *config.Config) (*Server, error) {
6669
},
6770
}
6871
} else {
69-
restConfig, err := rest.InClusterConfig()
70-
if err != nil {
71-
return nil, errors.Wrapf(err, "failed to get in cluster config")
72+
var restConfig *rest.Config
73+
var caCrtPool *x509.CertPool
74+
if len(serverOption.Kubeconfig) == 0 {
75+
restConfig, err = rest.InClusterConfig()
76+
if err != nil {
77+
return nil, errors.Wrapf(err, "failed to get in cluster config")
78+
}
79+
caCrtPool, err = certutil.NewPool(restConfig.TLSClientConfig.CAFile)
80+
} else {
81+
// This creates a client, first loading any specified kubeconfig\
82+
restConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
83+
&clientcmd.ClientConfigLoadingRules{ExplicitPath: serverOption.Kubeconfig},
84+
&clientcmd.ConfigOverrides{}).ClientConfig()
85+
caCrtPool, err = certutil.NewPoolFromBytes(restConfig.TLSClientConfig.CAData)
7286
}
7387
server.restConfig = restConfig
7488
superHttpsUrl, err := url.Parse(restConfig.Host)
7589
if err != nil {
7690
return nil, errors.Wrapf(err, "unable to parse apiserver address")
7791
}
7892
server.superAPIServerAddress = superHttpsUrl
79-
caCrtPool, err := certutil.NewPool(restConfig.TLSClientConfig.CAFile)
8093
if err != nil {
8194
return nil, errors.Wrapf(err, "unable to parse ca file")
8295
}

virtualcluster/pkg/vn-agent/server/test/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmK
548548
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
549549
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
550550
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
551+
github.com/imdario/mergo v0.3.10 h1:6q5mVkdH/vYmqngx7kZQTjJ5HRsx+ImorDIEQ+beJgc=
551552
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
552553
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
553554
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=

virtualcluster/pkg/vn-agent/server/test/server_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import (
6161
"k8s.io/kubernetes/pkg/volume"
6262
"k8s.io/utils/pointer"
6363

64+
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/cmd/vn-agent/app/options"
6465
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/config"
6566
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/server"
6667
"sigs.k8s.io/cluster-api-provider-nested/virtualcluster/pkg/vn-agent/testcerts"
@@ -124,7 +125,7 @@ func newServerTestWithDebug(enableDebugging bool, streamingServer streaming.Serv
124125
server, err := server.NewServer(&config.Config{
125126
KubeletClientCert: &kubeletClientCert,
126127
KubeletServerHost: fv.kubeletServer.testHTTPServer.URL,
127-
})
128+
}, &options.ServerOption{})
128129
if err != nil {
129130
panic(errors.Wrap(err, "new server"))
130131
}

0 commit comments

Comments
 (0)