@@ -23,6 +23,8 @@ import (
23
23
"fmt"
24
24
"net/http"
25
25
26
+ "github.com/SparebankenVest/azure-key-vault-to-kubernetes/pkg/azure/credentialprovider"
27
+ "github.com/google/go-containerregistry/pkg/authn"
26
28
"github.com/google/go-containerregistry/pkg/authn/k8schain"
27
29
"github.com/google/go-containerregistry/pkg/name"
28
30
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -50,13 +52,17 @@ type ImageRegistryOptions struct {
50
52
51
53
// Registry impl
52
54
type Registry struct {
53
- imageCache * cache.Cache
55
+ authType string
56
+ imageCache * cache.Cache
57
+ credentialProvider credentialprovider.CredentialProvider
54
58
}
55
59
56
60
// NewRegistry creates and initializes registry
57
- func NewRegistry (cloudConfigPath string ) ImageRegistry {
61
+ func NewRegistry (authType string , credentialProvider credentialprovider. CredentialProvider ) ImageRegistry {
58
62
return & Registry {
59
- imageCache : cache .New (cache .NoExpiration , cache .NoExpiration ),
63
+ authType : authType ,
64
+ imageCache : cache .New (cache .NoExpiration , cache .NoExpiration ),
65
+ credentialProvider : credentialProvider ,
60
66
}
61
67
}
62
68
@@ -100,31 +106,83 @@ func (r *Registry) GetImageConfig(
100
106
containerInfo .ImagePullSecrets = append (containerInfo .ImagePullSecrets , imagePullSecret .Name )
101
107
}
102
108
103
- imageConfig , err := getImageConfig (ctx , client , containerInfo , opt )
109
+ remoteOptions , err := getContainerRegistryRemoteOptions (ctx , client , containerInfo , r .authType , opt , r .credentialProvider )
110
+ if err != nil {
111
+ return nil , fmt .Errorf ("failed to get remote options: %w" , err )
112
+ }
113
+
114
+ imageConfig , err := getImageConfig (containerInfo , remoteOptions )
104
115
if imageConfig != nil && allowToCache {
105
116
r .imageCache .Set (container .Image , imageConfig , cache .DefaultExpiration )
106
117
}
107
118
108
119
return imageConfig , err
109
120
}
110
121
111
- // getImageConfig download image blob from registry
112
- func getImageConfig (ctx context.Context , client kubernetes.Interface , container containerInfo , opt ImageRegistryOptions ) (* v1.Config , error ) {
113
- authChain , err := k8schain .New (
114
- ctx ,
115
- client ,
116
- k8schain.Options {
117
- Namespace : container .Namespace ,
118
- ServiceAccountName : container .ServiceAccountName ,
119
- ImagePullSecrets : container .ImagePullSecrets ,
120
- },
121
- )
122
+ // getContainerRegistryRemoteOptions get container registry remote option
123
+ func getContainerRegistryRemoteOptions (ctx context.Context , client kubernetes.Interface , container containerInfo , authType string , opt ImageRegistryOptions , r credentialprovider.CredentialProvider ) ([]remote.Option , error ) {
124
+ ref , err := name .ParseReference (container .Image )
122
125
if err != nil {
123
- return nil , err
126
+ return nil , fmt .Errorf ("failed to parse image reference: %w" , err )
127
+ }
128
+ registry := ref .Context ().Registry .Name ()
129
+
130
+ klog .InfoS ("using registry" , "imageRegistry" , registry )
131
+
132
+ authChain := new (authn.Keychain )
133
+ switch authType {
134
+ case "azureCloudConfig" :
135
+ klog .InfoS ("using cloudConfig for registry authentication" , "config.authType" , authType )
136
+ dockerConfigEntry , err := r .GetAcrCredentials (container .Image )
137
+ if err != nil {
138
+ return nil , fmt .Errorf ("cannot fetch acr credentials: %w" , err )
139
+ }
140
+
141
+ if dockerConfigEntry .Username != "" {
142
+
143
+ sec := []corev1.Secret { //{
144
+ * dockerCfgSecretType .Create (container .Namespace , "secret" , registry , authn.AuthConfig {
145
+ Username : dockerConfigEntry .Username , Password : dockerConfigEntry .Password ,
146
+ }),
147
+ }
148
+ * authChain , err = k8schain .NewFromPullSecrets (
149
+ ctx ,
150
+ sec ,
151
+ )
152
+ if err != nil {
153
+ return nil , err
154
+ }
155
+ } else {
156
+ * authChain , err = k8schain .New (
157
+ ctx ,
158
+ client ,
159
+ k8schain.Options {
160
+ Namespace : container .Namespace ,
161
+ ServiceAccountName : container .ServiceAccountName },
162
+ )
163
+ if err != nil {
164
+ return nil , err
165
+ }
166
+ }
167
+
168
+ default :
169
+ klog .InfoS ("using imagePullSecrets for registry authentication" , "config.authType" , authType )
170
+ * authChain , err = k8schain .New (
171
+ ctx ,
172
+ client ,
173
+ k8schain.Options {
174
+ Namespace : container .Namespace ,
175
+ ServiceAccountName : container .ServiceAccountName ,
176
+ ImagePullSecrets : container .ImagePullSecrets ,
177
+ },
178
+ )
179
+ if err != nil {
180
+ return nil , err
181
+ }
124
182
}
125
183
126
184
options := []remote.Option {
127
- remote .WithAuthFromKeychain (authChain ),
185
+ remote .WithAuthFromKeychain (* authChain ),
128
186
}
129
187
130
188
if opt .SkipVerify {
@@ -133,7 +191,11 @@ func getImageConfig(ctx context.Context, client kubernetes.Interface, container
133
191
}
134
192
options = append (options , remote .WithTransport (tr ))
135
193
}
194
+ return options , err
195
+ }
136
196
197
+ // getImageConfig download image blob from registry
198
+ func getImageConfig (container containerInfo , options []remote.Option ) (* v1.Config , error ) {
137
199
ref , err := name .ParseReference (container .Image )
138
200
if err != nil {
139
201
return nil , fmt .Errorf ("failed to parse image reference: %w" , err )
0 commit comments