Updating EndpointSlice controllers to support NodeName field

This commit is contained in:
Rob Scott
2020-11-10 17:50:39 -08:00
parent e9573eef4c
commit d985438772
6 changed files with 120 additions and 1 deletions

View File

@@ -27,8 +27,12 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/rand"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/features"
utilpointer "k8s.io/utils/pointer"
)
func TestNewEndpointSlice(t *testing.T) {
@@ -76,6 +80,86 @@ func TestNewEndpointSlice(t *testing.T) {
}
}
func TestAddressToEndpoint(t *testing.T) {
testCases := []struct {
name string
epAddress v1.EndpointAddress
expectedEndpoint discovery.Endpoint
ready bool
nodeNameGateEnabled bool
}{{
name: "simple + gate enabled",
epAddress: v1.EndpointAddress{
IP: "10.1.2.3",
Hostname: "foo",
NodeName: utilpointer.StringPtr("node-abc"),
TargetRef: &v1.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
Namespace: "default",
Name: "foo",
},
},
ready: true,
nodeNameGateEnabled: true,
expectedEndpoint: discovery.Endpoint{
Addresses: []string{"10.1.2.3"},
Hostname: utilpointer.StringPtr("foo"),
Conditions: discovery.EndpointConditions{
Ready: utilpointer.BoolPtr(true),
},
Topology: map[string]string{
"kubernetes.io/hostname": "node-abc",
},
TargetRef: &v1.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
Namespace: "default",
Name: "foo",
},
NodeName: utilpointer.StringPtr("node-abc"),
},
}, {
name: "simple + gate disabled",
epAddress: v1.EndpointAddress{
IP: "10.1.2.3",
Hostname: "foo",
NodeName: utilpointer.StringPtr("node-abc"),
TargetRef: &v1.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
Namespace: "default",
Name: "foo",
},
},
ready: true,
nodeNameGateEnabled: false,
expectedEndpoint: discovery.Endpoint{
Addresses: []string{"10.1.2.3"},
Hostname: utilpointer.StringPtr("foo"),
Conditions: discovery.EndpointConditions{
Ready: utilpointer.BoolPtr(true),
},
Topology: map[string]string{
"kubernetes.io/hostname": "node-abc",
},
TargetRef: &v1.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
Namespace: "default",
Name: "foo",
},
},
}}
for _, tc := range testCases {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EndpointSliceNodeName, tc.nodeNameGateEnabled)()
ep := addressToEndpoint(tc.epAddress, tc.ready)
assert.EqualValues(t, tc.expectedEndpoint, *ep)
}
}
// Test helpers
func newClientset() *fake.Clientset {