-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.go
140 lines (113 loc) · 4.26 KB
/
types.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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"
"errors"
"fmt"
"net/url"
"strings"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build/registry"
"namespacelabs.dev/foundation/internal/cli/fncobra/name"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var (
mapping = map[string]func(context.Context, cfg.Configuration) (Manager, error){}
ErrNoRegistry = errors.New("no registry configured")
registryConfigType = cfg.DefineConfigType[*registry.Registry]()
registryProviderConfigType = cfg.DefineConfigType[*registry.Provider]()
)
// XXX use external plugin system.
func Register(name string, make func(context.Context, cfg.Configuration) (Manager, error)) {
mapping[strings.ToLower(name)] = make
}
type Manager interface {
Access() oci.RegistryAccess
AllocateName(repository, tag string) compute.Computable[oci.RepositoryWithParent]
}
func GetRegistry(ctx context.Context, env cfg.Context) (Manager, error) {
return GetRegistryFromConfig(ctx, env.Environment().Name, env.Configuration())
}
func GetRegistryFromConfig(ctx context.Context, env string, cfg cfg.Configuration) (Manager, error) {
p, ok := registryProviderConfigType.CheckGet(cfg)
if ok && p.Provider != "" {
return getRegistryByName(ctx, cfg, p.Provider)
}
r, ok := registryConfigType.CheckGet(cfg)
if ok && r.Url != "" {
if trimmed := strings.TrimPrefix(r.Url, "http://"); trimmed != r.Url {
r.Url = trimmed
r.Insecure = true
}
if r.Transport != nil && r.Transport.Ssh != nil {
if r.Transport.Ssh.RemoteAddr == "" {
// We pass a dummy scheme just to facilitate parsing.
parsed, err := url.Parse("//" + r.Url)
if err != nil {
return nil, fnerrors.New("transport.ssh: failed to compute remote address while parsing url: %w", err)
}
r.Transport.Ssh.RemoteAddr = parsed.Host
}
}
return MakeStaticRegistry(r), nil
}
if env == "" {
return nil, ErrNoRegistry
}
return nil, fnerrors.UsageError(
fmt.Sprintf("Run `%s prepare local --env=%s` to set it up.", name.CmdName, env),
"No registry configured in the environment %q.", env)
}
func getRegistryByName(ctx context.Context, conf cfg.Configuration, name string) (Manager, error) {
if m, ok := mapping[name]; ok {
return m(ctx, conf)
}
return nil, fnerrors.New("%q is not a known registry provider", name)
}
func StaticRepository(parent Manager, repository string, access oci.RegistryAccess) compute.Computable[oci.RepositoryWithParent] {
return compute.Map(tasks.Action("registry.static-repository").Arg("repository", repository), compute.Inputs().
Bool("insecure", access.InsecureRegistry).
Proto("transport", access.Transport).
JSON("repository", repository).
Indigestible("parent", parent).
Indigestible("keychain", access.Keychain),
compute.Output{NotCacheable: true},
func(ctx context.Context, r compute.Resolved) (oci.RepositoryWithParent, error) {
return oci.RepositoryWithParent{
Parent: parent,
RepositoryWithAccess: oci.RepositoryWithAccess{
RegistryAccess: access,
Repository: repository,
},
}, nil
})
}
func Precomputed(tag oci.RepositoryWithParent) compute.Computable[oci.RepositoryWithParent] {
return precomputedTag{tag: tag}
}
type precomputedTag struct {
tag oci.RepositoryWithParent
compute.PrecomputeScoped[oci.RepositoryWithParent]
}
var _ compute.Digestible = precomputedTag{}
func (r precomputedTag) Inputs() *compute.In {
return compute.Inputs().JSON("tag", r.tag)
}
func (r precomputedTag) Output() compute.Output {
return compute.Output{NotCacheable: true}
}
func (r precomputedTag) Action() *tasks.ActionEvent {
return tasks.Action("registry.precomputed-repository").Arg("repository", r.tag.Repository)
}
func (r precomputedTag) Compute(ctx context.Context, _ compute.Resolved) (oci.RepositoryWithParent, error) {
return r.tag, nil
}
func (r precomputedTag) ComputeDigest(ctx context.Context) (schema.Digest, error) {
return r.tag.ComputeDigest(ctx)
}