Skip to content

Commit

Permalink
Provide detailed error for removed apiVersions
Browse files Browse the repository at this point in the history
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
lblackstone committed Sep 17, 2019
1 parent 0d180f1 commit d47489e
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 1 deletion.
140 changes: 140 additions & 0 deletions pkg/cluster/version.go
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
}
165 changes: 165 additions & 0 deletions pkg/cluster/version_test.go
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)
}
}
}
33 changes: 33 additions & 0 deletions pkg/kinds/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package kinds

import (
"fmt"

"github.com/pulumi/pulumi-kubernetes/pkg/cluster"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -27,6 +30,23 @@ func DeprecatedApiVersion(gvk schema.GroupVersionKind) bool {
return SuggestedApiVersion(gvk) != gvkStr(gvk)
}

// RemovedApiVersion returns true if the given GVK has been removed in the given k8s version, and the corresponding
// ServerVersion where the GVK was removed.
func RemovedApiVersion(gvk schema.GroupVersionKind, version cluster.ServerVersion) (bool, *cluster.ServerVersion) {
var removedIn cluster.ServerVersion

switch gvk.GroupVersion() {
case schema.GroupVersion{Group: "extensions", Version: "v1beta1"},
schema.GroupVersion{Group: "apps", Version: "v1beta1"},
schema.GroupVersion{Group: "apps", Version: "v1beta2"}:
removedIn = cluster.ServerVersion{Major: 1, Minor: 16}
default:
return false, nil
}

return version.Compare(removedIn) >= 0, &removedIn
}

// SuggestedApiVersion returns a string with the suggested apiVersion for a given GVK.
// This is used to provide useful warning messages when a user creates a resource using a deprecated GVK.
func SuggestedApiVersion(gvk schema.GroupVersionKind) string {
Expand All @@ -48,5 +68,18 @@ func SuggestedApiVersion(gvk schema.GroupVersionKind) string {
default:
return gvkStr(gvk)
}
}

// RemovedApiError is returned if the provided GVK does not exist in the targeted k8s cluster because the apiVersion
// has been deprecated and removed.
type RemovedApiError struct {
GVK schema.GroupVersionKind
Version *cluster.ServerVersion
}

func (e *RemovedApiError) Error() string {
if e.Version == nil {
return fmt.Sprintf("apiVersion %q was removed in a previous version of Kubernetes", gvkStr(e.GVK))
}
return fmt.Sprintf("apiVersion %q was removed in Kubernetes %s", gvkStr(e.GVK), e.Version)
}
Loading

0 comments on commit d47489e

Please sign in to comment.