@@ -23,6 +23,7 @@ import (
23
23
"io/ioutil"
24
24
"os"
25
25
"regexp"
26
+ "strings"
26
27
"time"
27
28
28
29
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2019-05-01/containerregistry"
@@ -186,7 +187,7 @@ func (a *acrProvider) Provide(image string) credentialprovider.DockerConfig {
186
187
cfg := credentialprovider.DockerConfig {}
187
188
188
189
if a .config .UseManagedIdentityExtension {
189
- if loginServer := parseACRLoginServerFromImage (image ); loginServer == "" {
190
+ if loginServer := a . parseACRLoginServerFromImage (image ); loginServer == "" {
190
191
klog .V (4 ).Infof ("image(%s) is not from ACR, skip MSI authentication" , image )
191
192
} else {
192
193
if cred , err := getACRDockerEntryFromARMToken (a , loginServer ); err == nil {
@@ -203,6 +204,28 @@ func (a *acrProvider) Provide(image string) credentialprovider.DockerConfig {
203
204
}
204
205
cfg [url ] = * cred
205
206
}
207
+
208
+ // Handle the custom cloud case
209
+ // In clouds where ACR is not yet deployed, the string will be empty
210
+ if a .environment != nil && strings .Contains (a .environment .ContainerRegistryDNSSuffix , ".azurecr." ) {
211
+ customAcrSuffix := "*" + a .environment .ContainerRegistryDNSSuffix
212
+ hasBeenAdded := false
213
+ for _ , url := range containerRegistryUrls {
214
+ if strings .EqualFold (url , customAcrSuffix ) {
215
+ hasBeenAdded = true
216
+ break
217
+ }
218
+ }
219
+
220
+ if ! hasBeenAdded {
221
+ cred := & credentialprovider.DockerConfigEntry {
222
+ Username : a .config .AADClientID ,
223
+ Password : a .config .AADClientSecret ,
224
+ Email : dummyRegistryEmail ,
225
+ }
226
+ cfg [customAcrSuffix ] = * cred
227
+ }
228
+ }
206
229
}
207
230
208
231
// add ACR anonymous repo support: use empty username and password for anonymous access
@@ -252,10 +275,24 @@ func getACRDockerEntryFromARMToken(a *acrProvider, loginServer string) (*credent
252
275
// parseACRLoginServerFromImage takes image as parameter and returns login server of it.
253
276
// Parameter `image` is expected in following format: foo.azurecr.io/bar/imageName:version
254
277
// If the provided image is not an acr image, this function will return an empty string.
255
- func parseACRLoginServerFromImage (image string ) string {
278
+ func ( a * acrProvider ) parseACRLoginServerFromImage (image string ) string {
256
279
match := acrRE .FindAllString (image , - 1 )
257
280
if len (match ) == 1 {
258
281
return match [0 ]
259
282
}
283
+
284
+ // handle the custom cloud case
285
+ if a != nil && a .environment != nil {
286
+ cloudAcrSuffix := a .environment .ContainerRegistryDNSSuffix
287
+ cloudAcrSuffixLength := len (cloudAcrSuffix )
288
+ if cloudAcrSuffixLength > 0 {
289
+ customAcrSuffixIndex := strings .Index (image , cloudAcrSuffix )
290
+ if customAcrSuffixIndex != - 1 {
291
+ endIndex := customAcrSuffixIndex + cloudAcrSuffixLength
292
+ return image [0 :endIndex ]
293
+ }
294
+ }
295
+ }
296
+
260
297
return ""
261
298
}
0 commit comments