mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	add e2e test on the hostport predicates
This commit is contained in:
		@@ -903,42 +903,9 @@ func PodFitsHostPorts(pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *s
 | 
			
		||||
 | 
			
		||||
	existingPorts := nodeInfo.UsedPorts()
 | 
			
		||||
 | 
			
		||||
	// try to see whether two hostPorts will conflict or not
 | 
			
		||||
	for existingPort := range existingPorts {
 | 
			
		||||
		existingHostPortInfo := decode(existingPort)
 | 
			
		||||
 | 
			
		||||
		if existingHostPortInfo.hostIP == "0.0.0.0" {
 | 
			
		||||
			// loop through all the want hostPort to see if there exists a conflict
 | 
			
		||||
			for wantPort := range wantPorts {
 | 
			
		||||
				wantHostPortInfo := decode(wantPort)
 | 
			
		||||
 | 
			
		||||
				// if there already exists one hostPort whose hostIP is 0.0.0.0, then the other want hostport (which has the same protocol and port) will not fit
 | 
			
		||||
				if wantHostPortInfo.hostPort == existingHostPortInfo.hostPort && wantHostPortInfo.protocol == existingHostPortInfo.protocol {
 | 
			
		||||
					return false, []algorithm.PredicateFailureReason{ErrPodNotFitsHostPorts}, nil
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for wantPort := range wantPorts {
 | 
			
		||||
		wantHostPortInfo := decode(wantPort)
 | 
			
		||||
 | 
			
		||||
		if wantHostPortInfo.hostIP == "0.0.0.0" {
 | 
			
		||||
			// loop through all the existing hostPort to see if there exists a conflict
 | 
			
		||||
			for existingPort := range existingPorts {
 | 
			
		||||
				existingHostPortInfo := decode(existingPort)
 | 
			
		||||
 | 
			
		||||
				// if there already exists one hostPort whose hostIP may be 127.0.0.1, then a hostPort (which wants 0.0.0.0 hostIP and has the same protocol and port) will not fit
 | 
			
		||||
				if wantHostPortInfo.hostPort == existingHostPortInfo.hostPort && wantHostPortInfo.protocol == existingHostPortInfo.protocol {
 | 
			
		||||
					return false, []algorithm.PredicateFailureReason{ErrPodNotFitsHostPorts}, nil
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			// general check hostPort conflict procedure for hostIP is not 0.0.0.0
 | 
			
		||||
			if wantPort != "" && existingPorts[wantPort] {
 | 
			
		||||
				return false, []algorithm.PredicateFailureReason{ErrPodNotFitsHostPorts}, nil
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	// try to see whether existingPorts and  wantPorts will conflict or not
 | 
			
		||||
	if portsConflict(existingPorts, wantPorts) {
 | 
			
		||||
		return false, []algorithm.PredicateFailureReason{ErrPodNotFitsHostPorts}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true, nil, nil
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ import (
 | 
			
		||||
	"k8s.io/api/core/v1"
 | 
			
		||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/labels"
 | 
			
		||||
	schedutil "k8s.io/kubernetes/plugin/pkg/scheduler/util"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// FindLabelsInSet gets as many key/value pairs as possible out of a label set.
 | 
			
		||||
@@ -98,7 +99,7 @@ type hostPortInfo struct {
 | 
			
		||||
	hostPort string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// decode a string ("protocol/hostIP/hostPort") to *hostPortInfo object
 | 
			
		||||
// decode decodes string ("protocol/hostIP/hostPort") to *hostPortInfo object.
 | 
			
		||||
func decode(info string) *hostPortInfo {
 | 
			
		||||
	hostPortInfoSlice := strings.Split(info, "/")
 | 
			
		||||
 | 
			
		||||
@@ -112,3 +113,48 @@ func decode(info string) *hostPortInfo {
 | 
			
		||||
		hostPort: hostPort,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// specialPortConflictCheck detects whether specailHostPort(whose hostIP is 0.0.0.0) is conflict with otherHostPorts.
 | 
			
		||||
// return true if we have a conflict.
 | 
			
		||||
func specialPortConflictCheck(specialHostPort string, otherHostPorts map[string]bool) bool {
 | 
			
		||||
	specialHostPortInfo := decode(specialHostPort)
 | 
			
		||||
 | 
			
		||||
	if specialHostPortInfo.hostIP == schedutil.DefaultBindAllHostIP {
 | 
			
		||||
		// loop through all the otherHostPorts to see if there exists a conflict
 | 
			
		||||
		for hostPortItem := range otherHostPorts {
 | 
			
		||||
			hostPortInfo := decode(hostPortItem)
 | 
			
		||||
 | 
			
		||||
			// if there exists one hostPortItem which has the same hostPort and protocol with the specialHostPort, that will cause a conflict
 | 
			
		||||
			if specialHostPortInfo.hostPort == hostPortInfo.hostPort && specialHostPortInfo.protocol == hostPortInfo.protocol {
 | 
			
		||||
				return true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// portsConflict check whether existingPorts and wantPorts conflict with each other
 | 
			
		||||
// return true if we have a conflict
 | 
			
		||||
func portsConflict(existingPorts, wantPorts map[string]bool) bool {
 | 
			
		||||
 | 
			
		||||
	for existingPort := range existingPorts {
 | 
			
		||||
		if specialPortConflictCheck(existingPort, wantPorts) {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for wantPort := range wantPorts {
 | 
			
		||||
		if specialPortConflictCheck(wantPort, existingPorts) {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// general check hostPort conflict procedure for hostIP is not 0.0.0.0
 | 
			
		||||
		if existingPorts[wantPort] {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -113,3 +113,151 @@ func Test_decode(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test_specialPortConflictCheck(t *testing.T) {
 | 
			
		||||
	type args struct {
 | 
			
		||||
		specialHostPort string
 | 
			
		||||
		otherHostPorts  map[string]bool
 | 
			
		||||
	}
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name string
 | 
			
		||||
		args args
 | 
			
		||||
		want bool
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name: "test-1",
 | 
			
		||||
			args: args{
 | 
			
		||||
				specialHostPort: "TCP/0.0.0.0/80",
 | 
			
		||||
				otherHostPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.2/8080": true,
 | 
			
		||||
					"TCP/127.0.0.1/80":   true,
 | 
			
		||||
					"UDP/127.0.0.2/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test-2",
 | 
			
		||||
			args: args{
 | 
			
		||||
				specialHostPort: "TCP/0.0.0.0/80",
 | 
			
		||||
				otherHostPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.2/8080": true,
 | 
			
		||||
					"UDP/127.0.0.1/80":   true,
 | 
			
		||||
					"UDP/127.0.0.2/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test-3",
 | 
			
		||||
			args: args{
 | 
			
		||||
				specialHostPort: "TCP/0.0.0.0/80",
 | 
			
		||||
				otherHostPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.2/8080": true,
 | 
			
		||||
					"TCP/127.0.0.1/8090": true,
 | 
			
		||||
					"UDP/127.0.0.2/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test-4",
 | 
			
		||||
			args: args{
 | 
			
		||||
				specialHostPort: "TCP/0.0.0.0/80",
 | 
			
		||||
				otherHostPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.2/8080": true,
 | 
			
		||||
					"UDP/127.0.0.1/8090": true,
 | 
			
		||||
					"TCP/127.0.0.2/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: false,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			if got := specialPortConflictCheck(tt.args.specialHostPort, tt.args.otherHostPorts); got != tt.want {
 | 
			
		||||
				t.Errorf("specialPortConflictCheck() = %v, want %v", got, tt.want)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test_portsConflict(t *testing.T) {
 | 
			
		||||
	type args struct {
 | 
			
		||||
		existingPorts map[string]bool
 | 
			
		||||
		wantPorts     map[string]bool
 | 
			
		||||
	}
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name string
 | 
			
		||||
		args args
 | 
			
		||||
		want bool
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name: "test1",
 | 
			
		||||
			args: args{
 | 
			
		||||
				existingPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
				wantPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test2",
 | 
			
		||||
			args: args{
 | 
			
		||||
				existingPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.2/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
				wantPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test3",
 | 
			
		||||
			args: args{
 | 
			
		||||
				existingPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
				wantPorts: map[string]bool{
 | 
			
		||||
					"UDP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: false,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test4",
 | 
			
		||||
			args: args{
 | 
			
		||||
				existingPorts: map[string]bool{
 | 
			
		||||
					"TCP/0.0.0.0/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
				wantPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: true,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name: "test5",
 | 
			
		||||
			args: args{
 | 
			
		||||
				existingPorts: map[string]bool{
 | 
			
		||||
					"TCP/127.0.0.1/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
				wantPorts: map[string]bool{
 | 
			
		||||
					"TCP/0.0.0.0/8080": true,
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			want: true,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
		t.Run(tt.name, func(t *testing.T) {
 | 
			
		||||
			if got := portsConflict(tt.args.existingPorts, tt.args.wantPorts); got != tt.want {
 | 
			
		||||
				t.Errorf("portsConflict() = %v, want %v", got, tt.want)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user