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

feat: Allow to mount secrets into che containers as subpath #1788

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 58 additions & 3 deletions pkg/deploy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,50 @@
for _, secretObj := range secrets.Items {
switch secretObj.Annotations[constants.CheEclipseOrgMountAs] {
case "file":
voluseSource := corev1.VolumeSource{
volumeSource := corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretObj.Name,
},
}

volume := corev1.Volume{
Name: secretObj.Name,
VolumeSource: voluseSource,
VolumeSource: volumeSource,
}
specDeployment.Spec.Template.Spec.Volumes = append(specDeployment.Spec.Template.Spec.Volumes, volume)

volumeMount := corev1.VolumeMount{
Name: secretObj.Name,
MountPath: secretObj.Annotations[constants.CheEclipseOrgMountPath],
}
container.VolumeMounts = append(container.VolumeMounts, volumeMount)
case "subpath":
volumeSource := corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretObj.Name,
},
}

volume := corev1.Volume{
Name: secretObj.Name,
VolumeSource: volumeSource,
}
specDeployment.Spec.Template.Spec.Volumes = append(specDeployment.Spec.Template.Spec.Volumes, volume)
container.VolumeMounts = append(container.VolumeMounts, volumeMount)

for fileName, _ := range secretObj.Data {
mountPath := secretObj.Annotations[constants.CheEclipseOrgMountPath]
if strings.HasSuffix(mountPath, "/") {
mountPath += fileName

Check warning on line 319 in pkg/deploy/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/deploy/deployment.go#L319

Added line #L319 was not covered by tests
} else {
mountPath += "/" + fileName
}
volumeMount := corev1.VolumeMount{
Name: secretObj.Name,
MountPath: mountPath,
SubPath: fileName,
}
container.VolumeMounts = append(container.VolumeMounts, volumeMount)
}
case "env":
secret := &corev1.Secret{}
exists, err := GetNamespacedObject(deployContext, secretObj.Name, secret)
Expand Down Expand Up @@ -402,6 +427,36 @@
specDeployment.Spec.Template.Spec.Volumes = append(specDeployment.Spec.Template.Spec.Volumes, volume)
container.VolumeMounts = append(container.VolumeMounts, volumeMount)

case "subpath":
volumeSource := corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapObj.Name,
},
},
}

volume := corev1.Volume{
Name: configMapObj.Name,
VolumeSource: volumeSource,
}
specDeployment.Spec.Template.Spec.Volumes = append(specDeployment.Spec.Template.Spec.Volumes, volume)

for fileName, _ := range configMapObj.Data {
mountPath := configMapObj.Annotations[constants.CheEclipseOrgMountPath]
if strings.HasSuffix(mountPath, "/") {
mountPath += fileName

Check warning on line 448 in pkg/deploy/deployment.go

View check run for this annotation

Codecov / codecov/patch

pkg/deploy/deployment.go#L448

Added line #L448 was not covered by tests
} else {
mountPath += "/" + fileName
}
volumeMount := corev1.VolumeMount{
Name: configMapObj.Name,
MountPath: mountPath,
SubPath: fileName,
}
container.VolumeMounts = append(container.VolumeMounts, volumeMount)
}

case "env":
configmap := &corev1.ConfigMap{}
exists, err := GetNamespacedObject(deployContext, configMapObj.Name, configmap)
Expand Down
150 changes: 148 additions & 2 deletions pkg/deploy/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,79 @@ func TestMountSecret(t *testing.T) {
},
},
{
name: "Mount env variable",
name: "Mount secret as subpath",
initDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
ResourceVersion: "0",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{}},
},
},
},
},
expectedDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
ResourceVersion: "0",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "test-volume",
},
},
},
},
Containers: []corev1.Container{
{
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/test-path/key",
SubPath: "key",
},
},
},
},
},
},
},
},
initObjects: []runtime.Object{
&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-volume",
Namespace: "eclipse-che",
Labels: map[string]string{
constants.KubernetesPartOfLabelKey: constants.CheEclipseOrg,
constants.KubernetesComponentLabelKey: "che-secret", // corresponds to deployment name
},
Annotations: map[string]string{
constants.CheEclipseOrgMountAs: "subpath",
constants.CheEclipseOrgMountPath: "/test-path",
},
},
Data: map[string][]byte{
"key": []byte("key-data"),
},
},
},
},
{
name: "Mount secret as env variable",
initDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
Expand Down Expand Up @@ -424,7 +496,81 @@ func TestMountConfigMaps(t *testing.T) {
},
},
{
name: "Mount env variable",
name: "Mount configmap as subpath",
initDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
ResourceVersion: "0",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{}},
},
},
},
},
expectedDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
ResourceVersion: "0",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-volume",
},
},
},
},
},
Containers: []corev1.Container{
{
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/test-path/key",
SubPath: "key",
},
},
},
},
},
},
},
},
initObjects: []runtime.Object{
&corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-volume",
Namespace: "eclipse-che",
Labels: map[string]string{
constants.KubernetesPartOfLabelKey: constants.CheEclipseOrg,
constants.KubernetesComponentLabelKey: "che-configmap", // corresponds to deployment name
},
Annotations: map[string]string{
constants.CheEclipseOrgMountAs: "subpath",
constants.CheEclipseOrgMountPath: "/test-path",
},
},
Data: map[string]string{
"key": "key-data",
},
},
},
},
{
name: "Mount configmap as env variable",
initDeployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "che",
Expand Down
Loading