-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide detailed error for removed apiVersions
Before creating any resource, check if the requested apiVersion has been deprecated/removed on the targeted version of k8s, and print a detailed error message if so. This should avoid confusion from users who try to use unsupported versions.
- Loading branch information
1 parent
0d180f1
commit d47489e
Showing
5 changed files
with
406 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2016-2019, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/version" | ||
"k8s.io/client-go/discovery" | ||
) | ||
|
||
// ServerVersion captures k8s major.minor version in a parsed form | ||
type ServerVersion struct { | ||
Major, Minor int | ||
} | ||
|
||
func (v ServerVersion) String() string { | ||
return fmt.Sprintf("%d.%d", v.Major, v.Minor) | ||
} | ||
|
||
// Compare returns -1/0/+1 iff v is less than / equal / greater than input version. | ||
func (v ServerVersion) Compare(version ServerVersion) int { | ||
a := v.Major | ||
b := version.Major | ||
|
||
if a == b { | ||
a = v.Minor | ||
b = version.Minor | ||
} | ||
|
||
var res int | ||
if a > b { | ||
res = 1 | ||
} else if a == b { | ||
res = 0 | ||
} else { | ||
res = -1 | ||
} | ||
return res | ||
} | ||
|
||
// GetServerVersion attempts to retrieve the server version from k8s. | ||
// Returns the configured default version in case this fails. | ||
func GetServerVersion(cdi discovery.CachedDiscoveryInterface) ServerVersion { | ||
defaultSV := ServerVersion{ | ||
Major: 1, | ||
Minor: 14, | ||
} | ||
|
||
if sv, err := cdi.ServerVersion(); err == nil { | ||
if v, err := parseVersion(sv); err == nil { | ||
return v | ||
} else { | ||
return defaultSV | ||
} | ||
} | ||
|
||
return defaultSV | ||
} | ||
|
||
// gitVersion captures k8s major.minor.patch version in a parsed form | ||
type gitVersion struct { | ||
Major, Minor, Patch int | ||
} | ||
|
||
func (gv gitVersion) String() string { | ||
return fmt.Sprintf("%d.%d.%d", gv.Major, gv.Minor, gv.Patch) | ||
} | ||
|
||
func parseGitVersion(versionString string) (gitVersion, error) { | ||
// Format v0.0.0(-master+$Format:%h$) | ||
gitVersionRe := regexp.MustCompile(`v([0-9]+).([0-9]+).([0-9]+).*`) | ||
|
||
parsedVersion := gitVersionRe.FindStringSubmatch(versionString) | ||
if len(parsedVersion) != 4 { | ||
err := fmt.Errorf("unable to parse git version %q", versionString) | ||
return gitVersion{}, err | ||
} | ||
|
||
var gv gitVersion | ||
var err error | ||
gv.Major, err = strconv.Atoi(parsedVersion[1]) | ||
if err != nil { | ||
return gitVersion{}, err | ||
} | ||
gv.Minor, err = strconv.Atoi(parsedVersion[2]) | ||
if err != nil { | ||
return gitVersion{}, err | ||
} | ||
gv.Patch, err = strconv.Atoi(parsedVersion[3]) | ||
if err != nil { | ||
return gitVersion{}, err | ||
} | ||
|
||
return gv, nil | ||
} | ||
|
||
// parseVersion parses version.Info into a serverVersion struct | ||
func parseVersion(v *version.Info) (ServerVersion, error) { | ||
fallbackToGitVersion := false | ||
|
||
major, err := strconv.Atoi(v.Major) | ||
if err != nil { | ||
fallbackToGitVersion = true | ||
} | ||
|
||
// trim "+" in minor version (happened on GKE) | ||
v.Minor = strings.TrimSuffix(v.Minor, "+") | ||
|
||
minor, err := strconv.Atoi(v.Minor) | ||
if err != nil { | ||
fallbackToGitVersion = true | ||
} | ||
|
||
if fallbackToGitVersion { | ||
gv, err := parseGitVersion(v.GitVersion) | ||
if err != nil { | ||
return ServerVersion{}, err | ||
} | ||
|
||
return ServerVersion{Major: gv.Major, Minor: gv.Minor}, nil | ||
} | ||
|
||
return ServerVersion{Major: major, Minor: minor}, 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,165 @@ | ||
// Copyright 2016-2019, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cluster | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/version" | ||
) | ||
|
||
func TestServerVersion_Compare(t *testing.T) { | ||
v := ServerVersion{Major: 2, Minor: 3} | ||
|
||
tests := []struct { | ||
name string | ||
input ServerVersion | ||
version ServerVersion | ||
want int | ||
}{ | ||
{"Older major", ServerVersion{1, 0}, v, 1}, | ||
{"Older minor", ServerVersion{2, 0}, v, 1}, | ||
{"Equal", ServerVersion{2, 3}, v, 0}, | ||
{"Newer minor", ServerVersion{2, 4}, v, -1}, | ||
{"Newer major", ServerVersion{3, 0}, v, -1}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := v.Compare(tt.input); got != tt.want { | ||
t.Errorf("Compare() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_parseGitVersion(t *testing.T) { | ||
type args struct { | ||
versionString string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want gitVersion | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid", | ||
args: args{"v1.13.1"}, | ||
want: gitVersion{1, 13, 1}, | ||
}, | ||
{ | ||
name: "Valid + suffix", | ||
args: args{"v1.8.8-test.0"}, | ||
want: gitVersion{1, 8, 8}, | ||
}, | ||
{ | ||
name: "Missing v", | ||
args: args{"1.13.0"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Missing patch", | ||
args: args{"1.13"}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := parseGitVersion(tt.args.versionString) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("parseGitVersion() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseGitVersion() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseVersion(t *testing.T) { | ||
tests := []struct { | ||
input version.Info | ||
expected ServerVersion | ||
error bool | ||
}{ | ||
{ | ||
input: version.Info{Major: "1", Minor: "6"}, | ||
expected: ServerVersion{Major: 1, Minor: 6}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "70"}, | ||
expected: ServerVersion{Major: 1, Minor: 70}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "6x"}, | ||
error: true, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "8+"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "", Minor: "", GitVersion: "v1.8.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "", GitVersion: "v1.8.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "", Minor: "8", GitVersion: "v1.8.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "", Minor: "", GitVersion: "v1.8.8-test.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "8", GitVersion: "v1.9.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 8}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "9", GitVersion: "v1.9.1"}, | ||
expected: ServerVersion{Major: 1, Minor: 9}, | ||
}, | ||
{ | ||
input: version.Info{Major: "1", Minor: "13", GitVersion: "v1.13.0"}, | ||
expected: ServerVersion{Major: 1, Minor: 13}, | ||
}, | ||
{ | ||
input: version.Info{Major: "", Minor: "", GitVersion: "v1.a"}, | ||
error: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
v, err := parseVersion(&test.input) | ||
if test.error { | ||
if err == nil { | ||
t.Errorf("test %s should have failed and did not", test.input) | ||
} | ||
continue | ||
} | ||
if err != nil { | ||
t.Errorf("test %v failed: %v", test.input, err) | ||
continue | ||
} | ||
if v != test.expected { | ||
t.Errorf("Expected %#v, got %#v", test.expected, v) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.