Skip to content

Commit

Permalink
Switch to pflag (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Sep 9, 2024
1 parent 23bc53d commit d2ba3bd
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 90 deletions.
6 changes: 3 additions & 3 deletions charts/yawol-controller/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
4 changes: 2 additions & 2 deletions charts/yawol-controller/templates/yawol-cloud-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 15 additions & 15 deletions charts/yawol-controller/templates/yawol-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
63 changes: 31 additions & 32 deletions cmd/yawol-cloud-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
)

Expand All @@ -36,8 +38,6 @@ var (
setupLog = ctrl.Log.WithName("setup")
)

type loadbalancerClassNames []string

const (
// Namespace in for LoadBalancer CRs
EnvClusterNamespace = "CLUSTER_NAMESPACE"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
64 changes: 37 additions & 27 deletions cmd/yawol-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"time"

Expand All @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions cmd/yawollet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -68,28 +69,35 @@ 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.")

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)
}
ctx := context.Background()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
Expand Down

0 comments on commit d2ba3bd

Please sign in to comment.