Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow declaration of leader instance #790

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/terraform/aws/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ output "kubeone_hosts" {
cloud_provider = "aws"
private_address = aws_instance.control_plane.*.private_ip
hostnames = aws_instance.control_plane.*.private_dns
leader_ip = aws_instance.control_plane.0.private_ip
ssh_agent_socket = var.ssh_agent_socket
ssh_port = var.ssh_port
ssh_private_key_file = var.ssh_private_key_file
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/kubeone/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func (c KubeOneCluster) RandomHost() HostConfig {
// Followers returns all but the first configured host. Only call
// this after validating the cluster config to ensure hosts exist.
func (c KubeOneCluster) Followers() []HostConfig {
return c.Hosts[1:]
followers := []HostConfig{}
for _, h := range c.Hosts {
if !h.IsLeader {
followers = append(followers, h)
}
}
return followers
}

// SetHostname sets the hostname for the given host
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/kubeone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ type HostConfig struct {
BastionPort int `json:"bastionPort"`
BastionUser string `json:"bastionUser"`
Hostname string `json:"hostname"`
IsLeader bool `json:"isLeader"`

// Information populated at the runtime
OperatingSystem string `json:"-"`
IsLeader bool `json:"-"`
}

// APIEndpoint is the endpoint used to communicate with the Kubernetes API
Expand Down
14 changes: 12 additions & 2 deletions pkg/apis/kubeone/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ func SetDefaults_Hosts(obj *KubeOneCluster) {
return
}

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

// Define a unique ID for each host
for idx := range obj.Hosts {
if setDefaultLeader && obj.Hosts[idx].IsLeader {
// override setting default leader, as explicit leader already
// defined
setDefaultLeader = false
}
obj.Hosts[idx].ID = idx
defaultHostConfig(&obj.Hosts[idx])
}

if setDefaultLeader {
// In absence of explicitly defined leader set the first host to be the
// default leader
obj.Hosts[0].IsLeader = true
}
}

func SetDefaults_APIEndpoints(obj *KubeOneCluster) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/kubeone/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ type HostConfig struct {
BastionPort int `json:"bastionPort"`
BastionUser string `json:"bastionUser"`
Hostname string `json:"hostname"`
IsLeader bool `json:"isLeader"`

// Information populated at the runtime
OperatingSystem string `json:"-"`
IsLeader bool `json:"-"`
}

// APIEndpoint is the endpoint used to communicate with the Kubernetes API
Expand Down
4 changes: 2 additions & 2 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.

7 changes: 7 additions & 0 deletions pkg/apis/kubeone/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ func ValidateCloudProviderSpec(p kubeone.CloudProviderSpec, fldPath *field.Path)
func ValidateHostConfig(hosts []kubeone.HostConfig, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}

leaderFound := false
for _, h := range hosts {
if leaderFound && h.IsLeader {
allErrs = append(allErrs, field.Invalid(fldPath, h.IsLeader, "only 1 leader is allowed"))
}
if h.IsLeader {
leaderFound = true
}
if len(h.PublicAddress) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath, h.PublicAddress, "no public IP/address given"))
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/installer/installation/kubeadm_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package installation

import (
"time"

kubeoneapi "github.com/kubermatic/kubeone/pkg/apis/kubeone"
"github.com/kubermatic/kubeone/pkg/scripts"
"github.com/kubermatic/kubeone/pkg/ssh"
Expand Down Expand Up @@ -50,7 +52,7 @@ func initKubernetesLeader(s *state.State) error {
return s.RunTaskOnLeader(func(s *state.State, node *kubeoneapi.HostConfig, conn ssh.Connection) error {
s.Logger.Infoln("Running kubeadm…")

cmd, err := scripts.KubeadmInit(s.WorkDir, node.ID, s.KubeadmVerboseFlag(), s.JoinCommand, "1h")
cmd, err := scripts.KubeadmInit(s.WorkDir, node.ID, s.KubeadmVerboseFlag(), s.JoinToken, time.Hour.String())
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/terraform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type controlPlane struct {
CloudProvider *string `json:"cloud_provider"`
PublicAddress []string `json:"public_address"`
PrivateAddress []string `json:"private_address"`
LeaderIP string `json:"leader_ip"`
Hostnames []string `json:"hostnames"`
SSHUser string `json:"ssh_user"`
SSHPort int `json:"ssh_port"`
Expand Down Expand Up @@ -202,6 +203,7 @@ func newHostConfig(id int, publicIP, privateIP, hostname string, cp controlPlane
Bastion: cp.Bastion,
BastionPort: cp.BastionPort,
BastionUser: cp.BastionUser,
IsLeader: cp.LeaderIP == publicIP || cp.LeaderIP == privateIP,
}
}

Expand Down