Skip to content

Commit

Permalink
fix: Helm supports complex values in values.yaml fragments (such as…
Browse files Browse the repository at this point in the history
… annotations or arrays)

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Oct 3, 2023
1 parent fa659af commit 4585ef9
Show file tree
Hide file tree
Showing 22 changed files with 508 additions and 164 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Usage:
* Fix #2397: Default JKube work directory (`jkube.workDir`) changed from `${project.build.directory}/jkube` to `${project.build.directory}/jkube-temp`
* Fix #2393: Remove timestamp from `org.label-schema.build-date` LABEL to utilize docker cache
* Fix #2399: Helm no longer generates default function; broadens support for different value types
* Fix #2400: Helm supports complex values in `values.yaml` fragments (such as annotations or arrays)

_**Note**_:
- Container Images generated using jkube opinionated defaults no longer contain full timestamp in `org.label-schema.build-date` label. The label contains the build date in the format `yyyy-MM-dd`.
Expand Down
67 changes: 67 additions & 0 deletions gradle-plugin/it/src/it/helm-complex-values/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
plugins {
id 'org.eclipse.jkube.kubernetes' version "${jKubeVersion}"
id 'org.eclipse.jkube.openshift' version "${jKubeVersion}"
id 'java'
}

group = 'org.eclipse.jkube.integration.tests.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

def extensionConfig = {
offline = true
images {
image {
name = 'repository/helm:latest'
build {
from = 'repository/from:latest'
ports = [8080]
}
}
}
helm {
parameters = [
{
name = 'deployment.annotations'
value = '''
{{- toYaml .Values.common.annotations | nindent 4 }}
{{- toYaml .Values.deployment.annotations | nindent 4 }}
'''
},
{
name = 'deployment.hostAliases'
value = '{{- toYaml .Values.deployment.hostAliases | nindent 8 }}'
},
{
name = 'service.annotations'
value = '''
{{- toYaml .Values.common.annotations | nindent 4 }}
{{- toYaml .Values.service.annotations | nindent 4 }}
'''
}, {
name = 'replicaCount'
value = 1
}]
}
}

kubernetes(extensionConfig)
openshift(extensionConfig)

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
labels:
app: helm-complex-values
provider: jkube
version: "@ignore@"
group: org.eclipse.jkube.integration.tests.gradle
name: helm-complex-values
annotations:
example.org/env: dev
example.org/kind: ServiceDev
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: helm-complex-values
provider: jkube
group: org.eclipse.jkube.integration.tests.gradle
- apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: helm-complex-values
provider: jkube
version: "@ignore@"
group: org.eclipse.jkube.integration.tests.gradle
name: helm-complex-values
annotations:
example.org/env: dev
example.org/kind: DeploymentDev
spec:
replicas: 1
revisionHistoryLimit: 2
selector:
matchLabels:
app: helm-complex-values
provider: jkube
group: org.eclipse.jkube.integration.tests.gradle
template:
metadata:
annotations: "@ignore@"
labels:
app: helm-complex-values
provider: jkube
version: "@ignore@"
group: org.eclipse.jkube.integration.tests.gradle
spec:
hostAliases:
- hostnames:
- dev.example.com
ip: 127.0.0.1
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: HOSTNAME
valueFrom:
fieldRef:
fieldPath: metadata.name
image: repository/helm:latest
imagePullPolicy: IfNotPresent
name: repository-helm-complex-values
ports:
- containerPort: 8080
name: http
protocol: TCP
securityContext:
privileged: false
20 changes: 20 additions & 0 deletions gradle-plugin/it/src/it/helm-complex-values/expected/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
replicaCount: 1
common:
annotations:
example.org/one: one
example.org/two: two
example.org/env: prod
deployment:
annotations:
example.org/kind: Deployment
hostAliases:
- ip: 1.1.1.1
hostnames:
- one.example.com
- ip: 1.1.1.2
hostnames:
- two.example.com
service:
annotations:
example.org/kind: Service
18 changes: 18 additions & 0 deletions gradle-plugin/it/src/it/helm-complex-values/gradle.dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

replicaCount = 1
deployment.annotations = example.org/env: dev\n example.org/kind: DeploymentDev
deployment.hostAliases = - ip: 127.0.0.1\n hostnames: ['dev.example.com']
service.annotations = example.org/env: dev\n example.org/kind: ServiceDev
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

metadata:
annotations:
${deployment.annotations}
spec:
replicas: ${replicaCount}
template:
spec:
hostAliases:
${deployment.hostAliases}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

metadata:
annotations:
${service.annotations}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

common:
annotations:
example.org/one: one
example.org/two: two
example.org/env: prod

deployment:
annotations:
example.org/kind: Deployment
hostAliases:
- ip: 1.1.1.1
hostnames:
- one.example.com
- ip: 1.1.1.2
hostnames:
- two.example.com
service:
annotations:
example.org/kind: Service
3 changes: 3 additions & 0 deletions gradle-plugin/it/src/it/helm-fragment-and-dsl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def extensionConfig = {
}]
icon = 'https://example.com/icon-is-overridden'
parameters = [{
name = 'annotations'
value = '{{- toYaml .Values.annotations | nindent 4 }}'
}, {
name = 'replicaCount'
value = 1
}, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# Red Hat, Inc. - initial API and implementation
#

metadata:
annotations:
${annotations}
spec:
replicas: ${replicaCount}
template:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
# Red Hat, Inc. - initial API and implementation
#

annotations:
example.org/one: one
example.org/two: two

deployment:
imagePullPolicy: Always
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.tests;

import org.eclipse.jkube.kit.common.ResourceVerify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.nio.file.Files;

import static org.assertj.core.api.Assertions.assertThat;

class HelmComplexValuesIT {

@RegisterExtension
public final ITGradleRunnerExtension gradleRunner = new ITGradleRunnerExtension();

@BeforeEach
void setUp() {
gradleRunner.withITProject("helm-complex-values");
}
@Test
void outerLoop() throws Exception {
// When
gradleRunner.withArguments("clean", "k8sResource", "k8sHelm").build();
// Then
ResourceVerify.verifyResourceDescriptors(
gradleRunner.resolveFile("build", "jkube", "helm", "helm-complex-values", "kubernetes", "values.yaml"),
gradleRunner.resolveFile("expected", "values.yaml"));
assertThat(ResourceVerify.readFile(gradleRunner.resolveFile(
"build", "jkube", "helm", "helm-complex-values", "kubernetes", "templates", "helm-complex-values-deployment.yaml")))
.contains("annotations: {{- toYaml .Values.common.annotations | nindent 4 }}\n" +
" {{- toYaml .Values.deployment.annotations | nindent 4 }}")
.contains("hostAliases: {{- toYaml .Values.deployment.hostAliases | nindent 8 }}")
.contains("replicas: {{ .Values.replicaCount }}");
assertThat(ResourceVerify.readFile(gradleRunner.resolveFile(
"build", "jkube", "helm", "helm-complex-values", "kubernetes", "templates", "helm-complex-values-service.yaml")))
.contains("annotations: {{- toYaml .Values.common.annotations | nindent 4 }}\n" +
" {{- toYaml .Values.service.annotations | nindent 4 }}");
}

@Test
void innerLoop() throws Exception {
try {
// Given
// gradle.properties with properties to resolve placeholders in fragments
Files.copy(gradleRunner.resolveFile("gradle.dev.properties").toPath(),
gradleRunner.resolveFile("gradle.properties").toPath());
// When
gradleRunner.withArguments("clean", "k8sResource").build();
// Then
ResourceVerify.verifyResourceDescriptors(gradleRunner.resolveDefaultKubernetesResourceFile(),
gradleRunner.resolveFile("expected", "inner-loop.yaml"));
} finally {
Files.deleteIfExists(gradleRunner.resolveFile("gradle.properties").toPath());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Object readWithPath(File file, String path) throws IOException {
return JsonPath.parse(json, Configuration.builder().options(REQUIRE_PROPERTIES).build()).read(path);
}

private static String readFile(File path) throws IOException {
public static String readFile(File path) throws IOException {
return new String(FileCopyUtils.copyToByteArray(Files.newInputStream(path.toPath())), Charset.defaultCharset());
}

Expand Down
Loading

0 comments on commit 4585ef9

Please sign in to comment.