Skip to content

Commit

Permalink
Initial cluster config is created from bootstrap config and defaults (#…
Browse files Browse the repository at this point in the history
…132)

* SetClusterConfigDefaults with bootstrap

* set cluster config from bootstrap method, tests

* set defaults

* typo

* code improvements

* move validate cidr

* improve set defaults

* simplify test

* improve set defaults test
  • Loading branch information
louiseschmidtgen authored Feb 16, 2024
1 parent be87111 commit b6c2aae
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/k8s/pkg/k8sd/app/hooks_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ func onBootstrapControlPlane(s *state.State, initConfig map[string]string) error
if err != nil {
return fmt.Errorf("failed to unmarshal bootstrap config: %w", err)
}
cfg, err := types.MergeClusterConfig(types.DefaultClusterConfig(), types.ClusterConfigFromBootstrapConfig(bootstrapConfig))
if err != nil {
return fmt.Errorf("failed initialize cluster config from bootstrap config: %w", err)

cfg := types.ClusterConfigFromBootstrapConfig(bootstrapConfig)
cfg.SetDefaults()
if err := cfg.Validate(); err != nil {
return fmt.Errorf("invalid cluster configuration: %w", err)
}

nodeIP := net.ParseIP(s.Address().Hostname())
if nodeIP == nil {
return fmt.Errorf("failed to parse node IP address %q", s.Address().Hostname())
Expand Down
56 changes: 41 additions & 15 deletions src/k8s/pkg/k8sd/types/cluster_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package types

import apiv1 "github.com/canonical/k8s/api/v1"
import (
"fmt"
"net"
"strings"

apiv1 "github.com/canonical/k8s/api/v1"
)

// ClusterConfig is the control plane configuration format of the k8s cluster.
// ClusterConfig should attempt to use structured fields wherever possible.
Expand Down Expand Up @@ -49,20 +55,40 @@ type K8sDqlite struct {
Port int `yaml:"port,omitempty"`
}

func DefaultClusterConfig() ClusterConfig {
return ClusterConfig{
Network: Network{
PodCIDR: "10.1.0.0/16",
ServiceCIDR: "10.152.183.0/24",
},
APIServer: APIServer{
Datastore: "k8s-dqlite",
SecurePort: 6443,
AuthorizationMode: "Node,RBAC",
},
K8sDqlite: K8sDqlite{
Port: 9000,
},
func (c *ClusterConfig) Validate() error {
clusterCIDRs := strings.Split(c.Network.PodCIDR, ",")
if len(clusterCIDRs) != 1 && len(clusterCIDRs) != 2 {
return fmt.Errorf("invalid number of cluster CIDRs: %d", len(clusterCIDRs))
}

for _, cidr := range clusterCIDRs {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
return fmt.Errorf("invalid CIDR: %w", err)
}
}

return nil
}

func (c *ClusterConfig) SetDefaults() {
if c.Network.PodCIDR == "" {
c.Network.PodCIDR = "10.1.0.0/16"
}
if c.Network.ServiceCIDR == "" {
c.Network.ServiceCIDR = "10.152.183.0/24"
}
if c.APIServer.Datastore == "" {
c.APIServer.Datastore = "k8s-dqlite"
}
if c.APIServer.SecurePort == 0 {
c.APIServer.SecurePort = 6443
}
if c.APIServer.AuthorizationMode == "" {
c.APIServer.AuthorizationMode = "Node,RBAC"
}
if c.K8sDqlite.Port == 0 {
c.K8sDqlite.Port = 9000
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/k8s/pkg/k8sd/types/cluster_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,52 @@ func TestClusterConfigFromBootstrapConfig(t *testing.T) {
g.Expect(types.ClusterConfigFromBootstrapConfig(&bootstrapConfig)).To(Equal(expectedConfig))
}

func TestValidateCIDR(t *testing.T) {
g := NewWithT(t)
// Create a new BootstrapConfig with default values
validConfig := types.ClusterConfig{
Network: types.Network{
PodCIDR: "10.1.0.0/16,2001:0db8::/32",
},
}

err := validConfig.Validate()
g.Expect(err).To(BeNil())

// Create a new BootstrapConfig with invalid CIDR
invalidConfig := types.ClusterConfig{
Network: types.Network{
PodCIDR: "bananas",
},
}
err = invalidConfig.Validate()
g.Expect(err).ToNot(BeNil())
}

func TestSetDefaults(t *testing.T) {
g := NewWithT(t)
clusterConfig := types.ClusterConfig{}

// Set defaults
expectedConfig := types.ClusterConfig{
Network: types.Network{
PodCIDR: "10.1.0.0/16",
ServiceCIDR: "10.152.183.0/24",
},
APIServer: types.APIServer{
Datastore: "k8s-dqlite",
SecurePort: 6443,
AuthorizationMode: "Node,RBAC",
},
K8sDqlite: types.K8sDqlite{
Port: 9000,
},
}

clusterConfig.SetDefaults()
g.Expect(clusterConfig).To(Equal(expectedConfig))
}

type mergeClusterConfigTestCase struct {
name string
old types.ClusterConfig
Expand Down

0 comments on commit b6c2aae

Please sign in to comment.