2019-06-27 19:00:43 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-07-26 21:54:44 +00:00
|
|
|
"path/filepath"
|
2019-06-27 19:00:43 +00:00
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-06-27 19:00:43 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2019-07-26 21:54:44 +00:00
|
|
|
"github.com/rancher/kine/pkg/client"
|
2019-06-30 19:39:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-06-27 19:00:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
k3sRuntimeEtcdPath = "/k3s/runtime"
|
|
|
|
)
|
|
|
|
|
2019-07-17 07:25:34 +00:00
|
|
|
// fetchBootstrapData copies the bootstrap data (certs, keys, passwords)
|
2019-08-25 05:27:24 +00:00
|
|
|
// from etcd to individual files specified by cfg.Runtime.
|
2019-07-26 21:54:44 +00:00
|
|
|
func fetchBootstrapData(ctx context.Context, cfg *config.Control, c client.Client) error {
|
2019-07-14 07:49:08 +00:00
|
|
|
logrus.Info("Fetching bootstrap data from etcd")
|
2019-07-26 21:54:44 +00:00
|
|
|
gr, err := c.Get(ctx, k3sRuntimeEtcdPath)
|
2019-06-27 19:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
if gr.Modified == 0 {
|
2019-06-27 19:00:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-30 19:39:54 +00:00
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
paths, err := objToMap(&cfg.Runtime.ControlRuntimeBootstrap)
|
2019-06-30 15:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
files := map[string][]byte{}
|
|
|
|
if err := json.Unmarshal(gr.Data, &files); err != nil {
|
2019-06-27 19:00:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
for pathKey, data := range files {
|
|
|
|
path, ok := paths[pathKey]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2019-07-12 23:18:53 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to mkdir %s", filepath.Dir(path))
|
|
|
|
}
|
2019-06-30 19:39:54 +00:00
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
if err := ioutil.WriteFile(path, data, 0700); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to write to %s", path)
|
|
|
|
}
|
2019-06-27 19:00:43 +00:00
|
|
|
}
|
2019-06-30 19:39:54 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
// storeBootstrapData copies the bootstrap data in the opposite direction to
|
|
|
|
// fetchBootstrapData.
|
|
|
|
func storeBootstrapData(ctx context.Context, cfg *config.Control, client client.Client) error {
|
|
|
|
if cfg.BootstrapReadOnly {
|
|
|
|
return nil
|
2019-06-30 19:39:54 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
|
|
|
|
paths, err := objToMap(&cfg.Runtime.ControlRuntimeBootstrap)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2019-06-30 19:39:54 +00:00
|
|
|
}
|
2019-06-27 19:00:43 +00:00
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
dataMap := map[string][]byte{}
|
|
|
|
for pathKey, path := range paths {
|
|
|
|
if path == "" {
|
|
|
|
continue
|
2019-06-30 15:28:42 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
2019-06-30 15:28:42 +00:00
|
|
|
if err != nil {
|
2019-07-26 21:54:44 +00:00
|
|
|
return errors.Wrapf(err, "failed to read %s", path)
|
2019-06-30 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
dataMap[pathKey] = data
|
2019-06-30 19:39:54 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
|
|
|
|
bytes, err := json.Marshal(dataMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-30 19:39:54 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
|
|
|
|
return client.Put(ctx, k3sRuntimeEtcdPath, bytes)
|
2019-06-27 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:54:44 +00:00
|
|
|
func objToMap(obj interface{}) (map[string]string, error) {
|
|
|
|
bytes, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-27 19:00:43 +00:00
|
|
|
}
|
2019-07-26 21:54:44 +00:00
|
|
|
data := map[string]string{}
|
|
|
|
return data, json.Unmarshal(bytes, &data)
|
2019-06-27 19:00:43 +00:00
|
|
|
}
|