-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathartifactregistry.go
96 lines (83 loc) · 3.03 KB
/
artifactregistry.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package registry
import (
"context"
"fmt"
"strings"
artifactregistry "cloud.google.com/go/artifactregistry/apiv1"
"cloud.google.com/go/artifactregistry/apiv1/artifactregistrypb"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/artifacts/registry"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/gcp/gke"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
func Register() {
registry.Register("gcp/artifactregistry", func(ctx context.Context, ck cfg.Configuration) (registry.Manager, error) {
cluster, err := gke.ConfiguredCluster(ctx, ck)
if err != nil {
return nil, err
}
return manager{cluster}, nil
})
oci.RegisterDomainKeychain("pkg.dev", DefaultKeychain, oci.Keychain_UseOnWrites)
}
type manager struct {
cluster *gke.Cluster
}
func (m manager) Access() oci.RegistryAccess {
return oci.RegistryAccess{
InsecureRegistry: false,
Keychain: keychain{m.cluster.TokenSource},
}
}
func (m manager) AllocateName(repository, tag string) compute.Computable[oci.RepositoryWithParent] {
return compute.Inline(tasks.Action("artifactregistry.allocate-repository").Arg("repository", repository),
func(ctx context.Context) (oci.RepositoryWithParent, error) {
client, err := artifactregistry.NewClient(ctx, option.WithTokenSource(m.cluster.TokenSource))
if err != nil {
return oci.RepositoryWithParent{}, err
}
const repoID = "namespace-managed"
location := region(m.cluster.Cluster.Location)
op, err := client.CreateRepository(ctx, &artifactregistrypb.CreateRepositoryRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", m.cluster.ProjectID, location),
RepositoryId: repoID,
Repository: &artifactregistrypb.Repository{
Format: artifactregistrypb.Repository_DOCKER,
},
})
if err != nil {
if status.Code(err) != codes.AlreadyExists {
return oci.RepositoryWithParent{}, fnerrors.InvocationError("gcp/artifactregistry", "failed to construct request: %w", err)
}
} else if err == nil {
if _, err := op.Wait(ctx); err != nil {
return oci.RepositoryWithParent{}, fnerrors.InvocationError("gcp/artifactregistry", "failed: %w", err)
}
}
repo := oci.RepositoryWithAccess{
RegistryAccess: m.Access(),
Repository: fmt.Sprintf("%s-docker.pkg.dev/%s/%s/%s", location, m.cluster.ProjectID, repoID, repository),
UserTag: tag,
}
return oci.RepositoryWithParent{
Parent: m,
RepositoryWithAccess: repo,
}, nil
})
}
func region(loc string) string {
parts := strings.Split(loc, "-")
if len(parts) > 2 {
parts = parts[:2]
}
return strings.Join(parts, "-")
}