Skip to content

Commit

Permalink
Add tenantDefaultServiceAccount to FluxInstance API
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Jul 3, 2024
1 parent 86bfe14 commit 317dfeb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
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

0 comments on commit 317dfeb

Please sign in to comment.