From 66f1f283e226e04317cc87dbb18cb59a655697f4 Mon Sep 17 00:00:00 2001 From: f41gh7 Date: Mon, 14 Feb 2022 21:35:54 +0300 Subject: [PATCH] Adds mitigation to the kubernetes server parsing clean up + symbol use default values in case of failure https://github.com/VictoriaMetrics/operator/issues/428 --- controllers/factory/k8stools/version.go | 22 ++++++++++++++++------ internal/manager/manager.go | 21 ++++++++++++--------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/controllers/factory/k8stools/version.go b/controllers/factory/k8stools/version.go index bec91249..6274b60a 100644 --- a/controllers/factory/k8stools/version.go +++ b/controllers/factory/k8stools/version.go @@ -4,6 +4,7 @@ import ( "fmt" "k8s.io/apimachinery/pkg/version" "strconv" + "strings" ) var ( @@ -11,18 +12,27 @@ var ( ServerMinorVersion uint64 ) -// TrySetKubernetesServerVersion parses kubernetes -func TrySetKubernetesServerVersion(vi *version.Info) error { - v, err := strconv.ParseUint(vi.Minor, 10, 64) +// SetKubernetesVersionWithDefaults parses kubernetes version response with given default versions +func SetKubernetesVersionWithDefaults(vi *version.Info, defaultMinor, defaultMajor uint64) error { + + var warnMessage string + minor := strings.Trim(vi.Minor, "+") + v, err := strconv.ParseUint(minor, 10, 64) if err != nil { - return fmt.Errorf("cannot parse kubernetes server minor version: %q as uint: %w", vi.Minor, err) + v = defaultMinor + warnMessage = fmt.Sprintf("cannot parse minor kubernetes version response: %s, err: %s, using default: %d\n", vi.Minor, err, defaultMinor) } ServerMinorVersion = v - v, err = strconv.ParseUint(vi.Major, 10, 64) + major := strings.Trim(vi.Major, "+") + v, err = strconv.ParseUint(major, 10, 64) if err != nil { - return fmt.Errorf("cannot parse kubernetes server major version: %q as uint: %w", vi.Major, err) + v = defaultMajor + warnMessage += fmt.Sprintf("cannot parse major kubernetes version response: %s, err: %s, using default: %d\n", vi.Major, err, defaultMajor) } ServerMajorVersion = v + if len(warnMessage) > 0 { + return fmt.Errorf(warnMessage) + } return nil } diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 9c184d01..d4bafa4f 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -36,13 +36,15 @@ var ( setupLog = ctrl.Log.WithName("setup") enableLeaderElection = flag.Bool("enable-leader-election", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - enableWebhooks = flag.Bool("webhook.enable", false, "adds webhook server, you must mount cert and key or use cert-manager") - disableCRDOwnership = flag.Bool("controller.disableCRDOwnership", false, "disables CRD ownership add to cluster wide objects, must be disabled for clusters, lower than v1.16.0") - webhooksDir = flag.String("webhook.certDir", "/tmp/k8s-webhook-server/serving-certs/", "root directory for webhook cert and key") - webhookCertName = flag.String("webhook.certName", "tls.crt", "name of webhook server Tls certificate inside tls.certDir") - webhookKeyName = flag.String("webhook.keyName", "tls.key", "name of webhook server Tls key inside tls.certDir") - metricsAddr = flag.String("metrics-addr", ":8080", "The address the metric endpoint binds to.") - listenAddr = flag.String("http.listenAddr", ":8435", "http server listen addr - serves victoria-metrics http server + metrics.") + enableWebhooks = flag.Bool("webhook.enable", false, "adds webhook server, you must mount cert and key or use cert-manager") + disableCRDOwnership = flag.Bool("controller.disableCRDOwnership", false, "disables CRD ownership add to cluster wide objects, must be disabled for clusters, lower than v1.16.0") + webhooksDir = flag.String("webhook.certDir", "/tmp/k8s-webhook-server/serving-certs/", "root directory for webhook cert and key") + webhookCertName = flag.String("webhook.certName", "tls.crt", "name of webhook server Tls certificate inside tls.certDir") + webhookKeyName = flag.String("webhook.keyName", "tls.key", "name of webhook server Tls key inside tls.certDir") + metricsAddr = flag.String("metrics-addr", ":8080", "The address the metric endpoint binds to.") + listenAddr = flag.String("http.listenAddr", ":8435", "http server listen addr - serves victoria-metrics http server + metrics.") + defaultKubernetesMinorVersion = flag.Uint64("default.kubernetesVersion.minor", 21, "Minor version of kubernetes server, if operator cannot parse actual kubernetes response") + defaultKubernetesMajorVersion = flag.Uint64("default.kubernetesVersion.major", 1, "Major version of kubernetes server, if operator cannot parse actual kubernetes response") ) func init() { @@ -265,8 +267,9 @@ func RunManager(ctx context.Context) error { if err != nil { return fmt.Errorf("cannot get kubernetes server version: %w", err) } - if err := k8stools.TrySetKubernetesServerVersion(k8sServerVersion); err != nil { - return fmt.Errorf("cannot set kubernetes server version: %w", err) + if err := k8stools.SetKubernetesVersionWithDefaults(k8sServerVersion, *defaultKubernetesMinorVersion, *defaultKubernetesMajorVersion); err != nil { + // log error and do nothing, because we are using sane default values. + setupLog.Error(err, "cannot parse kubernetes version, using default flag values") } setupLog.Info("using kubernetes server version", "version", k8sServerVersion.String())