Skip to content

Commit

Permalink
Use pull secret from cluster under test to get image info
Browse files Browse the repository at this point in the history
  • Loading branch information
xueqzhan committed May 7, 2024
1 parent f18ba21 commit 0907f0a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func updateDeploymentENVs(deployment *appsv1.Deployment, deploymentID, serviceCl
func (pna *podNetworkAvalibility) StartCollection(ctx context.Context, adminRESTConfig *rest.Config, recorder monitorapi.RecorderWriter) error {
deploymentID := uuid.New().String()

openshiftTestsImagePullSpec, err := GetOpenshiftTestsImagePullSpec(ctx, adminRESTConfig, pna.payloadImagePullSpec)
openshiftTestsImagePullSpec, err := GetOpenshiftTestsImagePullSpec(ctx, adminRESTConfig, pna.payloadImagePullSpec, nil)
if err != nil {
pna.notSupportedReason = &monitortestframework.NotSupportedError{Reason: fmt.Sprintf("unable to determine openshift-tests image: %v", err)}
return pna.notSupportedReason
Expand Down
64 changes: 57 additions & 7 deletions pkg/monitortests/network/disruptionpodnetwork/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"context"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/exec"
"strings"

Expand All @@ -15,7 +18,8 @@ import (
)

// GetOpenshiftTestsImagePullSpec returns the pull spec or an error.
func GetOpenshiftTestsImagePullSpec(ctx context.Context, adminRESTConfig *rest.Config, suggestedPayloadImage string) (string, error) {
// IN ginkgo environment, oc needs to be created before BeforeEach and passed in
func GetOpenshiftTestsImagePullSpec(ctx context.Context, adminRESTConfig *rest.Config, suggestedPayloadImage string, oc *exutil.CLI) (string, error) {
if len(suggestedPayloadImage) == 0 {
configClient, err := configclient.NewForConfig(adminRESTConfig)
if err != nil {
Expand All @@ -31,23 +35,69 @@ func GetOpenshiftTestsImagePullSpec(ctx context.Context, adminRESTConfig *rest.C
suggestedPayloadImage = clusterVersion.Status.History[0].Image
}

fmt.Printf("payload image: %v\n", suggestedPayloadImage)
logrus.Infof("payload image reported by CV: %v\n", suggestedPayloadImage)
// runImageExtract extracts src from specified image to dst
cmd := exec.Command("oc", "adm", "release", "info", suggestedPayloadImage, "--image-for=tests")
out := &bytes.Buffer{}
outStr := ""
errOut := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = errOut
if err := cmd.Run(); err != nil {
var oc = exutil.NewCLI("default").AsAdmin()
out, err := oc.WithoutNamespace().Run("adm").Args("release", "info", suggestedPayloadImage, "--image-for=tests").Output()
logrus.WithError(err).Errorf("unable to determine openshift-tests image through exec: %v", errOut.String())
// Now try the wrapper to see if it makes a difference
if oc == nil {
oc = exutil.NewCLIWithoutNamespace("openshift-tests")
}
outStr, err = oc.Run("adm", "release", "info", suggestedPayloadImage).Args("--image-for=tests").Output()
if err != nil {
return "", fmt.Errorf("still unable to determine openshift-tests image: %v: %v", err, errOut.String())
logrus.WithError(err).Errorf("unable to determine openshift-tests image through oc wrapper with default ps: %v", outStr)

kubeClient := oc.AdminKubeClient()
// Try to use the same pull secret as the cluster under test
imagePullSecret, err := kubeClient.CoreV1().Secrets("openshift-config").Get(context.Background(), "pull-secret", metav1.GetOptions{})
if err != nil {
logrus.WithError(err).Errorf("unable to get pull secret from cluster: %v", err)
return "", fmt.Errorf("unable to get pull secret from cluster: %v", err)
}

// cache file to local temp location
imagePullFile, err := ioutil.TempFile("", "image-pull-secret")
if err != nil {
logrus.WithError(err).Errorf("unable to create a temporary file: %v", err)
return "", fmt.Errorf("unable to create a temporary file: %v", err)
}
defer os.Remove(imagePullFile.Name())

// write the content
imagePullSecretBytes := imagePullSecret.Data[".dockerconfigjson"]
if _, err = imagePullFile.Write(imagePullSecretBytes); err != nil {
logrus.WithError(err).Errorf("unable to write pull secret to temp file: %v", err)
return "", fmt.Errorf("unable to write pull secret to temp file: %v", err)
}
if err = imagePullFile.Close(); err != nil {
logrus.WithError(err).Errorf("unable to close file: %v", err)
return "", fmt.Errorf("unable to close file: %v", err)
}

outStr, err = oc.Run("adm", "release", "info", suggestedPayloadImage).Args("--image-for=tests", "--registry-config", imagePullFile.Name()).Output()
if err != nil {
logrus.WithError(err).Errorf("unable to determine openshift-tests image through oc wrapper with cluster ps")

// What is the mirror mode

return "", fmt.Errorf("unable to determine openshift-tests image oc wrapper with cluster ps: %v", err)
} else {
logrus.Infof("successfully getting image for test with oc wrapper with cluster ps: %s\n", outStr)
}
} else {
logrus.Infof("successfully getting image for test with oc wrapper with default ps: %s\n", outStr)
}
fmt.Printf("-----admin release info is %v\n", out)
} else {
outStr = out.String()
}

openshiftTestsImagePullSpec := strings.TrimSpace(out.String())
openshiftTestsImagePullSpec := strings.TrimSpace(outStr)
fmt.Printf("openshift-tests image pull spec is %v\n", openshiftTestsImagePullSpec)

return openshiftTestsImagePullSpec, nil
Expand Down
2 changes: 1 addition & 1 deletion test/extended/operators/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ = g.Describe(fmt.Sprintf("[sig-arch][Late][Jira:%q]", "kube-apiserver"), g
inClusterPKIContent, err := gatherCertsFromPlatformNamespaces(ctx, kubeClient, masters)
o.Expect(err).NotTo(o.HaveOccurred())

openshiftTestImagePullSpec, err := disruptionpodnetwork.GetOpenshiftTestsImagePullSpec(ctx, oc.AdminConfig(), "")
openshiftTestImagePullSpec, err := disruptionpodnetwork.GetOpenshiftTestsImagePullSpec(ctx, oc.AdminConfig(), "", oc)
// Skip metal jobs if test image pullspec cannot be determined
if jobType.Platform != "metal" || err == nil {
o.Expect(err).NotTo(o.HaveOccurred())
Expand Down

0 comments on commit 0907f0a

Please sign in to comment.