Create resolveSettings

This commit is contained in:
John Gardiner Myers 2019-11-12 22:25:45 -08:00
parent d6ceffed36
commit adaf903b90
3 changed files with 230 additions and 1 deletions

View File

@ -6,6 +6,7 @@ go_library(
"delete.go",
"instancegroups.go",
"rollingupdate.go",
"settings.go",
],
importpath = "k8s.io/kops/pkg/instancegroups",
visibility = ["//visibility:public"],
@ -20,6 +21,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
@ -29,7 +31,10 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["rollingupdate_test.go"],
srcs = [
"rollingupdate_test.go",
"settings_test.go",
],
embed = [":go_default_library"],
deps = [
"//cloudmock/aws/mockautoscaling:go_default_library",
@ -42,6 +47,7 @@ go_test(
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
"//vendor/k8s.io/client-go/testing:go_default_library",

View File

@ -0,0 +1,56 @@
/*
Copyright 2019 The Kubernetes Authors.
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 instancegroups
import (
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kops/pkg/apis/kops"
)
func resolveSettings(cluster *kops.Cluster, group *kops.InstanceGroup, numInstances int) kops.RollingUpdate {
rollingUpdate := kops.RollingUpdate{}
if group.Spec.RollingUpdate != nil {
rollingUpdate = *group.Spec.RollingUpdate
}
if def := cluster.Spec.RollingUpdate; def != nil {
if rollingUpdate.MaxUnavailable == nil {
rollingUpdate.MaxUnavailable = def.MaxUnavailable
}
}
if rollingUpdate.MaxUnavailable == nil || rollingUpdate.MaxUnavailable.IntVal < 0 {
one := intstr.FromInt(1)
rollingUpdate.MaxUnavailable = &one
}
if rollingUpdate.MaxUnavailable.Type == intstr.String {
unavailable, err := intstr.GetValueFromIntOrPercent(rollingUpdate.MaxUnavailable, numInstances, false)
if err != nil {
// If unparseable use the default value
unavailable = 1
}
if unavailable <= 0 {
// While we round down, percentages should resolve to a minimum of 1
unavailable = 1
}
unavailableInt := intstr.FromInt(unavailable)
rollingUpdate.MaxUnavailable = &unavailableInt
}
return rollingUpdate
}

View File

@ -0,0 +1,167 @@
/*
Copyright 2019 The Kubernetes Authors.
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 instancegroups
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kops/pkg/apis/kops"
)
func TestSettings(t *testing.T) {
for _, tc := range []struct {
name string
defaultValue interface{}
nonDefaultValue interface{}
}{
{
name: "MaxUnavailable",
defaultValue: intstr.FromInt(1),
nonDefaultValue: intstr.FromInt(2),
},
} {
t.Run(tc.name, func(t *testing.T) {
defaultCluster := &kops.RollingUpdate{}
setFieldValue(defaultCluster, tc.name, tc.defaultValue)
nonDefaultCluster := &kops.RollingUpdate{}
setFieldValue(nonDefaultCluster, tc.name, tc.nonDefaultValue)
defaultGroup := &kops.RollingUpdate{}
setFieldValue(defaultGroup, tc.name, tc.defaultValue)
nonDefaultGroup := &kops.RollingUpdate{}
setFieldValue(nonDefaultGroup, tc.name, tc.nonDefaultValue)
assertResolvesValue(t, tc.name, tc.defaultValue, nil, nil, "nil nil")
assertResolvesValue(t, tc.name, tc.defaultValue, &kops.RollingUpdate{}, nil, "{nil} nil")
assertResolvesValue(t, tc.name, tc.defaultValue, defaultCluster, nil, "{default} nil")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, nonDefaultCluster, nil, "{nonDefault} nil")
assertResolvesValue(t, tc.name, tc.defaultValue, nil, &kops.RollingUpdate{}, "nil {nil}")
assertResolvesValue(t, tc.name, tc.defaultValue, &kops.RollingUpdate{}, &kops.RollingUpdate{}, "{nil} {nil}")
assertResolvesValue(t, tc.name, tc.defaultValue, defaultCluster, &kops.RollingUpdate{}, "{default} {nil}")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, nonDefaultCluster, &kops.RollingUpdate{}, "{nonDefault} {nil}")
assertResolvesValue(t, tc.name, tc.defaultValue, nil, defaultGroup, "nil {default}")
assertResolvesValue(t, tc.name, tc.defaultValue, &kops.RollingUpdate{}, defaultGroup, "{nil} {default}")
assertResolvesValue(t, tc.name, tc.defaultValue, defaultCluster, defaultGroup, "{default} {default}")
assertResolvesValue(t, tc.name, tc.defaultValue, nonDefaultCluster, defaultGroup, "{nonDefault} {default}")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, nil, nonDefaultGroup, "nil {nonDefault}")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, &kops.RollingUpdate{}, nonDefaultGroup, "{nil} {nonDefault}")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, defaultCluster, nonDefaultGroup, "{default} {nonDefault}")
assertResolvesValue(t, tc.name, tc.nonDefaultValue, nonDefaultCluster, nonDefaultGroup, "{nonDefault} {nonDefault}")
})
}
}
func setFieldValue(aStruct interface{}, fieldName string, fieldValue interface{}) {
field := reflect.ValueOf(aStruct).Elem().FieldByName(fieldName)
value := reflect.New(field.Type().Elem())
value.Elem().Set(reflect.ValueOf(fieldValue))
field.Set(value)
}
func assertResolvesValue(t *testing.T, name string, expected interface{}, rollingUpdateDefault *kops.RollingUpdate, rollingUpdate *kops.RollingUpdate, msg interface{}) bool {
cluster := kops.Cluster{
Spec: kops.ClusterSpec{
RollingUpdate: rollingUpdateDefault,
},
}
instanceGroup := kops.InstanceGroup{
Spec: kops.InstanceGroupSpec{
RollingUpdate: rollingUpdate,
},
}
rollingUpdateDefaultCopy := rollingUpdateDefault.DeepCopy()
rollingUpdateCopy := rollingUpdate.DeepCopy()
resolved := resolveSettings(&cluster, &instanceGroup, 1)
value := reflect.ValueOf(resolved).FieldByName(name)
assert.Equal(t, rollingUpdateDefault, cluster.Spec.RollingUpdate, "cluster not modified")
assert.True(t, reflect.DeepEqual(rollingUpdateDefault, rollingUpdateDefaultCopy), "RollingUpdate not modified")
assert.Equal(t, rollingUpdate, instanceGroup.Spec.RollingUpdate, "instancegroup not modified")
assert.True(t, reflect.DeepEqual(rollingUpdate, rollingUpdateCopy), "RollingUpdate not modified")
return assert.NotNil(t, value.Interface(), msg) &&
assert.Equal(t, expected, value.Elem().Interface(), msg)
}
func TestMaxUnavailable(t *testing.T) {
for _, tc := range []struct {
numInstances int
value string
expected int32
}{
{
numInstances: 1,
value: "0",
expected: 0,
},
{
numInstances: 1,
value: "0%",
expected: 1,
},
{
numInstances: 10,
value: "39%",
expected: 3,
},
{
numInstances: 10,
value: "100%",
expected: 10,
},
{
numInstances: 5,
value: "fnord",
expected: 1,
},
{
numInstances: 5,
value: "-3",
expected: 1,
},
{
numInstances: 5,
value: "-3%",
expected: 1,
},
} {
t.Run(fmt.Sprintf("%s %d", tc.value, tc.numInstances), func(t *testing.T) {
value := intstr.Parse(tc.value)
rollingUpdate := kops.RollingUpdate{
MaxUnavailable: &value,
}
instanceGroup := kops.InstanceGroup{
Spec: kops.InstanceGroupSpec{
RollingUpdate: &rollingUpdate,
},
}
resolved := resolveSettings(&kops.Cluster{}, &instanceGroup, tc.numInstances)
assert.Equal(t, intstr.Int, resolved.MaxUnavailable.Type)
assert.Equal(t, tc.expected, resolved.MaxUnavailable.IntVal)
})
}
}