Skip to content
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

Add service account selector support in ACNP #3044

Merged
merged 1 commit into from Feb 17, 2022

Conversation

GraysonWu
Copy link
Contributor

@GraysonWu GraysonWu commented Nov 23, 2021

Fixes #2927.

This PR added serviceAccount field support to ACNP. It uses
Namespace and Name to specify a ServiceAccount and all Pods with this
ServiceAccount will be selected as workloads. It could be used in
egress to, ingress from and appliedTo of both policy and single
rule.
To implement this feature, this PR also added a custom label to
all Pods internally, which looks like:
internal.antrea.io/service-account:[ServiceAccountName]. And when
process ACNP, serviceAccount will be translate to a GroupSelector
with Namespace and PodSelector to select Pods we need.

Signed-off-by: wgrayson wgrayson@vmware.com

@codecov-commenter
Copy link

codecov-commenter commented Nov 23, 2021

Codecov Report

Merging #3044 (42088ae) into main (15de4cf) will decrease coverage by 5.98%.
The diff coverage is 78.20%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3044      +/-   ##
==========================================
- Coverage   60.95%   54.97%   -5.99%     
==========================================
  Files         266      371     +105     
  Lines       26508    50828   +24320     
==========================================
+ Hits        16159    27943   +11784     
- Misses       8597    20480   +11883     
- Partials     1752     2405     +653     
Flag Coverage Δ
e2e-tests 53.74% <62.82%> (?)
integration-tests 36.28% <ø> (?)
kind-e2e-tests 49.56% <21.81%> (+1.73%) ⬆️
unit-tests 41.67% <76.36%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/controller/networkpolicy/validate.go 73.77% <53.57%> (+32.98%) ⬆️
...g/controller/networkpolicy/clusternetworkpolicy.go 81.03% <87.09%> (+7.64%) ⬆️
pkg/controller/grouping/group_entity_index.go 90.57% <100.00%> (-3.45%) ⬇️
pkg/controller/networkpolicy/crd_utils.go 90.20% <100.00%> (+2.97%) ⬆️
pkg/agent/cniserver/pod_configuration_linux.go 26.31% <0.00%> (-40.36%) ⬇️
pkg/controller/ipam/antrea_ipam_controller.go 48.71% <0.00%> (-31.57%) ⬇️
pkg/controller/networkpolicy/endpoint_querier.go 61.46% <0.00%> (-29.97%) ⬇️
pkg/controller/egress/controller.go 61.11% <0.00%> (-27.34%) ⬇️
.../registry/networkpolicy/clustergroupmember/rest.go 64.28% <0.00%> (-23.95%) ⬇️
pkg/agent/cniserver/ipam/antrea_ipam.go 55.55% <0.00%> (-23.62%) ⬇️
... and 337 more

@GraysonWu GraysonWu force-pushed the sa-selector branch 2 times, most recently from 3a108a6 to 32414cc Compare December 4, 2021 03:20
@GraysonWu GraysonWu changed the title [WIP] Add service account selector support in ACNP Add service account selector support in ACNP Dec 4, 2021
@GraysonWu GraysonWu changed the title Add service account selector support in ACNP [WIP] Add service account selector support in ACNP Dec 7, 2021
@GraysonWu GraysonWu force-pushed the sa-selector branch 5 times, most recently from afdac9f to a1a6976 Compare December 15, 2021 02:13
@GraysonWu GraysonWu force-pushed the sa-selector branch 3 times, most recently from 59be42d to b5480c4 Compare January 6, 2022 20:07
@GraysonWu GraysonWu changed the title [WIP] Add service account selector support in ACNP Add service account selector support in ACNP Jan 6, 2022
@GraysonWu GraysonWu force-pushed the sa-selector branch 3 times, most recently from af508b0 to 01782b4 Compare January 7, 2022 21:48
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/controller/grouping/custom_label.go Outdated Show resolved Hide resolved
pkg/controller/networkpolicy/networkpolicy_controller.go Outdated Show resolved Hide resolved
// re-processed based on if they are affected by an added/updated/deleted
// ServiceAccount.
func (n *NetworkPolicyController) filterACNPsByServiceAccount(sa *v1.ServiceAccount) (acnps []*crdv1alpha1.ClusterNetworkPolicy) {
acnpObjs, err := n.cnpInformer.Informer().GetIndexer().ByIndex(ServiceAccountsPeerIndex, HasServiceAccountsPeer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnqn is there any issue with defining an index like this which simply partitions all the objects into 2 different groups?

pkg/controller/networkpolicy/clusternetworkpolicy.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha2/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
@GraysonWu GraysonWu force-pushed the sa-selector branch 2 times, most recently from 6c6b9c9 to 341a695 Compare January 20, 2022 22:55
@GraysonWu GraysonWu force-pushed the sa-selector branch 2 times, most recently from 4f64e47 to 3399901 Compare January 28, 2022 23:50
docs/antrea-network-policy.md Outdated Show resolved Hide resolved
docs/antrea-network-policy.md Outdated Show resolved Hide resolved
docs/antrea-network-policy.md Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
pkg/controller/grouping/group_entity_index.go Outdated Show resolved Hide resolved
pkg/controller/networkpolicy/clusternetworkpolicy.go Outdated Show resolved Hide resolved
pkg/controller/networkpolicy/validate.go Outdated Show resolved Hide resolved
pkg/controller/networkpolicy/validate.go Outdated Show resolved Hide resolved
pkg/controller/networkpolicy/validate.go Show resolved Hide resolved
test/e2e/k8s_util.go Outdated Show resolved Hide resolved
test/e2e/k8s_util.go Outdated Show resolved Hide resolved
pkg/apis/crd/v1alpha1/types.go Outdated Show resolved Hide resolved
// workloads in AppliedTo/To/From fields.
// Cannot be set with any other selector.
// +optional
ServiceAccount *NamespacedName `json:"serviceAccount,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see the API is really simple now!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #2755, the reference struct is named ServiceReference:

// ServiceReference represents a reference to a v1.Service.
type ServiceReference struct {
	// Name of the Service
	Name string `json:"name"`
	// Namespace of the Service
	Namespace string `json:"namespace,omitempty"`
}

I feel we should either unify them to NamespacedName or create ServiceAccountReference for this one. Is there any plan?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I slightly prefer to unify them to NamespacedName which could be used to refer to other resources. And if we will use verbose API in the future then I guess this generic struct will still be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If unifying them sounds good, do you suggest unifying them in this PR or another one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest another PR.

@GraysonWu GraysonWu force-pushed the sa-selector branch 4 times, most recently from 6365995 to 1fd84d4 Compare February 11, 2022 22:55
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall

// workloads in AppliedTo/To/From fields.
// Cannot be set with any other selector.
// +optional
ServiceAccount *NamespacedName `json:"serviceAccount,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #2755, the reference struct is named ServiceReference:

// ServiceReference represents a reference to a v1.Service.
type ServiceReference struct {
	// Name of the Service
	Name string `json:"name"`
	// Namespace of the Service
	Namespace string `json:"namespace,omitempty"`
}

I feel we should either unify them to NamespacedName or create ServiceAccountReference for this one. Is there any plan?

pkg/controller/grouping/group_entity_index.go Outdated Show resolved Hide resolved
docs/antrea-network-policy.md Outdated Show resolved Hide resolved
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Could you squash the commits and add commit message?

// workloads in AppliedTo/To/From fields.
// Cannot be set with any other selector.
// +optional
ServiceAccount *NamespacedName `json:"serviceAccount,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest another PR.

Fixes antrea-io#2927

This PR added `serviceAccount` field support to ACNP. It uses
Namespace and Name to specify a ServiceAccount and all Pods with this
ServiceAccount will be selected as workloads. It could be used in
egress `to`, ingress `from` and `appliedTo` of both policy and single
rule. To implement this feature, this PR also added a custom label to
all Pods internally, which looks like:
`internal.antrea.io/service-account:[ServiceAccountName]`. And when
process ACNP, `serviceAccount` will be translate to a `GroupSelector`
with `Namespace` and `PodSelector` to select Pods we need.

Signed-off-by: wgrayson <wgrayson@vmware.com>
@GraysonWu
Copy link
Contributor Author

LGTM. Could you squash the commits and add commit message?

Thanks. Done.

@GraysonWu
Copy link
Contributor Author

/test-all

@GraysonWu
Copy link
Contributor Author

/test-integration

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@antoninbas antoninbas merged commit 78e3583 into antrea-io:main Feb 17, 2022
@antoninbas antoninbas deleted the sa-selector branch February 17, 2022 21:42
bangqipropel pushed a commit to bangqipropel/antrea that referenced this pull request Mar 2, 2022
Fixes antrea-io#2927

This PR added `serviceAccount` field support to ACNP. It uses
Namespace and Name to specify a ServiceAccount and all Pods with this
ServiceAccount will be selected as workloads. It could be used in
egress `to`, ingress `from` and `appliedTo` of both policy and single
rule. To implement this feature, this PR also added a custom label to
all Pods internally, which looks like:
`internal.antrea.io/service-account:[ServiceAccountName]`. And when
process ACNP, `serviceAccount` will be translate to a `GroupSelector`
with `Namespace` and `PodSelector` to select Pods we need.

Signed-off-by: wgrayson <wgrayson@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support serviceAccountSelector for NetworkPolicy
7 participants