Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server version parsing #158

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (

require (
github.com/adrg/xdg v0.4.0
github.com/blang/semver/v4 v4.0.0
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
27 changes: 22 additions & 5 deletions pkg/common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package common

import (
"fmt"
"os"
"strings"
"time"

"github.com/blang/semver/v4"
"github.com/spf13/viper"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"os"

"sigs.k8s.io/hydrophone/pkg/log"
"strings"
"time"
)

// PrintInfo prints the information about the cluster
Expand All @@ -35,10 +38,14 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {
time.Sleep(2 * time.Second)
serverVersion, err := clientSet.ServerVersion()
if err != nil {
log.Fatal("Error fetching server version: ", err)
log.Fatalf("Error fetching server version: %v", err)
}
trimmedVersion, err := trimVersion(serverVersion.String())
if err != nil {
log.Fatalf("Error trimming server version: %v", err)
}
if viper.Get("conformance-image") == "" {
viper.Set("conformance-image", fmt.Sprintf("registry.k8s.io/conformance:%s", serverVersion.String()))
viper.Set("conformance-image", fmt.Sprintf("registry.k8s.io/conformance:%s", trimmedVersion))
}
if viper.Get("busybox-image") == "" {
viper.Set("busybox-image", busyboxImage)
Expand Down Expand Up @@ -96,3 +103,13 @@ func ValidateArgs() error {
}
return nil
}

func trimVersion(version string) (string, error) {
version = strings.TrimPrefix(version, "v")

parsedVersion, err := semver.Parse(version)
if err != nil {
return "", err
}
return parsedVersion.FinalizeVersion(), nil
}
28 changes: 28 additions & 0 deletions pkg/common/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,31 @@ func TestValidateArgs(t *testing.T) {
})
}
}

func TestTrimVersion(t *testing.T) {

testCases := []struct {
name string
version string
expectedVersion string
}{
{
name: "stable version",
version: "v1.28.6",
expectedVersion: "1.28.6",
},
{
name: "pre released version",
version: "v1.28.6+0fb426",
expectedVersion: "1.28.6",
},
}

// Run the test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
trimmedVersion, _ := trimVersion(tc.version)
assert.Equal(t, tc.expectedVersion, trimmedVersion)
})
}
}