@@ -130,29 +130,39 @@ function ProjectFromExistingNamespace({ onBack }: { onBack: () => void }) {
130130
131131 setIsCreating ( true ) ;
132132 try {
133- const maybeExistingNamespace = namespaces ?. find ( it => it . metadata . name === selectedNamespace ) ;
134- if ( maybeExistingNamespace ) {
135- await maybeExistingNamespace . patch ( {
133+ const existingNamespaces = namespaces ?. filter ( it => it . metadata . name === selectedNamespace ) ;
134+ const clustersWithExistingNamespace = existingNamespaces ?. map ( it => it . cluster ) ?? [ ] ;
135+ if ( existingNamespaces && existingNamespaces . length > 0 ) {
136+ // Update all existing namespaces with the same name across selected clusters
137+ await Promise . all (
138+ existingNamespaces . map ( namespace =>
139+ namespace . patch ( {
140+ metadata : {
141+ labels : {
142+ [ PROJECT_ID_LABEL ] : projectName ,
143+ } ,
144+ } ,
145+ } )
146+ )
147+ ) ;
148+ }
149+
150+ // Create new namespace in all selected clusters that don't already have it
151+ const clustersWithoutNamespace = selectedClusters . filter (
152+ it => ! clustersWithExistingNamespace . includes ( it )
153+ ) ;
154+ for ( const cluster of clustersWithoutNamespace ) {
155+ const namespace = {
156+ kind : 'Namespace' ,
157+ apiVersion : 'v1' ,
136158 metadata : {
159+ name : toKubernetesName ( typedNamespace ) ,
137160 labels : {
138161 [ PROJECT_ID_LABEL ] : projectName ,
139162 } ,
140- } ,
141- } ) ;
142- } else {
143- for ( const cluster of selectedClusters ) {
144- const namespace = {
145- kind : 'Namespace' ,
146- apiVersion : 'v1' ,
147- metadata : {
148- name : toKubernetesName ( typedNamespace ) ,
149- labels : {
150- [ PROJECT_ID_LABEL ] : projectName ,
151- } ,
152- } as any ,
153- } as KubeObjectInterface ;
154- await apply ( namespace , cluster ) ;
155- }
163+ } as any ,
164+ } as KubeObjectInterface ;
165+ await apply ( namespace , cluster ) ;
156166 }
157167
158168 history . push ( createRouteURL ( 'projectDetails' , { name : projectName } ) ) ;
0 commit comments