Skip to content

Commit

Permalink
Improve the new KubeOneCluster API and add missing fields (#379)
Browse files Browse the repository at this point in the history
* Update the KubeOneCluster API

* Add the Name field
* Move the Credentials field from MachineControllerConfig to KubeOneCluster
* Change APIEndpoint.Port to int
* Use json.RawMessage instead of *runtime.Extension
* Make defaulting NPE safe
* Validate the Name field

Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>

* Update code generated files

Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>

* Run go fmt

Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>
  • Loading branch information
xmudrii authored and kubermatic-bot committed Apr 26, 2019
1 parent 4f4e49f commit c8770ba
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 189 deletions.
22 changes: 13 additions & 9 deletions pkg/apis/kubeone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package kubeone

import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -27,6 +28,8 @@ import (
type KubeOneCluster struct {
metav1.TypeMeta `json:",inline"`

// Name is the name of the cluster
Name string
// Hosts describes the control plane nodes and how to access them
Hosts []HostConfig `json:"hosts,omitempty"`
// APIEndpoints are pairs of address and port used to communicate with the Kubernetes API
Expand All @@ -45,6 +48,8 @@ type KubeOneCluster struct {
MachineController *MachineControllerConfig `json:"machineController,omitempty"`
// Features enables and configures additional cluster features
Features Features `json:"features,omitempty"`
// Credentials used for machine-controller and external CCM
Credentials map[string]string `json:"credentials,omitempty"`
}

// HostConfig describes a single control plane node.
Expand All @@ -69,7 +74,7 @@ type APIEndpoint struct {
Host string `json:"host"`

// Port is the port used to reach to the API
Port string `json:"port"`
Port int `json:"port"`
}

// CloudProviderName represents the name of a provider
Expand Down Expand Up @@ -123,20 +128,19 @@ type WorkerConfig struct {

// ProviderSpec describes a worker node
type ProviderSpec struct {
CloudProviderSpec *runtime.RawExtension `json:"cloudProviderSpec"`
Labels map[string]string `json:"labels"`
SSHPublicKeys []string `json:"sshPublicKeys"`
OperatingSystem string `json:"operatingSystem"`
OperatingSystemSpec *runtime.RawExtension `json:"operatingSystemSpec"`
CloudProviderSpec json.RawMessage `json:"cloudProviderSpec"`
Labels map[string]string `json:"labels"`
SSHPublicKeys []string `json:"sshPublicKeys"`
OperatingSystem string `json:"operatingSystem"`
OperatingSystemSpec json.RawMessage `json:"operatingSystemSpec"`
}

// MachineControllerConfig configures kubermatic machine-controller deployment
type MachineControllerConfig struct {
Deploy bool `json:"deploy"`
// Provider is provider to be used for machine-controller
// Defaults and must be same as chosen cloud provider, unless cloud provider is set to None
Provider CloudProviderName `json:"provider"`
Credentials map[string]string `json:"credentials"`
Provider CloudProviderName `json:"provider"`
}

// Features controls what features will be enabled on the cluster
Expand Down
115 changes: 0 additions & 115 deletions pkg/apis/kubeone/v1alpha1/credentials.go

This file was deleted.

38 changes: 28 additions & 10 deletions pkg/apis/kubeone/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ func SetDefaults_KubeOneCluster(obj *KubeOneCluster) {
SetDefaults_APIEndpoints(obj)
SetDefaults_ClusterNetwork(obj)
SetDefaults_MachineController(obj)
SetDefaults_Features(obj)
}

func SetDefaults_Hosts(obj *KubeOneCluster) {
// No hosts, so skip defaulting
if len(obj.Hosts) == 0 {
return
}

// Set first host to be the leader
obj.Hosts[0].IsLeader = true

Expand All @@ -54,13 +60,33 @@ func SetDefaults_Hosts(obj *KubeOneCluster) {
}

func SetDefaults_APIEndpoints(obj *KubeOneCluster) {
// If no API endpoint is provided, assume the public address is an endpoint
if len(obj.APIEndpoints) == 0 {
if len(obj.Hosts) == 0 {
// No hosts, so can't default to the first one
return
}
obj.APIEndpoints = []APIEndpoint{
{
Host: obj.Hosts[0].PublicAddress,
Port: 6443,
},
}
} else {
// There's APIEndpoint provided, default host and port
for i := range obj.APIEndpoints {
if len(obj.APIEndpoints[i].Host) == 0 {
if len(obj.Hosts) > 0 {
// Can only default to the first host if it exists
obj.APIEndpoints[i].Host = obj.Hosts[0].PublicAddress
}
}
if obj.APIEndpoints[i].Port == 0 {
obj.APIEndpoints[i].Port = 6443
}
}
}

}

func SetDefaults_ClusterNetwork(obj *KubeOneCluster) {
Expand All @@ -77,17 +103,9 @@ func SetDefaults_MachineController(obj *KubeOneCluster) {
}
}

// If ProviderName is not None default to cloud provider and ensure user have not
// manually provided machine-controller provider different than cloud provider.
// If ProviderName is None, take user input or default to None.
if obj.CloudProvider.Name != CloudProviderNameNone {
if obj.MachineController.Provider == "" {
obj.MachineController.Provider = obj.CloudProvider.Name
}
if obj.MachineController.Provider == "" {
obj.MachineController.Provider = obj.CloudProvider.Name
}

// TODO(xmudrii): error
obj.MachineController.Credentials, _ = obj.MachineController.Provider.ProviderCredentials()
}

func SetDefaults_Features(obj *KubeOneCluster) {
Expand Down
22 changes: 13 additions & 9 deletions pkg/apis/kubeone/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package v1alpha1

import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -27,6 +28,8 @@ import (
type KubeOneCluster struct {
metav1.TypeMeta `json:",inline"`

// Name is the name of the cluster
Name string
// Hosts describes the control plane nodes and how to access them
Hosts []HostConfig `json:"hosts,omitempty"`
// APIEndpoints are pairs of address and port used to communicate with the Kubernetes API
Expand All @@ -45,6 +48,8 @@ type KubeOneCluster struct {
MachineController *MachineControllerConfig `json:"machineController,omitempty"`
// Features enables and configures additional cluster features
Features Features `json:"features,omitempty"`
// Credentials used for machine-controller and external CCM
Credentials map[string]string `json:"credentials,omitempty"`
}

// HostConfig describes a single control plane node.
Expand All @@ -69,7 +74,7 @@ type APIEndpoint struct {
Host string `json:"host"`

// Port is the port used to reach to the API
Port string `json:"port"`
Port int `json:"port"`
}

// CloudProviderName represents the name of a provider
Expand Down Expand Up @@ -123,20 +128,19 @@ type WorkerConfig struct {

// ProviderSpec describes a worker node
type ProviderSpec struct {
CloudProviderSpec *runtime.RawExtension `json:"cloudProviderSpec"`
Labels map[string]string `json:"labels"`
SSHPublicKeys []string `json:"sshPublicKeys"`
OperatingSystem string `json:"operatingSystem"`
OperatingSystemSpec *runtime.RawExtension `json:"operatingSystemSpec"`
CloudProviderSpec json.RawMessage `json:"cloudProviderSpec"`
Labels map[string]string `json:"labels"`
SSHPublicKeys []string `json:"sshPublicKeys"`
OperatingSystem string `json:"operatingSystem"`
OperatingSystemSpec json.RawMessage `json:"operatingSystemSpec"`
}

// MachineControllerConfig configures kubermatic machine-controller deployment
type MachineControllerConfig struct {
Deploy bool `json:"deploy"`
// Provider is provider to be used for machine-controller
// Defaults and must be same as chosen cloud provider, unless cloud provider is set to None
Provider CloudProviderName `json:"provider"`
Credentials map[string]string `json:"credentials"`
Provider CloudProviderName `json:"provider"`
}

// Features controls what features will be enabled on the cluster
Expand Down
15 changes: 9 additions & 6 deletions pkg/apis/kubeone/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c8770ba

Please sign in to comment.