mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Move service test-helper funcs to a package
This commit is contained in:
parent
b1fcbab801
commit
175f4f3387
141
pkg/api/service/testing/make.go
Normal file
141
pkg/api/service/testing/make.go
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 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 testing
|
||||||
|
|
||||||
|
import (
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
|
||||||
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tweak is a function that modifies a Service.
|
||||||
|
type Tweak func(*api.Service)
|
||||||
|
|
||||||
|
// MakeService helps construct Service objects (which pass API validation) more
|
||||||
|
// legibly and tersely than a Go struct definition. By default this produces
|
||||||
|
// a ClusterIP service with a single port and a trivial selector. The caller
|
||||||
|
// can pass any number of tweak functions to further modify the result.
|
||||||
|
func MakeService(name string, tweaks ...Tweak) *api.Service {
|
||||||
|
// NOTE: Any field that would be populated by defaulting needs to be
|
||||||
|
// present and valid here.
|
||||||
|
svc := &api.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: metav1.NamespaceDefault,
|
||||||
|
},
|
||||||
|
Spec: api.ServiceSpec{
|
||||||
|
Selector: map[string]string{"k": "v"},
|
||||||
|
SessionAffinity: api.ServiceAffinityNone,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// Default to ClusterIP
|
||||||
|
SetTypeClusterIP(svc)
|
||||||
|
// Default to 1 port
|
||||||
|
SetPorts(MakeServicePort("", 93, intstr.FromInt(76), api.ProtocolTCP))(svc)
|
||||||
|
|
||||||
|
for _, tweak := range tweaks {
|
||||||
|
tweak(svc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return svc
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTypeClusterIP sets the service type to ClusterIP and clears other fields.
|
||||||
|
func SetTypeClusterIP(svc *api.Service) {
|
||||||
|
svc.Spec.Type = api.ServiceTypeClusterIP
|
||||||
|
for i := range svc.Spec.Ports {
|
||||||
|
svc.Spec.Ports[i].NodePort = 0
|
||||||
|
}
|
||||||
|
svc.Spec.ExternalName = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTypeNodePort sets the service type to NodePort and clears other fields.
|
||||||
|
func SetTypeNodePort(svc *api.Service) {
|
||||||
|
svc.Spec.Type = api.ServiceTypeNodePort
|
||||||
|
svc.Spec.ExternalTrafficPolicy = api.ServiceExternalTrafficPolicyTypeCluster
|
||||||
|
svc.Spec.ExternalName = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTypeLoadBalancer sets the service type to LoadBalancer and clears other fields.
|
||||||
|
func SetTypeLoadBalancer(svc *api.Service) {
|
||||||
|
svc.Spec.Type = api.ServiceTypeLoadBalancer
|
||||||
|
svc.Spec.ExternalTrafficPolicy = api.ServiceExternalTrafficPolicyTypeCluster
|
||||||
|
svc.Spec.ExternalName = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTypeExternalName sets the service type to ExternalName and clears other fields.
|
||||||
|
func SetTypeExternalName(svc *api.Service) {
|
||||||
|
svc.Spec.Type = api.ServiceTypeExternalName
|
||||||
|
svc.Spec.ExternalName = "example.com"
|
||||||
|
svc.Spec.ExternalTrafficPolicy = ""
|
||||||
|
svc.Spec.ClusterIP = ""
|
||||||
|
svc.Spec.ClusterIPs = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPorts sets the service ports list.
|
||||||
|
func SetPorts(ports ...api.ServicePort) Tweak {
|
||||||
|
return func(svc *api.Service) {
|
||||||
|
svc.Spec.Ports = ports
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeServicePort helps construct ServicePort objects which pass API
|
||||||
|
// validation.
|
||||||
|
func MakeServicePort(name string, port int, tgtPort intstr.IntOrString, proto api.Protocol) api.ServicePort {
|
||||||
|
return api.ServicePort{
|
||||||
|
Name: name,
|
||||||
|
Port: int32(port),
|
||||||
|
TargetPort: tgtPort,
|
||||||
|
Protocol: proto,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetClusterIPs sets the service ClusterIP and ClusterIPs fields.
|
||||||
|
func SetClusterIPs(ips ...string) Tweak {
|
||||||
|
return func(svc *api.Service) {
|
||||||
|
svc.Spec.ClusterIP = ips[0]
|
||||||
|
svc.Spec.ClusterIPs = ips
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetIPFamilies sets the service IPFamilies field.
|
||||||
|
func SetIPFamilies(families ...api.IPFamily) Tweak {
|
||||||
|
return func(svc *api.Service) {
|
||||||
|
svc.Spec.IPFamilies = families
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetIPFamilyPolicy sets the service IPFamilyPolicy field.
|
||||||
|
func SetIPFamilyPolicy(policy api.IPFamilyPolicyType) Tweak {
|
||||||
|
return func(svc *api.Service) {
|
||||||
|
svc.Spec.IPFamilyPolicy = &policy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNodePorts sets the values for each node port, in order. If less values
|
||||||
|
// are specified than there are ports, the rest are untouched.
|
||||||
|
func SetNodePorts(values ...int) Tweak {
|
||||||
|
return func(svc *api.Service) {
|
||||||
|
for i := range svc.Spec.Ports {
|
||||||
|
if i >= len(values) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
svc.Spec.Ports[i].NodePort = int32(values[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user