Skip to content

Commit 8cfccaf

Browse files
authored
Merge pull request #213 from headlamp-k8s/minikube-three
minikube: Add default cluster name, and validation for existing cluster names
2 parents 885efd0 + 82e94bd commit 8cfccaf

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

minikube/src/CommandCluster/CommandDialog.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Loader } from '@kinvolk/headlamp-plugin/lib/CommonComponents';
2+
import { useClustersConf } from '@kinvolk/headlamp-plugin/lib/k8s';
23
import { Card } from '@mui/material';
34
import { Typography } from '@mui/material';
45
import Box from '@mui/material/Box';
@@ -60,8 +61,30 @@ export default function CommandDialog({
6061
}: CommandDialogProps) {
6162
const [clusterName, setClusterName] = React.useState(initialClusterName);
6263
const [driver, setDriver] = React.useState('');
64+
const [nameTaken, setNameTaken] = React.useState(false);
6365

6466
const history = useHistory();
67+
const clusters = useClustersConf() || {};
68+
const clusterNames = Object.keys(clusters);
69+
70+
React.useEffect(() => {
71+
if (!initialClusterName) {
72+
setClusterName(generateClusterName(clusterNames));
73+
}
74+
}, [initialClusterName, clusterNames]);
75+
76+
function generateClusterName(existingNames: string[]): string {
77+
const baseName = 'minikube';
78+
let newName = baseName;
79+
let counter = 1;
80+
81+
while (existingNames.includes(newName)) {
82+
newName = `${baseName}-${counter}`;
83+
counter++;
84+
}
85+
86+
return newName;
87+
}
6588

6689
if (acting && open && !running) {
6790
if (askClusterName) {
@@ -88,9 +111,13 @@ export default function CommandDialog({
88111
label="Cluster Name"
89112
value={clusterName}
90113
onChange={function handleNameChange(event: React.ChangeEvent<HTMLInputElement>) {
91-
setClusterName(event.target.value);
114+
const name = event.target.value;
115+
setClusterName(name);
116+
setNameTaken(clusterNames.includes(name));
92117
}}
93118
variant="outlined"
119+
error={nameTaken}
120+
helperText={nameTaken ? 'Cluster name is already taken' : ''}
94121
/>
95122
</Box>
96123
</FormControl>
@@ -123,6 +150,7 @@ export default function CommandDialog({
123150
}}
124151
variant="contained"
125152
color="primary"
153+
disabled={nameTaken && askClusterName}
126154
>
127155
{`${command}`}
128156
</Button>

0 commit comments

Comments
 (0)