-
Notifications
You must be signed in to change notification settings - Fork 8
/
types.go
146 lines (117 loc) · 4.39 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
141
142
143
144
145
146
// 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"
"strings"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build/registry"
"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 {
// Returns true if calls to the registry should be made over HTTP (instead of HTTPS).
IsInsecure() bool
AllocateName(repository string) compute.Computable[oci.AllocatedRepository]
AttachKeychain(oci.ImageID) (oci.AllocatedRepository, error)
}
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
}
return MakeStaticRegistry(r), nil
}
if env == "" {
return nil, ErrNoRegistry
}
return nil, fnerrors.UsageError(
fmt.Sprintf("Run `ns prepare local --env=%s` to set it up.", 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 StaticName(parent Manager, imageID oci.ImageID, insecure bool, keychain oci.Keychain) compute.Computable[oci.AllocatedRepository] {
return compute.Map(tasks.Action("registry.allocate-tag").Arg("ref", imageID.ImageRef()), compute.Inputs().
Bool("insecure", insecure).
JSON("imageID", imageID).
Indigestible("parent", parent).
Indigestible("keychain", keychain),
compute.Output{NotCacheable: true},
func(ctx context.Context, r compute.Resolved) (oci.AllocatedRepository, error) {
return oci.AllocatedRepository{
Parent: parent,
TargetRepository: oci.TargetRepository{
InsecureRegistry: insecure,
ImageID: imageID,
Keychain: keychain,
},
}, nil
})
}
func AllocateName(ctx context.Context, env cfg.Context, pkg schema.PackageName) (compute.Computable[oci.AllocatedRepository], error) {
registry, err := GetRegistry(ctx, env)
if err != nil {
return nil, err
}
return registry.AllocateName(pkg.String()), nil
}
func RawAllocateName(ctx context.Context, ck cfg.Configuration, repo string) (compute.Computable[oci.AllocatedRepository], error) {
registry, err := GetRegistryFromConfig(ctx, "", ck)
if err != nil {
return nil, err
}
return registry.AllocateName(repo), nil
}
func Precomputed(tag oci.AllocatedRepository) compute.Computable[oci.AllocatedRepository] {
return precomputedTag{tag: tag}
}
type precomputedTag struct {
tag oci.AllocatedRepository
compute.PrecomputeScoped[oci.AllocatedRepository]
}
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.tag").Arg("ref", r.tag.ImageRef())
}
func (r precomputedTag) Compute(ctx context.Context, _ compute.Resolved) (oci.AllocatedRepository, error) {
return r.tag, nil
}
func (r precomputedTag) ComputeDigest(ctx context.Context) (schema.Digest, error) {
return r.tag.ComputeDigest(ctx)
}