From 043d2bbd35d5c9c8bc6deddc37ea4aaf6346c8fb Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 28 May 2024 15:13:44 +0200 Subject: [PATCH] openstack-seeder: support selecting or ignoring multiple namespaces The implementation is backwards-compatible with existing invocations. --- openstack-seeder/cmd/main.go | 4 +-- .../pkg/seeder/controller/controller.go | 31 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/openstack-seeder/cmd/main.go b/openstack-seeder/cmd/main.go index 70f2d0e75..a6c41a694 100644 --- a/openstack-seeder/cmd/main.go +++ b/openstack-seeder/cmd/main.go @@ -49,8 +49,8 @@ func main() { pflag.StringVar(&options.KubeConfig, "kubeconfig", "", "Path to kubeconfig file with authorization and master location information.") pflag.BoolVar(&options.DryRun, "dry-run", false, "Only pretend to seed.") pflag.StringVar(&options.InterfaceType, "interface", "internal", "Openstack service interface type to use.") - pflag.StringVar(&options.IgnoreNamespace, "ignorenamespace", "", "Ignore seeds from a certain k8s Namespace.") - pflag.StringVar(&options.OnlyNamespace, "onlynamespace", "", "Only apply seeds from a certain k8s Namespace.") + pflag.StringArrayVar(&options.IgnoreNamespaces, "ignorenamespace", nil, "Ignore seeds from a certain k8s Namespace (can be given multiple times to ignore multiple namespaces).") + pflag.StringArrayVar(&options.OnlyNamespaces, "onlynamespace", nil, "Only apply seeds from a certain k8s Namespace (can be given multiple times to watch multiple namespaces).") pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() diff --git a/openstack-seeder/pkg/seeder/controller/controller.go b/openstack-seeder/pkg/seeder/controller/controller.go index 639d844f2..2f93ed80b 100644 --- a/openstack-seeder/pkg/seeder/controller/controller.go +++ b/openstack-seeder/pkg/seeder/controller/controller.go @@ -19,6 +19,8 @@ package controller import ( "context" "flag" + "slices" + "gopkg.in/yaml.v2" "k8s.io/apimachinery/pkg/fields" @@ -33,13 +35,14 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "fmt" - "github.com/getsentry/raven-go" - "github.com/golang/glog" - "k8s.io/client-go/tools/clientcmd" "os" "os/exec" "strings" "time" + + "github.com/getsentry/raven-go" + "github.com/golang/glog" + "k8s.io/client-go/tools/clientcmd" ) var ( @@ -48,11 +51,11 @@ var ( ) type Options struct { - KubeConfig string - DryRun bool - InterfaceType string - IgnoreNamespace string - OnlyNamespace string + KubeConfig string + DryRun bool + InterfaceType string + IgnoreNamespaces []string + OnlyNamespaces []string } type SeederController struct { @@ -210,15 +213,15 @@ func (c *SeederController) seedApply(seed *seederv1.OpenstackSeed) { } // to allow to ignore seeds from a particular namespace - if seed.ObjectMeta.Namespace == c.Options.IgnoreNamespace { - glog.Infof("Ignoring seeds from %s Namespace.", c.Options.IgnoreNamespace) + if slices.Contains(c.Options.IgnoreNamespaces, seed.ObjectMeta.Namespace) { + glog.Infof("Ignoring seeds from %s Namespace.", seed.ObjectMeta.Namespace) return } // to only apply seeds from a particular namespace and ignore the rest - if c.Options.OnlyNamespace != "" { - if seed.ObjectMeta.Namespace != c.Options.OnlyNamespace { - glog.Infof("Ignoring seeds from %s Namespace. Only seeds from %s Namespace will be applied.", seed.ObjectMeta.Namespace, c.Options.OnlyNamespace) + if len(c.Options.OnlyNamespaces) > 0 { + if slices.Contains(c.Options.OnlyNamespaces, seed.ObjectMeta.Namespace) { + glog.Infof("Ignoring seeds from %s Namespace. Only seeds from %v Namespaces will be applied.", seed.ObjectMeta.Namespace, c.Options.OnlyNamespaces) return } } @@ -266,7 +269,7 @@ func (c *SeederController) seedApply(seed *seederv1.OpenstackSeed) { cmd.Stderr = os.Stderr if err = cmd.Start(); err != nil { - glog.Errorf("ERROR: could not spawn %s: ", seeder_name, err) + glog.Errorf("ERROR: could not spawn %s: %v", seeder_name, err) } stdin.Write(yaml_seed)