@@ -19,6 +19,7 @@ limitations under the License.
19
19
package kubeadm
20
20
21
21
import (
22
+ "bytes"
22
23
"io/ioutil"
23
24
"os"
24
25
"os/exec"
@@ -87,40 +88,62 @@ etcd:
87
88
name: $(HOSTNAME)
88
89
data-dir: /var/lib/etcd/data`
89
90
90
- func execCommand (command string , subcommand ... string ) error {
91
+ func execCommand (log logr. Logger , command string , subcommand ... string ) error {
91
92
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
93
105
}
94
106
95
107
// GenerateTemplates generates the manifests for the nested apiserver,
96
108
// controller-manager and etcd by calling the `kubeadm init phase control-plane/etcd`.
97
109
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" )
98
116
if err := generateKubeadmConfig (clusterName ); err != nil {
99
117
return nil , err
100
118
}
101
119
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 )
102
125
// generate the manifests
103
- if err := execCommand (KubeadmExecPath , KASSubcommand ... ); err != nil {
126
+ if err := execCommand (log , KubeadmExecPath , KASSubcommand ... ); err != nil {
104
127
return nil , errors .Wrap (err , "fail to generate the apiserver manifests" )
105
128
}
106
- if err := execCommand (KubeadmExecPath , KCMSubcommand ... ); err != nil {
129
+ if err := execCommand (log , KubeadmExecPath , KCMSubcommand ... ); err != nil {
107
130
return nil , errors .Wrap (err , "fail to generate the controller-manager manifests" )
108
131
}
109
- if err := execCommand (KubeadmExecPath , EtcdSubcommand ... ); err != nil {
132
+ if err := execCommand (log , KubeadmExecPath , EtcdSubcommand ... ); err != nil {
110
133
return nil , errors .Wrap (err , "fail to generate the etcd manifests" )
111
134
}
112
135
log .Info ("static pod manifests generated" )
113
136
114
137
var loadErr error
115
- KASManifests , loadErr := ioutil .ReadFile (KASManifestsPath )
138
+ KASManifests , loadErr := ioutil .ReadFile ("/" + clusterName + KASManifestsPath )
116
139
if loadErr != nil {
117
140
return nil , errors .Wrap (loadErr , "fail to load the apiserver manifests" )
118
141
}
119
- KCMManifests , loadErr := ioutil .ReadFile (KCMManifestsPath )
142
+ KCMManifests , loadErr := ioutil .ReadFile ("/" + clusterName + KCMManifestsPath )
120
143
if loadErr != nil {
121
144
return nil , errors .Wrap (loadErr , "fail to load the controller-manager manifests" )
122
145
}
123
- EtcdManifests , loadErr := ioutil .ReadFile (EtcdManifestsPath )
146
+ EtcdManifests , loadErr := ioutil .ReadFile ("/" + clusterName + EtcdManifestsPath )
124
147
if loadErr != nil {
125
148
return nil , errors .Wrap (loadErr , "fail to load the etcd manifests" )
126
149
}
@@ -139,7 +162,7 @@ func generateKubeadmConfig(clusterName string) error {
139
162
if err != nil {
140
163
return err
141
164
}
142
- return os .WriteFile (DefaultKubeadmConfigPath , []byte (completedKubeadmConfig ), 0600 )
165
+ return os .WriteFile ("/" + clusterName + DefaultKubeadmConfigPath , []byte (completedKubeadmConfig ), 0600 )
143
166
}
144
167
145
168
func completeDefaultKubeadmConfig (clusterName string ) (string , error ) {
0 commit comments