2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-09-08 17:56:18 +00:00
|
|
|
"errors"
|
2021-10-12 22:00:28 +00:00
|
|
|
"io/ioutil"
|
2020-09-14 21:14:30 +00:00
|
|
|
"log"
|
2019-10-27 05:53:25 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-06-22 20:42:34 +00:00
|
|
|
"os"
|
2019-10-27 05:53:25 +00:00
|
|
|
"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"
|
2021-06-22 20:42:34 +00:00
|
|
|
"github.com/rancher/k3s/pkg/etcd"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/k3s/pkg/version"
|
2021-07-03 08:37:19 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/core"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-09-24 06:06:00 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-10-27 05:53:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-22 20:35:58 +00:00
|
|
|
// newListener returns a new TCP listener and HTTP request handler using dynamiclistener.
|
2020-09-24 06:29:25 +00:00
|
|
|
// dynamiclistener will use the cluster's Server CA to sign the dynamically generate certificate,
|
|
|
|
// and will sync the certs into the Kubernetes datastore, with a local disk cache.
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) newListener(ctx context.Context) (net.Listener, http.Handler, error) {
|
2021-06-22 20:42:34 +00:00
|
|
|
if c.managedDB != nil {
|
|
|
|
if _, err := os.Stat(etcd.ResetFile(c.config)); err == nil {
|
|
|
|
// delete the dynamic listener file if it exists after restoration to fix restoration
|
|
|
|
// on fresh nodes
|
|
|
|
os.Remove(filepath.Join(c.config.DataDir, "tls/dynamic-cert.json"))
|
|
|
|
}
|
|
|
|
}
|
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-09-24 06:43:53 +00:00
|
|
|
ExpirationDaysCheck: config.CertificateRenewDays,
|
|
|
|
Organization: []string{version.Program},
|
2021-09-01 06:50:23 +00:00
|
|
|
SANs: append(c.config.SANs, "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc."+c.config.ClusterDomain),
|
2020-09-24 06:43:53 +00:00
|
|
|
CN: version.Program,
|
2020-07-23 02:34:51 +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
|
|
|
},
|
2021-12-02 21:19:16 +00:00
|
|
|
RegenerateCerts: func() bool {
|
|
|
|
const regenerateDynamicListenerFile = "dynamic-cert-regenerate"
|
|
|
|
dynamicListenerRegenFilePath := filepath.Join(c.config.DataDir, "tls", regenerateDynamicListenerFile)
|
|
|
|
if _, err := os.Stat(dynamicListenerRegenFilePath); err == nil {
|
|
|
|
os.Remove(dynamicListenerRegenFilePath)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2019-10-27 05:53:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// initClusterAndHTTPS sets up the dynamic tls listener, request router,
|
|
|
|
// and cluster database. Once the database is up, it starts the supervisor http server.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) initClusterAndHTTPS(ctx context.Context) error {
|
2020-09-24 06:43:53 +00:00
|
|
|
// Set up dynamiclistener TLS listener and request handler
|
|
|
|
listener, handler, err := c.newListener(ctx)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Get the base request handler
|
2019-10-27 05:53:25 +00:00
|
|
|
handler, err = c.getHandler(handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:40:00 +00:00
|
|
|
// Config the cluster database and allow it to add additional request handlers
|
|
|
|
handler, err = c.initClusterDB(ctx, handler)
|
2019-10-27 05:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Create a HTTP server with the registered request handlers, using logrus for logging
|
2019-10-27 05:53:25 +00:00
|
|
|
server := http.Server{
|
2021-10-22 22:56:01 +00:00
|
|
|
Handler: handler,
|
|
|
|
}
|
2021-10-12 22:00:28 +00:00
|
|
|
|
|
|
|
if logrus.IsLevelEnabled(logrus.DebugLevel) {
|
|
|
|
server.ErrorLog = log.New(logrus.StandardLogger().Writer(), "Cluster-Http-Server ", log.LstdFlags)
|
|
|
|
} else {
|
|
|
|
server.ErrorLog = log.New(ioutil.Discard, "Cluster-Http-Server", 0)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Start the supervisor http server on the tls listener
|
2019-10-27 05:53:25 +00:00
|
|
|
go func() {
|
2020-09-24 06:43:53 +00:00
|
|
|
err := server.Serve(listener)
|
2021-09-08 17:56:18 +00:00
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logrus.Fatalf("server stopped: %v", err)
|
|
|
|
}
|
2019-10-27 05:53:25 +00:00
|
|
|
}()
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Shutdown the http server when the context is closed
|
2019-10-27 05:53:25 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
server.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// tlsStorage creates an in-memory cache for dynamiclistener's certificate, backed by a file on disk
|
|
|
|
// and the Kubernetes datastore.
|
2019-10-27 05:53:25 +00:00
|
|
|
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-09-24 06:06:00 +00:00
|
|
|
}, metav1.NamespaceSystem, version.Program+"-serving", cache)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|