Skip to content

Commit

Permalink
Update Pod annotations for Service type change,
Browse files Browse the repository at this point in the history
includes a UT for the same.
Fixes antrea-io#1910
  • Loading branch information
shubhamavi committed Mar 3, 2021
1 parent 7d9dc3b commit 1b2e111
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pkg/agent/nodeportlocal/k8s/npl_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,19 @@ func (c *NPLController) enqueueSvcUpdate(oldObj, newObj interface{}) {
} else if newSvcAnnotation == "true" {
podKeys = sets.NewString(c.getPodsFromService(newSvc)...)
}
} else if !reflect.DeepEqual(oldSvc.Spec.Selector, newSvc.Spec.Selector) {
}

var oldPodSet, newPodSet sets.String
if oldSvc.Spec.Type != newSvc.Spec.Type {
newPodSet = sets.NewString(c.getPodsFromService(newSvc)...)
podKeys = podKeys.Union(newPodSet)
}

if !reflect.DeepEqual(oldSvc.Spec.Selector, newSvc.Spec.Selector) {
// Disjunctive union of Pods from both Service sets.
oldPodSet := sets.NewString(c.getPodsFromService(oldSvc)...)
newPodSet := sets.NewString(c.getPodsFromService(newSvc)...)
podKeys = oldPodSet.Difference(newPodSet).Union(newPodSet.Difference(oldPodSet))
oldPodSet = sets.NewString(c.getPodsFromService(oldSvc)...)
newPodSet = sets.NewString(c.getPodsFromService(newSvc)...)
podKeys = podKeys.Union(oldPodSet.Difference(newPodSet).Union(newPodSet.Difference(oldPodSet)))
}

for podKey := range podKeys {
Expand Down
18 changes: 18 additions & 0 deletions pkg/agent/nodeportlocal/npl_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ func TestSvcNamespaceUpdate(t *testing.T) {
assert.False(t, testData.portTable.RuleExists(defaultPodIP, defaultPort))
}

// TestSvcTypeUpdate updates Service type from ClusterIP to NodePort
// and checks whether Pod annotations are removed.
func TestSvcTypeUpdate(t *testing.T) {
testData, testSvc, testPod := setUpWithTestServiceAndPod(t)
defer testData.tearDown()

// Update Service type to NodePort.
testSvc.Spec.Type = "NodePort"
_, err := testData.k8sClient.CoreV1().Services(defaultNS).Update(context.TODO(), testSvc, metav1.UpdateOptions{})
require.NoError(t, err, "Service update failed")
t.Logf("successfully updated Service: %v", testSvc)

// Check that annotation and the rule are removed.
_, err = testData.pollForPodAnnotation(testPod.Name, false)
require.NoError(t, err, "Poll for annotation check failed")
assert.False(t, testData.portTable.RuleExists(defaultPodIP, defaultPort))
}

// TestSvcUpdateAnnotation updates the Service spec to disabled NPL. It then verifies that the Pod's
// NPL annotation is removed and that the port table is updated.
func TestSvcUpdateAnnotation(t *testing.T) {
Expand Down

0 comments on commit 1b2e111

Please sign in to comment.