/
container_registry_utils.go
156 lines (136 loc) · 4.82 KB
/
container_registry_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"context"
"fmt"
"os/exec"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
acr "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerregistry/armcontainerregistry"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/pointer"
)
// CreateContainerRegistry creates a test acr
func (tc *AzureTestClient) CreateContainerRegistry() (acr.Registry, error) {
acrClient := tc.createACRClient()
rgName, location := tc.GetResourceGroup(), tc.GetLocation()
acrName := "e2eacr" + string(uuid.NewUUID())[0:4]
template := acr.Registry{
SKU: &acr.SKU{
Name: to.Ptr(acr.SKUNameStandard),
Tier: to.Ptr(acr.SKUTierStandard),
},
Name: pointer.String(acrName),
Location: pointer.String(location),
}
Logf("Creating ACR %s in resource group %s.", acrName, rgName)
registry, err := acrClient.Create(context.Background(), rgName, acrName, template)
if err != nil {
return acr.Registry{}, err
}
if err := wait.PollUntilContextTimeout(context.Background(), 10*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) {
_, err := acrClient.Get(ctx, rgName, acrName)
if err != nil {
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "not be found") {
return false, nil
}
return false, err
}
return true, nil
}); err != nil {
return acr.Registry{}, err
}
Logf("Created ACR %s in resource group %s.", acrName, rgName)
return *registry, nil
}
// DeleteContainerRegistry deletes an existing acr
func (tc *AzureTestClient) DeleteContainerRegistry(registryName string) (err error) {
acrClient := tc.createACRClient()
rgName := tc.GetResourceGroup()
Logf("Deleting acr %s in resource group %s.", registryName, rgName)
err = acrClient.Delete(context.Background(), rgName, registryName)
if err != nil {
return fmt.Errorf("failed to delete acr %s in resource group %s with error: %w", registryName, rgName, err)
}
return nil
}
func AZACRLogin() (err error) {
authConfig, armConfig, _, err := azureAuthConfigFromTestProfile()
if err != nil {
return err
}
Logf("Attempting az login with azure cred.")
//nolint:gosec // G204 ignore this!
cmd := exec.Command("az", "login", "--service-principal",
"--username", authConfig.AADClientID,
"--password", authConfig.AADClientSecret,
"--tenant", armConfig.TenantID)
if err = cmd.Run(); err != nil {
return fmt.Errorf("az failed to login with error: %w", err)
}
Logf("az login success.")
cmd = exec.Command("az", "account", "show")
var output []byte
if output, err = cmd.Output(); err != nil {
return fmt.Errorf("az failed to account show with output: %s\n error: %w", string(output), err)
}
Logf("az account show success.")
return nil
}
// PushImageToACR pull an image from Docker Hub and push
// it to the given azure container registry
func (tc *AzureTestClient) PushImageToACR(registryName, image string) (string, error) {
acrClient := tc.createACRClient()
rgName := tc.GetResourceGroup()
err := acrClient.ImportImage(context.Background(), rgName, registryName, acr.ImportImageParameters{
Source: &acr.ImportSource{
RegistryURI: pointer.String("docker.io"),
SourceImage: pointer.String("library/" + image + ":latest"),
},
TargetTags: []*string{
to.Ptr(image + ":latest"),
},
Mode: to.Ptr(acr.ImportModeNoForce),
})
if err != nil {
return "", fmt.Errorf("failed to import image %s from docker hub to acr %s with error: %w", image, registryName, err)
}
return "latest", nil
}
// AZACRCacheCreate enables acr cache for a image.
func AZACRCacheCreate(acrName, ruleName, imageURL, imageName, rg string) (err error) {
printAZVersion()
Logf("Attempting az acr cache create for image URL %q.", imageURL)
cmd := exec.Command("az", "acr", "cache", "create",
"-r", acrName,
"-n", ruleName,
"-s", imageURL,
"-t", imageName,
"-g", rg)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("az acr cache create failed with output %s\n error: %w", string(output), err)
}
Logf("az acr cache create success.")
return nil
}
func printAZVersion() {
cmd := exec.Command("az", "version")
output, err := cmd.CombinedOutput()
if err != nil {
Logf("az version failed with output %s\n error: %w", string(output), err)
}
Logf("az version success: %s", string(output))
}