mirror of https://github.com/k3s-io/k3s
Improve "constraint violation" error message.
parent
63c07ad58b
commit
ad4c2ee630
|
@ -17,11 +17,16 @@ limitations under the License.
|
|||
package constraint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
// Allowed returns true if pods is a collection of bound pods
|
||||
// which can run without conflict on a single minion.
|
||||
func Allowed(pods []api.BoundPod) bool {
|
||||
return !PortsConflict(pods)
|
||||
func Allowed(pods []api.BoundPod) error {
|
||||
if (PortsConflict(pods)) {
|
||||
return fmt.Errorf("conflicting ports");
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||
package constraint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
|
@ -40,11 +41,11 @@ func podWithContainers(containers ...api.Container) api.BoundPod {
|
|||
|
||||
func TestAllowed(t *testing.T) {
|
||||
table := []struct {
|
||||
allowed bool
|
||||
err error
|
||||
pods []api.BoundPod
|
||||
}{
|
||||
{
|
||||
allowed: true,
|
||||
err: nil,
|
||||
pods: []api.BoundPod{
|
||||
podWithContainers(
|
||||
containerWithHostPorts(1, 2, 3),
|
||||
|
@ -57,7 +58,7 @@ func TestAllowed(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
allowed: true,
|
||||
err: nil,
|
||||
pods: []api.BoundPod{
|
||||
podWithContainers(
|
||||
containerWithHostPorts(0, 0),
|
||||
|
@ -70,7 +71,7 @@ func TestAllowed(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
allowed: false,
|
||||
err: fmt.Errorf("conflicting ports"),
|
||||
pods: []api.BoundPod{
|
||||
podWithContainers(
|
||||
containerWithHostPorts(3, 3),
|
||||
|
@ -78,7 +79,7 @@ func TestAllowed(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
allowed: false,
|
||||
err: fmt.Errorf("conflicting ports"),
|
||||
pods: []api.BoundPod{
|
||||
podWithContainers(
|
||||
containerWithHostPorts(6),
|
||||
|
@ -91,7 +92,7 @@ func TestAllowed(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, item := range table {
|
||||
if e, a := item.allowed, Allowed(item.pods); e != a {
|
||||
if e, a := item.err, Allowed(item.pods); e != a && e.Error() != a.Error() {
|
||||
t.Errorf("Expected %v, got %v: \n%v\v", e, a, item.pods)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -214,8 +214,8 @@ func (r *Registry) assignPod(ctx api.Context, podID string, machine string) erro
|
|||
err = r.AtomicUpdate(contKey, &api.BoundPods{}, func(in runtime.Object) (runtime.Object, error) {
|
||||
boundPodList := in.(*api.BoundPods)
|
||||
boundPodList.Items = append(boundPodList.Items, *boundPod)
|
||||
if !constraint.Allowed(boundPodList.Items) {
|
||||
return nil, fmt.Errorf("the assignment would cause a constraint violation")
|
||||
if e := constraint.Allowed(boundPodList.Items); e != nil {
|
||||
return nil, fmt.Errorf("the assignment would cause the following constraint violation: %v", e)
|
||||
}
|
||||
return boundPodList, nil
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue