Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 0411fb7

Browse files
author
Diego Pontoriero
committed
Fix apiserver etcd-servers flag when self-hosting.
This adds the self-hosted etcd cluster to the temporary control plane config, which should hopefully mitigate situations where the control plane loses contact with etcd during the boot-etcd -> self-hosted-etcd pivot and gives up leader status as a result. I also fixed the hack/single-node config to not start local etcd when running with self-hosting.
1 parent 23dfa99 commit 0411fb7

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

hack/single-node/bootkube-up

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fi
2323
if [ ! -d "cluster" ]; then
2424
../../_output/bin/${local_os}/bootkube render --asset-dir=cluster --api-servers=https://172.17.4.100:443 ${etcd_render_flags}
2525
cp user-data.sample cluster/user-data
26+
if [ ${SELF_HOST_ETCD} = "false" ]; then
27+
cat user-data-etcd.sample >> cluster/user-data
28+
fi
2629
fi
2730

2831
# Start the VM
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: etcd-member.service
2+
enable: true
3+
drop-ins:
4+
- name: 10-version.conf
5+
content: |
6+
[Service]
7+
Environment="ETCD_IMAGE_TAG=v3.1.0"
8+
command: start

hack/single-node/user-data.sample

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
coreos:
44
units:
5-
- name: etcd-member.service
6-
enable: true
7-
drop-ins:
8-
- name: 10-version.conf
9-
content: |
10-
[Service]
11-
Environment="ETCD_IMAGE_TAG=v3.1.0"
12-
command: start
135
- name: kubelet.service
146
enable: true
157
command: start

pkg/bootkube/bootkube.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ func makeAPIServerFlags(config Config) ([]string, error) {
8585
if err != nil {
8686
return []string{}, err
8787
}
88+
etcdServers := config.EtcdServer.String()
89+
if config.SelfHostedEtcd {
90+
// When self-hosting etcd we also point to the (not yet started) permanent cluster since the
91+
// bootstrap node will go away at some point.
92+
etcdServiceURL, err := detectEtcdServiceURL(config.AssetDir)
93+
if err != nil {
94+
return nil, err
95+
}
96+
etcdServers += fmt.Sprintf(",%s", etcdServiceURL)
97+
}
8898
return []string{
8999
"--bind-address=0.0.0.0",
90100
"--secure-port=443",
@@ -96,7 +106,7 @@ func makeAPIServerFlags(config Config) ([]string, error) {
96106
"--kubelet-client-certificate=" + filepath.Join(config.AssetDir, asset.AssetPathAPIServerCert),
97107
"--client-ca-file=" + filepath.Join(config.AssetDir, asset.AssetPathCACert),
98108
"--authorization-mode=RBAC",
99-
"--etcd-servers=" + config.EtcdServer.String(),
109+
"--etcd-servers=" + etcdServers,
100110
"--service-cluster-ip-range=" + serviceCIDR,
101111
"--service-account-key-file=" + filepath.Join(config.AssetDir, asset.AssetPathServiceAccountPubKey),
102112
"--admission-control=NamespaceLifecycle,ServiceAccount",

pkg/bootkube/parse.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ func detectEtcdIP(assetDir string) (string, error) {
9191
return service.Spec.ClusterIP, nil
9292
}
9393

94+
// detectEtcdServiceURL deserializes the etcd-service URL.
95+
func detectEtcdServiceURL(assetDir string) (string, error) {
96+
path := filepath.Join(assetDir, asset.AssetPathEtcdSvc)
97+
b, err := ioutil.ReadFile(path)
98+
if err != nil {
99+
return "", fmt.Errorf("can't read file %s: %v", path, err)
100+
}
101+
var service v1.Service
102+
err = yaml.Unmarshal(b, &service)
103+
if err != nil {
104+
return "", fmt.Errorf("can't unmarshal %s: %v", path, err)
105+
}
106+
if numPorts := len(service.Spec.Ports); numPorts != 1 {
107+
return "", fmt.Errorf("expected 1 etcd cluster port, found: %d", numPorts)
108+
}
109+
return fmt.Sprintf("http://%s:%d", service.Spec.ClusterIP, service.Spec.Ports[0]), nil
110+
}
111+
94112
func findFlag(flagName string, args []string) string {
95113
for _, arg := range args {
96114
if strings.HasPrefix(arg, flagName+"=") {

0 commit comments

Comments
 (0)