mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Svc REST: Allow multi-IP-family in tests
This commit is contained in:
parent
6cc9ef3874
commit
bdbf2c6ef4
@ -31,14 +31,14 @@ import (
|
|||||||
"k8s.io/apiserver/pkg/registry/generic"
|
"k8s.io/apiserver/pkg/registry/generic"
|
||||||
genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
|
genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
|
||||||
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
|
||||||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
|
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||||
|
svctest "k8s.io/kubernetes/pkg/api/service/testing"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
|
"k8s.io/kubernetes/pkg/features"
|
||||||
"k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
|
"k8s.io/kubernetes/pkg/registry/core/service/ipallocator"
|
||||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
|
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
|
||||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
|
||||||
"k8s.io/kubernetes/pkg/features"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeIPAllocator(cidr *net.IPNet) ipallocator.Interface {
|
func makeIPAllocator(cidr *net.IPNet) ipallocator.Interface {
|
||||||
@ -49,7 +49,7 @@ func makeIPAllocator(cidr *net.IPNet) ipallocator.Interface {
|
|||||||
return al
|
return al
|
||||||
}
|
}
|
||||||
|
|
||||||
func newStorage(t *testing.T) (*GenericREST, *StatusREST, *etcd3testing.EtcdTestServer) {
|
func newStorage(t *testing.T, ipFamilies []api.IPFamily) (*GenericREST, *StatusREST, *etcd3testing.EtcdTestServer) {
|
||||||
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
|
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
|
||||||
restOptions := generic.RESTOptions{
|
restOptions := generic.RESTOptions{
|
||||||
StorageConfig: etcdStorage.ForResource(schema.GroupResource{Resource: "services"}),
|
StorageConfig: etcdStorage.ForResource(schema.GroupResource{Resource: "services"}),
|
||||||
@ -57,78 +57,57 @@ func newStorage(t *testing.T) (*GenericREST, *StatusREST, *etcd3testing.EtcdTest
|
|||||||
DeleteCollectionWorkers: 1,
|
DeleteCollectionWorkers: 1,
|
||||||
ResourcePrefix: "services",
|
ResourcePrefix: "services",
|
||||||
}
|
}
|
||||||
ipAllocs := map[api.IPFamily]ipallocator.Interface{
|
|
||||||
api.IPv4Protocol: makeIPAllocator(makeIPNet(t)),
|
ipAllocs := map[api.IPFamily]ipallocator.Interface{}
|
||||||
|
for _, fam := range ipFamilies {
|
||||||
|
switch fam {
|
||||||
|
case api.IPv4Protocol:
|
||||||
|
_, cidr, _ := netutils.ParseCIDRSloppy("10.0.0.0/16")
|
||||||
|
ipAllocs[fam] = makeIPAllocator(cidr)
|
||||||
|
case api.IPv6Protocol:
|
||||||
|
_, cidr, _ := netutils.ParseCIDRSloppy("2000::/108")
|
||||||
|
ipAllocs[fam] = makeIPAllocator(cidr)
|
||||||
|
default:
|
||||||
|
t.Fatalf("Unknown IPFamily: %v", fam)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
serviceStorage, statusStorage, err := NewGenericREST(restOptions, api.IPv4Protocol, ipAllocs, nil)
|
|
||||||
|
serviceStorage, statusStorage, err := NewGenericREST(restOptions, ipFamilies[0], ipAllocs, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error from REST storage: %v", err)
|
t.Fatalf("unexpected error from REST storage: %v", err)
|
||||||
}
|
}
|
||||||
return serviceStorage, statusStorage, server
|
return serviceStorage, statusStorage, server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is used in generic registry tests.
|
||||||
func validService() *api.Service {
|
func validService() *api.Service {
|
||||||
singleStack := api.IPFamilyPolicySingleStack
|
return svctest.MakeService("foo",
|
||||||
clusterInternalTrafficPolicy := api.ServiceInternalTrafficPolicyCluster
|
svctest.SetClusterIPs(api.ClusterIPNone),
|
||||||
|
svctest.SetIPFamilyPolicy(api.IPFamilyPolicySingleStack),
|
||||||
return &api.Service{
|
svctest.SetIPFamilies(api.IPv4Protocol))
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "foo",
|
|
||||||
Namespace: metav1.NamespaceDefault,
|
|
||||||
},
|
|
||||||
Spec: api.ServiceSpec{
|
|
||||||
Selector: map[string]string{"bar": "baz"},
|
|
||||||
ClusterIP: api.ClusterIPNone,
|
|
||||||
ClusterIPs: []string{api.ClusterIPNone},
|
|
||||||
IPFamilyPolicy: &singleStack,
|
|
||||||
IPFamilies: []api.IPFamily{api.IPv4Protocol},
|
|
||||||
SessionAffinity: "None",
|
|
||||||
Type: api.ServiceTypeClusterIP,
|
|
||||||
Ports: []api.ServicePort{{
|
|
||||||
Port: 6502,
|
|
||||||
Protocol: api.ProtocolTCP,
|
|
||||||
TargetPort: intstr.FromInt(6502),
|
|
||||||
}},
|
|
||||||
InternalTrafficPolicy: &clusterInternalTrafficPolicy,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreate(t *testing.T) {
|
func TestCreate(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store)
|
test := genericregistrytest.New(t, storage.Store)
|
||||||
validService := validService()
|
svc := validService()
|
||||||
validService.ObjectMeta = metav1.ObjectMeta{}
|
svc.ObjectMeta = metav1.ObjectMeta{} // because genericregistrytest
|
||||||
test.TestCreate(
|
test.TestCreate(
|
||||||
// valid
|
// valid
|
||||||
validService,
|
svc,
|
||||||
// invalid
|
// invalid
|
||||||
&api.Service{
|
&api.Service{
|
||||||
Spec: api.ServiceSpec{},
|
Spec: api.ServiceSpec{},
|
||||||
},
|
},
|
||||||
// invalid
|
|
||||||
&api.Service{
|
|
||||||
Spec: api.ServiceSpec{
|
|
||||||
Selector: map[string]string{"bar": "baz"},
|
|
||||||
ClusterIPs: []string{"invalid"},
|
|
||||||
SessionAffinity: "None",
|
|
||||||
Type: api.ServiceTypeClusterIP,
|
|
||||||
Ports: []api.ServicePort{{
|
|
||||||
Port: 6502,
|
|
||||||
Protocol: api.ProtocolTCP,
|
|
||||||
TargetPort: intstr.FromInt(6502),
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
clusterInternalTrafficPolicy := api.ServiceInternalTrafficPolicyCluster
|
clusterInternalTrafficPolicy := api.ServiceInternalTrafficPolicyCluster
|
||||||
|
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
||||||
@ -157,7 +136,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate().ReturnDeletedObject()
|
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate().ReturnDeletedObject()
|
||||||
@ -165,7 +144,7 @@ func TestDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
||||||
@ -173,7 +152,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestList(t *testing.T) {
|
func TestList(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
test := genericregistrytest.New(t, storage.Store).AllowCreateOnUpdate()
|
||||||
@ -181,7 +160,7 @@ func TestList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWatch(t *testing.T) {
|
func TestWatch(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
test := genericregistrytest.New(t, storage.Store)
|
test := genericregistrytest.New(t, storage.Store)
|
||||||
@ -205,7 +184,7 @@ func TestWatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestShortNames(t *testing.T) {
|
func TestShortNames(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
expected := []string{"svc"}
|
expected := []string{"svc"}
|
||||||
@ -213,7 +192,7 @@ func TestShortNames(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCategories(t *testing.T) {
|
func TestCategories(t *testing.T) {
|
||||||
storage, _, server := newStorage(t)
|
storage, _, server := newStorage(t, []api.IPFamily{api.IPv4Protocol})
|
||||||
defer server.Terminate(t)
|
defer server.Terminate(t)
|
||||||
defer storage.Store.DestroyFunc()
|
defer storage.Store.DestroyFunc()
|
||||||
expected := []string{"all"}
|
expected := []string{"all"}
|
||||||
|
Loading…
Reference in New Issue
Block a user