2019-01-22 21:14:58 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-11-12 23:05:09 +00:00
|
|
|
"sort"
|
2019-01-22 21:14:58 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
errors2 "github.com/pkg/errors"
|
2021-02-13 01:06:23 +00:00
|
|
|
"github.com/rancher/k3s/pkg/agent/util"
|
2019-05-09 22:05:51 +00:00
|
|
|
v12 "github.com/rancher/k3s/pkg/apis/k3s.cattle.io/v1"
|
|
|
|
v1 "github.com/rancher/k3s/pkg/generated/controllers/k3s.cattle.io/v1"
|
|
|
|
"github.com/rancher/wrangler/pkg/apply"
|
|
|
|
"github.com/rancher/wrangler/pkg/merr"
|
|
|
|
"github.com/rancher/wrangler/pkg/objectset"
|
2019-01-22 21:14:58 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2020-01-29 23:40:49 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-01-22 21:14:58 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-01-29 23:40:49 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2019-01-22 21:14:58 +00:00
|
|
|
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-10 18:27:36 +00:00
|
|
|
ControllerName = "deploy"
|
|
|
|
ns = "kube-system"
|
|
|
|
startKey = "_start_"
|
2019-01-22 21:14:58 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 23:40:49 +00:00
|
|
|
func WatchFiles(ctx context.Context, apply apply.Apply, addons v1.AddonController, disables map[string]bool, bases ...string) error {
|
2019-01-22 21:14:58 +00:00
|
|
|
w := &watcher{
|
2019-05-26 06:42:09 +00:00
|
|
|
apply: apply,
|
2019-01-22 21:14:58 +00:00
|
|
|
addonCache: addons.Cache(),
|
|
|
|
addons: addons,
|
|
|
|
bases: bases,
|
2020-01-29 23:40:49 +00:00
|
|
|
disables: disables,
|
2020-04-22 16:58:41 +00:00
|
|
|
modTime: map[string]time.Time{},
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 04:15:02 +00:00
|
|
|
addons.Enqueue("", startKey)
|
2019-05-09 22:05:51 +00:00
|
|
|
addons.OnChange(ctx, "addon-start", func(key string, _ *v12.Addon) (*v12.Addon, error) {
|
2019-02-08 04:15:02 +00:00
|
|
|
if key == startKey {
|
2019-03-07 18:24:18 +00:00
|
|
|
go w.start(ctx)
|
2019-02-08 04:15:02 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
2019-01-22 21:14:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcher struct {
|
2019-05-09 22:05:51 +00:00
|
|
|
apply apply.Apply
|
|
|
|
addonCache v1.AddonCache
|
2019-01-22 21:14:58 +00:00
|
|
|
addons v1.AddonClient
|
|
|
|
bases []string
|
2020-01-29 23:40:49 +00:00
|
|
|
disables map[string]bool
|
2020-04-22 16:58:41 +00:00
|
|
|
modTime map[string]time.Time
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) start(ctx context.Context) {
|
2019-03-07 18:24:18 +00:00
|
|
|
force := true
|
2019-01-22 21:14:58 +00:00
|
|
|
for {
|
2019-03-07 18:24:18 +00:00
|
|
|
if err := w.listFiles(force); err == nil {
|
|
|
|
force = false
|
|
|
|
} else {
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Errorf("Failed to process config: %v", err)
|
2019-03-07 18:24:18 +00:00
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) listFiles(force bool) error {
|
|
|
|
var errs []error
|
|
|
|
for _, base := range w.bases {
|
|
|
|
if err := w.listFilesIn(base, force); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
return merr.NewErrors(errs...)
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) listFilesIn(base string, force bool) error {
|
2019-10-11 09:42:24 +00:00
|
|
|
files := map[string]os.FileInfo{}
|
|
|
|
if err := filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
files[path] = info
|
2019-01-22 21:14:58 +00:00
|
|
|
return nil
|
2019-10-11 09:42:24 +00:00
|
|
|
}); err != nil {
|
2019-01-22 21:14:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
skips := map[string]bool{}
|
2019-11-12 23:05:09 +00:00
|
|
|
keys := make([]string, len(files))
|
|
|
|
keyIndex := 0
|
|
|
|
for path, file := range files {
|
2019-01-22 21:14:58 +00:00
|
|
|
if strings.HasSuffix(file.Name(), ".skip") {
|
|
|
|
skips[strings.TrimSuffix(file.Name(), ".skip")] = true
|
|
|
|
}
|
2019-11-12 23:05:09 +00:00
|
|
|
keys[keyIndex] = path
|
|
|
|
keyIndex++
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
2019-11-12 23:05:09 +00:00
|
|
|
sort.Strings(keys)
|
2019-01-22 21:14:58 +00:00
|
|
|
|
|
|
|
var errs []error
|
2019-11-12 23:05:09 +00:00
|
|
|
for _, path := range keys {
|
2020-01-29 23:40:49 +00:00
|
|
|
if shouldDisableService(base, path, w.disables) {
|
|
|
|
if err := w.delete(path); err != nil {
|
|
|
|
errs = append(errs, errors2.Wrapf(err, "failed to delete %s", path))
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-11-12 23:05:09 +00:00
|
|
|
if skipFile(files[path].Name(), skips) {
|
2019-01-22 21:14:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-04-22 16:58:41 +00:00
|
|
|
modTime := files[path].ModTime()
|
|
|
|
if !force && modTime.Equal(w.modTime[path]) {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-11 09:42:24 +00:00
|
|
|
if err := w.deploy(path, !force); err != nil {
|
|
|
|
errs = append(errs, errors2.Wrapf(err, "failed to process %s", path))
|
2020-04-22 16:58:41 +00:00
|
|
|
} else {
|
|
|
|
w.modTime[path] = modTime
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
return merr.NewErrors(errs...)
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) deploy(path string, compareChecksum bool) error {
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := name(path)
|
|
|
|
addon, err := w.addon(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
checksum := checksum(content)
|
|
|
|
if compareChecksum && checksum == addon.Spec.Checksum {
|
2019-02-08 04:15:02 +00:00
|
|
|
logrus.Debugf("Skipping existing deployment of %s, check=%v, checksum %s=%s", path, compareChecksum, checksum, addon.Spec.Checksum)
|
2019-01-22 21:14:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
objectSet, err := objectSet(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
if err := w.apply.WithOwner(&addon).Apply(objectSet); err != nil {
|
2019-02-08 04:15:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
addon.Spec.Source = path
|
|
|
|
addon.Spec.Checksum = checksum
|
|
|
|
addon.Status.GVKs = nil
|
|
|
|
|
|
|
|
if addon.UID == "" {
|
|
|
|
_, err := w.addons.Create(&addon)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.addons.Update(&addon)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:40:49 +00:00
|
|
|
func (w *watcher) delete(path string) error {
|
|
|
|
name := name(path)
|
|
|
|
addon, err := w.addon(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that the addon is completely removed before deleting the objectSet,
|
|
|
|
// so return when err == nil, otherwise pods may get stuck terminating
|
|
|
|
if err := w.addons.Delete(addon.Namespace, addon.Name, &metav1.DeleteOptions{}); err == nil || !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objectSet, err := objectSet(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var gvk []schema.GroupVersionKind
|
|
|
|
for k := range objectSet.ObjectsByGVK() {
|
|
|
|
gvk = append(gvk, k)
|
|
|
|
}
|
|
|
|
// apply an empty set with owner & gvk data to delete
|
|
|
|
if err := w.apply.WithOwner(&addon).WithGVK(gvk...).Apply(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Remove(path)
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (w *watcher) addon(name string) (v12.Addon, error) {
|
2019-01-22 21:14:58 +00:00
|
|
|
addon, err := w.addonCache.Get(ns, name)
|
|
|
|
if errors.IsNotFound(err) {
|
2019-05-09 22:05:51 +00:00
|
|
|
addon = v12.NewAddon(ns, name, v12.Addon{})
|
2019-01-22 21:14:58 +00:00
|
|
|
} else if err != nil {
|
2019-05-09 22:05:51 +00:00
|
|
|
return v12.Addon{}, err
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
return *addon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func objectSet(content []byte) (*objectset.ObjectSet, error) {
|
|
|
|
objs, err := yamlToObjects(bytes.NewBuffer(content))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
os := objectset.NewObjectSet()
|
|
|
|
os.Add(objs...)
|
|
|
|
return os, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func name(path string) string {
|
|
|
|
name := filepath.Base(path)
|
|
|
|
return strings.SplitN(name, ".", 2)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func checksum(bytes []byte) string {
|
|
|
|
d := sha256.Sum256(bytes)
|
|
|
|
return hex.EncodeToString(d[:])
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:36:26 +00:00
|
|
|
func isEmptyYaml(yaml []byte) bool {
|
|
|
|
isEmpty := true
|
|
|
|
lines := bytes.Split(yaml, []byte("\n"))
|
2019-03-14 12:12:02 +00:00
|
|
|
for _, l := range lines {
|
|
|
|
s := bytes.TrimSpace(l)
|
|
|
|
if string(s) != "---" && !bytes.HasPrefix(s, []byte("#")) && string(s) != "" {
|
2019-03-14 11:36:26 +00:00
|
|
|
isEmpty = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isEmpty
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
func yamlToObjects(in io.Reader) ([]runtime.Object, error) {
|
|
|
|
var result []runtime.Object
|
|
|
|
reader := yamlDecoder.NewYAMLReader(bufio.NewReaderSize(in, 4096))
|
|
|
|
for {
|
|
|
|
raw, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:36:26 +00:00
|
|
|
if !isEmptyYaml(raw) {
|
|
|
|
obj, err := toObjects(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
|
2019-03-14 11:36:26 +00:00
|
|
|
result = append(result, obj...)
|
|
|
|
}
|
2019-01-22 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toObjects(bytes []byte) ([]runtime.Object, error) {
|
|
|
|
bytes, err := yamlDecoder.ToJSON(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-14 11:36:26 +00:00
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
obj, _, err := unstructured.UnstructuredJSONScheme.Decode(bytes, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, ok := obj.(*unstructured.UnstructuredList); ok {
|
|
|
|
var result []runtime.Object
|
|
|
|
for _, obj := range l.Items {
|
|
|
|
copy := obj
|
|
|
|
result = append(result, ©)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []runtime.Object{obj}, nil
|
|
|
|
}
|
2019-04-17 22:13:11 +00:00
|
|
|
|
|
|
|
func skipFile(fileName string, skips map[string]bool) bool {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(fileName, "."):
|
|
|
|
return true
|
|
|
|
case skips[fileName]:
|
|
|
|
return true
|
2021-02-13 01:06:23 +00:00
|
|
|
case util.HasSuffixI(fileName, ".yaml", ".yml", ".json"):
|
2019-04-17 22:13:11 +00:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 23:40:49 +00:00
|
|
|
|
|
|
|
func shouldDisableService(base, fileName string, disables map[string]bool) bool {
|
|
|
|
relFile := strings.TrimPrefix(fileName, base)
|
|
|
|
namePath := strings.Split(relFile, string(os.PathSeparator))
|
|
|
|
for i := 1; i < len(namePath); i++ {
|
|
|
|
subPath := filepath.Join(namePath[0:i]...)
|
|
|
|
if disables[subPath] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 01:06:23 +00:00
|
|
|
if !util.HasSuffixI(fileName, ".yaml", ".yml", ".json") {
|
2020-01-29 23:40:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
baseFile := filepath.Base(fileName)
|
|
|
|
suffix := filepath.Ext(baseFile)
|
|
|
|
baseName := strings.TrimSuffix(baseFile, suffix)
|
|
|
|
if disables[baseName] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|