Skip to content

Commit

Permalink
Merge pull request #6795 from nilo19/fix/test-env-1.28
Browse files Browse the repository at this point in the history
[release-1.28] fix: Keep auth envs when building e2e azure clients
  • Loading branch information
k8s-ci-robot authored Aug 11, 2024
2 parents 4af3bcc + 1d561f9 commit 4f27751
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 72 deletions.
10 changes: 2 additions & 8 deletions tests/e2e/auth/cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ var _ = Describe("Azure Credential Provider", Label(utils.TestSuiteLabelCredenti
}
}()

err = utils.DockerLogin(*registry.Name)
Expect(err).NotTo(HaveOccurred())

image := "nginx"
tag, err := utils.PushImageToACR(*registry.Name, image)
Expect(tag).NotTo(Equal(""))
tag, err := tc.PushImageToACR(*registry.Name, image)
Expect(err).NotTo(HaveOccurred())
Expect(tag).NotTo(Equal(""))

acrImageURL := fmt.Sprintf("%s.azurecr.io/%s:%s", *registry.Name, image, tag)
podTemplate := createPodPullingFromACR(image, acrImageURL, "linux")
Expand All @@ -86,9 +83,6 @@ var _ = Describe("Azure Credential Provider", Label(utils.TestSuiteLabelCredenti
result, err := utils.WaitPodTo(v1.PodRunning, cs, podTemplate, ns.Name)
Expect(result).To(BeTrue())
Expect(err).NotTo(HaveOccurred())

err = utils.DockerLogout()
Expect(err).NotTo(HaveOccurred())
})

It("should be able to create an ACR cache and pull images from it", Label(utils.TestSuiteLabelOOTCredential), func() {
Expand Down
8 changes: 3 additions & 5 deletions tests/e2e/utils/azure_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ func azureAuthConfigFromTestProfile() (*AzureAuthConfig, error) {
c.UserAssignedIdentityID = managedIdentityClientIDEnv
}
} else if federatedTokenFileEnv != "" {
c = &AzureAuthConfig{
AADFederatedTokenFile: federatedTokenFileEnv,
UseFederatedWorkloadIdentityExtension: true,
AADClientID: aadClientIDEnv,
}
c.AADFederatedTokenFile = federatedTokenFileEnv
c.UseFederatedWorkloadIdentityExtension = true
c.AADClientID = aadClientIDEnv
} else {
return c, fmt.Errorf("failed to get Azure auth config from environment")
}
Expand Down
78 changes: 19 additions & 59 deletions tests/e2e/utils/container_registry_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,70 +72,30 @@ func (tc *AzureTestClient) DeleteContainerRegistry(registryName string) (err err
return nil
}

// DockerLogin execute the `docker login` if docker is available
func DockerLogin(registryName string) (err error) {
authConfig, err := azureAuthConfigFromTestProfile()
if err != nil {
return err
}

cmd := exec.Command("docker", "-v")
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed to execute docker command with error: %w", err)
}

Logf("Attempting Docker login with azure cred.")
arg0 := "--username=" + authConfig.AADClientID
arg1 := "--password=" + authConfig.AADClientSecret
cmd = exec.Command("docker",
"login",
arg0,
arg1,
registryName+".azurecr.io")
if err = cmd.Run(); err != nil {
return fmt.Errorf("docker failed to login with error: %w", err)
}
Logf("Docker login success.")
return nil
}

// DockerLogout execute the `docker logout` if docker is available
func DockerLogout() (err error) {
Logf("Docker logout.")
cmd := exec.Command("docker", "logout")
return cmd.Run()
}

// PushImageToACR pull an image from MCR and push
// it to the given azure container registry
func PushImageToACR(registryName, image string) (tag string, err error) {
Logf("Pulling %s from MCR.", image)
imageNameMapToMCR := map[string]string{
"nginx": "mcr.microsoft.com/mirror/docker/library/nginx:1.25",
}
mcrImage := imageNameMapToMCR[image]

cmd := exec.Command("docker", "pull", mcrImage)
if err = cmd.Run(); err != nil {
return "", fmt.Errorf("failed pulling %s with error: %w", image, err)
}
func (tc *AzureTestClient) PushImageToACR(registryName, image string) (tag string, err error) {
acrClient := tc.createACRClient()
rgName := tc.GetResourceGroup()

Logf("Tagging image.")
tagSuffix := string(uuid.NewUUID())[0:4]
registry := fmt.Sprintf("%s.azurecr.io/%s:e2e-%s", registryName, image, tagSuffix)
cmd = exec.Command("docker", "tag", mcrImage, registry)
if err = cmd.Run(); err != nil {
return "", fmt.Errorf("failed tagging nginx image with error: %w", err)
tag = "1.25"
future, err := acrClient.ImportImage(context.Background(), rgName, registryName, acr.ImportImageParameters{
Source: &acr.ImportSource{
RegistryURI: pointer.String("mcr.microsoft.com"),
SourceImage: pointer.String("mirror/docker/library/" + image + ":" + tag),
},
TargetTags: &[]string{
image + ":" + tag,
},
Mode: acr.NoForce,
})
if err != nil {
return "", fmt.Errorf("failed to import image %s from mcr to acr %s with error: %w", image, registryName, err)
}

Logf("Pushing image to ACR.")
cmd = exec.Command("docker", "push", registry)
if err = cmd.Run(); err != nil {
return "", fmt.Errorf("failed pushing %s image to registry %s with error: %w", image, registryName, err)
err = future.WaitForCompletionRef(context.Background(), acrClient.Client)
if err != nil {
return "", fmt.Errorf("failed to import image %s from mcr to acr %s with error: %w", image, registryName, err)
}

Logf("Pushing image success.")
tag = fmt.Sprintf("e2e-%s", tagSuffix)
return tag, nil
}

Expand Down

0 comments on commit 4f27751

Please sign in to comment.