-
Notifications
You must be signed in to change notification settings - Fork 20
/
init.go
136 lines (118 loc) · 4.27 KB
/
init.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
/*
* Copyright 2019 gosoon.
*
* 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.
*/
package kubeadminit
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gosoon/glog"
"github.com/gosoon/kubernetes-operator/pkg/exec"
"github.com/gosoon/kubernetes-operator/pkg/internal/cluster/create/actions"
"github.com/gosoon/kubernetes-operator/pkg/internal/cluster/kubeadm"
"github.com/pkg/errors"
)
// kubeadmInitAction implements action for executing the kubadm init
// and a set of default post init operations like e.g. install the
// CNI network plugin.
type action struct{}
// NewAction returns a new action for kubeadm init
func NewAction() actions.Action {
return &action{}
}
// Execute runs the action
func (a *action) Execute(ctx *actions.ActionContext) error {
ctx.Status.Start("Starting control-plane")
defer ctx.Status.End(false)
// run kubeadm
cmd := exec.Command(
// --service-cidr
// --kubernetes-version
// --image-repository
// --pod-network-cidr
// init because this is the control plane node
"kubeadm", "init",
// preflight errors are expected, in particular for swap being enabled
// TODO(bentheelder): limit the set of acceptable errors
"--ignore-preflight-errors=all",
// specify our generated config file
"--config=/tmp/install/kubeadm.conf",
"--skip-token-print",
// increase verbosity for debugging
"--v=6",
)
lines, err := exec.CombinedOutputLines(cmd)
glog.Info(strings.Join(lines, "\n"))
if err != nil {
glog.Error(err)
return errors.Wrap(err, "failed to init node with kubeadm")
}
kubeConfigPath := ctx.Cluster.KubeConfigPath
hostPort := kubeadm.APIServerPort
// set apiServerAddress is externalLoadBalancer or nodeAddress
apiServerAddress := ctx.Cluster.ExternalLoadBalancer
if ctx.Cluster.ExternalLoadBalancer == "" {
apiServerAddress = ctx.Cluster.NodeAddress
}
if err := writeKubeConfig(kubeConfigPath, apiServerAddress, hostPort); err != nil {
glog.Error(err)
return errors.Wrap(err, "failed to init node with kubeadm")
}
// if we are only provisioning one node, remove the master taint
// https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#master-isolation
// mark success
ctx.Status.End(true)
return nil
}
// matches kubeconfig server entry like:
// server: https://172.17.0.2:6443
// which we rewrite to:
// server: https://$ADDRESS:$PORT
var serverAddressRE = regexp.MustCompile(`^(\s+server:) https://.*:\d+$`)
// writeKubeConfig writes a fixed KUBECONFIG to dest
// this should only be called on a control plane node
// While copyng to the host machine the control plane address
// is replaced with local host and the control plane port with
// a randomly generated port reserved during node creation.
func writeKubeConfig(dest string, hostAddress string, hostPort int) error {
cmd := exec.Command("cat", "/etc/kubernetes/admin.conf")
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
return errors.Wrap(err, "failed to get kubeconfig from node")
}
// fix the config file, swapping out the server for the forwarded localhost:port
var buff bytes.Buffer
for _, line := range lines {
match := serverAddressRE.FindStringSubmatch(line)
if len(match) > 1 {
addr := net.JoinHostPort(hostAddress, fmt.Sprintf("%d", hostPort))
line = fmt.Sprintf("%s https://%s", match[1], addr)
}
buff.WriteString(line)
buff.WriteString("\n")
}
// create the directory to contain the KUBECONFIG file.
// 0755 is taken from client-go's config handling logic: https://github.com/kubernetes/client-go/blob/5d107d4ebc00ee0ea606ad7e39fd6ce4b0d9bf9e/tools/clientcmd/ loader.go#L412
err = os.MkdirAll(filepath.Dir(dest), 0755)
if err != nil {
return errors.Wrap(err, "failed to create kubeconfig output directory")
}
return ioutil.WriteFile(dest, buff.Bytes(), 0600)
}