-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadbalance.go
137 lines (118 loc) · 3.47 KB
/
loadbalance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package phases
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"context"
"fmt"
"net"
"strconv"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/carlory/firefly/pkg/adm/cmd/options"
"github.com/carlory/firefly/pkg/adm/cmd/phases/workflow"
cmdutil "github.com/carlory/firefly/pkg/adm/cmd/util"
"github.com/carlory/firefly/pkg/adm/util/apiclient"
)
var (
loadBalanceExample = cmdutil.Examples(`
# Run pre-flight checks for kubeadm init using a config file.
kubeadm init phase loadbalance --config kubeadm-config.yaml
`)
)
// NewLoadBalancePhase creates a kubeadm workflow phase that implements preflight checks for a new control-plane node.
func NewLoadBalancePhase() workflow.Phase {
return workflow.Phase{
Name: "loadbalance",
Example: loadBalanceExample,
Run: runLoadBalance,
InheritFlags: []string{
options.CfgPath,
options.IgnorePreflightErrors,
options.DryRun,
},
}
}
// runLoadBalance executes preflight checks logic.
func runLoadBalance(c workflow.RunData) error {
data, ok := c.(InitData)
if !ok {
return errors.New("preflight phase invoked with an invalid data struct")
}
fmt.Println("[loadbalance] Creating loadbalance for the controlplane")
client, err := data.Client()
if err != nil {
return err
}
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-apiserver",
Namespace: data.Cfg().FireflyNamespace,
Labels: map[string]string{
"component": "kube-apiserver",
"tier": "control-plane",
},
},
Spec: v1.ServiceSpec{
Type: v1.ServiceTypeNodePort,
Selector: map[string]string{
"component": "kube-apiserver",
"tier": "control-plane",
},
Ports: []v1.ServicePort{
{
Port: 6443,
NodePort: 31000,
},
},
},
}
err = apiclient.CreateOrUpdateService(client, svc)
if err != nil {
return err
}
// wait for the service to be ready if its type is LoadBalancer
got, err := client.CoreV1().Services(svc.Namespace).Get(context.TODO(), svc.Name, metav1.GetOptions{})
if err != nil {
return err
}
switch got.Spec.Type {
case v1.ServiceTypeClusterIP:
data.Cfg().ControlPlaneEndpoint = net.JoinHostPort(got.Spec.ClusterIP, strconv.Itoa(int(got.Spec.Ports[0].Port)))
case v1.ServiceTypeNodePort:
address, err := findNodeAddress(client)
if err != nil {
return err
}
data.Cfg().ControlPlaneEndpoint = net.JoinHostPort(address, strconv.Itoa(int(got.Spec.Ports[0].NodePort)))
}
return nil
}
func findNodeAddress(client kubernetes.Interface) (string, error) {
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", err
}
if len(nodes.Items) == 0 {
return "", errors.New("no nodes found")
}
for _, node := range nodes.Items {
for _, addr := range node.Status.Addresses {
if addr.Address != "" {
return addr.Address, nil
}
}
}
return "", errors.New("no node address found")
}