diff --git a/charts/yawol-controller/templates/_helpers.tpl b/charts/yawol-controller/templates/_helpers.tpl index 054a6452..dcb2149e 100644 --- a/charts/yawol-controller/templates/_helpers.tpl +++ b/charts/yawol-controller/templates/_helpers.tpl @@ -3,7 +3,7 @@ apps/v1 {{- end -}} {{- define "logFlags" }} -- -zap-stacktrace-level={{ .Values.logging.stacktraceLevel }} -- -zap-log-level={{ .Values.logging.level }} -- -zap-encoder={{ .Values.logging.encoding }} +- --zap-stacktrace-level={{ .Values.logging.stacktraceLevel }} +- --zap-log-level={{ .Values.logging.level }} +- --zap-encoder={{ .Values.logging.encoding }} {{- end }} diff --git a/charts/yawol-controller/templates/yawol-cloud-controller.yaml b/charts/yawol-controller/templates/yawol-cloud-controller.yaml index 798a9b3b..6e46cce1 100644 --- a/charts/yawol-controller/templates/yawol-cloud-controller.yaml +++ b/charts/yawol-controller/templates/yawol-cloud-controller.yaml @@ -40,9 +40,9 @@ spec: {{- if .Values.yawolCloudController.additionalArguments }} {{ toYaml .Values.yawolCloudController.additionalArguments | indent 8 }} {{- end }} - - -leader-elect + - --leader-elect {{- if .Values.yawolClassName }} - - -classname={{ .Values.yawolClassName }} + - --classname={{ .Values.yawolClassName }} {{- end }} {{- include "logFlags" . | indent 8 }} env: diff --git a/charts/yawol-controller/templates/yawol-controller.yaml b/charts/yawol-controller/templates/yawol-controller.yaml index 0390c95b..2a8a7c3d 100644 --- a/charts/yawol-controller/templates/yawol-controller.yaml +++ b/charts/yawol-controller/templates/yawol-controller.yaml @@ -35,16 +35,16 @@ spec: - containerPort: 8080 name: metrics args: - - -leader-elect - - -enable-loadbalancer-controller + - --leader-elect + - --enable-loadbalancer-controller {{- if .Values.openstackTimeout }} - - -openstack-timeout={{ .Values.openstackTimeout }} + - --openstack-timeout={{ .Values.openstackTimeout }} {{- end }} {{- if .Values.yawolController.errorBackoffBaseDelay }} - - -error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} + - --error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} {{- end }} {{- if .Values.yawolController.errorBackoffMaxDelay }} - - -error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} + - --error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} {{- end }} {{- include "logFlags" . | indent 10 }} env: @@ -73,13 +73,13 @@ spec: - containerPort: 8081 name: metrics args: - - -leader-elect - - -enable-loadbalancerset-controller + - --leader-elect + - --enable-loadbalancerset-controller {{- if .Values.yawolController.errorBackoffBaseDelay }} - - -error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} + - --error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} {{- end }} {{- if .Values.yawolController.errorBackoffMaxDelay }} - - -error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} + - --error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} {{- end }} {{- include "logFlags" . | indent 10 }} env: @@ -108,19 +108,19 @@ spec: - containerPort: 8082 name: metrics args: - - -leader-elect - - -enable-loadbalancermachine-controller + - --leader-elect + - --enable-loadbalancermachine-controller {{- if .Values.yawolletRequeueTime }} - - -yawollet-requeue-time={{ .Values.yawolletRequeueTime }} + - --yawollet-requeue-time={{ .Values.yawolletRequeueTime }} {{- end }} {{- if .Values.openstackTimeout }} - - -openstack-timeout={{ .Values.openstackTimeout }} + - --openstack-timeout={{ .Values.openstackTimeout }} {{- end }} {{- if .Values.yawolController.errorBackoffBaseDelay }} - - -error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} + - --error-backoff-base-delay={{ .Values.yawolController.errorBackoffBaseDelay }} {{- end }} {{- if .Values.yawolController.errorBackoffMaxDelay }} - - -error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} + - --error-backoff-max-delay={{ .Values.yawolController.errorBackoffMaxDelay }} {{- end }} {{- include "logFlags" . | indent 10 }} env: diff --git a/cmd/yawol-cloud-controller/main.go b/cmd/yawol-cloud-controller/main.go index 983e5e33..607e99b3 100644 --- a/cmd/yawol-cloud-controller/main.go +++ b/cmd/yawol-cloud-controller/main.go @@ -3,11 +3,12 @@ package main import ( "context" "flag" + "fmt" "os" "strconv" - "strings" "time" + "github.com/spf13/pflag" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -16,10 +17,6 @@ import ( "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/cache" - yawolv1beta1 "github.com/stackitcloud/yawol/api/v1beta1" - "github.com/stackitcloud/yawol/controllers/yawol-cloud-controller/controlcontroller" - "github.com/stackitcloud/yawol/controllers/yawol-cloud-controller/targetcontroller" - "github.com/stackitcloud/yawol/internal/helper" "go.uber.org/zap/zapcore" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -28,6 +25,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/metrics/server" + + yawolv1beta1 "github.com/stackitcloud/yawol/api/v1beta1" + "github.com/stackitcloud/yawol/controllers/yawol-cloud-controller/controlcontroller" + "github.com/stackitcloud/yawol/controllers/yawol-cloud-controller/targetcontroller" + "github.com/stackitcloud/yawol/internal/helper" //+kubebuilder:scaffold:imports ) @@ -36,8 +38,6 @@ var ( setupLog = ctrl.Log.WithName("setup") ) -type loadbalancerClassNames []string - const ( // Namespace in for LoadBalancer CRs EnvClusterNamespace = "CLUSTER_NAMESPACE" @@ -79,7 +79,7 @@ func main() { var targetEnableLeaderElection bool var targetKubeconfig string var controlKubeconfig string - var classNames loadbalancerClassNames + var classNames []string var emptyClassName bool // settings for leases var leasesDurationInt int @@ -90,39 +90,47 @@ func main() { var leasesRenewDeadline time.Duration var leasesRetryPeriod time.Duration - flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") - flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") - flag.BoolVar(&controlEnableLeaderElection, "leader-elect", false, + fs := pflag.NewFlagSet("yawol-cloud-controller", pflag.ExitOnError) + + fs.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") + fs.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") + fs.BoolVar(&controlEnableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - flag.BoolVar(&targetEnableLeaderElection, "target-leader-elect", false, + fs.BoolVar(&targetEnableLeaderElection, "target-leader-elect", false, "Enable leader election for target manager. "+ "Enabling this will ensure there is only one active target manager.") - flag.StringVar(&targetKubeconfig, "target-kubeconfig", "", + fs.StringVar(&targetKubeconfig, "target-kubeconfig", "", "K8s credentials for watching the Service resources.") - flag.StringVar(&controlKubeconfig, "control-kubeconfig", "", + fs.StringVar(&controlKubeconfig, "control-kubeconfig", "", "K8s credentials for deploying the LoadBalancer resources.") - flag.Var(&classNames, "classname", + fs.StringSliceVar(&classNames, "classname", classNames, "Only listen to Services with the given className. Can be set multiple times. "+ - "If no classname is set it will defaults to "+helper.DefaultLoadbalancerClass+" "+ + "If no classname is set it will default to "+helper.DefaultLoadbalancerClass+" "+ "and services without class. See also --empty-classname.") - flag.BoolVar(&emptyClassName, "empty-classname", true, + fs.BoolVar(&emptyClassName, "empty-classname", true, "Listen to services without a loadBalancerClass. Default is true.") - flag.IntVar(&leasesDurationInt, "leases-duration", 60, + fs.IntVar(&leasesDurationInt, "leases-duration", 60, "Is the time in seconds a non-leader will wait until forcing to acquire leadership.") - flag.IntVar(&leasesRenewDeadlineInt, "leases-renew-deadline", 50, + fs.IntVar(&leasesRenewDeadlineInt, "leases-renew-deadline", 50, "Is the time in seconds how long the current controller will retry before giving up.") - flag.IntVar(&leasesRetryPeriodInt, "leases-retry-period", 10, + fs.IntVar(&leasesRetryPeriodInt, "leases-retry-period", 10, "Is the time in seconds how long the controller waits between lease actions.") - flag.StringVar(&leasesLeaderElectionResourceLock, "leases-leader-election-resource-lock", "leases", + fs.StringVar(&leasesLeaderElectionResourceLock, "leases-leader-election-resource-lock", "leases", "The resource type which is used for leader election (default 'leases', can be also: 'configmaps' or 'configmapsleases').") opts := zap.Options{ Development: true, TimeEncoder: zapcore.ISO8601TimeEncoder, } - opts.BindFlags(flag.CommandLine) - flag.Parse() + zapFlagSet := flag.NewFlagSet("zap", flag.ContinueOnError) + opts.BindFlags(zapFlagSet) + fs.AddGoFlagSet(zapFlagSet) + + if err := fs.Parse(os.Args[1:]); err != nil { + fmt.Println(err) + os.Exit(1) + } if len(classNames) == 0 { classNames = append(classNames, helper.DefaultLoadbalancerClass) @@ -381,12 +389,3 @@ func getInfrastructureDefaultsFromEnvOrDie() targetcontroller.InfrastructureDefa InternalLB: ptr.To(internalLb), } } - -func (i *loadbalancerClassNames) String() string { - return strings.Join(*i, ",") -} - -func (i *loadbalancerClassNames) Set(value string) error { - *i = append(*i, value) - return nil -} diff --git a/cmd/yawol-controller/main.go b/cmd/yawol-controller/main.go index d01cdf1a..1f009c80 100644 --- a/cmd/yawol-controller/main.go +++ b/cmd/yawol-controller/main.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "fmt" "os" "time" @@ -16,19 +17,20 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/manager" - yawolv1beta1 "github.com/stackitcloud/yawol/api/v1beta1" - helpermetrics "github.com/stackitcloud/yawol/internal/metrics" + "github.com/spf13/pflag" + "go.uber.org/zap/zapcore" + "golang.org/x/time/rate" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" - discovery "k8s.io/client-go/discovery" + "k8s.io/client-go/discovery" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/util/workqueue" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/metrics/server" - "go.uber.org/zap/zapcore" - "golang.org/x/time/rate" - "k8s.io/client-go/util/workqueue" - "sigs.k8s.io/controller-runtime/pkg/log/zap" + yawolv1beta1 "github.com/stackitcloud/yawol/api/v1beta1" + helpermetrics "github.com/stackitcloud/yawol/internal/metrics" //+kubebuilder:scaffold:imports ) @@ -79,52 +81,60 @@ func main() { var leasesRenewDeadline time.Duration var leasesRetryPeriod time.Duration - flag.StringVar(&metricsAddrLb, "metrics-addr-lb", ":8080", "The address the metric endpoint binds to.") - flag.StringVar(&metricsAddrLbs, "metrics-addr-lbm", ":8081", "The address the metric endpoint binds to.") - flag.StringVar(&metricsAddrLbm, "metrics-addr-lbs", ":8082", "The address the metric endpoint binds to.") + fs := pflag.NewFlagSet("yawol-controller", pflag.ExitOnError) - flag.StringVar(&probeAddr, "health-probe-bind-address", ":8083", "The address the probe endpoint binds to.") - flag.BoolVar(&enableLeaderElection, "leader-elect", false, + fs.StringVar(&metricsAddrLb, "metrics-addr-lb", ":8080", "The address the metric endpoint binds to.") + fs.StringVar(&metricsAddrLbs, "metrics-addr-lbm", ":8081", "The address the metric endpoint binds to.") + fs.StringVar(&metricsAddrLbm, "metrics-addr-lbs", ":8082", "The address the metric endpoint binds to.") + + fs.StringVar(&probeAddr, "health-probe-bind-address", ":8083", "The address the probe endpoint binds to.") + fs.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - flag.IntVar(&concurrentWorkersPerReconciler, "concurrent-workers", 30, "Defines the amount of concurrent workers per reconciler.") - flag.DurationVar(&errorBackoffBaseDelay, "error-backoff-base-delay", 5*time.Millisecond, + fs.IntVar(&concurrentWorkersPerReconciler, "concurrent-workers", 30, "Defines the amount of concurrent workers per reconciler.") + fs.DurationVar(&errorBackoffBaseDelay, "error-backoff-base-delay", 5*time.Millisecond, "Defines the base delay of reconciles in case of an error.") - flag.DurationVar(&errorBackoffMaxDelay, "error-backoff-max-delay", 1000*time.Second, + fs.DurationVar(&errorBackoffMaxDelay, "error-backoff-max-delay", 1000*time.Second, "Defines the max delay of reconciles in case of an error.") - flag.BoolVar(&lbController, "enable-loadbalancer-controller", false, + fs.BoolVar(&lbController, "enable-loadbalancer-controller", false, "Enable loadbalancer controller manager. ") - flag.BoolVar(&lbSetController, "enable-loadbalancerset-controller", false, + fs.BoolVar(&lbSetController, "enable-loadbalancerset-controller", false, "Enable loadbalancer-set controller manager. ") - flag.BoolVar(&lbMachineController, "enable-loadbalancermachine-controller", false, + fs.BoolVar(&lbMachineController, "enable-loadbalancermachine-controller", false, "Enable loadbalancer-machine controller manager. ") - flag.IntVar(&yawolletRequeueTime, "yawollet-requeue-time", 0, + fs.IntVar(&yawolletRequeueTime, "yawollet-requeue-time", 0, "yawollet requeue time in seconds for reconcile if object was successful reconciled. "+ "Values less than 5 are set to 5 and greater than 170 are set to 170. "+ "If unset the default from yawollet is used.") - flag.DurationVar(&lbmDeletionGracePeriod, "lbm-deletion-grace-period", 2*time.Minute, + fs.DurationVar(&lbmDeletionGracePeriod, "lbm-deletion-grace-period", 2*time.Minute, "Grace period before deleting a load balancer machine AFTER the machine has first been identified as unready.", ) - flag.DurationVar(&openstackTimeout, "openstack-timeout", 20*time.Second, "Timeout for all requests against Openstack.") + fs.DurationVar(&openstackTimeout, "openstack-timeout", 20*time.Second, "Timeout for all requests against Openstack.") - flag.IntVar(&leasesDurationInt, "leases-duration", 60, + fs.IntVar(&leasesDurationInt, "leases-duration", 60, "Is the time in seconds a non-leader will wait until forcing to acquire leadership.") - flag.IntVar(&leasesRenewDeadlineInt, "leases-renew-deadline", 50, + fs.IntVar(&leasesRenewDeadlineInt, "leases-renew-deadline", 50, "Is the time in seconds how long the current controller will retry before giving up.") - flag.IntVar(&leasesRetryPeriodInt, "leases-retry-period", 10, + fs.IntVar(&leasesRetryPeriodInt, "leases-retry-period", 10, "Is the time in seconds how long the controller waits between lease actions.") - flag.StringVar(&leasesLeaderElectionResourceLock, "leases-leader-election-resource-lock", "leases", + fs.StringVar(&leasesLeaderElectionResourceLock, "leases-leader-election-resource-lock", "leases", "The resource type which is used for leader election (default 'leases', can be also: 'configmaps' or 'configmapsleases').") opts := zap.Options{ Development: true, TimeEncoder: zapcore.ISO8601TimeEncoder, } - opts.BindFlags(flag.CommandLine) - flag.Parse() + zapFlagSet := flag.NewFlagSet("zap", flag.ContinueOnError) + opts.BindFlags(zapFlagSet) + fs.AddGoFlagSet(zapFlagSet) + + if err := fs.Parse(os.Args[1:]); err != nil { + fmt.Println(err) + os.Exit(1) + } leasesDuration = time.Duration(leasesDurationInt) * time.Second leasesRenewDeadline = time.Duration(leasesRenewDeadlineInt) * time.Second diff --git a/cmd/yawollet/main.go b/cmd/yawollet/main.go index e1943cf5..98044121 100644 --- a/cmd/yawollet/main.go +++ b/cmd/yawollet/main.go @@ -10,6 +10,7 @@ import ( "time" "github.com/envoyproxy/go-control-plane/pkg/resource/v3" + "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/fields" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" @@ -68,18 +69,20 @@ func main() { var requeueTime int var keepalivedStatsFile string - flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. Default is disabled.") - flag.StringVar(&probeAddr, "health-probe-bind-address", "127.0.0.1:8080", "The address the probe endpoint binds to.") + fs := pflag.NewFlagSet("yawollet", pflag.ExitOnError) - flag.StringVar(&namespace, "namespace", "", "The namespace from lb und lbm object.") - flag.StringVar(&loadbalancerName, "loadbalancer-name", "", "Name of lb object.") - flag.StringVar(&loadbalancerMachineName, "loadbalancer-machine-name", "", "Name of lbm object.") - flag.StringVar(&listenAddress, "listen-address", "", "Address that envoy should listen.") - flag.StringVar(&listenInterface, "listen-interface", "", "Interface that envoy should listen on. Ignored if listen-address is set.") - flag.IntVar(&requeueTime, "requeue-time", 30, "Requeue Time in seconds for reconcile if object was successful reconciled. "+ + fs.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. Default is disabled.") + fs.StringVar(&probeAddr, "health-probe-bind-address", "127.0.0.1:8080", "The address the probe endpoint binds to.") + + fs.StringVar(&namespace, "namespace", "", "The namespace from lb und lbm object.") + fs.StringVar(&loadbalancerName, "loadbalancer-name", "", "Name of lb object.") + fs.StringVar(&loadbalancerMachineName, "loadbalancer-machine-name", "", "Name of lbm object.") + fs.StringVar(&listenAddress, "listen-address", "", "Address that envoy should listen.") + fs.StringVar(&listenInterface, "listen-interface", "", "Interface that envoy should listen on. Ignored if listen-address is set.") + fs.IntVar(&requeueTime, "requeue-time", 30, "Requeue Time in seconds for reconcile if object was successful reconciled. "+ "Values less than 5 are set to 5 and greater than 170 are set to 170") - flag.StringVar(&keepalivedStatsFile, "keepalived-stats-file", "/tmp/keepalived.stats", + fs.StringVar(&keepalivedStatsFile, "keepalived-stats-file", "/tmp/keepalived.stats", "Stats file for keepalived (default: /tmp/keepalived.stats). "+ "If set to empty no keepalived stats will be used for conditions and metrics.") @@ -87,9 +90,14 @@ func main() { Development: true, TimeEncoder: zapcore.ISO8601TimeEncoder, } - opts.BindFlags(flag.CommandLine) - flag.Parse() + zapFlagSet := flag.NewFlagSet("zap", flag.ContinueOnError) + opts.BindFlags(zapFlagSet) + fs.AddGoFlagSet(zapFlagSet) + if err := fs.Parse(os.Args[1:]); err != nil { + fmt.Println(err) + os.Exit(1) + } ctx := context.Background() ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))