Skip to content

Commit

Permalink
Add Tier field for ANP and use UID as keyFunc for internal store
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyanngg committed Aug 27, 2020
1 parent 0f1e8fb commit e109aa4
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 13 deletions.
21 changes: 21 additions & 0 deletions build/yamls/antrea-aks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ metadata:
app: antrea
name: networkpolicies.security.antrea.tanzu.vmware.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.tier
description: The Tier to which this AntreaNetworkPolicy belongs to.
name: Tier
type: string
- JSONPath: .spec.priority
description: The Priority of this AntreaNetworkPolicy relative to other policies.
format: float
name: Priority
type: number
- JSONPath: .metadata.creationTimestamp
name: Age
type: date
group: security.antrea.tanzu.vmware.com
names:
kind: NetworkPolicy
Expand Down Expand Up @@ -329,6 +342,14 @@ spec:
maximum: 10000
minimum: 1
type: number
tier:
enum:
- Emergency
- SecurityOps
- NetworkOps
- Platform
- Application
type: string
required:
- appliedTo
- priority
Expand Down
21 changes: 21 additions & 0 deletions build/yamls/antrea-eks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ metadata:
app: antrea
name: networkpolicies.security.antrea.tanzu.vmware.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.tier
description: The Tier to which this AntreaNetworkPolicy belongs to.
name: Tier
type: string
- JSONPath: .spec.priority
description: The Priority of this AntreaNetworkPolicy relative to other policies.
format: float
name: Priority
type: number
- JSONPath: .metadata.creationTimestamp
name: Age
type: date
group: security.antrea.tanzu.vmware.com
names:
kind: NetworkPolicy
Expand Down Expand Up @@ -329,6 +342,14 @@ spec:
maximum: 10000
minimum: 1
type: number
tier:
enum:
- Emergency
- SecurityOps
- NetworkOps
- Platform
- Application
type: string
required:
- appliedTo
- priority
Expand Down
21 changes: 21 additions & 0 deletions build/yamls/antrea-gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ metadata:
app: antrea
name: networkpolicies.security.antrea.tanzu.vmware.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.tier
description: The Tier to which this AntreaNetworkPolicy belongs to.
name: Tier
type: string
- JSONPath: .spec.priority
description: The Priority of this AntreaNetworkPolicy relative to other policies.
format: float
name: Priority
type: number
- JSONPath: .metadata.creationTimestamp
name: Age
type: date
group: security.antrea.tanzu.vmware.com
names:
kind: NetworkPolicy
Expand Down Expand Up @@ -329,6 +342,14 @@ spec:
maximum: 10000
minimum: 1
type: number
tier:
enum:
- Emergency
- SecurityOps
- NetworkOps
- Platform
- Application
type: string
required:
- appliedTo
- priority
Expand Down
21 changes: 21 additions & 0 deletions build/yamls/antrea-ipsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ metadata:
app: antrea
name: networkpolicies.security.antrea.tanzu.vmware.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.tier
description: The Tier to which this AntreaNetworkPolicy belongs to.
name: Tier
type: string
- JSONPath: .spec.priority
description: The Priority of this AntreaNetworkPolicy relative to other policies.
format: float
name: Priority
type: number
- JSONPath: .metadata.creationTimestamp
name: Age
type: date
group: security.antrea.tanzu.vmware.com
names:
kind: NetworkPolicy
Expand Down Expand Up @@ -329,6 +342,14 @@ spec:
maximum: 10000
minimum: 1
type: number
tier:
enum:
- Emergency
- SecurityOps
- NetworkOps
- Platform
- Application
type: string
required:
- appliedTo
- priority
Expand Down
21 changes: 21 additions & 0 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ metadata:
app: antrea
name: networkpolicies.security.antrea.tanzu.vmware.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.tier
description: The Tier to which this AntreaNetworkPolicy belongs to.
name: Tier
type: string
- JSONPath: .spec.priority
description: The Priority of this AntreaNetworkPolicy relative to other policies.
format: float
name: Priority
type: number
- JSONPath: .metadata.creationTimestamp
name: Age
type: date
group: security.antrea.tanzu.vmware.com
names:
kind: NetworkPolicy
Expand Down Expand Up @@ -329,6 +342,14 @@ spec:
maximum: 10000
minimum: 1
type: number
tier:
enum:
- Emergency
- SecurityOps
- NetworkOps
- Platform
- Application
type: string
required:
- appliedTo
- priority
Expand Down
16 changes: 16 additions & 0 deletions build/yamls/base/crds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,19 @@ spec:
- anp
# Prune any unknown fields
preserveUnknownFields: false
additionalPrinterColumns:
- name: Tier
type: string
description: The Tier to which this AntreaNetworkPolicy belongs to.
JSONPath: .spec.tier
- name: Priority
type: number
format: float
description: The Priority of this AntreaNetworkPolicy relative to other policies.
JSONPath: .spec.priority
- name: Age
type: date
JSONPath: .metadata.creationTimestamp
validation:
openAPIV3Schema:
type: object
Expand All @@ -367,6 +380,9 @@ spec:
- priority
type: object
properties:
tier:
type: string
enum: ['Emergency', 'SecurityOps', 'NetworkOps', 'Platform', 'Application']
priority:
type: number
format: float
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/networkpolicy/antreanetworkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ func (n *NetworkPolicyController) processAntreaNetworkPolicy(np *secv1alpha1.Net
Priority: int32(idx),
})
}
tierPriority := getTierPriority(np.Spec.Tier)
internalNetworkPolicy := &antreatypes.NetworkPolicy{
Name: np.Name,
Namespace: np.Namespace,
UID: np.UID,
AppliedToGroups: appliedToGroupNames,
Rules: rules,
Priority: &np.Spec.Priority,
TierPriority: &tierPriority,
}
return internalNetworkPolicy
}
10 changes: 0 additions & 10 deletions pkg/controller/networkpolicy/clusternetworkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,6 @@ func (n *NetworkPolicyController) deleteCNP(old interface{}) {
n.deleteDereferencedAddressGroups(oldInternalNP)
}

// getTierPriority retrieves the priority associated with the input Tier name.
// If the Tier name is empty, by default, the lowest priority Application Tier
// is returned.
func getTierPriority(tier string) networking.TierPriority {
if tier == "" {
return antreatypes.TierApplication
}
return tierPriorityMap[tier]
}

// processClusterNetworkPolicy creates an internal NetworkPolicy instance
// corresponding to the secv1alpha1.ClusterNetworkPolicy object. This method
// does not commit the internal NetworkPolicy in store, instead returns an
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/networkpolicy/crd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ func (n *NetworkPolicyController) createAddressGroupForCRD(peer secv1alpha1.Netw
n.addressGroupStore.Create(addressGroup)
return normalizedUID
}

// getTierPriority retrieves the priority associated with the input Tier name.
// If the Tier name is empty, by default, the lowest priority Application Tier
// is returned.
func getTierPriority(tier string) networking.TierPriority {
if tier == "" {
return antreatypes.TierApplication
}
return tierPriorityMap[tier]
}
6 changes: 5 additions & 1 deletion pkg/controller/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const (
)

var (
keyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc
keyFunc = InternalNetworkPolicyKeyFunc

// uuidNamespace is a uuid.UUID type generated from a string to be
// used to generate uuid.UUID for internal Antrea objects like
Expand Down Expand Up @@ -290,6 +290,10 @@ func (n *NetworkPolicyController) heartbeat(name string) {
}
}

func InternalNetworkPolicyKeyFunc(obj metav1.Object) (string, error) {
return string(obj.GetUID()), nil
}

func (n *NetworkPolicyController) GetNetworkPolicyNum() int {
return len(n.internalNetworkPolicyStore.List())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

"github.com/vmware-tanzu/antrea/pkg/apis/networking/v1beta1"
)
Expand Down Expand Up @@ -86,6 +87,7 @@ var policies = []*networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress-egress",
Namespace: "testNamespace",
UID: types.UID("uid-1"),
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
Expand Down Expand Up @@ -123,6 +125,7 @@ var policies = []*networkingv1.NetworkPolicy{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default-deny-egress",
UID: types.UID("uid-2"),
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/networkpolicy/store/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/vmware-tanzu/antrea/pkg/apiserver/storage"
"github.com/vmware-tanzu/antrea/pkg/apiserver/storage/ram"
"github.com/vmware-tanzu/antrea/pkg/controller/types"
"github.com/vmware-tanzu/antrea/pkg/k8s"
)

const (
Expand Down Expand Up @@ -122,7 +121,7 @@ func NetworkPolicyKeyFunc(obj interface{}) (string, error) {
if !ok {
return "", fmt.Errorf("object is not *types.NetworkPolicy: %v", obj)
}
return k8s.NamespacedName(policy.Namespace, policy.Name), nil
return string(policy.UID), nil
}

// NewNetworkPolicyStore creates a store of NetworkPolicy.
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/networkpolicy/store/networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"

Expand Down Expand Up @@ -156,6 +157,7 @@ func TestGetNetworkPolicyByIndex(t *testing.T) {
policy1 := &types.NetworkPolicy{
Namespace: "foo",
Name: "bar",
UID: apitypes.UID("uid-1"),
Rules: []networking.NetworkPolicyRule{{
Direction: networking.DirectionIn,
From: networking.NetworkPolicyPeer{AddressGroups: []string{"addressGroup1"}},
Expand All @@ -166,6 +168,7 @@ func TestGetNetworkPolicyByIndex(t *testing.T) {
policy2 := &types.NetworkPolicy{
Namespace: "foo2",
Name: "bar2",
UID: apitypes.UID("uid-2"),
Rules: []networking.NetworkPolicyRule{{
Direction: networking.DirectionIn,
From: networking.NetworkPolicyPeer{AddressGroups: []string{"addressGroup1", "addressGroup2"}},
Expand Down

0 comments on commit e109aa4

Please sign in to comment.