Skip to content

Commit

Permalink
fix: make context and kubeconfigPath nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Apr 17, 2023
1 parent 116586e commit d33c85a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useTargetClusterNamespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function useTargetClusterNamespaces(): [string[], Dispatch<SetStateAction

useEffect(() => {
const setClusterNamespaces = async () => {
if (!kubeConfigPath.trim().length) {
if (!kubeConfigPath?.trim().length) {
setNamespaces([]);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/redux/appConfig/appConfig.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const kubeConfigPathSelector = createSelector(
(state: RootState) => state.config.projectConfig?.kubeConfig?.path,
(state: RootState) => state.config.kubeConfig.path,
],
(projectKubeConfigPath, kubeConfigPath) => projectKubeConfigPath || kubeConfigPath || ''
(projectKubeConfigPath, kubeConfigPath) => projectKubeConfigPath ?? kubeConfigPath ?? undefined
);

export const currentConfigSelector = createDeepEqualSelector(
Expand Down
4 changes: 2 additions & 2 deletions src/redux/services/applyFileWithConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function applyFileWithConfirm(
selectedPath: string,
fileMap: FileMapType,
dispatch: ThunkDispatch<any, any, any>,
kubeconfig: string,
context: string
kubeconfig: string | undefined,
context: string | undefined
) {
const title = `Deploy ${fileMap[selectedPath].name} to cluster [${context}]?`;

Expand Down
4 changes: 2 additions & 2 deletions src/redux/services/restartDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {execute} from '@shared/utils/commands';
type RestartDeploymentOptions = {
name: string;
namespace: string;
currentContext: string;
kubeConfigPath: string;
currentContext?: string;
kubeConfigPath?: string;
};

async function restartDeployment(options: RestartDeploymentOptions) {
Expand Down
4 changes: 2 additions & 2 deletions src/redux/services/scaleDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type ScaleDeploymentOptions = {
replicas: number;
name: string;
namespace: string;
currentContext: string;
kubeConfigPath: string;
currentContext?: string;
kubeConfigPath?: string;
};

async function scaleDeployment(options: ScaleDeploymentOptions) {
Expand Down
7 changes: 3 additions & 4 deletions src/redux/thunks/applyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {trackEvent} from '@shared/utils/telemetry';
/**
* Invokes kubectl for the content of the specified resource
*/

function applyFileToCluster(filePath: string, kubeconfig: string, context: string) {
function applyFileToCluster(filePath: string, kubeconfig?: string, context?: string) {
return applyYamlToCluster({
yaml: fs.readFileSync(filePath, 'utf8'),
kubeconfig,
Expand All @@ -35,8 +34,8 @@ export async function applyFile(
filePath: string,
fileMap: FileMapType,
dispatch: AppDispatch,
kubeconfig: string,
context: string
kubeconfig?: string,
context?: string
) {
dispatch(setApplyingResource(true));

Expand Down
19 changes: 6 additions & 13 deletions src/redux/thunks/applyHelmChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,16 @@ function applyHelmChartToCluster(
valuesFile: HelmValuesFile,
helmChart: HelmChart,
fileMap: FileMapType,
kubeconfig: string,
context: string,
kubeconfig: string | undefined,
context: string | undefined,
namespace?: string,
shouldCreateNamespace?: boolean
) {
trackEvent('cluster/deploy_helm_chart');
const chartPath = path.dirname(getAbsoluteHelmChartPath(helmChart, fileMap));

let args = [
'install',
'-f',
getAbsoluteValuesFilePath(valuesFile, fileMap),
helmChart.name,
chartPath,
'--kube-context',
context,
];
let args = ['install', '-f', getAbsoluteValuesFilePath(valuesFile, fileMap), helmChart.name, chartPath];
if (context) args.push('--kube-context', context);

if (namespace) {
args.push(...['--namespace', namespace]);
Expand Down Expand Up @@ -68,8 +61,8 @@ export async function applyHelmChart(
helmChart: HelmChart,
fileMap: FileMapType,
dispatch: AppDispatch,
kubeconfig: string,
context: string,
kubeconfig: string | undefined,
context: string | undefined,
namespace?: string,
shouldCreateNamespace?: boolean
) {
Expand Down
5 changes: 3 additions & 2 deletions src/redux/thunks/applyYaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {runCommandInMainThread} from '@shared/utils/commands';

interface ApplyToClusterParams {
yaml: string;
context: string;
context?: string;
kubeconfig?: string;
namespace?: {
name: string;
Expand All @@ -17,7 +17,8 @@ interface ApplyToClusterParams {
}

export function applyYamlToCluster({yaml, context, namespace, kubeconfig}: ApplyToClusterParams) {
const kubectlArgs = ['--context', context, 'apply', '-f', '-'];
const globalFlags = context ? ['--context', context] : [];
const kubectlArgs = [...globalFlags, 'apply', '-f', '-'];

if (namespace) {
kubectlArgs.unshift(...['--namespace', namespace.name]);
Expand Down

0 comments on commit d33c85a

Please sign in to comment.