Skip to content

Commit ebf1636

Browse files
committed
kubeadm: adjust the logic around etcd data directory creation
- Ensure the directory is created with 0700 via a new function called CreateDataDirectory(). - Call this function in the init phases instead of the manual call to MkdirAll. - Call this function when joining control-plane nodes with local etcd. If the directory creation is left to the kubelet via the static Pod hostPath mounts, it will end up with 0755 which is not desired.
1 parent 3352c44 commit ebf1636

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

cmd/kubeadm/app/cmd/phases/init/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ go_library(
4444
"//cmd/kubeadm/app/preflight:go_default_library",
4545
"//cmd/kubeadm/app/util/apiclient:go_default_library",
4646
"//cmd/kubeadm/app/util/dryrun:go_default_library",
47+
"//cmd/kubeadm/app/util/etcd:go_default_library",
4748
"//cmd/kubeadm/app/util/pkiutil:go_default_library",
4849
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
4950
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",

cmd/kubeadm/app/cmd/phases/init/etcd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ package phases
1818

1919
import (
2020
"fmt"
21-
"os"
2221

2322
"github.com/pkg/errors"
2423
"k8s.io/klog/v2"
2524
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2625
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
2726
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
2827
etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd"
28+
etcdutil "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd"
2929
)
3030

3131
var (
@@ -87,8 +87,9 @@ func runEtcdPhaseLocal() func(c workflow.RunData) error {
8787
if cfg.Etcd.External == nil {
8888
// creates target folder if doesn't exist already
8989
if !data.DryRun() {
90-
if err := os.MkdirAll(cfg.Etcd.Local.DataDir, 0700); err != nil {
91-
return errors.Wrapf(err, "failed to create etcd directory %q", cfg.Etcd.Local.DataDir)
90+
// Create the etcd data directory
91+
if err := etcdutil.CreateDataDirectory(cfg.Etcd.Local.DataDir); err != nil {
92+
return err
9293
}
9394
} else {
9495
fmt.Printf("[dryrun] Would ensure that %q directory is present\n", cfg.Etcd.Local.DataDir)

cmd/kubeadm/app/cmd/phases/join/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ go_library(
2929
"//cmd/kubeadm/app/phases/uploadconfig:go_default_library",
3030
"//cmd/kubeadm/app/preflight:go_default_library",
3131
"//cmd/kubeadm/app/util/apiclient:go_default_library",
32+
"//cmd/kubeadm/app/util/etcd:go_default_library",
3233
"//cmd/kubeadm/app/util/kubeconfig:go_default_library",
3334
"//staging/src/k8s.io/api/core/v1:go_default_library",
3435
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",

cmd/kubeadm/app/cmd/phases/join/controlplanejoin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd"
3030
markcontrolplanephase "k8s.io/kubernetes/cmd/kubeadm/app/phases/markcontrolplane"
3131
uploadconfigphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/uploadconfig"
32+
etcdutil "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd"
3233
)
3334

3435
var controlPlaneJoinExample = cmdutil.Examples(`
@@ -131,6 +132,11 @@ func runEtcdPhase(c workflow.RunData) error {
131132
return nil
132133
}
133134

135+
// Create the etcd data directory
136+
if err := etcdutil.CreateDataDirectory(cfg.Etcd.Local.DataDir); err != nil {
137+
return err
138+
}
139+
134140
// Adds a new etcd instance; in order to do this the new etcd instance should be "announced" to
135141
// the existing etcd members before being created.
136142
// This operation must be executed after kubelet is already started in order to minimize the time

cmd/kubeadm/app/util/etcd/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["etcd.go"],
5+
srcs = [
6+
"etcd.go",
7+
"etcddata.go",
8+
],
69
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/util/etcd",
710
visibility = ["//visibility:public"],
811
deps = [
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package etcd
18+
19+
import (
20+
"os"
21+
22+
"github.com/pkg/errors"
23+
)
24+
25+
// CreateDataDirectory creates the etcd data directory (commonly /var/lib/etcd) with the right permissions.
26+
func CreateDataDirectory(dir string) error {
27+
if err := os.MkdirAll(dir, 0700); err != nil {
28+
return errors.Wrapf(err, "failed to create the etcd data directory: %q", dir)
29+
}
30+
return nil
31+
}

0 commit comments

Comments
 (0)