Skip to content

Commit

Permalink
Fix deadlock issue and address comments
Browse files Browse the repository at this point in the history
Signed-off-by: wgrayson <wgrayson@vmware.com>
  • Loading branch information
GraysonWu committed Jul 16, 2022
1 parent b72a300 commit a881218
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
12 changes: 10 additions & 2 deletions pkg/agent/controller/networkpolicy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func toServicesIndexFunc(obj interface{}) ([]string, error) {
func (r *ruleCache) appliedToServicesIndexFunc(obj interface{}) ([]string, error) {
rule := obj.(*rule)
appliedToSvcNamespacedName := sets.String{}
memberSet, exist := r.unionAppliedToGroups(rule.AppliedToGroups)
memberSet, exist := r.unionAppliedToGroupsLocked(rule.AppliedToGroups)
if !exist {
return []string{}, nil
}
Expand Down Expand Up @@ -477,8 +477,9 @@ func (c *ruleCache) processGroupIDUpdates() {
for _, toSvcRule := range toSvcRules {
c.dirtyRuleHandler(toSvcRule.(*rule).ID)
}

c.appliedToSetLock.RLock()
appliedToSvcRules, err := c.rules.ByIndex(appliedToServicesIndex, svcStr)
c.appliedToSetLock.RUnlock()
if err != nil {
continue
}
Expand Down Expand Up @@ -926,6 +927,13 @@ func (c *ruleCache) unionAppliedToGroups(groupNames []string) (v1beta.GroupMembe
c.appliedToSetLock.RLock()
defer c.appliedToSetLock.RUnlock()

return c.unionAppliedToGroupsLocked(groupNames)
}

// unionAppliedToGroupsLocked gets the union of pods of the provided appliedTo groups.
// If any group is found, the union and true will be returned. Otherwise an empty set and false will be returned.
// It's caller's responsibility to lock and unlock c.appliedToSetLock.
func (c *ruleCache) unionAppliedToGroupsLocked(groupNames []string) (v1beta.GroupMemberSet, bool) {
anyExists := false
set := v1beta.NewGroupMemberSet()
for _, groupName := range groupNames {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/controlplane/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ type GroupMember struct {
IPs []IPAddress
// Ports is the list NamedPort of the GroupMember.
Ports []NamedPort
// Service maintains the reference to the Service.
// Service is the reference to the Service. It can only be used in an AppliedTo
// Group and only a NodePort type Service can be referred by this field.
Service *ServiceReference
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/controlplane/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ type GroupMember struct {
Ports []NamedPort `json:"ports,omitempty" protobuf:"bytes,4,rep,name=ports"`
// Node maintains the reference to the Node.
Node *NodeReference `json:"node,omitempty" protobuf:"bytes,5,opt,name=node"`
// Service maintains the reference to the Service.
// Service is the reference to the Service. It can only be used in an AppliedTo
// Group and only a NodePort type Service can be referred by this field.
Service *ServiceReference `json:"service,omitempty" protobuf:"bytes,6,opt,name=service"`
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/crd/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,10 @@ type NetworkPolicyPeer struct {
// A NodeSelector cannot be set in AppliedTo field or set with any other selector.
// +optional
NodeSelector *metav1.LabelSelector `json:"nodeSelector,omitempty"`
// Select a certain Service which match the NamespacedName.
// Select a certain Service which matches the NamespacedName.
// A Service can only be set in either policy level AppliedTo field in a policy
// that only has ingress rules or rule level AppliedTo field in an ingress rule.
// Only a NodePort Service can be referred by this field.
// Cannot be set with any other selector.
// +optional
Service *NamespacedName `json:"service,omitempty"`
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/types/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type AppliedToGroup struct {
// Selector describes how the group selects pods.
// Selector can't be used with Service.
Selector *GroupSelector
// Service describes the Service this group selects.
// Service refers to the Service this group selects. Only a NodePort type Service
// can be referred by this field.
// Service can't be used with Selector.
Service *controlplane.ServiceReference
// GroupMemberByNode is a mapping from nodeName to a set of GroupMembers on the Node,
Expand Down

0 comments on commit a881218

Please sign in to comment.