Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app: home: Add delete button for clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent T <vtaylor@microsoft.com>
vyncent-t committed Nov 14, 2024
1 parent d24fc9f commit 59d4961
Showing 3 changed files with 75 additions and 24 deletions.
14 changes: 14 additions & 0 deletions backend/cmd/headlamp.go
Original file line number Diff line number Diff line change
@@ -1384,6 +1384,20 @@ func (c *HeadlampConfig) deleteCluster(w http.ResponseWriter, r *http.Request) {
return
}

alsoRemoveFromKubeConfig := true

if alsoRemoveFromKubeConfig {

Check failure on line 1389 in backend/cmd/headlamp.go

GitHub Actions / build

File is not `gofmt`-ed with `-s` (gofmt)
// delete context from actual deafult kubecofig file

Check failure on line 1390 in backend/cmd/headlamp.go

GitHub Actions / build

`deafult` is a misspelling of `default` (misspell)
// to do : replace the hard coding of this line with an actual path
err = kubeconfig.RemoveContextFromFile(name, filepath.Join("/home/vynty/.kube/config"))

Check failure on line 1392 in backend/cmd/headlamp.go

GitHub Actions / build

badCall: suspicious Join on 1 argument (gocritic)
if err != nil {
logger.Log(logger.LevelError, map[string]string{"cluster": name},
err, "my cool error")
http.Error(w, "removing cluster from kubeconfig", http.StatusInternalServerError)
return

Check failure on line 1397 in backend/cmd/headlamp.go

GitHub Actions / build

return statements should not be cuddled if block has more than two lines (wsl)
}
}

kubeConfigPersistenceFile, err := defaultKubeConfigPersistenceFile()
if err != nil {
logger.Log(logger.LevelError, map[string]string{"cluster": name},
83 changes: 60 additions & 23 deletions frontend/src/components/App/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Icon } from '@iconify/react';
import { useTheme } from '@mui/material';
import { Checkbox, useTheme } from '@mui/material';
import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import ListItemText from '@mui/material/ListItemText';
@@ -25,13 +25,32 @@ import { ConfirmDialog } from '../../common';
import ResourceTable from '../../common/Resource/ResourceTable';
import RecentClusters from './RecentClusters';

/**
* Gets the origin of a cluster.
*
* @param cluster
* @returns A description of where the cluster is picked up from: dynamic, in-cluster, or from a kubeconfig file.
*/
function getOrigin(cluster: Cluster): string {
if (cluster.meta_data?.source === 'kubeconfig') {
const kubeconfigPath = process.env.KUBECONFIG ?? '~/.kube/config';
return `Kubeconfig: ${kubeconfigPath}`;
} else if (cluster.meta_data?.source === 'dynamic_cluster') {
return t('translation|Plugin');
} else if (cluster.meta_data?.source === 'in_cluster') {
return t('translation|In-cluster');
}
return 'Unknown';
}

function ContextMenu({ cluster }: { cluster: Cluster }) {
const { t } = useTranslation(['translation']);
const history = useHistory();
const dispatch = useDispatch();
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const menuId = useId('context-menu');
const [openConfirmDialog, setOpenConfirmDialog] = React.useState(false);
const [openDeleteDynamicDialog, setOpenDeleteDynamicDialog] = React.useState(false);

function removeCluster(cluster: Cluster) {
deleteCluster(cluster.name || '')
@@ -92,7 +111,8 @@ function ContextMenu({ cluster }: { cluster: Cluster }) {
>
<ListItemText>{t('translation|Settings')}</ListItemText>
</MenuItem>
{helpers.isElectron() && cluster.meta_data?.source === 'dynamic_cluster' && (

{helpers.isElectron() && (
<MenuItem
onClick={() => {
setOpenConfirmDialog(true);
@@ -108,17 +128,52 @@ function ContextMenu({ cluster }: { cluster: Cluster }) {
open={openConfirmDialog}
handleClose={() => setOpenConfirmDialog(false)}
onConfirm={() => {
setOpenConfirmDialog(false);
removeCluster(cluster);
if (cluster.meta_data?.source !== 'dynamic_cluster') {
setOpenDeleteDynamicDialog(true);
} else {
setOpenConfirmDialog(false);
removeCluster(cluster);
}
}}
title={t('translation|Delete Cluster')}
description={t(
'translation|Are you sure you want to remove the cluster "{{ clusterName }}"?',
'translation|Are you sure you want to remove the cluster "{{ clusterName }}"? from {{ source }}',
{
clusterName: cluster.name,
source: getOrigin(cluster),
}
)}
/>

<ConfirmDialog
open={openDeleteDynamicDialog}
handleClose={() => setOpenDeleteDynamicDialog(false)}
onConfirm={() => {
setOpenDeleteDynamicDialog(false);
removeCluster(cluster);
}}
title={t('translation|Delete Cluster')}
description={
<>
{t(
'translation|The cluster "{{ clusterName }}" is not a dynamic cluster from Headlamp, this cluster will be deleted from {{ source }}.',
{
clusterName: cluster.name,
source: getOrigin(cluster),
}
)}

<>
<Typography>Accept</Typography>
<Checkbox
checked={false}
onChange={() => {}}
inputProps={{ 'aria-label': 'primary checkbox' }}
/>
</>
</>
}
/>
</>
);
}
@@ -239,24 +294,6 @@ function HomeComponent(props: HomeComponentProps) {
.sort();
}

/**
* Gets the origin of a cluster.
*
* @param cluster
* @returns A description of where the cluster is picked up from: dynamic, in-cluster, or from a kubeconfig file.
*/
function getOrigin(cluster: Cluster): string {
if (cluster.meta_data?.source === 'kubeconfig') {
const kubeconfigPath = process.env.KUBECONFIG ?? '~/.kube/config';
return `Kubeconfig: ${kubeconfigPath}`;
} else if (cluster.meta_data?.source === 'dynamic_cluster') {
return t('translation|Plugin');
} else if (cluster.meta_data?.source === 'in_cluster') {
return t('translation|In-cluster');
}
return 'Unknown';
}

const memoizedComponent = React.useMemo(
() => (
<PageGrid>
2 changes: 1 addition & 1 deletion frontend/src/components/common/ConfirmDialog.tsx
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import { DialogTitle } from './Dialog';

export interface ConfirmDialogProps extends MuiDialogProps {
title: string;
description: string;
description: string | JSX.Element;
onConfirm: () => void;
handleClose: () => void;
}

0 comments on commit 59d4961

Please sign in to comment.