Skip to content

Commit

Permalink
Fixes multiple namespaces bug (#276)
Browse files Browse the repository at this point in the history
* fix ns bug

* add tests

* rename some variables

* rename ns to namespace

* fix delete + correctly type

* add typing to input obj parser
  • Loading branch information
OliverMKing authored Feb 6, 2023
1 parent 756cc0a commit ecb4882
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 81 deletions.
4 changes: 3 additions & 1 deletion src/actions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export async function deploy(
for (const ingressResource of ingressResources) {
await kubectl.getResource(
KubernetesConstants.DiscoveryAndLoadBalancerResource.INGRESS,
ingressResource.name
ingressResource.name,
false,
ingressResource.namespace
)
}
core.endGroup()
Expand Down
8 changes: 5 additions & 3 deletions src/strategyHelpers/blueGreen/blueGreenHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export async function deleteGreenObjects(
const resourcesToDelete: K8sDeleteObject[] = toDelete.map((obj) => {
return {
name: getBlueGreenResourceName(obj.metadata.name, GREEN_SUFFIX),
kind: obj.kind
kind: obj.kind,
namespace: obj.metadata.namespace
}
})

Expand Down Expand Up @@ -234,9 +235,10 @@ export function isServiceSelectorSubsetOfMatchLabel(
export async function fetchResource(
kubectl: Kubectl,
kind: string,
name: string
name: string,
namespace?: string
): Promise<K8sObject> {
const result = await kubectl.getResource(kind, name)
const result = await kubectl.getResource(kind, name, false, namespace)
if (result == null || !!result.stderr) {
return null
}
Expand Down
3 changes: 2 additions & 1 deletion src/strategyHelpers/blueGreen/ingressBlueGreenHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export async function validateIngresses(
const existingIngress = await fetchResource(
kubectl,
inputObject.kind,
inputObject.metadata.name
inputObject.metadata.name,
inputObject?.metadata?.namespace
)

const isValid =
Expand Down
3 changes: 2 additions & 1 deletion src/strategyHelpers/blueGreen/serviceBlueGreenHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export async function validateServicesState(
const existingService = await fetchResource(
kubectl,
serviceObject.kind,
serviceObject.metadata.name
serviceObject.metadata.name,
serviceObject?.metadata?.namespace
)

let isServiceGreen =
Expand Down
6 changes: 4 additions & 2 deletions src/strategyHelpers/blueGreen/smiBlueGreenHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export async function validateTrafficSplitsState(
let trafficSplitObject = await fetchResource(
kubectl,
TRAFFIC_SPLIT_OBJECT,
getBlueGreenResourceName(name, TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX)
getBlueGreenResourceName(name, TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX),
serviceObject?.metadata?.namespace
)
core.debug(
`ts object extracted was ${JSON.stringify(trafficSplitObject)}`
Expand Down Expand Up @@ -183,7 +184,8 @@ export async function cleanupSMI(
serviceObject.metadata.name,
GREEN_SUFFIX
),
kind: serviceObject.kind
kind: serviceObject.kind,
namespace: serviceObject?.metadata?.namespace
})
})

Expand Down
13 changes: 9 additions & 4 deletions src/strategyHelpers/canary/canaryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,13 @@ async function cleanUpCanary(
files: string[],
includeServices: boolean
): Promise<string[]> {
const deleteObject = async function (kind, name) {
const deleteObject = async function (
kind: string,
name: string,
namespace: string | undefined
) {
try {
const result = await kubectl.delete([kind, name])
const result = await kubectl.delete([kind, name], namespace)
checkForErrors([result])
} catch (ex) {
// Ignore failures of delete if it doesn't exist
Expand All @@ -213,6 +217,7 @@ async function cleanUpCanary(
for (const inputObject of parsedYaml) {
const name = inputObject.metadata.name
const kind = inputObject.kind
const namespace: string | undefined = inputObject?.metadata?.namespace

if (
isDeploymentEntity(kind) ||
Expand All @@ -222,8 +227,8 @@ async function cleanUpCanary(
const canaryObjectName = getCanaryResourceName(name)
const baselineObjectName = getBaselineResourceName(name)

await deleteObject(kind, canaryObjectName)
await deleteObject(kind, baselineObjectName)
await deleteObject(kind, canaryObjectName, namespace)
await deleteObject(kind, baselineObjectName, namespace)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/strategyHelpers/deploymentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ async function annotateResources(
)
if (annotateNamespace) {
annotateResults.push(
await kubectl.annotate('namespace', namespace, annotationKeyValStr)
await kubectl.annotate(
'namespace',
namespace,
annotationKeyValStr,
namespace
)
)
}
for (const file of files) {
Expand All @@ -243,6 +248,7 @@ async function annotateResources(
kubectl,
resource.type,
resource.name,
resource.namespace,
annotationKeyValStr,
allPods
)
Expand Down
2 changes: 2 additions & 0 deletions src/types/k8sObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface K8sObject {
metadata: {
name: string
labels: Map<string, string>
namespace?: string
}
kind: string
spec: any
Expand All @@ -16,6 +17,7 @@ export interface K8sServiceObject extends K8sObject {
export interface K8sDeleteObject {
name: string
kind: string
namespace?: string
}

export interface K8sIngress extends K8sObject {
Expand Down
Loading

0 comments on commit ecb4882

Please sign in to comment.