Skip to content

Commit dfe7cbe

Browse files
Allow option to skip tenant namespace creation
Signed-off-by: Anshuman Singh C <[email protected]>
1 parent 9b944da commit dfe7cbe

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

cmd/flux/create_tenant.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ const (
5858
)
5959

6060
type tenantFlags struct {
61-
namespaces []string
62-
clusterRole string
63-
account string
61+
namespaces []string
62+
clusterRole string
63+
account string
64+
skipNamespace bool
6465
}
6566

6667
var tenantArgs tenantFlags
@@ -69,6 +70,7 @@ func init() {
6970
createTenantCmd.Flags().StringSliceVar(&tenantArgs.namespaces, "with-namespace", nil, "namespace belonging to this tenant")
7071
createTenantCmd.Flags().StringVar(&tenantArgs.clusterRole, "cluster-role", "cluster-admin", "cluster role of the tenant role binding")
7172
createTenantCmd.Flags().StringVar(&tenantArgs.account, "with-service-account", "", "service account belonging to this tenant")
73+
createTenantCmd.Flags().BoolVar(&tenantArgs.skipNamespace, "skip-namespace-create", false, "skip namespace creation (namespace must exist already)")
7274
createCmd.AddCommand(createTenantCmd)
7375
}
7476

@@ -157,7 +159,7 @@ func createTenantCmdRun(cmd *cobra.Command, args []string) error {
157159

158160
if createArgs.export {
159161
for i := range tenantArgs.namespaces {
160-
if err := exportTenant(namespaces[i], accounts[i], roleBindings[i]); err != nil {
162+
if err := exportTenant(namespaces[i], accounts[i], roleBindings[i], tenantArgs.skipNamespace); err != nil {
161163
return err
162164
}
163165
}
@@ -173,9 +175,11 @@ func createTenantCmdRun(cmd *cobra.Command, args []string) error {
173175
}
174176

175177
for i := range tenantArgs.namespaces {
176-
logger.Actionf("applying namespace %s", namespaces[i].Name)
177-
if err := upsertNamespace(ctx, kubeClient, namespaces[i]); err != nil {
178-
return err
178+
if !tenantArgs.skipNamespace {
179+
logger.Actionf("applying namespace %s", namespaces[i].Name)
180+
if err := upsertNamespace(ctx, kubeClient, namespaces[i]); err != nil {
181+
return err
182+
}
179183
}
180184

181185
logger.Actionf("applying service account %s", accounts[i].Name)
@@ -284,19 +288,24 @@ func upsertRoleBinding(ctx context.Context, kubeClient client.Client, roleBindin
284288
return nil
285289
}
286290

287-
func exportTenant(namespace corev1.Namespace, account corev1.ServiceAccount, roleBinding rbacv1.RoleBinding) error {
288-
namespace.TypeMeta = metav1.TypeMeta{
289-
APIVersion: "v1",
290-
Kind: "Namespace",
291-
}
292-
data, err := yaml.Marshal(namespace)
293-
if err != nil {
294-
return err
295-
}
296-
data = bytes.Replace(data, []byte("spec: {}\n"), []byte(""), 1)
291+
func exportTenant(namespace corev1.Namespace, account corev1.ServiceAccount, roleBinding rbacv1.RoleBinding, skipNamespace bool) error {
292+
var data []byte
293+
var err error
297294

298-
printlnStdout("---")
299-
printlnStdout(resourceToString(data))
295+
if !skipNamespace {
296+
namespace.TypeMeta = metav1.TypeMeta{
297+
APIVersion: "v1",
298+
Kind: "Namespace",
299+
}
300+
data, err = yaml.Marshal(namespace)
301+
if err != nil {
302+
return err
303+
}
304+
data = bytes.Replace(data, []byte("spec: {}\n"), []byte(""), 1)
305+
306+
printlnStdout("---")
307+
printlnStdout(resourceToString(data))
308+
}
300309

301310
account.TypeMeta = metav1.TypeMeta{
302311
APIVersion: "v1",

cmd/flux/create_tenant_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func TestCreateTenant(t *testing.T) {
5454
args: "create tenant dev-team --with-namespace=apps --cluster-role=custom-role --export",
5555
assert: assertGoldenFile("./testdata/create_tenant/tenant-with-cluster-role.yaml"),
5656
},
57+
{
58+
name: "tenant with skip namespace create",
59+
args: "create tenant dev-team --with-namespace=apps --cluster-role=cluster-admin --skip-namespace-create --export",
60+
assert: assertGoldenFile("./testdata/create_tenant/tenant-with-skip-namespace-create.yaml"),
61+
},
5762
}
5863

5964
for _, tt := range tests {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
labels:
6+
toolkit.fluxcd.io/tenant: dev-team
7+
name: dev-team
8+
namespace: apps
9+
---
10+
apiVersion: rbac.authorization.k8s.io/v1
11+
kind: RoleBinding
12+
metadata:
13+
labels:
14+
toolkit.fluxcd.io/tenant: dev-team
15+
name: dev-team-reconciler
16+
namespace: apps
17+
roleRef:
18+
apiGroup: rbac.authorization.k8s.io
19+
kind: ClusterRole
20+
name: cluster-admin
21+
subjects:
22+
- apiGroup: rbac.authorization.k8s.io
23+
kind: User
24+
name: gotk:apps:reconciler
25+
- kind: ServiceAccount
26+
name: dev-team
27+
namespace: apps

0 commit comments

Comments
 (0)