英文:
go-containerregistry package lists GCR repositories from all projects / organizations
问题
我正在使用以下代码列出在GCR中可用的docker
仓库:
package main
import (
"context"
"fmt"
"log"
"github.com/google/go-containerregistry/pkg/authn"
gcr "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func main() {
auth, err := google.NewGcloudAuthenticator()
if err != nil {
log.Fatal(err)
}
fmt.Println(auth)
registry, err := gcr.NewRegistry("gcr.io")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
repos, err := remote.Catalog(ctx, registry, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
log.Fatal(err)
}
for _, repoName := range repos {
fmt.Println(repoName)
}
}
问题是,尽管我已经显式执行了gcloud config set project
命令来设置特定的项目,但上述程序的调用将列出我gcloud
访问权限下所有组织、所有项目的仓库。
有没有办法限制上述代码,以便只列出特定组织/项目的gcr仓库?
英文:
I am using the following code to list my docker
repositories that are available in GCR
package main
import (
"context"
"fmt"
"log"
"github.com/google/go-containerregistry/pkg/authn"
gcr "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func main() {
auth, err := google.NewGcloudAuthenticator()
if err != nil {
log.Fatal(err)
}
fmt.Println(auth)
registry, err := gcr.NewRegistry("gcr.io")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
repos, err := remote.Catalog(ctx, registry, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
log.Fatal(err)
}
for _, repoName := range repos {
fmt.Println(repoName)
}
}
The problem is that despite the fact that I have performed an explicit gcloud config set project
to a specific project, invocation of the above program will list ALL repos of ALL organizations and ALL projects my gcloud
has access to.
Is there a way to limit the code above so that it lists the gcr repos of a specific org/project?
答案1
得分: -1
请注意,gcloud config set project
只适用于 gcloud
(CLI)命令,而不适用于 SDK。
看起来这个 SDK 只实现了 Docker API,而 Docker API 并不理解项目的概念。建议您迁移到 Artifact Registry。您可以使用 REST 资源 projects.locations.repositories
的列表方法轻松获取特定项目的存储库列表。在这里,您可以找到一个 HTTP GET 的示例:
GET /v1beta2/{parent=projects/*/locations/*}/repositories
此答案是为了使评论中给出的解决方案对社区更加可见而创建和编辑的。
英文:
Please note that gcloud config set project
only applies to gcloud
(CLI) commands but not SDKs.
Seems like this SDK only implements the Docker API, which does not understand the concept of projects. The suggestion here is to migrate to Artifact Registry. You could easily get a list of the repos of a particular project using the list method of the REST Resource projects.locations.repositories
. Here you can find an HTTP GET example:
GET /v1beta2/{parent=projects/*/locations/*}/repositories
This answer was created and edited to make the solution given in the comments more visible to the community.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论