Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Accept PodSecurityPolicy name, bind to managed ServiceAccount if provided #433

Merged
merged 11 commits into from
Nov 7, 2022

Conversation

nathancoleman
Copy link
Member

@nathancoleman nathancoleman commented Nov 1, 2022

Note Depends on the changes proposed in hashicorp/consul-k8s#1672 which was recently merged.

Changes proposed in this PR:

When creating a Deployment for a given Gateway, we create a "managed" ServiceAccount if the GatewayClassConfig indicates that we should.

With the changes in this PR, we will also create a Role and RoleBinding attaching the named podSecurityPolicy from the GatewayClassConfig if one has been provided.

Note For convenience, the Helm chart changes merged in hashicorp/consul-k8s#1672 will create a PodSecurityPolicy that is reasonable for these deployments when .Values.global.enablePodSecurityPolicies; however, users may also create their own PodSecurityPolicy and reference that in the GatewayClassConfig if they wish.

How I've tested this PR:

Create a K8s cluster that requires PodSecurityPolicies. In GKE, this is:

$ gcloud beta container clusters create cluster-1 --region us-east1 --enable-pod-security-policy

Install the Consul Helm chart from hashicorp/consul-k8s#1672 and exercise Consul API Gateway functionality.

How I expect reviewers to test this PR:

See above

Checklist:

  • Tests added
  • CHANGELOG entry added

    Run make changelog-entry for guidance in authoring a changelog entry, and
    commit the resulting file, which should have a name matching your PR number.
    Entries should use imperative present tense (e.g. Add support for...)

@nathancoleman nathancoleman added the pr/no-changelog Skip the CI check that requires a changelog entry label Nov 1, 2022
@nathancoleman nathancoleman changed the title Create Role + Binding attaching PodSecurityPolicy to managed ServiceAccount Accept PodSecurityPolicy name, bind to managed ServiceAccount if provided Nov 2, 2022
@nathancoleman nathancoleman removed the pr/no-changelog Skip the CI check that requires a changelog entry label Nov 2, 2022
@nathancoleman nathancoleman marked this pull request as ready for review November 3, 2022 18:42
Copy link
Member Author

@nathancoleman nathancoleman left a comment

Choose a reason for hiding this comment

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

Personal review

@@ -73,6 +73,10 @@ spec:
namespace:
description: The Consul namespace to use for authentication.
type: string
podSecurityPolicy:
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a huge fan of having this named explicitly after podSecurityPolicy since it's already going away w/ K8s 1.25; however, the replacement for it is different enough that I don't think it makes sense to try and munge the concepts together into one field. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually like that it's so explicit. That will make it easier when we remove PSPs in a few years. What is our support policy for K8s versions?

Comment on lines +50 to +51
//+kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=roles;rolebindings,verbs=list;get;create;watch
//+kubebuilder:rbac:groups=policy,resources=podsecuritypolicies,verbs=use
Copy link
Member Author

Choose a reason for hiding this comment

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

The RBAC generated from these annotations is only used for our own testing inside this repo. These changes reflect the changes that have already been made to the Consul Helm chart in hashicorp/consul-k8s#1672

Comment on lines +94 to +97
role := config.RoleFor(gateway)
if role == nil {
return nil
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Following the pattern already established by the ServiceAccount code immediately above: return nil if we shouldn't be creating a Role and move along.

pkg/apis/v1alpha1/types.go Outdated Show resolved Hide resolved
Comment on lines +188 to +189
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Copy link
Member Author

Choose a reason for hiding this comment

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

Looked for some constants for these but had no luck finding any in the K8s SDK

Copy link
Contributor

@t-eckert t-eckert left a comment

Choose a reason for hiding this comment

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

Nice! I like that you included some refactoring work as well to keep things tidy

@@ -73,6 +73,10 @@ spec:
namespace:
description: The Consul namespace to use for authentication.
type: string
podSecurityPolicy:
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually like that it's so explicit. That will make it easier when we remove PSPs in a few years. What is our support policy for K8s versions?

@@ -437,6 +438,14 @@ func (g *gatewayClient) DeleteService(ctx context.Context, service *core.Service
return nil
}

func (g *gatewayClient) EnsureExists(ctx context.Context, obj client.Object, mutators ...func() error) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a really clean implementation!

@nathancoleman nathancoleman merged commit 0d984c0 into main Nov 7, 2022
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants