@ -61,6 +61,7 @@ type tenantFlags struct {
namespaces [ ] string
namespaces [ ] string
clusterRole string
clusterRole string
account string
account string
skipNamespace bool
}
}
var tenantArgs tenantFlags
var tenantArgs tenantFlags
@ -69,6 +70,7 @@ func init() {
createTenantCmd . Flags ( ) . StringSliceVar ( & tenantArgs . namespaces , "with-namespace" , nil , "namespace belonging to this tenant" )
createTenantCmd . Flags ( ) . StringSliceVar ( & tenantArgs . namespaces , "with-namespace" , nil , "namespace belonging to this tenant" )
createTenantCmd . Flags ( ) . StringVar ( & tenantArgs . clusterRole , "cluster-role" , "cluster-admin" , "cluster role of the tenant role binding" )
createTenantCmd . Flags ( ) . StringVar ( & tenantArgs . clusterRole , "cluster-role" , "cluster-admin" , "cluster role of the tenant role binding" )
createTenantCmd . Flags ( ) . StringVar ( & tenantArgs . account , "with-service-account" , "" , "service account belonging to this tenant" )
createTenantCmd . Flags ( ) . StringVar ( & tenantArgs . account , "with-service-account" , "" , "service account belonging to this tenant" )
createTenantCmd . Flags ( ) . BoolVar ( & tenantArgs . skipNamespace , "skip-namespace" , false , "skip namespace creation (namespace must exist already)" )
createCmd . AddCommand ( createTenantCmd )
createCmd . AddCommand ( createTenantCmd )
}
}
@ -157,7 +159,7 @@ func createTenantCmdRun(cmd *cobra.Command, args []string) error {
if createArgs . export {
if createArgs . export {
for i := range tenantArgs . namespaces {
for i := range tenantArgs . namespaces {
if err := exportTenant ( namespaces [ i ] , accounts [ i ] , roleBindings [ i ] ); err != nil {
if err := exportTenant ( namespaces [ i ] , accounts [ i ] , roleBindings [ i ] , tenantArgs . skipNamespace ); err != nil {
return err
return err
}
}
}
}
@ -173,10 +175,12 @@ func createTenantCmdRun(cmd *cobra.Command, args []string) error {
}
}
for i := range tenantArgs . namespaces {
for i := range tenantArgs . namespaces {
if ! tenantArgs . skipNamespace {
logger . Actionf ( "applying namespace %s" , namespaces [ i ] . Name )
logger . Actionf ( "applying namespace %s" , namespaces [ i ] . Name )
if err := upsertNamespace ( ctx , kubeClient , namespaces [ i ] ) ; err != nil {
if err := upsertNamespace ( ctx , kubeClient , namespaces [ i ] ) ; err != nil {
return err
return err
}
}
}
logger . Actionf ( "applying service account %s" , accounts [ i ] . Name )
logger . Actionf ( "applying service account %s" , accounts [ i ] . Name )
if err := upsertServiceAccount ( ctx , kubeClient , accounts [ i ] ) ; err != nil {
if err := upsertServiceAccount ( ctx , kubeClient , accounts [ i ] ) ; err != nil {
@ -284,12 +288,16 @@ func upsertRoleBinding(ctx context.Context, kubeClient client.Client, roleBindin
return nil
return nil
}
}
func exportTenant ( namespace corev1 . Namespace , account corev1 . ServiceAccount , roleBinding rbacv1 . RoleBinding ) error {
func exportTenant ( namespace corev1 . Namespace , account corev1 . ServiceAccount , roleBinding rbacv1 . RoleBinding , skipNamespace bool ) error {
var data [ ] byte
var err error
if ! skipNamespace {
namespace . TypeMeta = metav1 . TypeMeta {
namespace . TypeMeta = metav1 . TypeMeta {
APIVersion : "v1" ,
APIVersion : "v1" ,
Kind : "Namespace" ,
Kind : "Namespace" ,
}
}
data , err : = yaml . Marshal ( namespace )
data , err = yaml . Marshal ( namespace )
if err != nil {
if err != nil {
return err
return err
}
}
@ -297,6 +305,7 @@ func exportTenant(namespace corev1.Namespace, account corev1.ServiceAccount, rol
printlnStdout ( "---" )
printlnStdout ( "---" )
printlnStdout ( resourceToString ( data ) )
printlnStdout ( resourceToString ( data ) )
}
account . TypeMeta = metav1 . TypeMeta {
account . TypeMeta = metav1 . TypeMeta {
APIVersion : "v1" ,
APIVersion : "v1" ,