Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to a custom default values if exactly one provided. #5856

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions dashboard/src/components/DeploymentForm/DeploymentForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const defaultSelectedPkg = {
identifier: "test/test",
plugin: { name: "my.plugin", version: "0.0.1" },
} as AvailablePackageReference,
defaultValues: "package: defaults",
} as AvailablePackageDetail,
pkgVersion: "1.2.4",
values: "bar: foo",
values: "package: defaults",
};

const routePathParam = `/c/${defaultProps.cluster}/ns/${defaultProps.namespace}/apps/new/${defaultProps.plugin.name}/${defaultProps.plugin.version}/${defaultProps.packageCluster}/${defaultProps.packageNamespace}/${defaultProps.pkgName}/versions/${defaultProps.version}`;
Expand Down Expand Up @@ -128,6 +129,21 @@ it("fetches the available versions", () => {
);
});

describe("default values", () => {
it("uses the selected package default values", () => {
const wrapper = mountWrapper(
getStore({ packages: { selected: defaultSelectedPkg } } as IStoreState),
<Router history={history}>
<Route path={routePath}>
<DeploymentForm />
</Route>
</Router>,
);

expect(wrapper.find(DeploymentFormBody).prop("appValues")).toBe("package: defaults");
});
});

describe("renders an error", () => {
it("renders a custom error if the deployment failed", () => {
const wrapper = mountWrapper(
Expand Down Expand Up @@ -185,11 +201,11 @@ describe("renders an error", () => {
.find(DeploymentFormBody)
.prop("setValues");
act(() => {
handleValuesChange("foo: bar");
handleValuesChange("changed: defaults");
});
wrapper.update();

expect(wrapper.find(DeploymentFormBody).prop("appValues")).toBe("foo: bar");
expect(wrapper.find(DeploymentFormBody).prop("appValues")).toBe("changed: defaults");
});

it("changes values if the version changes and it has not been modified", () => {
Expand All @@ -201,7 +217,7 @@ describe("renders an error", () => {
</Route>
</Router>,
);
expect(wrapper.find(DeploymentFormBody).prop("appValues")).toBe("bar: foo");
expect(wrapper.find(DeploymentFormBody).prop("appValues")).toBe("package: defaults");
});

it("display the service account selector", () => {
Expand Down
58 changes: 57 additions & 1 deletion dashboard/src/reducers/availablepackages.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2021-2022 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0

import { AvailablePackageSummary, Context } from "gen/kubeappsapis/core/packages/v1alpha1/packages";
import {
AvailablePackageDetail,
AvailablePackageSummary,
Context,
} from "gen/kubeappsapis/core/packages/v1alpha1/packages";
import { Plugin } from "gen/kubeappsapis/core/plugins/v1alpha1/plugins";
import { getType } from "typesafe-actions";
import actions from "../actions";
Expand Down Expand Up @@ -498,4 +502,56 @@ describe("packageReducer", () => {
...initialState,
});
});

describe("receiveSelectedAvailablePackageDetail", () => {
const packageDetail = {
name: "test-package",
defaultValues: "default: values",
valuesSchema: "",
} as AvailablePackageDetail;

it("uses the package default values by default", () => {
const state = packageReducer(initialState, {
type: getType(actions.availablepackages.receiveSelectedAvailablePackageDetail) as any,
payload: {
selectedPackage: packageDetail,
},
});

expect(state.selected.values).toEqual("default: values");
});

it("uses the package custom default values if only one custom default values", () => {
const state = packageReducer(initialState, {
type: getType(actions.availablepackages.receiveSelectedAvailablePackageDetail) as any,
payload: {
selectedPackage: {
...packageDetail,
additionalDefaultValues: {
"values-custom": "custom: values",
},
} as AvailablePackageDetail,
},
});

expect(state.selected.values).toEqual("custom: values");
});

it("uses the package default values if more than one custom default values present", () => {
const state = packageReducer(initialState, {
type: getType(actions.availablepackages.receiveSelectedAvailablePackageDetail) as any,
payload: {
selectedPackage: {
...packageDetail,
additionalDefaultValues: {
"values-custom": "custom: values",
"values-other": "more: customdefaultvalues",
},
} as AvailablePackageDetail,
},
});

expect(state.selected.values).toEqual("default: values");
});
});
});
13 changes: 12 additions & 1 deletion dashboard/src/reducers/availablepackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getType } from "typesafe-actions";
import actions from "../actions";
import { PackagesAction } from "../actions/availablepackages";
import { NamespaceAction } from "../actions/namespace";
import { AvailablePackageDetail } from "gen/kubeappsapis/core/packages/v1alpha1/packages";

export const initialState: IPackageState = {
isFetching: false,
Expand All @@ -21,6 +22,16 @@ export const initialState: IPackageState = {
size: 20,
};

// defaultValues determines whether the package defaults or custom default
// values should be used.
function defaultValues(pkg: AvailablePackageDetail) {
const additionalValues = Object.values(pkg.additionalDefaultValues || []);
if (additionalValues.length === 1) {
return additionalValues[0];
}
return pkg.defaultValues || "";
}

const selectedPackageReducer = (
state: IPackageState["selected"],
action: PackagesAction | NamespaceAction,
Expand All @@ -35,7 +46,7 @@ const selectedPackageReducer = (
pkgVersion: action.payload.selectedPackage.version?.pkgVersion,
appVersion: action.payload.selectedPackage.version?.appVersion,
readme: action.payload.selectedPackage.readme,
values: action.payload.selectedPackage.defaultValues,
values: defaultValues(action.payload.selectedPackage),
schema:
action.payload.selectedPackage.valuesSchema !== ""
? (JSON.parse(action.payload.selectedPackage.valuesSchema) as JSONSchemaType<any>)
Expand Down
26 changes: 26 additions & 0 deletions integration/charts/simplechart-customvalues/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
9 changes: 9 additions & 0 deletions integration/charts/simplechart-customvalues/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

apiVersion: v2
name: simplechart-customvalues
description: A simple chart for testing custom values
type: application
version: 0.1.0
appVersion: "1.0.0"
65 changes: 65 additions & 0 deletions integration/charts/simplechart-customvalues/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{{/* Copyright 2022 the Kubeapps contributors. */}}
{{/* SPDX-License-Identifier: Apache-2.0 */}}

{{/*
Expand the name of the chart.
*/}}
{{- define "simplechart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "simplechart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "simplechart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "simplechart.labels" -}}
helm.sh/chart: {{ include "simplechart.chart" . }}
{{ include "simplechart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "simplechart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "simplechart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "simplechart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "simplechart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "simplechart.fullname" . }}
labels:
{{- include "simplechart.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "simplechart.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "simplechart.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "simplechart.serviceAccountName" . }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
18 changes: 18 additions & 0 deletions integration/charts/simplechart-customvalues/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

apiVersion: v1
kind: Service
metadata:
name: {{ include "simplechart.fullname" . }}
labels:
{{- include "simplechart.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "simplechart.selectorLabels" . | nindent 4 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

apiVersion: v1
kind: Pod
metadata:
name: "{{ include "simplechart.fullname" . }}-test-connection"
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "simplechart.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

image:
repository: bitnami/nginx
tag: "1.23.3-debian-11"
pullPolicy: IfNotPresent
19 changes: 19 additions & 0 deletions integration/charts/simplechart-customvalues/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0

image:
repository: bitnami/nginx
tag: "latest"
pullPolicy: IfNotPresent

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
create: false
name: "default"

service:
type: ClusterIP
port: 80
2 changes: 1 addition & 1 deletion script/chart-museum.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ installChartMuseum() {
--set persistence.enabled=true \
--set env.secret.BASIC_AUTH_USER=$CHARTMUSEUM_USER \
--set env.secret.BASIC_AUTH_PASS=$CHARTMUSEUM_PWD
info "Waiting for ChartMuseum to be ready..."
echo "Waiting for ChartMuseum to be ready..."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using echo like the rest of the script, so the script itself can be run without dependencies. Useful for setting up.

kubectl rollout status -w deployment/chartmuseum --namespace="${CHARTMUSEUM_NS}"

echo "Installing Ingress for ChartMuseum with access through host ${CHARTMUSEUM_HOSTNAME}"
Expand Down