Skip to content

Commit 3807fc1

Browse files
committed
trt-2163: check for registryAuthFilePath
1 parent 1c8dd58 commit 3807fc1

File tree

2 files changed

+77
-61
lines changed

2 files changed

+77
-61
lines changed

pkg/test/extensions/binary.go

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/pkg/errors"
2525
"github.com/sirupsen/logrus"
2626
"golang.org/x/mod/semver"
27-
kapierrs "k8s.io/apimachinery/pkg/api/errors"
28-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2927
"k8s.io/apimachinery/pkg/util/sets"
3028
"k8s.io/klog/v2"
3129
k8simage "k8s.io/kubernetes/test/utils/image"
@@ -35,7 +33,6 @@ import (
3533
"github.com/openshift/origin/pkg/clioptions/clusterdiscovery"
3634
"github.com/openshift/origin/pkg/clioptions/imagesetup"
3735
"github.com/openshift/origin/pkg/clioptions/upgradeoptions"
38-
"github.com/openshift/origin/test/extended/util"
3936
exutil "github.com/openshift/origin/test/extended/util"
4037
origingenerated "github.com/openshift/origin/test/extended/util/annotate/generated"
4138
"github.com/openshift/origin/test/extended/util/image"
@@ -422,64 +419,16 @@ func ExtractAllTestBinaries(ctx context.Context, parallelism int) (func(), TestB
422419
return nil, nil, errors.WithMessage(err, "couldn't determine release image")
423420
}
424421

425-
oc := util.NewCLIWithoutNamespace("default")
426-
427-
// To extract binaries bearing external tests, we must inspect the release
428-
// payload under tests as well as extract content from component images
429-
// referenced by that payload.
430-
// openshift-tests is frequently run in the context of a CI job, within a pod.
431-
// CI sets $RELEASE_IMAGE_LATEST to a pullspec for the release payload under test. This
432-
// pull spec resolve to:
433-
// 1. A build farm ci-op-* namespace / imagestream location (anonymous access permitted).
434-
// 2. A quay.io/openshift-release-dev location (for tests against promoted ART payloads -- anonymous access permitted).
435-
// 3. A registry.ci.openshift.org/ocp-<arch>/release:<tag> (request registry.ci.openshift.org token).
436-
// Within the pod, we don't necessarily have a pull-secret for #3 OR the component images
437-
// a payload references (which are private, unless in a ci-op-* imagestream).
438-
// We try the following options:
439-
// 1. If set, use the REGISTRY_AUTH_FILE environment variable to an auths file with
440-
// pull secrets capable of reading appropriate payload & component image
441-
// information.
442-
// 2. If it exists, use a file /run/secrets/ci.openshift.io/cluster-profile/pull-secret
443-
// (conventional location for pull-secret information for CI cluster profile).
444-
// 3. Use openshift-config secret/pull-secret from the cluster-under-test, if it exists
445-
// (Microshift does not).
446-
// 4. Use unauthenticated access to the payload image and component images.
447-
registryAuthFilePath := os.Getenv("REGISTRY_AUTH_FILE")
448-
449-
// if the environment variable is not set, extract the target cluster's
450-
// platform pull secret.
451-
if len(registryAuthFilePath) != 0 {
452-
logrus.Infof("Using REGISTRY_AUTH_FILE environment variable: %v", registryAuthFilePath)
453-
} else {
454-
455-
// See if the cluster-profile has stored a pull-secret at the conventional location.
456-
ciProfilePullSecretPath := "/run/secrets/ci.openshift.io/cluster-profile/pull-secret"
457-
_, err := os.Stat(ciProfilePullSecretPath)
458-
if !os.IsNotExist(err) {
459-
logrus.Infof("Detected %v; using cluster profile for image access", ciProfilePullSecretPath)
460-
registryAuthFilePath = ciProfilePullSecretPath
461-
} else {
462-
// Inspect the cluster-under-test and read its cluster pull-secret dockerconfigjson value.
463-
clusterPullSecret, err := oc.AdminKubeClient().CoreV1().Secrets("openshift-config").Get(context.Background(), "pull-secret", metav1.GetOptions{})
464-
if err != nil {
465-
if kapierrs.IsNotFound(err) {
466-
logrus.Warningf("Cluster has no openshift-config secret/pull-secret; falling back to unauthenticated image access")
467-
} else {
468-
return nil, nil, fmt.Errorf("unable to read ephemeral cluster pull secret: %w", err)
469-
}
470-
} else {
471-
tmpDir, err := os.MkdirTemp("", "external-binary")
472-
clusterDockerConfig := clusterPullSecret.Data[".dockerconfigjson"]
473-
registryAuthFilePath = filepath.Join(tmpDir, ".dockerconfigjson")
474-
err = os.WriteFile(registryAuthFilePath, clusterDockerConfig, 0600)
475-
if err != nil {
476-
return nil, nil, fmt.Errorf("unable to serialize target cluster pull-secret locally: %w", err)
477-
}
422+
tmpDir, err := os.MkdirTemp("", "external-binary")
423+
if err != nil {
424+
return nil, nil, fmt.Errorf("failed to create temporary directory: %w", err)
425+
}
478426

479-
defer os.RemoveAll(tmpDir)
480-
logrus.Infof("Using target cluster pull-secrets for registry auth")
481-
}
482-
}
427+
defer os.RemoveAll(tmpDir)
428+
429+
registryAuthFilePath, err := DetermineRegistryAuthFilePath(tmpDir)
430+
if err != nil {
431+
return nil, nil, fmt.Errorf("failed to determine registry auth file path: %w", err)
483432
}
484433

485434
externalBinaryProvider, err := NewExternalBinaryProvider(releaseImage, registryAuthFilePath)

pkg/test/extensions/util.go

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
kapierrs "k8s.io/apimachinery/pkg/api/errors"
1112
"os"
1213
"os/exec"
1314
"path"
@@ -280,8 +281,13 @@ func ExtractImageFromReleasePayload(releaseImage, imageTag string) (string, erro
280281
}
281282
defer os.RemoveAll(tmpDir)
282283

284+
registryAuthFilePath, err := DetermineRegistryAuthFilePath(tmpDir)
285+
if err != nil {
286+
return "", fmt.Errorf("failed to determine registry auth file path: %w", err)
287+
}
288+
283289
// Extract the ImageStream from the release payload
284-
imageStream, _, err := extractReleaseImageStream(tmpDir, releaseImage, "")
290+
imageStream, _, err := extractReleaseImageStream(tmpDir, releaseImage, registryAuthFilePath)
285291
if err != nil {
286292
return "", fmt.Errorf("failed to extract image references from release payload: %w", err)
287293
}
@@ -295,3 +301,64 @@ func ExtractImageFromReleasePayload(releaseImage, imageTag string) (string, erro
295301

296302
return "", fmt.Errorf("image tag %q not found in release payload %q", imageTag, releaseImage)
297303
}
304+
305+
func DetermineRegistryAuthFilePath(tmpDir string) (string, error) {
306+
oc := util.NewCLIWithoutNamespace("default")
307+
308+
// To extract binaries bearing external tests, we must inspect the release
309+
// payload under tests as well as extract content from component images
310+
// referenced by that payload.
311+
// openshift-tests is frequently run in the context of a CI job, within a pod.
312+
// CI sets $RELEASE_IMAGE_LATEST to a pullspec for the release payload under test. This
313+
// pull spec resolve to:
314+
// 1. A build farm ci-op-* namespace / imagestream location (anonymous access permitted).
315+
// 2. A quay.io/openshift-release-dev location (for tests against promoted ART payloads -- anonymous access permitted).
316+
// 3. A registry.ci.openshift.org/ocp-<arch>/release:<tag> (request registry.ci.openshift.org token).
317+
// Within the pod, we don't necessarily have a pull-secret for #3 OR the component images
318+
// a payload references (which are private, unless in a ci-op-* imagestream).
319+
// We try the following options:
320+
// 1. If set, use the REGISTRY_AUTH_FILE environment variable to an auths file with
321+
// pull secrets capable of reading appropriate payload & component image
322+
// information.
323+
// 2. If it exists, use a file /run/secrets/ci.openshift.io/cluster-profile/pull-secret
324+
// (conventional location for pull-secret information for CI cluster profile).
325+
// 3. Use openshift-config secret/pull-secret from the cluster-under-test, if it exists
326+
// (Microshift does not).
327+
// 4. Use unauthenticated access to the payload image and component images.
328+
registryAuthFilePath := os.Getenv("REGISTRY_AUTH_FILE")
329+
330+
// if the environment variable is not set, extract the target cluster's
331+
// platform pull secret.
332+
if len(registryAuthFilePath) != 0 {
333+
logrus.Infof("Using REGISTRY_AUTH_FILE environment variable: %v", registryAuthFilePath)
334+
} else {
335+
336+
// See if the cluster-profile has stored a pull-secret at the conventional location.
337+
ciProfilePullSecretPath := "/run/secrets/ci.openshift.io/cluster-profile/pull-secret"
338+
_, err := os.Stat(ciProfilePullSecretPath)
339+
if !os.IsNotExist(err) {
340+
logrus.Infof("Detected %v; using cluster profile for image access", ciProfilePullSecretPath)
341+
registryAuthFilePath = ciProfilePullSecretPath
342+
} else {
343+
// Inspect the cluster-under-test and read its cluster pull-secret dockerconfigjson value.
344+
clusterPullSecret, err := oc.AdminKubeClient().CoreV1().Secrets("openshift-config").Get(context.Background(), "pull-secret", metav1.GetOptions{})
345+
if err != nil {
346+
if kapierrs.IsNotFound(err) {
347+
logrus.Warningf("Cluster has no openshift-config secret/pull-secret; falling back to unauthenticated image access")
348+
} else {
349+
return "", fmt.Errorf("unable to read ephemeral cluster pull secret: %w", err)
350+
}
351+
} else {
352+
clusterDockerConfig := clusterPullSecret.Data[".dockerconfigjson"]
353+
registryAuthFilePath = filepath.Join(tmpDir, ".dockerconfigjson")
354+
err = os.WriteFile(registryAuthFilePath, clusterDockerConfig, 0600)
355+
if err != nil {
356+
return "", fmt.Errorf("unable to serialize target cluster pull-secret locally: %w", err)
357+
}
358+
logrus.Infof("Using target cluster pull-secrets for registry auth")
359+
}
360+
}
361+
}
362+
363+
return registryAuthFilePath, nil
364+
}

0 commit comments

Comments
 (0)