Skip to content

Commit

Permalink
Adds mitigation to the kubernetes server parsing
Browse files Browse the repository at this point in the history
clean up + symbol
use default values in case of failure

#428
  • Loading branch information
f41gh7 committed Feb 14, 2022
1 parent 42e3d4a commit 66f1f28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
22 changes: 16 additions & 6 deletions controllers/factory/k8stools/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ import (
"fmt"
"k8s.io/apimachinery/pkg/version"
"strconv"
"strings"
)

var (
ServerMajorVersion uint64
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
}

Expand Down
21 changes: 12 additions & 9 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 66f1f28

Please sign in to comment.