2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/rancher/dynamiclistener"
|
|
|
|
"github.com/rancher/dynamiclistener/factory"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/file"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/kubernetes"
|
|
|
|
"github.com/rancher/dynamiclistener/storage/memory"
|
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/wrangler-api/pkg/generated/controllers/core"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Cluster) newListener(ctx context.Context) (net.Listener, http.Handler, error) {
|
2020-04-28 22:00:30 +00:00
|
|
|
tcp, err := dynamiclistener.NewTCPListener(c.config.BindAddress, c.config.SupervisorPort)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, key, err := factory.LoadCerts(c.runtime.ServerCA, c.runtime.ServerCAKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := tlsStorage(ctx, c.config.DataDir, c.runtime)
|
|
|
|
return dynamiclistener.NewListener(tcp, storage, cert, key, dynamiclistener.Config{
|
2020-05-05 22:09:04 +00:00
|
|
|
CN: version.Program,
|
|
|
|
Organization: []string{version.Program},
|
2019-10-27 05:53:25 +00:00
|
|
|
TLSConfig: tls.Config{
|
2020-05-06 17:43:15 +00:00
|
|
|
ClientAuth: tls.RequestClientCert,
|
|
|
|
MinVersion: c.config.TLSMinVersion,
|
|
|
|
CipherSuites: c.config.TLSCipherSuites,
|
2019-10-27 05:53:25 +00:00
|
|
|
},
|
2019-11-13 06:04:53 +00:00
|
|
|
SANs: append(c.config.SANs, "localhost", "kubernetes", "kubernetes.default", "kubernetes.default.svc."+c.config.ClusterDomain),
|
2019-10-27 05:53:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
|
2019-10-27 05:53:25 +00:00
|
|
|
l, handler, err := c.newListener(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err = c.getHandler(handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, handler, err = c.initClusterDB(ctx, l, handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := http.Server{
|
|
|
|
Handler: handler,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := server.Serve(l)
|
|
|
|
logrus.Fatalf("server stopped: %v", err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tlsStorage(ctx context.Context, dataDir string, runtime *config.ControlRuntime) dynamiclistener.TLSStorage {
|
|
|
|
fileStorage := file.New(filepath.Join(dataDir, "tls/dynamic-cert.json"))
|
|
|
|
cache := memory.NewBacked(fileStorage)
|
|
|
|
return kubernetes.New(ctx, func() *core.Factory {
|
|
|
|
return runtime.Core
|
2020-05-05 22:09:04 +00:00
|
|
|
}, "kube-system", ""+version.Program+"-serving", cache)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|