From fe6b1c74489987040cdbd776d7f2b039fa625d11 Mon Sep 17 00:00:00 2001 From: Xav Paice Date: Tue, 6 Feb 2024 10:04:01 +1300 Subject: [PATCH] Add workaround for EKS version string (#1449) * Add workaround for EKS version string The EKS version string returned is not semver compliant. To work around this, we remove the suffix for version strings that contain -eks-. Fixes #1441 * Add parsing version test cases * Rename function --------- Co-authored-by: Evans Mungai --- pkg/analyze/cluster_version.go | 12 ++++++++- pkg/analyze/cluster_version_test.go | 39 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pkg/analyze/cluster_version.go b/pkg/analyze/cluster_version.go index de9790579..b40e89536 100644 --- a/pkg/analyze/cluster_version.go +++ b/pkg/analyze/cluster_version.go @@ -42,7 +42,7 @@ func analyzeClusterVersion(analyzer *troubleshootv1beta2.ClusterVersion, getColl return nil, errors.Wrap(err, "failed to parse cluster_version.json") } - k8sVersion, err := semver.Make(strings.TrimLeft(collectorClusterVersion.String, "v")) + k8sVersion, err := parseK8sVersionString(collectorClusterVersion.String) if err != nil { return nil, errors.Wrap(err, "failed to parse semver from cluster_version.json") } @@ -58,6 +58,16 @@ func title(checkName string) string { return checkName } +func parseK8sVersionString(version string) (semver.Version, error) { + // Workaround for https://github.com/aws/containers-roadmap/issues/1404 + // for EKS, replace pre-release gitVersion string with the release version + if strings.Contains(version, "-eks-") { + version = strings.Split(version, "-")[0] + } + + return semver.Make(strings.TrimLeft(version, "v")) +} + func analyzeClusterVersionResult(k8sVersion semver.Version, outcomes []*troubleshootv1beta2.Outcome, checkName string) (*AnalyzeResult, error) { for _, outcome := range outcomes { when := "" diff --git a/pkg/analyze/cluster_version_test.go b/pkg/analyze/cluster_version_test.go index 597da255b..0b26a9aea 100644 --- a/pkg/analyze/cluster_version_test.go +++ b/pkg/analyze/cluster_version_test.go @@ -6,6 +6,7 @@ import ( "github.com/blang/semver/v4" troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + "github.com/stretchr/testify/assert" ) func Test_analyzeClusterVersionResult(t *testing.T) { @@ -103,3 +104,41 @@ func Test_analyzeClusterVersionResult(t *testing.T) { }) } } + +func Test_parseVersionString(t *testing.T) { + tests := []struct { + name string + rawVersion string + want semver.Version + wantErr bool + }{ + { + name: "valid version", + rawVersion: "1.17.0", + want: semver.MustParse("1.17.0"), + }, + { + name: "valid version with v prefix", + rawVersion: "v1.17.0", + want: semver.MustParse("1.17.0"), + }, + { + name: "invalid version", + rawVersion: "v1.17", + want: semver.Version{}, + wantErr: true, + }, + { + name: "EKS version", + rawVersion: "v1.25.16-eks-8cb36c9", + want: semver.MustParse("1.25.16"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := parseK8sVersionString(tt.rawVersion) + assert.Equal(t, tt.wantErr, err != nil, "parseVersionString() error = %v, wantErr %v", err, tt.wantErr) + assert.Equal(t, tt.want, got, "parseVersionString() = %v, want %v", got, tt.want) + }) + } +}