|
1 | 1 | package resource
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "reflect"
|
| 6 | + "strings" |
5 | 7 | "testing"
|
| 8 | + "time" |
6 | 9 |
|
7 | 10 | corev1 "k8s.io/api/core/v1"
|
8 | 11 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
9 | 12 |
|
| 13 | + configv1 "github.com/openshift/api/config/v1" |
10 | 14 | imageregistryapiv1 "github.com/openshift/api/imageregistry/v1"
|
11 | 15 | v1 "github.com/openshift/api/imageregistry/v1"
|
12 | 16 |
|
13 | 17 | cirofake "github.com/openshift/cluster-image-registry-operator/pkg/client/fake"
|
14 | 18 | "github.com/openshift/cluster-image-registry-operator/pkg/defaults"
|
15 | 19 | "github.com/openshift/cluster-image-registry-operator/pkg/storage/emptydir"
|
| 20 | + "github.com/openshift/cluster-image-registry-operator/pkg/storage/s3" |
16 | 21 | )
|
17 | 22 |
|
18 | 23 | func buildFakeClient(config *v1.Config, nodes []*corev1.Node) *cirofake.Fixtures {
|
@@ -445,3 +450,104 @@ func verifyMount(mount corev1.VolumeMount, expected *volumeMount, t *testing.T)
|
445 | 450 | t.Errorf("expected mount path to be %s, got %s", expected.mountPath, mount.MountPath)
|
446 | 451 | }
|
447 | 452 | }
|
| 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 | +} |
0 commit comments