Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metricsproxy
import (
"fmt"
"sort"
"strings"

component "github.com/openshift/hypershift/support/controlplane-component"
"github.com/openshift/hypershift/support/metrics"
Expand Down Expand Up @@ -121,8 +122,9 @@ func certVolumesFromMonitors(cpContext component.WorkloadContext, namespace stri

for _, name := range names {
ref := refs[name]
volName := sanitizeVolumeName(name)
vol := corev1.Volume{
Name: name,
Name: volName,
}

if ref.isSecret {
Expand All @@ -144,14 +146,21 @@ func certVolumesFromMonitors(cpContext component.WorkloadContext, namespace stri

volumes = append(volumes, vol)
mounts = append(mounts, corev1.VolumeMount{
Name: name,
Name: volName,
MountPath: certBasePath + "/" + name,
})
}

return volumes, mounts, nil
}

// sanitizeVolumeName converts a resource name into a valid Kubernetes volume
// name by replacing dots with dashes. Kubernetes volume names must conform to
// DNS label rules which do not allow dots.
func sanitizeVolumeName(name string) string {
return strings.ReplaceAll(name, ".", "-")
}

// collectSecretOrConfigMapRef adds a SecretOrConfigMap reference to the refs map.
func collectSecretOrConfigMapRef(refs map[string]*certRef, ref prometheusoperatorv1.SecretOrConfigMap) {
if ref.Secret != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,38 @@ func TestCertVolumesFromMonitors(t *testing.T) {
}
})

t.Run("When resource names contain dots, it should sanitize volume names but preserve mount paths", func(t *testing.T) {
t.Parallel()

sm := newServiceMonitorWithTLS("cno", namespace, &prometheusoperatorv1.TLSConfig{
SafeTLSConfig: prometheusoperatorv1.SafeTLSConfig{
CA: prometheusoperatorv1.SecretOrConfigMap{
ConfigMap: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "openshift-service-ca.crt"},
Key: "service-ca.crt",
},
},
},
})

cpContext := newCertVolumeTestContext(namespace, scheme, sm)
volumes, mounts := assertCertVolumeCount(t, cpContext, namespace, 1, 1)

if volumes[0].Name != "openshift-service-ca-crt" {
t.Errorf("expected sanitized volume name openshift-service-ca-crt, got %s", volumes[0].Name)
}
if mounts[0].Name != "openshift-service-ca-crt" {
t.Errorf("expected sanitized mount name openshift-service-ca-crt, got %s", mounts[0].Name)
}
expectedPath := certBasePath + "/openshift-service-ca.crt"
if mounts[0].MountPath != expectedPath {
t.Errorf("expected mount path to preserve dots %q, got %q", expectedPath, mounts[0].MountPath)
}
if volumes[0].VolumeSource.ConfigMap.Name != "openshift-service-ca.crt" {
t.Errorf("expected ConfigMap source name to preserve original name, got %s", volumes[0].VolumeSource.ConfigMap.Name)
}
})

t.Run("When PodMonitor has no TLS config, it should be skipped", func(t *testing.T) {
t.Parallel()

Expand Down