From 371187c61d6f7f2fc9c95b9e52a13c316320cf5a Mon Sep 17 00:00:00 2001 From: Paul Rostorp Date: Tue, 1 Feb 2022 01:56:31 +0100 Subject: [PATCH] feat: default to PullZone namespace if storageZoneRef does not provide namespace --- manifests/crds/PullZone.yaml | 1 - src/managePullZone.ts | 19 ++++++++++--------- src/types.ts | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/manifests/crds/PullZone.yaml b/manifests/crds/PullZone.yaml index 12fa010..15a3416 100644 --- a/manifests/crds/PullZone.yaml +++ b/manifests/crds/PullZone.yaml @@ -33,7 +33,6 @@ spec: type: string required: - "name" - - "namespace" zoneType: type: string pattern: "^(premium|volume)" diff --git a/src/managePullZone.ts b/src/managePullZone.ts index 5c7fd58..160f604 100644 --- a/src/managePullZone.ts +++ b/src/managePullZone.ts @@ -3,7 +3,7 @@ import axios, { AxiosResponse } from "axios"; import { logger } from "./logger"; import { getStorageZoneCrStatusId } from "./manageStorageZone"; import { bunnyAPIHeaders } from "./operator"; -import { PullZone, PullZoneSpec } from "./types"; +import { PullZone } from "./types"; import { backOff } from "exponential-backoff"; import { StorageZoneNotReadyError } from "./utils/misc"; import { createK8Secret } from "./utils/k8Secret"; @@ -61,11 +61,12 @@ const getOrCreatePullZone = async (config: ICreatePullZoneProps): Promise => { + const { spec, metadata } = object; if (spec.storageZoneRef) { - const { name, namespace } = spec.storageZoneRef; + const { name, namespace = metadata.namespace } = spec.storageZoneRef; const id = await backOff(() => getStorageZoneCrStatusId(name, namespace, customObjectsAPIClient), { retry: (e, attempt) => { @@ -100,13 +101,13 @@ const updatePullZone = async (id: number, config: IUpdatePullZoneProps): Promise }; const getOrCreatePullZoneConfig = async ( - name: string, - spec: PullZoneSpec, + object: PullZone, customObjectsAPIClient: CustomObjectsApi ): Promise<{ createConfig: ICreatePullZoneProps; updateConfig: IUpdatePullZoneProps }> => { - const originConfig = await getOriginConfig(spec, customObjectsAPIClient); + const { spec, metadata } = object; + const originConfig = await getOriginConfig(object, customObjectsAPIClient); const createConfig: ICreatePullZoneProps = { - Name: name, + Name: metadata.name, Type: spec.zoneType.trim() == "premium" ? 0 : 1, ...originConfig, }; @@ -131,8 +132,8 @@ export const handlePullZoneModification = async ( k8sApiClient: CoreV1Api ): Promise => { try { - const { metadata, spec } = object; - const { createConfig, updateConfig } = await getOrCreatePullZoneConfig(metadata.name, spec, customObjectsAPIClient); + const { metadata } = object; + const { createConfig, updateConfig } = await getOrCreatePullZoneConfig(object, customObjectsAPIClient); const { Id } = await getOrCreatePullZone(createConfig); const zone = await updatePullZone(Id, updateConfig); await createK8Secret( diff --git a/src/types.ts b/src/types.ts index 97d5c91..15e0565 100644 --- a/src/types.ts +++ b/src/types.ts @@ -58,7 +58,7 @@ export interface PullZoneSpec { storageZoneId?: number; storageZoneRef?: { name: string; - namespace: string; + namespace?: string; }; zoneType: "premium" | "volume"; // defaults to volume zoneSecurityEnabled: boolean; // defaults to true