Skip to content

Commit

Permalink
fix(k8s): unhelpful error with conflicting namespace
Browse files Browse the repository at this point in the history
Cleaned up our annotations code slightly along the way. The actual fix is
in `garden-service/src/plugins/kubernetes/system.ts`, a rather simple one.

Fixes #818
  • Loading branch information
edvald committed Jun 11, 2019
1 parent d9473cc commit a116120
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
3 changes: 0 additions & 3 deletions garden-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export const GARDEN_VERSIONFILE_NAME = ".garden-version"
export const DEFAULT_PORT_PROTOCOL = "TCP"

export const DEFAULT_API_VERSION = "garden.io/v0"
export const GARDEN_ANNOTATION_PREFIX = "garden.io/"
export const GARDEN_ANNOTATION_KEYS_SERVICE = GARDEN_ANNOTATION_PREFIX + "service"
export const GARDEN_ANNOTATION_KEYS_VERSION = GARDEN_ANNOTATION_PREFIX + "version"

export const DEFAULT_TEST_TIMEOUT = 60 * 1000

Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/plugins/google/google-cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Module } from "../../types/module"
import { ServiceState, ServiceStatus, ingressHostnameSchema, Service } from "../../types/service"
import { resolve } from "path"
import * as Joi from "joi"
import { GARDEN_ANNOTATION_KEYS_VERSION } from "../../constants"
import { ExecTestSpec, execTestSchema } from "../exec"
import {
prepareEnvironment,
Expand All @@ -26,6 +25,7 @@ import { ConfigureModuleParams, ConfigureModuleResult } from "../../types/plugin
import { DeployServiceParams } from "../../types/plugin/service/deployService"
import { GetServiceStatusParams } from "../../types/plugin/service/getServiceStatus"
import { ServiceLimitSpec } from "../container/config"
import { gardenAnnotationKey } from "../../util/string"

const gcfModuleSpecSchema = baseServiceSpecSchema
.keys({
Expand Down Expand Up @@ -154,7 +154,7 @@ export async function getServiceStatus(
return {
providerId,
providerVersion: status.versionId,
version: status.labels[GARDEN_ANNOTATION_KEYS_VERSION],
version: status.labels[gardenAnnotationKey("version")],
state,
updatedAt: status.updateTime,
detail: status,
Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { waitForResources } from "../status"
import { apply, deleteObjectsByLabel } from "../kubectl"
import { getAppNamespace } from "../namespace"
import { PluginContext } from "../../../plugin-context"
import { GARDEN_ANNOTATION_KEYS_VERSION } from "../../../constants"
import { KubeApi } from "../api"
import { KubernetesProvider, KubernetesPluginContext } from "../config"
import { configureHotReload } from "../hot-reload"
Expand All @@ -27,6 +26,7 @@ import { LogEntry } from "../../../logger/log-entry"
import { DeployServiceParams } from "../../../types/plugin/service/deployService"
import { DeleteServiceParams } from "../../../types/plugin/service/deleteService"
import { millicpuToString, kilobytesToString } from "../util"
import { gardenAnnotationKey } from "../../../util/string"

export const DEFAULT_CPU_REQUEST = "10m"
export const DEFAULT_MEMORY_REQUEST = "64Mi"
Expand Down Expand Up @@ -74,8 +74,8 @@ export async function createContainerObjects(
const objects = [deployment, ...kubeservices, ...ingresses]

return objects.map(obj => {
set(obj, ["metadata", "annotations", "garden.io/generated"], "true")
set(obj, ["metadata", "annotations", GARDEN_ANNOTATION_KEYS_VERSION], version.versionString)
set(obj, ["metadata", "annotations", gardenAnnotationKey("generated")], "true")
set(obj, ["metadata", "annotations", gardenAnnotationKey("version")], version.versionString)
set(obj, ["metadata", "labels", "module"], service.module.name)
set(obj, ["metadata", "labels", "service"], service.name)
return obj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { getNamespace, getAppNamespace } from "../namespace"
import { KubernetesPluginContext } from "../config"
import { KubernetesResource } from "../types"
import { ServiceStatus } from "../../../types/service"
import { GARDEN_ANNOTATION_KEYS_SERVICE } from "../../../constants"
import { compareDeployedObjects, waitForResources } from "../status"
import { KubeApi } from "../api"
import { ModuleAndRuntimeActions } from "../../../types/plugin/plugin"
Expand All @@ -28,6 +27,7 @@ import { GetServiceStatusParams } from "../../../types/plugin/service/getService
import { DeployServiceParams } from "../../../types/plugin/service/deployService"
import { DeleteServiceParams } from "../../../types/plugin/service/deleteService"
import { GetServiceLogsParams } from "../../../types/plugin/service/getServiceLogs"
import { gardenAnnotationKey } from "../../../util/string"

export const kubernetesHandlers: Partial<ModuleAndRuntimeActions<KubernetesModule>> = {
build,
Expand Down Expand Up @@ -111,7 +111,7 @@ async function deleteService(params: DeleteServiceParams): Promise<ServiceStatus
log,
context,
namespace,
labelKey: GARDEN_ANNOTATION_KEYS_SERVICE,
labelKey: gardenAnnotationKey("service"),
labelValue: service.name,
objectTypes: uniq(manifests.map(m => m.kind)),
includeUninitialized: false,
Expand All @@ -131,7 +131,7 @@ async function getServiceLogs(params: GetServiceLogsParams<KubernetesModule>) {
}

function getSelector(service: KubernetesService) {
return `${GARDEN_ANNOTATION_KEYS_SERVICE}=${service.name}`
return `${gardenAnnotationKey("service")}=${service.name}`
}

async function getManifests(module: KubernetesModule): Promise<KubernetesResource[]> {
Expand All @@ -144,8 +144,8 @@ async function getManifests(module: KubernetesModule): Promise<KubernetesResourc

// Add a label, so that we can identify the manifests as part of this module, and prune if needed
return manifests.map(manifest => {
set(manifest, ["metadata", "annotations", GARDEN_ANNOTATION_KEYS_SERVICE], module.name)
set(manifest, ["metadata", "labels", GARDEN_ANNOTATION_KEYS_SERVICE], module.name)
set(manifest, ["metadata", "annotations", gardenAnnotationKey("service")], module.name)
set(manifest, ["metadata", "labels", gardenAnnotationKey("service")], module.name)
return manifest
})
}
5 changes: 3 additions & 2 deletions garden-service/src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { GetEnvironmentStatusParams } from "../../types/plugin/provider/getEnvir
import { kubectl, KUBECTL_DEFAULT_TIMEOUT } from "./kubectl"
import { LogEntry } from "../../logger/log-entry"
import { ConfigStore } from "../../config-store"
import { gardenAnnotationKey } from "../../util/string"

const GARDEN_VERSION = getPackageVersion()
type CreateNamespaceStatus = "pending" | "created"
Expand Down Expand Up @@ -52,8 +53,8 @@ export async function createNamespace(api: KubeApi, namespace: string) {
metadata: {
name: namespace,
annotations: {
"garden.io/generated": "true",
"garden.io/version": GARDEN_VERSION,
[gardenAnnotationKey("generated")]: "true",
[gardenAnnotationKey("version")]: GARDEN_VERSION,
},
},
})
Expand Down
5 changes: 3 additions & 2 deletions garden-service/src/plugins/kubernetes/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { LogEntry } from "../../logger/log-entry"
import { KubeApi } from "./api"
import { createNamespace } from "./namespace"
import { getPackageVersion } from "../../util/util"
import { deline } from "../../util/string"
import { deline, gardenAnnotationKey } from "../../util/string"
import { deleteNamespaces } from "./namespace"
import { PluginError } from "../../exceptions"
import { DashboardPage } from "../../config/dashboard"
Expand Down Expand Up @@ -84,7 +84,8 @@ export async function systemNamespaceUpToDate(
}
}

const versionInCluster = namespaceResource.metadata.annotations["garden.io/version"]
const annotations = namespaceResource.metadata.annotations || {}
const versionInCluster = annotations[gardenAnnotationKey("version")]

const upToDate = !!versionInCluster && semver.gte(semver.coerce(versionInCluster)!, SYSTEM_NAMESPACE_MIN_VERSION)

Expand Down
8 changes: 8 additions & 0 deletions garden-service/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ import _deline = require("deline")
// the import syntax, and it for some reason doesn't play nice with IDEs).
export const dedent = _dedent
export const deline = _deline

const gardenAnnotationPrefix = "garden.io/"

export type GardenAnnotationKey = "generated" | "service" | "version"

export function gardenAnnotationKey(key: GardenAnnotationKey) {
return gardenAnnotationPrefix + key
}

0 comments on commit a116120

Please sign in to comment.