mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Improve replication controller errro handling.
This commit is contained in:
parent
eb24c997e4
commit
1454d5d174
@ -158,9 +158,9 @@ func ResizeController(ctx api.Context, name string, replicas int, client client.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func portsFromString(spec string) []api.Port {
|
func portsFromString(spec string) ([]api.Port, error) {
|
||||||
if spec == "" {
|
if spec == "" {
|
||||||
return []api.Port{}
|
return []api.Port{}, nil
|
||||||
}
|
}
|
||||||
parts := strings.Split(spec, ",")
|
parts := strings.Split(spec, ",")
|
||||||
var result []api.Port
|
var result []api.Port
|
||||||
@ -168,7 +168,7 @@ func portsFromString(spec string) []api.Port {
|
|||||||
pieces := strings.Split(part, ":")
|
pieces := strings.Split(part, ":")
|
||||||
if len(pieces) < 1 || len(pieces) > 2 {
|
if len(pieces) < 1 || len(pieces) > 2 {
|
||||||
glog.Infof("Bad port spec: %s", part)
|
glog.Infof("Bad port spec: %s", part)
|
||||||
continue
|
return nil, fmt.Errorf("Bad port spec: %s", part)
|
||||||
}
|
}
|
||||||
host := 0
|
host := 0
|
||||||
container := 0
|
container := 0
|
||||||
@ -177,28 +177,28 @@ func portsFromString(spec string) []api.Port {
|
|||||||
container, err = strconv.Atoi(pieces[0])
|
container, err = strconv.Atoi(pieces[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Container port is not integer: %s %v", pieces[0], err)
|
glog.Errorf("Container port is not integer: %s %v", pieces[0], err)
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
host, err = strconv.Atoi(pieces[0])
|
host, err = strconv.Atoi(pieces[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Host port is not integer: %s %v", pieces[0], err)
|
glog.Errorf("Host port is not integer: %s %v", pieces[0], err)
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
container, err = strconv.Atoi(pieces[1])
|
container, err = strconv.Atoi(pieces[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Container port is not integer: %s %v", pieces[1], err)
|
glog.Errorf("Container port is not integer: %s %v", pieces[1], err)
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if container < 1 {
|
if container < 1 {
|
||||||
glog.Errorf("Container port is not valid: %d", container)
|
glog.Errorf("Container port is not valid: %d", container)
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result = append(result, api.Port{ContainerPort: container, HostPort: host})
|
result = append(result, api.Port{ContainerPort: container, HostPort: host})
|
||||||
}
|
}
|
||||||
return result
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
|
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
|
||||||
@ -206,6 +206,10 @@ func RunController(ctx api.Context, image, name string, replicas int, client cli
|
|||||||
if servicePort > 0 && !util.IsDNSLabel(name) {
|
if servicePort > 0 && !util.IsDNSLabel(name) {
|
||||||
return fmt.Errorf("Service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
|
return fmt.Errorf("Service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
|
||||||
}
|
}
|
||||||
|
ports, err := portsFromString(portSpec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
controller := &api.ReplicationController{
|
controller := &api.ReplicationController{
|
||||||
JSONBase: api.JSONBase{
|
JSONBase: api.JSONBase{
|
||||||
ID: name,
|
ID: name,
|
||||||
@ -223,7 +227,7 @@ func RunController(ctx api.Context, image, name string, replicas int, client cli
|
|||||||
{
|
{
|
||||||
Name: strings.ToLower(name),
|
Name: strings.ToLower(name),
|
||||||
Image: image,
|
Image: image,
|
||||||
Ports: portsFromString(portSpec),
|
Ports: ports,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -121,6 +121,27 @@ func TestRunController(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunControllerWithWrongArgs(t *testing.T) {
|
||||||
|
fakeClient := client.Fake{}
|
||||||
|
name := "name"
|
||||||
|
image := "foo/bar"
|
||||||
|
replicas := 3
|
||||||
|
err := RunController(api.NewDefaultContext(), image, name, replicas, &fakeClient, "8080:", -1)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Unexpected non-error: %#v", fakeClient.Actions)
|
||||||
|
}
|
||||||
|
RunController(api.NewDefaultContext(), image, name, replicas, &fakeClient, "8080:80", -1)
|
||||||
|
if len(fakeClient.Actions) != 1 || fakeClient.Actions[0].Action != "create-controller" {
|
||||||
|
t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
|
||||||
|
}
|
||||||
|
controller := fakeClient.Actions[0].Value.(*api.ReplicationController)
|
||||||
|
if controller.ID != name ||
|
||||||
|
controller.DesiredState.Replicas != replicas ||
|
||||||
|
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
|
||||||
|
t.Errorf("Unexpected controller: %#v", controller)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunControllerWithService(t *testing.T) {
|
func TestRunControllerWithService(t *testing.T) {
|
||||||
fakeClient := client.Fake{}
|
fakeClient := client.Fake{}
|
||||||
name := "name"
|
name := "name"
|
||||||
@ -273,7 +294,7 @@ func TestLoadAuthInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMakePorts(t *testing.T) {
|
func TestMakePorts(t *testing.T) {
|
||||||
var testCases = []struct {
|
var successTestCases = []struct {
|
||||||
spec string
|
spec string
|
||||||
ports []api.Port
|
ports []api.Port
|
||||||
}{
|
}{
|
||||||
@ -290,10 +311,27 @@ func TestMakePorts(t *testing.T) {
|
|||||||
[]api.Port{},
|
[]api.Port{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range testCases {
|
for _, tt := range successTestCases {
|
||||||
ports := portsFromString(tt.spec)
|
ports, err := portsFromString(tt.spec)
|
||||||
if !reflect.DeepEqual(ports, tt.ports) {
|
if !reflect.DeepEqual(ports, tt.ports) {
|
||||||
t.Errorf("Expected %#v, got %#v", tt.ports, ports)
|
t.Errorf("Expected %#v, got %#v", tt.ports, ports)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var failTestCases = []struct {
|
||||||
|
spec string
|
||||||
|
}{
|
||||||
|
{"8080:"},
|
||||||
|
{":80"},
|
||||||
|
{":"},
|
||||||
|
}
|
||||||
|
for _, tt := range failTestCases {
|
||||||
|
_, err := portsFromString(tt.spec)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Unexpected non-error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user