api: dropDisabledFields

This commit is contained in:
Khaled Henidak(Kal)
2019-06-21 17:42:53 +00:00
parent 54d42e6a65
commit 81468e2696
4 changed files with 259 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ func (nodeStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
node.Spec.ConfigSource = nil
node.Status.Config = nil
}
dropDisabledFields(node, nil)
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
@@ -82,6 +83,27 @@ func (nodeStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Objec
if !utilfeature.DefaultFeatureGate.Enabled(features.DynamicKubeletConfig) && !nodeConfigSourceInUse(oldNode) {
newNode.Spec.ConfigSource = nil
}
dropDisabledFields(newNode, oldNode)
}
func dropDisabledFields(node *api.Node, oldNode *api.Node) {
if !utilfeature.DefaultFeatureGate.Enabled(features.IPv6DualStack) && !multiNodeCIDRsInUse(oldNode) {
if len(node.Spec.PodCIDRs) > 1 {
node.Spec.PodCIDRs = node.Spec.PodCIDRs[0:1]
}
}
}
// multiNodeCIDRsInUse returns true if Node.Spec.PodCIDRs is greater than one
func multiNodeCIDRsInUse(node *api.Node) bool {
if node == nil {
return false
}
if len(node.Spec.PodCIDRs) > 1 {
return true
}
return false
}
// nodeConfigSourceInUse returns true if node's Spec ConfigSource is set(used)

View File

@@ -17,13 +17,19 @@ limitations under the License.
package node
import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/diff"
apitesting "k8s.io/kubernetes/pkg/api/testing"
api "k8s.io/kubernetes/pkg/apis/core"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features"
// install all api groups for testing
_ "k8s.io/kubernetes/pkg/api/testapi"
)
@@ -57,3 +63,106 @@ func TestSelectableFieldLabelConversions(t *testing.T) {
nil,
)
}
// helper creates a NodeNode with a set of PodCIDRs
func makeNodeWithCIDRs(podCIDRs []string) *api.Node {
return &api.Node{
Spec: api.NodeSpec{
PodCIDRs: podCIDRs,
},
}
}
func TestDropPodCIDRs(t *testing.T) {
testCases := []struct {
name string
node *api.Node
oldNode *api.Node
compareNode *api.Node
enableDualStack bool
}{
{
name: "nil pod cidrs",
enableDualStack: false,
node: makeNodeWithCIDRs(nil),
oldNode: nil,
compareNode: makeNodeWithCIDRs(nil),
},
{
name: "empty pod ips",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{}),
oldNode: nil,
compareNode: makeNodeWithCIDRs([]string{}),
},
{
name: "single family ipv6",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"2000::/10"}),
compareNode: makeNodeWithCIDRs([]string{"2000::/10"}),
},
{
name: "single family ipv4",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"10.0.0.0/8"}),
compareNode: makeNodeWithCIDRs([]string{"10.0.0.0/8"}),
},
{
name: "dualstack 4-6",
enableDualStack: true,
node: makeNodeWithCIDRs([]string{"10.0.0.0/8", "2000::/10"}),
compareNode: makeNodeWithCIDRs([]string{"10.0.0.0/8", "2000::/10"}),
},
{
name: "dualstack 6-4",
enableDualStack: true,
node: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
compareNode: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
},
{
name: "not dualstack 6-4=>4only",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
oldNode: nil,
compareNode: makeNodeWithCIDRs([]string{"2000::/10"}),
},
{
name: "not dualstack 6-4=>as is (used in old)",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
oldNode: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
compareNode: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
},
{
name: "not dualstack 6-4=>6only",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
oldNode: nil,
compareNode: makeNodeWithCIDRs([]string{"2000::/10"}),
},
{
name: "not dualstack 6-4=>as is (used in old)",
enableDualStack: false,
node: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
oldNode: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
compareNode: makeNodeWithCIDRs([]string{"2000::/10", "10.0.0.0/8"}),
},
}
for _, tc := range testCases {
func() {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.IPv6DualStack, tc.enableDualStack)()
dropDisabledFields(tc.node, tc.oldNode)
old := tc.oldNode.DeepCopy()
// old node should never be changed
if !reflect.DeepEqual(tc.oldNode, old) {
t.Errorf("%v: old node changed: %v", tc.name, diff.ObjectReflectDiff(tc.oldNode, old))
}
if !reflect.DeepEqual(tc.node, tc.compareNode) {
t.Errorf("%v: unexpected node spec: %v", tc.name, diff.ObjectReflectDiff(tc.node, tc.compareNode))
}
}()
}
}