Skip to content

Commit

Permalink
Add tls for kine
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Savian <vitor.savian@suse.com>

Bump kine

Signed-off-by: Vitor Savian <vitor.savian@suse.com>

Add integration tests for kine with tls

Signed-off-by: Vitor Savian <vitor.savian@suse.com>
  • Loading branch information
vitorsavian committed Mar 7, 2024
1 parent 1c8be1d commit 618f7cd
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1
github.com/json-iterator/go v1.1.12
github.com/k3s-io/helm-controller v0.15.9
github.com/k3s-io/kine v0.11.4
github.com/k3s-io/kine v0.11.5
github.com/klauspost/compress v1.17.2
github.com/kubernetes-sigs/cri-tools v0.0.0-00010101000000-000000000000
github.com/lib/pq v1.10.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,8 @@ github.com/k3s-io/etcd/server/v3 v3.5.9-k3s1 h1:B3039IkTPnwQEt4tIMjC6yd6b1Q3Z9ZZ
github.com/k3s-io/etcd/server/v3 v3.5.9-k3s1/go.mod h1:GgI1fQClQCFIzuVjlvdbMxNbnISt90gdfYyqiAIt65g=
github.com/k3s-io/helm-controller v0.15.9 h1:eBZq0KkZCDyWh4og+tyI43Nt9T5TNjc7QCFhAt1aR64=
github.com/k3s-io/helm-controller v0.15.9/go.mod h1:AYitg40howLjKloL/zdjDDOPL1jg/K5R4af0tQcyPR8=
github.com/k3s-io/kine v0.11.4 h1:ZIXQT4vPPKNL9DwLF4dQ11tWtpJ1C/7OKNIpFmTkImo=
github.com/k3s-io/kine v0.11.4/go.mod h1:NmwOWsWgB3aScq5+LEYytAaceqkG7lmCLLjjrWug8v4=
github.com/k3s-io/kine v0.11.5 h1:/oqepXJblSL3sYLhYDa22mRhrA3CVO6DzKcipnFdO8U=
github.com/k3s-io/kine v0.11.5/go.mod h1:NmwOWsWgB3aScq5+LEYytAaceqkG7lmCLLjjrWug8v4=
github.com/k3s-io/klog/v2 v2.100.1-k3s1 h1:xb/Ta8dpQuIZueQEw2YTZUYrKoILdBmPiITVkNmYPa0=
github.com/k3s-io/klog/v2 v2.100.1-k3s1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
github.com/k3s-io/kube-router/v2 v2.0.1 h1:UCsdkQjSfOkVakixilRDDkG9yq775GBSKxBfsyUj8ng=
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Server struct {
DatastoreCAFile string
DatastoreCertFile string
DatastoreKeyFile string
KineTLS bool
AdvertiseIP string
AdvertisePort int
DisableScheduler bool
Expand Down Expand Up @@ -310,6 +311,11 @@ var ServerFlags = []cli.Flag{
Usage: "(flags) Customized flag for kube-cloud-controller-manager process",
Value: &ServerConfig.ExtraCloudControllerArgs,
},
&cli.BoolFlag{
Name: "kine-tls",
Usage: "(db) Enable tls for kine",
Destination: &ServerConfig.KineTLS,
},
&cli.StringFlag{
Name: "datastore-endpoint",
Usage: "(db) Specify etcd, NATS, MySQL, Postgres, or SQLite (default) data source name",
Expand Down
5 changes: 2 additions & 3 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func RunWithControllers(app *cli.Context, leaderControllers server.CustomControl
}

func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomControllers, controllers server.CustomControllers) error {
var (
err error
)
var err error
// Validate build env
cmds.MustValidateGolang()

Expand Down Expand Up @@ -154,6 +152,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.Datastore.BackendTLSConfig.CAFile = cfg.DatastoreCAFile
serverConfig.ControlConfig.Datastore.BackendTLSConfig.CertFile = cfg.DatastoreCertFile
serverConfig.ControlConfig.Datastore.BackendTLSConfig.KeyFile = cfg.DatastoreKeyFile
serverConfig.ControlConfig.KineTLS = cfg.KineTLS
serverConfig.ControlConfig.AdvertiseIP = cfg.AdvertiseIP
serverConfig.ControlConfig.AdvertisePort = cfg.AdvertisePort
serverConfig.ControlConfig.FlannelBackend = cfg.FlannelBackend
Expand Down
25 changes: 19 additions & 6 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *Cluster) Start(ctx context.Context) (<-chan struct{}, error) {
return nil, err
}

if err := c.startStorage(ctx); err != nil {
if err := c.startStorage(ctx, false); err != nil {
return nil, err
}

Expand Down Expand Up @@ -132,12 +132,19 @@ func (c *Cluster) Start(ctx context.Context) (<-chan struct{}, error) {
// This calls into the kine endpoint code, which sets up the database client
// and unix domain socket listener if using an external database. In the case of an etcd
// backend it just returns the user-provided etcd endpoints and tls config.
func (c *Cluster) startStorage(ctx context.Context) error {
if c.storageStarted {
func (c *Cluster) startStorage(ctx context.Context, bootstrap bool) error {
if c.storageStarted && !c.config.KineTLS {
return nil
}
c.storageStarted = true

if !bootstrap {
// set the tls config for the kine storage
c.config.Datastore.ServerTLSConfig.CAFile = c.config.Runtime.ETCDServerCA
c.config.Datastore.ServerTLSConfig.CertFile = c.config.Runtime.ServerETCDCert
c.config.Datastore.ServerTLSConfig.KeyFile = c.config.Runtime.ServerETCDKey
}

// start listening on the kine socket as an etcd endpoint, or return the external etcd endpoints
etcdConfig, err := endpoint.Listen(ctx, c.config.Datastore)
if err != nil {
Expand All @@ -148,9 +155,15 @@ func (c *Cluster) startStorage(ctx context.Context) error {
// based on what the kine wrapper tells us about the datastore. Single-node datastores like sqlite don't require
// leader election, while basically all others (etcd, external database, etc) do since they allow multiple servers.
c.config.Runtime.EtcdConfig = etcdConfig
c.config.Datastore.BackendTLSConfig = etcdConfig.TLSConfig
c.config.Datastore.Endpoint = strings.Join(etcdConfig.Endpoints, ",")
c.config.NoLeaderElect = !etcdConfig.LeaderElect

// after the bootstrap we need to set the args for api-server with kine in unixs or just set the
// values if the datastoreTLS is not enabled
if !bootstrap || !c.config.KineTLS {
c.config.Datastore.BackendTLSConfig = etcdConfig.TLSConfig
c.config.Datastore.Endpoint = strings.Join(etcdConfig.Endpoints, ",")
c.config.NoLeaderElect = !etcdConfig.LeaderElect
}

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/cluster/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
const maxBootstrapWaitAttempts = 5

func RotateBootstrapToken(ctx context.Context, config *config.Control, oldToken string) error {

token, err := util.ReadTokenFromFile(config.Runtime.ServerToken, config.Runtime.ServerCA, config.DataDir)
if err != nil {
return err
Expand Down Expand Up @@ -151,7 +150,7 @@ func bootstrapKeyData(ctx context.Context, storageClient client.Client) (*client
// bootstrap key as a lock. This function will not return successfully until either the
// bootstrap key has been locked, or data is read into the struct.
func (c *Cluster) storageBootstrap(ctx context.Context) error {
if err := c.startStorage(ctx); err != nil {
if err := c.startStorage(ctx, true); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type Control struct {
KubeConfigMode string
HelmJobImage string
DataDir string
KineTLS bool
Datastore endpoint.Config `json:"-"`
Disables map[string]bool
DisableAPIServer bool
Expand Down
15 changes: 6 additions & 9 deletions pkg/daemons/control/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ const (
RequestHeaderCN = "system:auth-proxy"
)

var (
kubeconfigTemplate = template.Must(template.New("kubeconfig").Parse(`apiVersion: v1
var kubeconfigTemplate = template.Must(template.New("kubeconfig").Parse(`apiVersion: v1
clusters:
- cluster:
server: {{.URL}}
Expand All @@ -64,7 +63,6 @@ users:
client-certificate: {{.ClientCert}}
client-key: {{.ClientKey}}
`))
)

func migratePassword(p *passwd.Passwd) error {
server, _ := p.Pass("server")
Expand Down Expand Up @@ -283,9 +281,7 @@ func genEncryptedNetworkInfo(controlConfig *config.Control) error {
}

func getServerPass(passwd *passwd.Passwd, config *config.Control) (string, error) {
var (
err error
)
var err error

serverPass := config.Token
if serverPass == "" {
Expand Down Expand Up @@ -448,14 +444,16 @@ func genServerCerts(config *config.Control) error {
}

func genETCDCerts(config *config.Control) error {

runtime := config.Runtime
regen, err := createSigningCertKey("etcd-server", runtime.ETCDServerCA, runtime.ETCDServerCAKey)
if err != nil {
return err
}

altNames := &certutil.AltNames{}
altNames := &certutil.AltNames{
DNSNames: []string{"kine.sock"},
}

addSANs(altNames, config.SANs)

if _, err := createClientCertKey(regen, "etcd-client", nil,
Expand Down Expand Up @@ -845,5 +843,4 @@ func genCloudConfig(controlConfig *config.Control) error {
return err
}
return os.WriteFile(controlConfig.Runtime.CloudControllerConfig, b, 0600)

}
1 change: 0 additions & 1 deletion pkg/daemons/control/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func prepare(ctx context.Context, config *config.Control) error {
deps.CreateRuntimeCertFiles(config)

cluster := cluster.New(config)

if err := cluster.Bootstrap(ctx, config.ClusterReset); err != nil {
return err
}
Expand Down
74 changes: 65 additions & 9 deletions tests/integration/startup/startup_int_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"errors"
"os"
"path/filepath"
"testing"
Expand All @@ -13,9 +14,11 @@ import (
. "github.com/onsi/gomega/gstruct"
)

var startupServer *testutil.K3sServer
var startupServerArgs = []string{}
var testLock int
var (
startupServer *testutil.K3sServer
startupServerArgs = []string{}
testLock int
)

var _ = BeforeSuite(func() {
if testutil.IsExistingServer() {
Expand All @@ -27,7 +30,6 @@ var _ = BeforeSuite(func() {
})

var _ = Describe("startup tests", Ordered, func() {

When("a default server is created", func() {
It("is created with no arguments", func() {
var err error
Expand All @@ -39,6 +41,59 @@ var _ = Describe("startup tests", Ordered, func() {
return testutil.K3sDefaultDeployments()
}, "120s", "5s").Should(Succeed())
})
It("has kine without tls", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(startupServer, "Kine available at unix://kine.sock")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding kine sock")
}, "30s", "2s").Should(Succeed())
})
It("does not use kine with tls after bootstrap", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(startupServer, "Kine available at unixs://kine.sock")
if err != nil {
return err
}
if match {
return errors.New("Kine with tls when the kine-tls is not set")
}
return nil
}, "30s", "2s").Should(Succeed())
})
It("dies cleanly", func() {
Expect(testutil.K3sKillServer(startupServer)).To(Succeed())
Expect(testutil.K3sCleanup(-1, "")).To(Succeed())
})
})
When("a server with kine-tls is created", func() {
It("is created with kine-tls", func() {
var err error
startupServerArgs = []string{"--kine-tls"}
startupServer, err = testutil.K3sStartServer(startupServerArgs...)
Expect(err).ToNot(HaveOccurred())
})
It("has the default pods deployed", func() {
Eventually(func() error {
return testutil.K3sDefaultDeployments()
}, "120s", "5s").Should(Succeed())
})
It("set kine to use tls", func() {
Eventually(func() error {
match, err := testutil.SearchK3sLog(startupServer, "Kine available at unixs://kine.sock")
if err != nil {
return err
}
if match {
return nil
}
return errors.New("error finding unixs://kine.sock")
}, "30s", "2s").Should(Succeed())
})
It("dies cleanly", func() {
Expect(testutil.K3sKillServer(startupServer)).To(Succeed())
Expect(testutil.K3sCleanup(-1, "")).To(Succeed())
Expand Down Expand Up @@ -263,13 +318,14 @@ var _ = Describe("startup tests", Ordered, func() {
Expect(testutil.K3sCleanup(-1, "")).To(Succeed())
})
})

})

var failed bool
var _ = AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})
var (
failed bool
_ = AfterEach(func() {
failed = failed || CurrentSpecReport().Failed()
})
)

var _ = AfterSuite(func() {
if !testutil.IsExistingServer() {
Expand Down

0 comments on commit 618f7cd

Please sign in to comment.