@@ -63,6 +63,12 @@ import { emptyIfUndefined, toLowerCase } from '../../utils/string';
6363import { dumpYaml , loadYaml } from '../../utils/yaml' ;
6464import { Checkbox , Select } from '../Controls' ;
6565import { PackageLink , RepositoriesLink , RepositoryLink } from '../Links' ;
66+ import {
67+ applyNamespaceState ,
68+ getNamespaceDefaultState ,
69+ getNamespaceState ,
70+ NamespaceState ,
71+ } from './utils/namespaceState' ;
6672import {
6773 applyValidateResourcesState ,
6874 getValidateResourcesDefaultState ,
@@ -77,6 +83,13 @@ const useStyles = makeStyles(() => ({
7783 marginTop : '16px' ,
7884 } ,
7985 } ,
86+ checkboxConditionalElements : {
87+ marginLeft : '32px' ,
88+ marginTop : '8px' ,
89+ '& > *' : {
90+ marginTop : '16px' ,
91+ } ,
92+ } ,
8093} ) ) ;
8194
8295export enum AddPackagePageAction {
@@ -170,6 +183,10 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
170183 site : '' ,
171184 } ) ;
172185
186+ const [ namespaceState , setNamespaceState ] = useState < NamespaceState > (
187+ getNamespaceDefaultState ( ) ,
188+ ) ;
189+
173190 const [ validateResourcesState , setValidateResourcesState ] =
174191 useState < ValidateResourcesState > ( getValidateResourcesDefaultState ( ) ) ;
175192
@@ -325,6 +342,9 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
325342 site : emptyIfUndefined ( thisKptfile . info ?. site ) ,
326343 } ) ;
327344
345+ const thisNamespaceState = getNamespaceState ( thisKptfile , resources ) ;
346+ setNamespaceState ( thisNamespaceState ) ;
347+
328348 const validateState = getValidateResourcesState ( thisKptfile ) ;
329349 setValidateResourcesState ( validateState ) ;
330350 } ;
@@ -338,6 +358,7 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
338358 site : '' ,
339359 } ) ;
340360
361+ setNamespaceState ( getNamespaceDefaultState ) ;
341362 setValidateResourcesState ( getValidateResourcesDefaultState ( ) ) ;
342363 }
343364 } , [ api , sourcePackageRevision ] ) ;
@@ -419,7 +440,11 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
419440 updatedResourcesMap ,
420441 kptFunctions ,
421442 ) ;
422-
443+ updatedResourcesMap = await applyNamespaceState (
444+ namespaceState ,
445+ updatedResourcesMap ,
446+ kptFunctions ,
447+ ) ;
423448 updatedResourcesMap = updateKptfileInfo ( updatedResourcesMap ) ;
424449
425450 if ( updatedResourcesMap !== resourcesMap ) {
@@ -606,12 +631,21 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
606631 variant = "outlined"
607632 value = { kptfileState . name }
608633 name = "name"
609- onChange = { e =>
634+ onChange = { e => {
635+ const newPackageName = e . target . value ;
636+
610637 setKptfileState ( s => ( {
611638 ...s ,
612- name : e . target . value ,
613- } ) )
614- }
639+ name : newPackageName ,
640+ } ) ) ;
641+
642+ if ( ! sourcePackageRevision ) {
643+ setNamespaceState ( s => ( {
644+ ...s ,
645+ namespace : newPackageName ,
646+ } ) ) ;
647+ }
648+ } }
615649 fullWidth
616650 helperText = { `The name of the ${ targetRepositoryPackageDescriptorLowercase } to create.` }
617651 />
@@ -654,6 +688,94 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
654688 </ div >
655689 </ SimpleStepperStep >
656690
691+ < SimpleStepperStep title = "Namespace" >
692+ < div className = { classes . stepContent } >
693+ { namespaceState . advancedConfiguration && (
694+ < Alert severity = "info" icon = { false } >
695+ The namespace is configured using advanced settings on the{ ' ' }
696+ { sourcePackageRevision ?. spec . packageName } { ' ' }
697+ { sourceRepositoryPackageDescriptorLowercase } and can only be
698+ updated after the { targetRepositoryPackageDescriptorLowercase } { ' ' }
699+ is created.
700+ </ Alert >
701+ ) }
702+
703+ { ! namespaceState . advancedConfiguration && (
704+ < Fragment >
705+ < Checkbox
706+ label = "Set the same namespace for all namespace scoped resources"
707+ checked = { namespaceState . setNamespace }
708+ onChange = { isChecked =>
709+ setNamespaceState ( s => ( {
710+ ...s ,
711+ setNamespace : isChecked ,
712+ } ) )
713+ }
714+ helperText = "This ensures that all resources have the namespace that can be easily changed in a single place. The namespace can either be static or set to the name of a deployment when a deployment instance is created."
715+ />
716+
717+ { namespaceState . setNamespace && (
718+ < div className = { classes . checkboxConditionalElements } >
719+ { ! sourcePackageRevision && (
720+ < Checkbox
721+ label = { `Add namespace resource to the ${ targetRepositoryPackageDescriptorLowercase } ` }
722+ checked = { namespaceState . createNamespace }
723+ onChange = { isChecked =>
724+ setNamespaceState ( s => ( {
725+ ...s ,
726+ createNamespace : isChecked ,
727+ } ) )
728+ }
729+ helperText = { `If checked, a namespace resource will be added to the ${ targetRepositoryPackageDescriptorLowercase } .` }
730+ />
731+ ) }
732+
733+ < Select
734+ label = "Namespace Option"
735+ onChange = { value =>
736+ setNamespaceState ( s => ( {
737+ ...s ,
738+ namespaceOption : value ,
739+ namespace : s . namespace || kptfileState . name ,
740+ } ) )
741+ }
742+ selected = { namespaceState . namespaceOption }
743+ items = { [
744+ {
745+ label : 'Set specific namespace' ,
746+ value : 'user-defined' ,
747+ } ,
748+ {
749+ label :
750+ 'Set namespace to the name of the deployment instance' ,
751+ value : 'deployment' ,
752+ } ,
753+ ] }
754+ helperText = "The logic to set the name of the namespace."
755+ />
756+
757+ { namespaceState . namespaceOption === 'user-defined' && (
758+ < TextField
759+ label = "Namespace"
760+ variant = "outlined"
761+ value = { namespaceState . namespace }
762+ onChange = { e =>
763+ setNamespaceState ( s => ( {
764+ ...s ,
765+ namespace : e . target . value ,
766+ } ) )
767+ }
768+ fullWidth
769+ helperText = "The namespace all namespace scoped resources will be set to."
770+ />
771+ ) }
772+ </ div >
773+ ) }
774+ </ Fragment >
775+ ) }
776+ </ div >
777+ </ SimpleStepperStep >
778+
657779 < SimpleStepperStep title = "Validate Resources" >
658780 < div className = { classes . stepContent } >
659781 < Checkbox
0 commit comments