-
Notifications
You must be signed in to change notification settings - Fork 9
/
registry.go
94 lines (78 loc) · 2.45 KB
/
registry.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
// 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 nscloud
import (
"context"
"strings"
"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/nscloud/api"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var DefaultKeychain oci.Keychain = api.DefaultKeychain
type nscloudRegistry struct {
registry *api.ImageRegistry
}
func RegisterRegistry() {
registry.Register("nscloud", func(ctx context.Context, ck cfg.Configuration) (registry.Manager, error) {
return nscloudRegistry{}, nil
})
}
func (r nscloudRegistry) Access() oci.RegistryAccess {
return oci.RegistryAccess{
InsecureRegistry: false,
Keychain: DefaultKeychain,
}
}
func (r nscloudRegistry) AllocateName(repository, tag string) compute.Computable[oci.RepositoryWithParent] {
return compute.Map(tasks.Action("nscloud.allocate-repository").Arg("repository", repository),
compute.Inputs().Str("repository", repository),
compute.Output{},
func(ctx context.Context, _ compute.Resolved) (oci.RepositoryWithParent, error) {
registry, err := r.fetchRegistry(ctx)
if err != nil {
return oci.RepositoryWithParent{}, err
}
url := registry.EndpointAddress
if url == "" {
return oci.RepositoryWithParent{}, fnerrors.InternalError("registry is missing endpoint")
}
if registry.Repository != "" {
if strings.HasSuffix(url, "/") {
url += registry.Repository
} else {
url += "/" + registry.Repository
}
}
if strings.HasSuffix(url, "/") {
url += repository
} else {
url += "/" + repository
}
return oci.RepositoryWithParent{
Parent: r,
RepositoryWithAccess: oci.RepositoryWithAccess{
Repository: url,
UserTag: tag,
RegistryAccess: r.Access(),
},
}, nil
})
}
func (r nscloudRegistry) fetchRegistry(ctx context.Context) (*api.ImageRegistry, error) {
if r.registry != nil {
return r.registry, nil
}
rs, err := api.GetImageRegistry(ctx, api.Methods)
if err != nil {
return nil, err
}
if rs.NSCR == nil {
return nil, fnerrors.InternalError("expected nscr to be in response")
}
return rs.NSCR, nil
}