Add --disable flag

pull/1379/head
Erik Wilson 2020-01-29 16:40:49 -07:00 committed by Craig Jellick
parent 3592d0bdd9
commit 0374c4f63d
6 changed files with 91 additions and 22 deletions

View File

@ -193,8 +193,8 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
Destination: &ServerConfig.DefaultLocalStoragePath, Destination: &ServerConfig.DefaultLocalStoragePath,
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "no-deploy", Name: "disable",
Usage: "(components) Do not deploy packaged components (valid items: coredns, servicelb, traefik, local-storage, metrics-server)", Usage: "(components) Do not deploy packaged components and delete any deployed components (valid items: coredns, servicelb, traefik, local-storage, metrics-server)",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "disable-scheduler", Name: "disable-scheduler",
@ -272,6 +272,10 @@ func NewServerCommand(action func(*cli.Context) error) cli.Command {
// Hidden/Deprecated flags below // Hidden/Deprecated flags below
FlannelFlag, FlannelFlag,
cli.StringSliceFlag{
Name: "no-deploy",
Usage: "(deprecated) Do not deploy packaged components (valid items: coredns, servicelb, traefik, local-storage, metrics-server)",
},
cli.StringFlag{ cli.StringFlag{
Name: "cluster-secret", Name: "cluster-secret",
Usage: "(deprecated) use --token", Usage: "(deprecated) use --token",

View File

@ -151,19 +151,21 @@ func run(app *cli.Context, cfg *cmds.Server) error {
serverConfig.ControlConfig.DefaultLocalStoragePath = cfg.DefaultLocalStoragePath serverConfig.ControlConfig.DefaultLocalStoragePath = cfg.DefaultLocalStoragePath
} }
noDeploys := make([]string, 0) serverConfig.ControlConfig.Skips = map[string]bool{}
for _, noDeploy := range app.StringSlice("no-deploy") { for _, noDeploy := range app.StringSlice("no-deploy") {
for _, splitNoDeploy := range strings.Split(noDeploy, ",") { for _, v := range strings.Split(noDeploy, ",") {
noDeploys = append(noDeploys, splitNoDeploy) serverConfig.ControlConfig.Skips[v] = true
} }
} }
serverConfig.ControlConfig.Disables = map[string]bool{}
for _, noDeploy := range noDeploys { for _, disable := range app.StringSlice("disable") {
if noDeploy == "servicelb" { for _, v := range strings.Split(disable, ",") {
serverConfig.ControlConfig.Skips[v] = true
serverConfig.ControlConfig.Disables[v] = true
}
}
if serverConfig.ControlConfig.Skips["servicelb"] {
serverConfig.DisableServiceLB = true serverConfig.DisableServiceLB = true
continue
}
serverConfig.ControlConfig.Skips = append(serverConfig.ControlConfig.Skips, noDeploy)
} }
logrus.Info("Starting k3s ", app.App.Version) logrus.Info("Starting k3s ", app.App.Version)

View File

@ -96,7 +96,8 @@ type Control struct {
KubeConfigOutput string KubeConfigOutput string
KubeConfigMode string KubeConfigMode string
DataDir string DataDir string
Skips []string Skips map[string]bool
Disables map[string]bool
Datastore endpoint.Config Datastore endpoint.Config
NoScheduler bool NoScheduler bool
ExtraAPIArgs []string ExtraAPIArgs []string

View File

@ -22,8 +22,10 @@ import (
"github.com/rancher/wrangler/pkg/objectset" "github.com/rancher/wrangler/pkg/objectset"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml" yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
) )
@ -32,12 +34,13 @@ const (
startKey = "_start_" startKey = "_start_"
) )
func WatchFiles(ctx context.Context, apply apply.Apply, addons v1.AddonController, bases ...string) error { func WatchFiles(ctx context.Context, apply apply.Apply, addons v1.AddonController, disables map[string]bool, bases ...string) error {
w := &watcher{ w := &watcher{
apply: apply, apply: apply,
addonCache: addons.Cache(), addonCache: addons.Cache(),
addons: addons, addons: addons,
bases: bases, bases: bases,
disables: disables,
} }
addons.Enqueue("", startKey) addons.Enqueue("", startKey)
@ -56,6 +59,7 @@ type watcher struct {
addonCache v1.AddonCache addonCache v1.AddonCache
addons v1.AddonClient addons v1.AddonClient
bases []string bases []string
disables map[string]bool
} }
func (w *watcher) start(ctx context.Context) { func (w *watcher) start(ctx context.Context) {
@ -111,6 +115,12 @@ func (w *watcher) listFilesIn(base string, force bool) error {
var errs []error var errs []error
for _, path := range keys { for _, path := range keys {
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
}
if skipFile(files[path].Name(), skips) { if skipFile(files[path].Name(), skips) {
continue continue
} }
@ -162,6 +172,39 @@ func (w *watcher) deploy(path string, compareChecksum bool) error {
return err return err
} }
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)
}
func (w *watcher) addon(name string) (v12.Addon, error) { func (w *watcher) addon(name string) (v12.Addon, error) {
addon, err := w.addonCache.Get(ns, name) addon, err := w.addonCache.Get(ns, name)
if errors.IsNotFound(err) { if errors.IsNotFound(err) {
@ -269,3 +312,28 @@ func skipFile(fileName string, skips map[string]bool) bool {
return true return true
} }
} }
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
}
}
switch {
case strings.HasSuffix(fileName, ".json"):
case strings.HasSuffix(fileName, ".yml"):
case strings.HasSuffix(fileName, ".yaml"):
default:
return false
}
baseFile := filepath.Base(fileName)
suffix := filepath.Ext(baseFile)
baseName := strings.TrimSuffix(baseFile, suffix)
if disables[baseName] {
return true
}
return false
}

View File

@ -11,12 +11,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func Stage(dataDir string, templateVars map[string]string, skipList []string) error { func Stage(dataDir string, templateVars map[string]string, skips map[string]bool) error {
skips := map[string]bool{}
for _, skip := range skipList {
skips[skip] = true
}
staging: staging:
for _, name := range AssetNames() { for _, name := range AssetNames() {
nameNoExtension := strings.TrimSuffix(name, filepath.Ext(name)) nameNoExtension := strings.TrimSuffix(name, filepath.Ext(name))

View File

@ -27,7 +27,6 @@ import (
v1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1" v1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/leader" "github.com/rancher/wrangler/pkg/leader"
"github.com/rancher/wrangler/pkg/resolvehome" "github.com/rancher/wrangler/pkg/resolvehome"
"github.com/rancher/wrangler/pkg/slice"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/net" "k8s.io/apimachinery/pkg/util/net"
@ -127,7 +126,7 @@ func startWrangler(ctx context.Context, config *Config) error {
} }
func masterControllers(ctx context.Context, sc *Context, config *Config) error { func masterControllers(ctx context.Context, sc *Context, config *Config) error {
if !slice.ContainsString(config.ControlConfig.Skips, "coredns") { if !config.ControlConfig.Skips["coredns"] {
if err := node.Register(ctx, sc.Core.Core().V1().ConfigMap(), sc.Core.Core().V1().Node()); err != nil { if err := node.Register(ctx, sc.Core.Core().V1().ConfigMap(), sc.Core.Core().V1().Node()); err != nil {
return err return err
} }
@ -176,7 +175,7 @@ func stageFiles(ctx context.Context, sc *Context, controlConfig *config.Control)
return err return err
} }
return deploy.WatchFiles(ctx, sc.Apply, sc.K3s.K3s().V1().Addon(), dataDir) return deploy.WatchFiles(ctx, sc.Apply, sc.K3s.K3s().V1().Addon(), controlConfig.Disables, dataDir)
} }
func HomeKubeConfig(write, rootless bool) (string, error) { func HomeKubeConfig(write, rootless bool) (string, error) {