Skip to content

Commit

Permalink
fix(k8s): error with missing metadata field
Browse files Browse the repository at this point in the history
This should fix a user-reported error of

```
Cannot read property 'namespace' of undefined
```

which is most likely due to an assumption of a metadata field existing
on a resource.

There may be a further underlying error, but this should at the very
least reveal what that is.
  • Loading branch information
edvald committed Feb 24, 2021
1 parent 2a3fbae commit e2bbe0d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export class KubeApi {
}

if (!namespace) {
namespace = manifest.metadata.namespace
namespace = manifest.metadata?.namespace
}

if (!namespace) {
Expand Down
6 changes: 5 additions & 1 deletion core/src/plugins/kubernetes/kubernetes-module/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export async function getManifests({

return Bluebird.map(manifests, async (manifest) => {
// Ensure a namespace is set, if not already set, and if required by the resource type
if (!manifest.metadata.namespace) {
if (!manifest.metadata?.namespace) {
if (!manifest.metadata) {
manifest.metadata = {}
}

try {
const info = await api.getApiResourceInfo(log, manifest.apiVersion, manifest.kind)

Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function readLogsFromApi({

const logs = await getPodLogs({
api,
namespace: pod.metadata.namespace || defaultNamespace,
namespace: pod.metadata?.namespace || defaultNamespace,
pod,
lineLimit: tail === -1 ? undefined : tail,
timestamps: true,
Expand Down Expand Up @@ -137,7 +137,7 @@ async function followLogs({
}) {
const sternArgs = [
`--context=${provider.config.context}`,
`--namespace=${pod.metadata.namespace || defaultNamespace}`,
`--namespace=${pod.metadata?.namespace || defaultNamespace}`,
`--exclude-container=garden-*`,
"--tail",
String(tail),
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export async function getModuleNamespace({
return getNamespace({
log,
ctx,
override: module.spec.namespace ? { name: module.spec.namespace } : undefined,
override: module.spec?.namespace ? { name: module.spec.namespace } : undefined,
provider,
skipCreate,
})
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/status/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function getResourceEvents(api: KubeApi, resource: KubernetesResour
`involvedObject.kind=${resource.kind},` +
`involvedObject.name=${resource.metadata.name}`

const namespace = resource.metadata.namespace
const namespace = resource.metadata?.namespace

const res = namespace
? await api.core.listNamespacedEvent(namespace, undefined, undefined, undefined, fieldSelector)
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/status/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function waitForServiceEndpoints(
}

const serviceName = service.metadata.name
const serviceNamespace = service.metadata.namespace || namespace
const serviceNamespace = service.metadata?.namespace || namespace

const pods = await getPods(api, serviceNamespace, selector)
const readyPodNames = pods
Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/kubernetes/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export async function checkResourceStatus(
) {
const handler = objHandlers[manifest.kind]

if (manifest.metadata.namespace) {
if (manifest.metadata?.namespace) {
namespace = manifest.metadata.namespace
}

Expand Down Expand Up @@ -364,7 +364,7 @@ export async function compareDeployedResources(
manifest.apiVersion = deployedResource.apiVersion

// the namespace property is silently dropped when added to non-namespaced resources
if (manifest.metadata.namespace && deployedResource.metadata.namespace === undefined) {
if (manifest.metadata?.namespace && deployedResource.metadata?.namespace === undefined) {
delete manifest.metadata.namespace
}

Expand Down Expand Up @@ -420,7 +420,7 @@ export async function getDeployedResource(
log: LogEntry
): Promise<KubernetesResource | null> {
const api = await KubeApi.factory(log, ctx, provider)
const namespace = resource.metadata.namespace || (await getAppNamespace(ctx, log, provider))
const namespace = resource.metadata?.namespace || (await getAppNamespace(ctx, log, provider))

try {
const res = await api.readBySpec({ namespace, manifest: resource, log })
Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function getAllPods(
}

if (isWorkload(resource)) {
return getWorkloadPods(api, resource.metadata.namespace || defaultNamespace, <KubernetesWorkload>resource)
return getWorkloadPods(api, resource.metadata?.namespace || defaultNamespace, <KubernetesWorkload>resource)
}

return []
Expand Down Expand Up @@ -126,13 +126,13 @@ export async function getCurrentWorkloadPods(api: KubeApi, namespace: string, re
export async function getWorkloadPods(api: KubeApi, namespace: string, resource: KubernetesWorkload) {
// We don't match on the garden.io/version label because it can fall out of sync during hot reloads
const selector = omit(getSelectorFromResource(resource), gardenAnnotationKey("version"))
const pods = await getPods(api, resource.metadata.namespace || namespace, selector)
const pods = await getPods(api, resource.metadata?.namespace || namespace, selector)

if (resource.kind === "Deployment") {
// Make sure we only return the pods from the current ReplicaSet
const selectorString = labelSelectorToString(selector)
const replicaSetRes = await api.apps.listNamespacedReplicaSet(
resource.metadata.namespace || namespace,
resource.metadata?.namespace || namespace,
undefined, // pretty
undefined, // allowWatchBookmarks
undefined, // _continue
Expand Down

0 comments on commit e2bbe0d

Please sign in to comment.