Skip to content

Commit d797e09

Browse files
committed
fix(controllers): hash repository labels longer than 63 chars
closes #33
1 parent 0b9b9b4 commit d797e09

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

controllers/pod_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func cachedImageFromSourceImage(sourceImage string) (*dcrenixiov1alpha1.CachedIm
269269
ObjectMeta: metav1.ObjectMeta{
270270
Name: sanitizedName,
271271
Labels: map[string]string{
272-
dcrenixiov1alpha1.RepositoryLabelName: registry.SanitizeName(named.Name()),
272+
dcrenixiov1alpha1.RepositoryLabelName: registry.RepositoryLabel(named.Name()),
273273
},
274274
},
275275
Spec: dcrenixiov1alpha1.CachedImageSpec{

controllers/pod_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
. "github.com/onsi/gomega"
1111
"gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
1212
dcrenixiov1alpha1 "gitlab.enix.io/products/docker-cache-registry/api/v1alpha1"
13+
"gitlab.enix.io/products/docker-cache-registry/internal/registry"
1314
corev1 "k8s.io/api/core/v1"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
)
@@ -136,7 +137,7 @@ func Test_cachedImageFromSourceImage(t *testing.T) {
136137
g.Expect(cachedImage.Spec.PullSecretNames).To(BeEmpty())
137138
g.Expect(cachedImage.Spec.PullSecretsNamespace).To(BeEmpty())
138139
g.Expect(cachedImage.Labels).To(Equal(map[string]string{
139-
dcrenixiov1alpha1.RepositoryLabelName: tt.expectedRepository,
140+
dcrenixiov1alpha1.RepositoryLabelName: registry.RepositoryLabel(tt.expectedRepository),
140141
}))
141142
})
142143
}

internal/proxy/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (p *Proxy) proxyRegistry(c *gin.Context, endpoint string, endpointIsOrigin
186186
}
187187

188188
func (p *Proxy) getBasicAuth(registryDomain string, repository string) (string, error) {
189-
repositoryLabel := registry.SanitizeName(registryDomain + "/" + repository)
189+
repositoryLabel := registry.RepositoryLabel(registryDomain + "/" + repository)
190190
cachedImages := &dcrenixiov1alpha1.CachedImageList{}
191191
secret := &corev1.Secret{}
192192

internal/registry/registry.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package registry
22

33
import (
4+
"crypto/sha256"
45
"errors"
6+
"fmt"
57
"net/http"
68
"regexp"
79
"strings"
@@ -118,3 +120,13 @@ func SanitizeName(image string) string {
118120
nameRegex := regexp.MustCompile(`[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*`)
119121
return strings.Join(nameRegex.FindAllString(image, -1), "-")
120122
}
123+
124+
func RepositoryLabel(repositoryName string) string {
125+
sanitizedName := SanitizeName(repositoryName)
126+
127+
if len(sanitizedName) > 63 {
128+
return fmt.Sprintf("%x", sha256.Sum224([]byte(sanitizedName)))
129+
}
130+
131+
return sanitizedName
132+
}

internal/registry/registry_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package registry
2+
3+
import (
4+
"crypto/sha256"
5+
"fmt"
6+
"testing"
7+
8+
. "github.com/onsi/gomega"
9+
)
10+
11+
func sha224(str string) string {
12+
return fmt.Sprintf("%x", sha256.Sum224([]byte(str)))
13+
}
14+
15+
func TestSanitizeName(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
image string
19+
expectedSanitizedImage string
20+
}{
21+
{
22+
name: "Basic",
23+
image: "docker.io/library/alpine",
24+
expectedSanitizedImage: "docker.io-library-alpine",
25+
},
26+
{
27+
name: "Advanced",
28+
image: "some-gitlab-registry.com:5000/group/another-group/project/backend:v1.0.0",
29+
expectedSanitizedImage: "some-gitlab-registry.com-5000-group-another-group-project-backend-v1.0.0",
30+
},
31+
}
32+
33+
g := NewWithT(t)
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
label := SanitizeName(tt.image)
37+
g.Expect(label).To(Equal(tt.expectedSanitizedImage))
38+
})
39+
}
40+
}
41+
42+
func TestRepositoryLabel(t *testing.T) {
43+
tests := []struct {
44+
name string
45+
repositoryName string
46+
expectedLabel string
47+
}{
48+
{
49+
name: "Basic",
50+
repositoryName: "docker.io/library/alpine",
51+
expectedLabel: "docker.io-library-alpine",
52+
},
53+
{
54+
name: "Long name",
55+
repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-operator",
56+
expectedLabel: sha224("docker.io-rancher-mirrored-prometheus-operator-prometheus-operator"),
57+
},
58+
{
59+
name: "63 chars",
60+
repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-opera",
61+
expectedLabel: "docker.io-rancher-mirrored-prometheus-operator-prometheus-opera",
62+
},
63+
{
64+
name: "64 chars",
65+
repositoryName: "docker.io/rancher/mirrored-prometheus-operator-prometheus-operat",
66+
expectedLabel: sha224("docker.io-rancher-mirrored-prometheus-operator-prometheus-operat"),
67+
},
68+
}
69+
70+
g := NewWithT(t)
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
label := RepositoryLabel(tt.repositoryName)
74+
g.Expect(label).To(Equal(tt.expectedLabel))
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)