create service add create ExternalName service implementation

This commit is contained in:
AdoHe
2016-11-16 04:47:10 -05:00
parent fdce738113
commit d66bcbfa61
8 changed files with 168 additions and 9 deletions

View File

@@ -24,14 +24,16 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"
"k8s.io/kubernetes/pkg/util/validation"
)
type ServiceCommonGeneratorV1 struct {
Name string
TCP []string
Type api.ServiceType
ClusterIP string
NodePort int
Name string
TCP []string
Type api.ServiceType
ClusterIP string
NodePort int
ExternalName string
}
type ServiceClusterIPGeneratorV1 struct {
@@ -46,6 +48,11 @@ type ServiceLoadBalancerGeneratorV1 struct {
ServiceCommonGeneratorV1
}
// TODO: is this really necessary?
type ServiceExternalNameGeneratorV1 struct {
ServiceCommonGeneratorV1
}
func (ServiceClusterIPGeneratorV1) ParamNames() []GeneratorParam {
return []GeneratorParam{
{"name", true},
@@ -67,6 +74,13 @@ func (ServiceLoadBalancerGeneratorV1) ParamNames() []GeneratorParam {
}
}
func (ServiceExternalNameGeneratorV1) ParamNames() []GeneratorParam {
return []GeneratorParam{
{"name", true},
{"externalname", true},
}
}
func parsePorts(portString string) (int32, intstr.IntOrString, error) {
portStringSlice := strings.Split(portString, ":")
@@ -100,9 +114,14 @@ func (s ServiceCommonGeneratorV1) GenerateCommon(params map[string]interface{})
if !isString {
return fmt.Errorf("expected string, saw %v for 'clusterip'", clusterip)
}
externalname, isString := params["externalname"].(string)
if !isString {
return fmt.Errorf("expected string, saw %v for 'externalname'", externalname)
}
s.Name = name
s.TCP = tcpStrings
s.ClusterIP = clusterip
s.ExternalName = externalname
return nil
}
@@ -145,6 +164,19 @@ func (s ServiceClusterIPGeneratorV1) Generate(params map[string]interface{}) (ru
return delegate.StructuredGenerate()
}
func (s ServiceExternalNameGeneratorV1) Generate(params map[string]interface{}) (runtime.Object, error) {
err := ValidateParams(s.ParamNames(), params)
if err != nil {
return nil, err
}
delegate := &ServiceCommonGeneratorV1{Type: api.ServiceTypeExternalName, ClusterIP: ""}
err = delegate.GenerateCommon(params)
if err != nil {
return nil, err
}
return delegate.StructuredGenerate()
}
// validate validates required fields are set to support structured generation
func (s ServiceCommonGeneratorV1) validate() error {
if len(s.Name) == 0 {
@@ -159,9 +191,14 @@ func (s ServiceCommonGeneratorV1) validate() error {
if s.ClusterIP == api.ClusterIPNone && len(s.TCP) > 0 {
return fmt.Errorf("can not map ports with clusterip=None")
}
if s.ClusterIP != api.ClusterIPNone && len(s.TCP) == 0 {
if s.ClusterIP != api.ClusterIPNone && len(s.TCP) == 0 && s.Type != api.ServiceTypeExternalName {
return fmt.Errorf("at least one tcp port specifier must be provided")
}
if s.Type == api.ServiceTypeExternalName {
if errs := validation.IsDNS1123Subdomain(s.ExternalName); len(errs) != 0 {
return fmt.Errorf("invalid service external name %s", s.ExternalName)
}
}
return nil
}
@@ -199,9 +236,10 @@ func (s ServiceCommonGeneratorV1) StructuredGenerate() (runtime.Object, error) {
Labels: labels,
},
Spec: api.ServiceSpec{
Type: api.ServiceType(s.Type),
Selector: selector,
Ports: ports,
Type: api.ServiceType(s.Type),
Selector: selector,
Ports: ports,
ExternalName: s.ExternalName,
},
}
if len(s.ClusterIP) > 0 {