Skip to content

Commit e584310

Browse files
authored
image-copy-gcp: support subrepositories (#282)
Modify the example to correctly copy subrepositories (like `charts/<chart-name>`).
1 parent 19d09e0 commit e584310

1 file changed

Lines changed: 47 additions & 12 deletions

File tree

image-copy-gcp/main.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,59 @@ func resolveRepositoryName(ctx context.Context, repoID string) (string, error) {
162162
return "", fmt.Errorf("getting token: %w", err)
163163
}
164164

165-
// Create client that uses the token
166-
client, err := v1.NewClients(ctx, env.APIEndpoint, tok.AccessToken)
165+
// Create clients that uses the token
166+
regc, err := v1.NewClients(ctx, env.APIEndpoint, tok.AccessToken)
167167
if err != nil {
168-
return "", fmt.Errorf("creating clients: %w", err)
168+
return "", fmt.Errorf("creating registry clients: %w", err)
169169
}
170-
171-
// Lookup the repository name from the ID
172-
repoList, err := client.Registry().ListRepos(ctx, &v1.RepoFilter{
173-
Id: repoID,
174-
})
170+
iamc, err := iam.NewClients(ctx, env.APIEndpoint, tok.AccessToken)
175171
if err != nil {
176-
return "", fmt.Errorf("listing repositories: %w", err)
172+
return "", fmt.Errorf("creating group clients: %w", err)
177173
}
178-
for _, repo := range repoList.Items {
179-
return repo.Name, nil
174+
175+
// Look up the name for each subrepository. This supports nested repos
176+
// like charts/cert-manager.
177+
var path []string
178+
parts := strings.Split(repoID, "/")
179+
for i := 1; i < len(parts); i++ {
180+
id := strings.Join(parts[:i+1], "/")
181+
182+
// Each element in the path may be a 'repo' or a 'group', so we
183+
// need to try both.
184+
var name string
185+
groupList, err := iamc.Groups().List(ctx, &iam.GroupFilter{
186+
Id: id,
187+
})
188+
if err != nil {
189+
return "", fmt.Errorf("listing groups for %s: %w", id, err)
190+
}
191+
for _, group := range groupList.Items {
192+
name = group.Name
193+
break
194+
}
195+
if name == "" {
196+
repoList, err := regc.Registry().ListRepos(ctx, &v1.RepoFilter{
197+
Id: id,
198+
})
199+
if err != nil {
200+
return "", fmt.Errorf("listing repositories for %s: %w", id, err)
201+
}
202+
for _, repo := range repoList.Items {
203+
name = repo.Name
204+
break
205+
}
206+
}
207+
if name == "" {
208+
return "", fmt.Errorf("couldn't find repository or group name for id: %s", id)
209+
}
210+
path = append(path, name)
211+
}
212+
213+
if len(path) == 0 {
214+
return "", fmt.Errorf("couldn't find full repository name for id: %s", repoID)
180215
}
181216

182-
return "", fmt.Errorf("couldn't find repository name for id: %s", repoID)
217+
return strings.Join(path, "/"), nil
183218
}
184219

185220
func newToken(ctx context.Context, audience string) (*sts.TokenPair, error) {

0 commit comments

Comments
 (0)