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 tenantDefaultServiceAccount to FluxInstance API #63

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/v1/fluxinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ type Cluster struct {
// +optional
Multitenant bool `json:"multitenant,omitempty"`

// TenantDefaultServiceAccount is the name of the service account
// to use as default when the multitenant lockdown is enabled.
// Defaults to the 'default' service account from the tenant namespace.
// +optional
TenantDefaultServiceAccount string `json:"tenantDefaultServiceAccount,omitempty"`

// NetworkPolicy restricts network access to the current namespace.
// Defaults to true.
// +kubebuilder:default:=true
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/fluxcd.controlplane.io_fluxinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ spec:
NetworkPolicy restricts network access to the current namespace.
Defaults to true.
type: boolean
tenantDefaultServiceAccount:
description: |-
TenantDefaultServiceAccount is the name of the service account
to use as default when the multitenant lockdown is enabled.
Defaults to the 'default' service account from the tenant namespace.
type: string
type:
default: kubernetes
description: |-
Expand Down
5 changes: 5 additions & 0 deletions docs/api/v1/fluxinstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ spec:
cluster:
type: openshift
multitenant: true
tenantDefaultServiceAccount: "flux"
networkPolicy: true
domain: "cluster.local"
```
Expand All @@ -292,6 +293,10 @@ The supported values are `kubernetes` (default), `openshift`, `aks`, `eks` and `
The `.spec.cluster.multitenant` field is optional and specifies whether to enable Flux
[multi-tenancy lockdown](https://fluxcd.io/flux/installation/configuration/multitenancy/).

The `.spec.cluster.tenantDefaultServiceAccount` is optional and specifies the default
service account used by Flux when reconciling `Kustomization` and `HelmRelease`
resources found in the tenant namespaces.

#### Cluster network policy

The `.spec.cluster.networkPolicy` field is optional and specifies whether to restrict network access
Expand Down
2 changes: 1 addition & 1 deletion internal/builder/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestBuild_Profiles(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
options.ComponentImages = ci

options.Patches = ProfileOpenShift + ProfileMultitenant
options.Patches = ProfileOpenShift + GetMultitenantProfile("")

result, err := Build(srcDir, dstDir, options)
g.Expect(err).NotTo(HaveOccurred())
Expand Down
14 changes: 12 additions & 2 deletions internal/builder/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package builder

import "fmt"

const ProfileOpenShift = `
- target:
kind: Deployment
Expand All @@ -22,7 +24,7 @@ const ProfileOpenShift = `
path: /metadata/labels/pod-security.kubernetes.io~1warn-version
`

const ProfileMultitenant = `
const profileMultitenant = `
- target:
kind: Deployment
name: "(kustomize-controller|helm-controller|notification-controller|image-reflector-controller|image-automation-controller)"
Expand All @@ -43,11 +45,19 @@ const ProfileMultitenant = `
patch: |-
- op: add
path: /spec/template/spec/containers/0/args/-
value: --default-service-account=default
value: --default-service-account=%s
- target:
kind: Kustomization
patch: |-
- op: add
path: /spec/serviceAccountName
value: kustomize-controller
`

func GetMultitenantProfile(defaultSA string) string {
if defaultSA == "" {
defaultSA = "default"
}

return fmt.Sprintf(profileMultitenant, defaultSA)
}
2 changes: 1 addition & 1 deletion internal/controller/fluxinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (r *FluxInstanceReconciler) build(ctx context.Context,
options.Patches += builder.ProfileOpenShift
}
if obj.GetCluster().Multitenant {
options.Patches += builder.ProfileMultitenant
options.Patches += builder.GetMultitenantProfile(obj.GetCluster().TenantDefaultServiceAccount)
}

if obj.Spec.Storage != nil {
Expand Down