Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit e71f308

Browse files
use rootfs to store manifests of different controlplane into different directory
1 parent da2067d commit e71f308

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ ENTRYPOINT [ "/start.sh", "/workspace/manager" ]
5050

5151
# Use distroless as minimal base image to package the manager binary
5252
# Refer to https://github.com/GoogleContainerTools/distroless for more details
53-
# FROM gcr.io/distroless/static:nonroot
54-
FROM ubuntu
53+
FROM gcr.io/distroless/static-debian10
5554
# Copy the controller-manager into a thin image
5655
WORKDIR /
5756
COPY --from=builder /workspace/manager .

controlplane/nested/kubeadm/kubeadm.go

+32-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
package kubeadm
2020

2121
import (
22+
"bytes"
2223
"io/ioutil"
2324
"os"
2425
"os/exec"
@@ -87,40 +88,62 @@ etcd:
8788
name: $(HOSTNAME)
8889
data-dir: /var/lib/etcd/data`
8990

90-
func execCommand(command string, subcommand ...string) error {
91+
func execCommand(log logr.Logger, command string, subcommand ...string) error {
9192
cmd := exec.Command(command, subcommand...)
92-
return cmd.Run()
93+
var (
94+
out bytes.Buffer
95+
stderr bytes.Buffer
96+
)
97+
cmd.Stdout = &out
98+
cmd.Stderr = &stderr
99+
if err := cmd.Run(); err != nil {
100+
log.Error(err, "fail to run kubeadm", "stderr", stderr.String())
101+
return err
102+
}
103+
log.Info("successfully execute the kubeadm command", "stdout", out.String())
104+
return nil
93105
}
94106

95107
// GenerateTemplates generates the manifests for the nested apiserver,
96108
// controller-manager and etcd by calling the `kubeadm init phase control-plane/etcd`.
97109
func GenerateTemplates(log logr.Logger, clusterName string) (map[string]string, error) {
110+
// create the cluster manifests directory if not exist
111+
if err := os.MkdirAll("/"+clusterName, 0755); err != nil {
112+
errors.Wrap(err, "fail to create the cluster manifests directory")
113+
}
114+
// defer os.RemoveAll("/" + clusterName)
115+
log.Info("cluster manifests directory is created")
98116
if err := generateKubeadmConfig(clusterName); err != nil {
99117
return nil, err
100118
}
101119
log.Info("kubeadmconfig is generated")
120+
121+
// store manifests of different nested cluster in different directory
122+
KASSubcommand = append(KASSubcommand, "--rootfs", "/"+clusterName)
123+
KCMSubcommand = append(KCMSubcommand, "--rootfs", "/"+clusterName)
124+
EtcdSubcommand = append(EtcdSubcommand, "--rootfs", "/"+clusterName)
102125
// generate the manifests
103-
if err := execCommand(KubeadmExecPath, KASSubcommand...); err != nil {
126+
if err := execCommand(log, KubeadmExecPath, KASSubcommand...); err != nil {
104127
return nil, errors.Wrap(err, "fail to generate the apiserver manifests")
105128
}
106-
if err := execCommand(KubeadmExecPath, KCMSubcommand...); err != nil {
129+
if err := execCommand(log, KubeadmExecPath, KCMSubcommand...); err != nil {
107130
return nil, errors.Wrap(err, "fail to generate the controller-manager manifests")
108131
}
109-
if err := execCommand(KubeadmExecPath, EtcdSubcommand...); err != nil {
132+
if err := execCommand(log, KubeadmExecPath, EtcdSubcommand...); err != nil {
110133
return nil, errors.Wrap(err, "fail to generate the etcd manifests")
111134
}
112135
log.Info("static pod manifests generated")
113136

114137
var loadErr error
115-
KASManifests, loadErr := ioutil.ReadFile(KASManifestsPath)
138+
KASManifests, loadErr := ioutil.ReadFile("/" + clusterName + KASManifestsPath)
116139
if loadErr != nil {
117140
return nil, errors.Wrap(loadErr, "fail to load the apiserver manifests")
118141
}
119-
KCMManifests, loadErr := ioutil.ReadFile(KCMManifestsPath)
142+
KCMManifests, loadErr := ioutil.ReadFile("/" + clusterName + KCMManifestsPath)
120143
if loadErr != nil {
121144
return nil, errors.Wrap(loadErr, "fail to load the controller-manager manifests")
122145
}
123-
EtcdManifests, loadErr := ioutil.ReadFile(EtcdManifestsPath)
146+
EtcdManifests, loadErr := ioutil.ReadFile("/" + clusterName + EtcdManifestsPath)
124147
if loadErr != nil {
125148
return nil, errors.Wrap(loadErr, "fail to load the etcd manifests")
126149
}
@@ -139,7 +162,7 @@ func generateKubeadmConfig(clusterName string) error {
139162
if err != nil {
140163
return err
141164
}
142-
return os.WriteFile(DefaultKubeadmConfigPath, []byte(completedKubeadmConfig), 0600)
165+
return os.WriteFile("/"+clusterName+DefaultKubeadmConfigPath, []byte(completedKubeadmConfig), 0600)
143166
}
144167

145168
func completeDefaultKubeadmConfig(clusterName string) (string, error) {

0 commit comments

Comments
 (0)