forked from SeldonIO/seldon-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix server version check for operator (SeldonIO#3570)
* fix server version check for operator * Add missing utils files * default to 1.18 if minor version unknown
- Loading branch information
1 parent
524fa7b
commit 05853bd
Showing
4 changed files
with
87 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package k8s | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-logr/logr" | ||
"k8s.io/client-go/discovery" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// This will cause CRD V1 to be installed | ||
const DefaultMinorVersion = 18 | ||
|
||
func GetServerVersion(client discovery.DiscoveryInterface, logger logr.Logger) (string, error) { | ||
serverVersion, err := client.ServerVersion() | ||
if err != nil { | ||
return "", err | ||
} | ||
logger.Info("Server version", "Major", serverVersion.Major, "Minor", serverVersion.Minor) | ||
majorVersion, err := strconv.Atoi(serverVersion.Major) | ||
if err != nil { | ||
logger.Error(err, "Failed to parse majorVersion defaulting to 1") | ||
majorVersion = 1 | ||
} | ||
minorVersion, err := strconv.Atoi(serverVersion.Minor) | ||
if err != nil { | ||
if strings.HasSuffix(serverVersion.Minor, "+") { | ||
minorVersion, err = strconv.Atoi(serverVersion.Minor[0 : len(serverVersion.Minor)-1]) | ||
if err != nil { | ||
logger.Error(err, "Failed to parse minorVersion defaulting to 12") | ||
minorVersion = DefaultMinorVersion | ||
} | ||
} else { | ||
logger.Error(err, "Failed to parse minorVersion defaulting to 12") | ||
minorVersion = DefaultMinorVersion | ||
} | ||
} | ||
return fmt.Sprintf("%d.%d", majorVersion, minorVersion), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package k8s | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/version" | ||
discovery "k8s.io/client-go/discovery/fake" | ||
coretesting "k8s.io/client-go/testing" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"testing" | ||
) | ||
|
||
func TestServerVersion(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
scenarios := map[string]struct { | ||
major string | ||
minor string | ||
expected string | ||
}{ | ||
"1.18 test": {major: "1", minor: "18", expected: "1.18"}, | ||
"GKE 1.20": {major: "1", minor: "20+", expected: "1.20"}, | ||
"Unknown minor version": {major: "1", minor: "XYZ", expected: "1.18"}, | ||
} | ||
|
||
for _, scenario := range scenarios { | ||
discoveryFake := &discovery.FakeDiscovery{Fake: &coretesting.Fake{}, | ||
FakedServerVersion: &version.Info{ | ||
Major: scenario.major, | ||
Minor: scenario.minor, | ||
}} | ||
serverVersion, err := GetServerVersion(discoveryFake, ctrl.Log) | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(serverVersion).To(Equal(scenario.expected)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters