From b4ff21c43fbd44e368eb6926f68c6a8271a9b149 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Wed, 11 Sep 2019 18:55:04 +0200 Subject: [PATCH] fix(k8s): helm returned deprecated manifest version for tiller Fixes #1166 --- .../src/plugins/kubernetes/helm/tiller.ts | 2 ++ garden-service/src/plugins/kubernetes/util.ts | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/garden-service/src/plugins/kubernetes/helm/tiller.ts b/garden-service/src/plugins/kubernetes/helm/tiller.ts index 52beb010df..43c34214f6 100644 --- a/garden-service/src/plugins/kubernetes/helm/tiller.ts +++ b/garden-service/src/plugins/kubernetes/helm/tiller.ts @@ -17,6 +17,7 @@ import { combineStates } from "../../../types/service" import { apply } from "../kubectl" import { KubernetesProvider, KubernetesPluginContext } from "../config" import chalk from "chalk" +import { convertDeprecatedManifestVersion } from "../util" const serviceAccountName = "garden-tiller" @@ -84,6 +85,7 @@ async function getTillerResources( }) const resources = safeLoadAll(tillerManifests) + .map(convertDeprecatedManifestVersion) return resources } diff --git a/garden-service/src/plugins/kubernetes/util.ts b/garden-service/src/plugins/kubernetes/util.ts index f273fb19a6..9d4d3b5dcf 100644 --- a/garden-service/src/plugins/kubernetes/util.ts +++ b/garden-service/src/plugins/kubernetes/util.ts @@ -288,3 +288,38 @@ export function prepareEnvVars(env: ContainerEnvVars): V1EnvVar[] { } }) } + +/** + * Makes sure a Kubernetes manifest has an up-to-date API version. + * See https://kubernetes.io/blog/2019/07/18/api-deprecations-in-1-16/ + * + * @param manifest any Kubernetes manifest + */ +export function convertDeprecatedManifestVersion(manifest: KubernetesResource): KubernetesResource { + const { apiVersion, kind } = manifest + + if (workloadTypes.includes(kind)) { + manifest.apiVersion = "apps/v1" + } else if (apiVersion === "extensions/v1beta1") { + switch (kind) { + case "NetworkPolicy": + manifest.apiVersion = "networking.k8s.io/v1" + break + + case "PodSecurityPolicy": + manifest.apiVersion = "policy/v1beta1" + break + } + } + + // apps/v1/Deployment requires spec.selector to be set + if (kind === "Deployment") { + if (manifest.spec && !manifest.spec.selector) { + manifest.spec.selector = { + ...{ matchLabels: manifest.spec.template.metadata.labels || manifest.metadata.labels }, + } + } + } + + return manifest +}