Skip to content

Commit 5c807a2

Browse files
Merge pull request #764 from dmage/cloudfront
Bug 2065224: Fix cloudfront middleware configuration
2 parents dcd9e65 + a248f22 commit 5c807a2

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

pkg/resource/podtemplatespec_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package resource
22

33
import (
4+
"context"
45
"reflect"
6+
"strings"
57
"testing"
8+
"time"
69

710
corev1 "k8s.io/api/core/v1"
811
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
912

13+
configv1 "github.com/openshift/api/config/v1"
1014
imageregistryapiv1 "github.com/openshift/api/imageregistry/v1"
1115
v1 "github.com/openshift/api/imageregistry/v1"
1216

1317
cirofake "github.com/openshift/cluster-image-registry-operator/pkg/client/fake"
1418
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
1519
"github.com/openshift/cluster-image-registry-operator/pkg/storage/emptydir"
20+
"github.com/openshift/cluster-image-registry-operator/pkg/storage/s3"
1621
)
1722

1823
func buildFakeClient(config *v1.Config, nodes []*corev1.Node) *cirofake.Fixtures {
@@ -445,3 +450,104 @@ func verifyMount(mount corev1.VolumeMount, expected *volumeMount, t *testing.T)
445450
t.Errorf("expected mount path to be %s, got %s", expected.mountPath, mount.MountPath)
446451
}
447452
}
453+
454+
func TestMakePodTemplateSpecS3CloudFront(t *testing.T) {
455+
ctx := context.Background()
456+
457+
testBuilder := cirofake.NewFixturesBuilder()
458+
config := &v1.Config{
459+
ObjectMeta: metav1.ObjectMeta{
460+
Name: "cluster",
461+
},
462+
Spec: v1.ImageRegistrySpec{
463+
Storage: v1.ImageRegistryConfigStorage{
464+
ManagementState: "Unmanaged",
465+
S3: &v1.ImageRegistryConfigStorageS3{
466+
Bucket: "bucket",
467+
Region: "region",
468+
Encrypt: true,
469+
CloudFront: &v1.ImageRegistryConfigStorageS3CloudFront{
470+
BaseURL: "https://cloudfront.example.com",
471+
KeypairID: "keypair-id",
472+
Duration: metav1.Duration{
473+
Duration: 300 * time.Second,
474+
},
475+
},
476+
VirtualHostedStyle: true,
477+
},
478+
},
479+
},
480+
}
481+
testBuilder.AddRegistryOperatorConfig(config)
482+
483+
infra := &configv1.Infrastructure{
484+
ObjectMeta: metav1.ObjectMeta{
485+
Name: "cluster",
486+
},
487+
Status: configv1.InfrastructureStatus{
488+
PlatformStatus: &configv1.PlatformStatus{
489+
Type: configv1.AWSPlatformType,
490+
AWS: &configv1.AWSPlatformStatus{
491+
Region: "region",
492+
},
493+
},
494+
},
495+
}
496+
testBuilder.AddInfraConfig(infra)
497+
498+
imageRegNs := &corev1.Namespace{
499+
ObjectMeta: metav1.ObjectMeta{
500+
Name: "openshift-image-registry",
501+
Annotations: map[string]string{
502+
"openshift.io/sa.scc.supplemental-groups": "1000430000/10000",
503+
},
504+
},
505+
}
506+
testBuilder.AddNamespaces(imageRegNs)
507+
508+
fixture := testBuilder.Build()
509+
s3Storage := s3.NewDriver(ctx, config.Spec.Storage.S3, fixture.Listers)
510+
pod, _, err := makePodTemplateSpec(fixture.KubeClient.CoreV1(), fixture.Listers.ProxyConfigs, s3Storage, config)
511+
if err != nil {
512+
t.Fatalf("error creating pod template: %v", err)
513+
}
514+
515+
ignoreEnvVar := func(name string) bool {
516+
return !strings.HasPrefix(name, "REGISTRY_STORAGE") && !strings.HasPrefix(name, "REGISTRY_MIDDLEWARE")
517+
}
518+
expectedEnvVars := map[string]corev1.EnvVar{
519+
"REGISTRY_STORAGE": {Value: "s3"},
520+
"REGISTRY_STORAGE_S3_BUCKET": {Value: "bucket"},
521+
"REGISTRY_STORAGE_S3_REGION": {Value: "region"},
522+
"REGISTRY_STORAGE_S3_ENCRYPT": {Value: "true"},
523+
"REGISTRY_STORAGE_S3_VIRTUALHOSTEDSTYLE": {Value: "true"},
524+
"REGISTRY_STORAGE_S3_USEDUALSTACK": {Value: "true"},
525+
"REGISTRY_STORAGE_S3_CREDENTIALSCONFIGPATH": {Value: "/var/run/secrets/cloud/credentials"},
526+
"REGISTRY_MIDDLEWARE_STORAGE": {Value: `- name: cloudfront
527+
options:
528+
baseurl: https://cloudfront.example.com
529+
privatekey: /etc/docker/cloudfront/private.pem
530+
keypairid: keypair-id
531+
duration: 5m0s
532+
ipfilteredby: none`},
533+
"REGISTRY_STORAGE_CACHE_BLOBDESCRIPTOR": {Value: "inmemory"},
534+
"REGISTRY_STORAGE_DELETE_ENABLED": {Value: "true"},
535+
}
536+
537+
for _, envVar := range pod.Spec.Containers[0].Env {
538+
expected, ok := expectedEnvVars[envVar.Name]
539+
if !ok {
540+
if !ignoreEnvVar(envVar.Name) {
541+
t.Errorf("unexpected env var %s", envVar.Name)
542+
}
543+
continue
544+
}
545+
if envVar.Value != expected.Value {
546+
t.Errorf("expected env var %s to have value %s, got %s", envVar.Name, expectedEnvVars[envVar.Name].Value, envVar.Value)
547+
}
548+
delete(expectedEnvVars, envVar.Name)
549+
}
550+
for name := range expectedEnvVars {
551+
t.Errorf("expected env var %s not found", name)
552+
}
553+
}

pkg/storage/s3/s3.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,39 @@ func (d *driver) ConfigEnv() (envs envvar.List, err error) {
363363
)
364364

365365
if d.Config.CloudFront != nil {
366+
// Use structs to make ordering deterministic
367+
type cloudFrontOptions struct {
368+
BaseURL string `json:"baseurl"`
369+
PrivateKey string `json:"privatekey"`
370+
KeypairID string `json:"keypairid"`
371+
Duration string `json:"duration"`
372+
IPFilteredBy string `json:"ipfilteredby"`
373+
}
374+
type middleware struct {
375+
Name string `json:"name"`
376+
Options interface{} `json:"options"`
377+
}
378+
379+
duration := "1200s"
380+
if d.Config.CloudFront.Duration.Duration != 0 {
381+
duration = d.Config.CloudFront.Duration.Duration.String()
382+
}
366383
envs = append(envs,
367-
envvar.EnvVar{Name: "REGISTRY_MIDDLEWARE_STORAGE_CLOUDFRONT_BASEURL", Value: d.Config.CloudFront.BaseURL},
368-
envvar.EnvVar{Name: "REGISTRY_MIDDLEWARE_STORAGE_CLOUDFRONT_KEYPAIRID", Value: d.Config.CloudFront.KeypairID},
369-
envvar.EnvVar{Name: "REGISTRY_MIDDLEWARE_STORAGE_CLOUDFRONT_DURATION", Value: d.Config.CloudFront.Duration.String()},
370-
envvar.EnvVar{Name: "REGISTRY_MIDDLEWARE_STORAGE_CLOUDFRONT_PRIVATEKEY", Value: "/etc/docker/cloudfront/private.pem"},
384+
envvar.EnvVar{
385+
Name: "REGISTRY_MIDDLEWARE_STORAGE",
386+
Value: []middleware{
387+
{
388+
Name: "cloudfront",
389+
Options: cloudFrontOptions{
390+
BaseURL: d.Config.CloudFront.BaseURL,
391+
PrivateKey: "/etc/docker/cloudfront/private.pem",
392+
KeypairID: d.Config.CloudFront.KeypairID,
393+
Duration: duration,
394+
IPFilteredBy: "none",
395+
},
396+
},
397+
},
398+
},
371399
)
372400
}
373401

0 commit comments

Comments
 (0)