-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove the pod reference in v1beta1 AddressGroup #1586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,9 +109,11 @@ func Convert_controlplane_GroupMember_To_v1beta1_GroupMember(in *controlplane.Gr | |
} | ||
|
||
func Convert_v1beta1_GroupMemberPod_To_controlplane_GroupMember(in *GroupMemberPod, out *controlplane.GroupMember, s conversion.Scope) error { | ||
out.Pod = &controlplane.PodReference{ | ||
Name: in.Pod.Name, | ||
Namespace: in.Pod.Namespace, | ||
if in.Pod != nil { | ||
out.Pod = &controlplane.PodReference{ | ||
Name: in.Pod.Name, | ||
Namespace: in.Pod.Namespace, | ||
} | ||
} | ||
out.IPs = []controlplane.IPAddress{controlplane.IPAddress(in.IP)} | ||
ports := make([]controlplane.NamedPort, len(in.Ports)) | ||
|
@@ -124,13 +126,20 @@ func Convert_v1beta1_GroupMemberPod_To_controlplane_GroupMember(in *GroupMemberP | |
return nil | ||
} | ||
|
||
func Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(in *controlplane.GroupMember, out *GroupMemberPod, s conversion.Scope) error { | ||
// Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod converts controlplane GroupMember to v1beta1 GroupMember | ||
// based on whether it's required to include Pod reference in the result. We must not include Pod reference when the | ||
// conversion is called for an AddressGroup as agents don't expect it in v1beta1 version. | ||
// This function doesn't match the pattern of conversion function which requires the last parameter to be | ||
// conversion.Scope so won't be registered to schema. | ||
func Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(in *controlplane.GroupMember, out *GroupMemberPod, includePodRef bool) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could have been a private function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely can be private, I kept its name as it was as code in this file doesn't follow normal go style anyway. |
||
if in.Pod == nil || len(in.IPs) > 1 { | ||
return fmt.Errorf("cannot convert ExternalEntity or dual stack Pod into GroupMemberPod") | ||
} | ||
out.Pod = &PodReference{ | ||
Name: in.Pod.Name, | ||
Namespace: in.Pod.Namespace, | ||
if includePodRef { | ||
out.Pod = &PodReference{ | ||
Name: in.Pod.Name, | ||
Namespace: in.Pod.Namespace, | ||
} | ||
} | ||
if len(in.IPs) > 0 { | ||
out.IP = IPAddress(in.IPs[0]) | ||
|
@@ -172,7 +181,7 @@ func Convert_controlplane_AddressGroup_To_v1beta1_AddressGroup(in *controlplane. | |
m := in.GroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, false); err != nil { | ||
return err | ||
} | ||
pods = append(pods, pod) | ||
|
@@ -227,7 +236,7 @@ func Convert_controlplane_AddressGroupPatch_To_v1beta1_AddressGroupPatch(in *con | |
m := in.AddedGroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, false); err != nil { | ||
return err | ||
} | ||
addedPods = append(addedPods, pod) | ||
|
@@ -241,7 +250,7 @@ func Convert_controlplane_AddressGroupPatch_To_v1beta1_AddressGroupPatch(in *con | |
m := in.RemovedGroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, false); err != nil { | ||
return err | ||
} | ||
removedPods = append(removedPods, pod) | ||
|
@@ -285,7 +294,7 @@ func Convert_controlplane_AppliedToGroup_To_v1beta1_AppliedToGroup(in *controlpl | |
m := in.GroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, true); err != nil { | ||
return err | ||
} | ||
pods = append(pods, pod) | ||
|
@@ -341,7 +350,7 @@ func Convert_controlplane_AppliedToGroupPatch_To_v1beta1_AppliedToGroupPatch(in | |
m := in.AddedGroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, true); err != nil { | ||
return err | ||
} | ||
addedPods = append(addedPods, pod) | ||
|
@@ -355,7 +364,7 @@ func Convert_controlplane_AppliedToGroupPatch_To_v1beta1_AppliedToGroupPatch(in | |
m := in.RemovedGroupMembers[i] | ||
if m.Pod != nil { | ||
var pod GroupMemberPod | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, nil); err != nil { | ||
if err := Convert_controlplane_GroupMember_To_v1beta1_GroupMemberPod(&m, &pod, true); err != nil { | ||
return err | ||
} | ||
removedPods = append(removedPods, pod) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,27 +157,43 @@ func TestDifferentNamedPorts(t *testing.T) { | |
t.Fatalf("Error when setting up test: %v", err) | ||
} | ||
defer teardownTest(t, data) | ||
data.testDifferentNamedPorts(t) | ||
checkFn, cleanupFn := data.setupDifferentNamedPorts(t) | ||
defer cleanupFn() | ||
checkFn() | ||
} | ||
|
||
func (data *TestData) testDifferentNamedPorts(t *testing.T) { | ||
func (data *TestData) setupDifferentNamedPorts(t *testing.T) (checkFn func(), cleanupFn func()) { | ||
var success bool | ||
var cleanupFuncs []func() | ||
cleanupFn = func() { | ||
for i := len(cleanupFuncs) - 1; i >= 0; i-- { | ||
cleanupFuncs[i]() | ||
} | ||
} | ||
// Call cleanupFn only if the function fails. In case of success, we will call cleanupFn in callers. | ||
defer func() { | ||
if !success { | ||
cleanupFn() | ||
} | ||
}() | ||
|
||
server0Port := 80 | ||
_, server0IPs, cleanupFunc := createAndWaitForPod(t, data, func(name string, nodeName string) error { | ||
server0Name, server0IPs, cleanupFunc := createAndWaitForPod(t, data, func(name string, nodeName string) error { | ||
return data.createServerPod(name, "http", server0Port, false) | ||
}, "test-server-", "") | ||
defer cleanupFunc() | ||
cleanupFuncs = append(cleanupFuncs, cleanupFunc) | ||
|
||
server1Port := 8080 | ||
_, server1IPs, cleanupFunc := createAndWaitForPod(t, data, func(name string, nodeName string) error { | ||
server1Name, server1IPs, cleanupFunc := createAndWaitForPod(t, data, func(name string, nodeName string) error { | ||
return data.createServerPod(name, "http", server1Port, false) | ||
}, "test-server-", "") | ||
defer cleanupFunc() | ||
cleanupFuncs = append(cleanupFuncs, cleanupFunc) | ||
|
||
client0Name, _, cleanupFunc := createAndWaitForPod(t, data, data.createBusyboxPodOnNode, "test-client-", "") | ||
defer cleanupFunc() | ||
cleanupFuncs = append(cleanupFuncs, cleanupFunc) | ||
|
||
client1Name, _, cleanupFunc := createAndWaitForPod(t, data, data.createBusyboxPodOnNode, "test-client-", "") | ||
defer cleanupFunc() | ||
cleanupFuncs = append(cleanupFuncs, cleanupFunc) | ||
|
||
preCheckFunc := func(server0IP, server1IP string) { | ||
// Both clients can connect to both servers. | ||
|
@@ -201,8 +217,14 @@ func (data *TestData) testDifferentNamedPorts(t *testing.T) { | |
|
||
// Create NetworkPolicy rule. | ||
spec := &networkingv1.NetworkPolicySpec{ | ||
// Apply to all Pods. | ||
PodSelector: metav1.LabelSelector{}, | ||
// Apply to two server Pods. | ||
PodSelector: metav1.LabelSelector{MatchExpressions: []metav1.LabelSelectorRequirement{ | ||
{ | ||
Key: "antrea-e2e", | ||
Operator: metav1.LabelSelectorOpIn, | ||
Values: []string{server0Name, server1Name}, | ||
}, | ||
}}, | ||
// Allow client0 to access named port: "http". | ||
Ingress: []networkingv1.NetworkPolicyIngressRule{{ | ||
Ports: []networkingv1.NetworkPolicyPort{{ | ||
|
@@ -217,15 +239,16 @@ func (data *TestData) testDifferentNamedPorts(t *testing.T) { | |
}, | ||
}}, | ||
} | ||
np, err := data.createNetworkPolicy("test-networkpolicy-allow-client0-to-http", spec) | ||
np, err := data.createNetworkPolicy(randName("test-networkpolicy-allow-client0-to-http"), spec) | ||
if err != nil { | ||
t.Fatalf("Error when creating network policy: %v", err) | ||
} | ||
defer func() { | ||
cleanupFuncs = append(cleanupFuncs, func() { | ||
if err = data.deleteNetworkpolicy(np); err != nil { | ||
t.Fatalf("Error when deleting network policy: %v", err) | ||
} | ||
}() | ||
}) | ||
time.Sleep(networkPolicyDelay) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it's changing the podSelector to "MatchExpressions" caused the networkpolicy takes a little more time to realize. The test now has a chance to fail without the sleep, I can confirm it's not because of the conversion function change as it also failed when I just changed the test code in this PR: https://github.com/vmware-tanzu/antrea/runs/1431829031?check_suite_focus=true |
||
|
||
npCheck := func(server0IP, server1IP string) { | ||
// client0 can connect to both servers. | ||
|
@@ -244,15 +267,18 @@ func (data *TestData) testDifferentNamedPorts(t *testing.T) { | |
} | ||
} | ||
|
||
// NetworkPolicy check. | ||
if clusterInfo.podV4NetworkCIDR != "" { | ||
npCheck(server0IPs.ipv4.String(), server1IPs.ipv4.String()) | ||
} | ||
checkFn = func() { | ||
// NetworkPolicy check. | ||
if clusterInfo.podV4NetworkCIDR != "" { | ||
npCheck(server0IPs.ipv4.String(), server1IPs.ipv4.String()) | ||
} | ||
|
||
if clusterInfo.podV6NetworkCIDR != "" { | ||
npCheck(server0IPs.ipv6.String(), server1IPs.ipv6.String()) | ||
if clusterInfo.podV6NetworkCIDR != "" { | ||
npCheck(server0IPs.ipv6.String(), server1IPs.ipv6.String()) | ||
} | ||
} | ||
|
||
success = true | ||
return | ||
} | ||
|
||
func TestDefaultDenyEgressPolicy(t *testing.T) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, is there any advantage to having conversion functions for non resources registered to the schema? I understand why
Convert_controlplane_AddressGroup_To_v1beta1_AddressGroup
needs to be registered, but it seems conversion functions for non resources will only be called indirectly by other conversion functions. Is it for auto-generated conversion functions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I wondered this as well, haven't found answer so blindly followed what K8s code does.
I can take a look what it would affect later and see if we could remove them. Created #1616 to track it.