-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create a proper apiv1.BootstrapConfig type * Update types.ClusterConfig <-> apiv1.BootstrapConfig * Adjust k8s bootstrap process for new bootstrap config * remove unused code * preserve default behaviour in k8s bootstrap * DatastoreServers set as []string on API * make sure to return zero value in case of error * stricter validation for datastore config
- Loading branch information
1 parent
3866fe4
commit 6178860
Showing
14 changed files
with
518 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// BootstrapConfig is used to seed cluster configuration when bootstrapping a new cluster. | ||
type BootstrapConfig struct { | ||
// ClusterConfig | ||
ClusterConfig UserFacingClusterConfig `json:"cluster-config,omitempty" yaml:"cluster-config,omitempty"` | ||
|
||
// Seed configuration for the control plane (flat on purpose). Empty values are ignored | ||
PodCIDR *string `json:"pod-cidr,omitempty" yaml:"pod-cidr,omitempty"` | ||
ServiceCIDR *string `json:"service-cidr,omitempty" yaml:"service-cidr,omitempty"` | ||
DisableRBAC *bool `json:"disable-rbac,omitempty" yaml:"disable-rbac,omitempty"` | ||
SecurePort *int `json:"secure-port,omitempty" yaml:"secure-port,omitempty"` | ||
CloudProvider *string `json:"cloud-provider,omitempty" yaml:"cloud-provider,omitempty"` | ||
K8sDqlitePort *int `json:"k8s-dqlite-port,omitempty" yaml:"k8s-dqlite-port,omitempty"` | ||
DatastoreType *string `json:"datastore-type,omitempty" yaml:"datastore-type,omitempty"` | ||
DatastoreServers []string `json:"datastore-servers,omitempty" yaml:"datastore-servers,omitempty"` | ||
DatastoreCACert *string `json:"datastore-ca-crt,omitempty" yaml:"datastore-ca-crt,omitempty"` | ||
DatastoreClientCert *string `json:"datastore-client-crt,omitempty" yaml:"datastore-client-crt,omitempty"` | ||
DatastoreClientKey *string `json:"datastore-client-key,omitempty" yaml:"datastore-client-key,omitempty"` | ||
|
||
// Seed configuration for certificates | ||
ExtraSANs []string `json:"extra-sans,omitempty" yaml:"extra-sans,omitempty"` | ||
} | ||
|
||
func (b *BootstrapConfig) GetDatastoreType() string { return getField(b.DatastoreType) } | ||
func (b *BootstrapConfig) GetDatastoreCACert() string { return getField(b.DatastoreCACert) } | ||
func (b *BootstrapConfig) GetDatastoreClientCert() string { return getField(b.DatastoreClientCert) } | ||
func (b *BootstrapConfig) GetDatastoreClientKey() string { return getField(b.DatastoreClientKey) } | ||
func (b *BootstrapConfig) GetK8sDqlitePort() int { return getField(b.K8sDqlitePort) } | ||
|
||
// ToMicrocluster converts a BootstrapConfig to a map[string]string for use in microcluster. | ||
func (b *BootstrapConfig) ToMicrocluster() (map[string]string, error) { | ||
config, err := json.Marshal(b) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal bootstrap config: %w", err) | ||
} | ||
|
||
return map[string]string{ | ||
"bootstrapConfig": string(config), | ||
}, nil | ||
} | ||
|
||
// BootstrapConfigFromMicrocluster parses a microcluster map[string]string and retrieves the BootstrapConfig. | ||
func BootstrapConfigFromMicrocluster(m map[string]string) (BootstrapConfig, error) { | ||
config := BootstrapConfig{} | ||
if err := json.Unmarshal([]byte(m["bootstrapConfig"]), &config); err != nil { | ||
return BootstrapConfig{}, fmt.Errorf("failed to unmarshal bootstrap config: %w", err) | ||
} | ||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package v1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
apiv1 "github.com/canonical/k8s/api/v1" | ||
"github.com/canonical/k8s/pkg/utils/vals" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestBootstrapConfigToMicrocluster(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
cfg := apiv1.BootstrapConfig{ | ||
ClusterConfig: apiv1.UserFacingClusterConfig{ | ||
Network: apiv1.NetworkConfig{ | ||
Enabled: vals.Pointer(true), | ||
}, | ||
DNS: apiv1.DNSConfig{ | ||
Enabled: vals.Pointer(true), | ||
ClusterDomain: vals.Pointer("cluster.local"), | ||
}, | ||
Ingress: apiv1.IngressConfig{ | ||
Enabled: vals.Pointer(true), | ||
}, | ||
LoadBalancer: apiv1.LoadBalancerConfig{ | ||
Enabled: vals.Pointer(true), | ||
L2Mode: vals.Pointer(true), | ||
CIDRs: vals.Pointer([]string{"10.0.0.0/24"}), | ||
}, | ||
LocalStorage: apiv1.LocalStorageConfig{ | ||
Enabled: vals.Pointer(true), | ||
LocalPath: vals.Pointer("/storage/path"), | ||
SetDefault: vals.Pointer(false), | ||
}, | ||
Gateway: apiv1.GatewayConfig{ | ||
Enabled: vals.Pointer(true), | ||
}, | ||
MetricsServer: apiv1.MetricsServerConfig{ | ||
Enabled: vals.Pointer(true), | ||
}, | ||
}, | ||
PodCIDR: vals.Pointer("10.100.0.0/16"), | ||
ServiceCIDR: vals.Pointer("10.200.0.0/16"), | ||
DisableRBAC: vals.Pointer(false), | ||
SecurePort: vals.Pointer(6443), | ||
CloudProvider: vals.Pointer("external"), | ||
K8sDqlitePort: vals.Pointer(9090), | ||
DatastoreType: vals.Pointer("k8s-dqlite"), | ||
ExtraSANs: []string{"custom.kubernetes"}, | ||
} | ||
|
||
microclusterConfig, err := cfg.ToMicrocluster() | ||
g.Expect(err).To(BeNil()) | ||
|
||
fromMicrocluster, err := apiv1.BootstrapConfigFromMicrocluster(microclusterConfig) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(fromMicrocluster).To(Equal(cfg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.