Repository navigation
Expand file tree
/
Copy pathregistry_mirror.go
More file actions
176 lines (152 loc) · 5.34 KB
/
Copy pathregistry_mirror.go
File metadata and controls
176 lines (152 loc) · 5.34 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cri
import (
"errors"
"fmt"
"net/url"
"github.com/siderolabs/gen/ensure"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/config/internal/registry"
"github.com/siderolabs/talos/pkg/machinery/config/types/meta"
"github.com/siderolabs/talos/pkg/machinery/config/validation"
)
//docgen:jsonschema
// RegistryMirrorConfig defines the RegistryMirrorConfig configuration name.
const RegistryMirrorConfig = "RegistryMirrorConfig"
func init() {
registry.Register(RegistryMirrorConfig, func(version string) config.Document {
switch version {
case "v1alpha1": //nolint:goconst
return &RegistryMirrorConfigV1Alpha1{}
default:
return nil
}
})
}
// Check interfaces.
var (
_ config.RegistryMirrorConfigDocument = &RegistryMirrorConfigV1Alpha1{}
_ config.Validator = &RegistryMirrorConfigV1Alpha1{}
_ config.NamedDocument = &RegistryMirrorConfigV1Alpha1{}
)
// RegistryMirrorConfigV1Alpha1 configures an image registry mirror.
//
// examples:
// - value: exampleRegistryMirrorConfigVAlpha1()
// alias: RegistryMirrorConfig
// schemaRoot: true
// schemaMeta: v1alpha1/RegistryMirrorConfig
type RegistryMirrorConfigV1Alpha1 struct {
meta.Meta `yaml:",inline"`
// description: |
// Registry name to apply the mirror configuration to.
//
// Registry name is the first segment of image identifier, with 'docker.io'
// being default one.
//
// A special name '*' can be used to define mirror configuration
// that applies to all registries.
// schemaRequired: true
MetaName string `yaml:"name"`
// description: |
// List of mirror endpoints for the registry.
// Mirrors will be used in the order they are specified,
// falling back to the default registry is `skipFallback` is not set to true.
RegistryEndpoints []RegistryEndpoint `yaml:"endpoints,omitempty"`
// description: |
// Skip fallback to the original registry if none of the mirrors are available
// or contain the requested image.
RegistrySkipFallback *bool `yaml:"skipFallback,omitempty"`
}
// RegistryEndpoint defines a registry mirror endpoint.
type RegistryEndpoint struct {
// description: |
// The URL of the registry mirror endpoint.
// schemaRequired: true
// examples:
// - value: >
// "https://my-registry-mirror.local:5000"
// schema:
// type: string
// pattern: "^(http|https)://"
EndpointURL meta.URL `yaml:"url"`
// description: |
// Use endpoint path as supplied, without adding `/v2/` suffix.
EndpointOverridePath *bool `yaml:"overridePath,omitempty"`
}
// NewRegistryMirrorConfigV1Alpha1 creates a new RegistryMirrorConfig config document.
func NewRegistryMirrorConfigV1Alpha1(name string) *RegistryMirrorConfigV1Alpha1 {
return &RegistryMirrorConfigV1Alpha1{
Meta: meta.Meta{
MetaKind: RegistryMirrorConfig,
MetaAPIVersion: "v1alpha1",
},
MetaName: name,
}
}
func exampleRegistryMirrorConfigVAlpha1() *RegistryMirrorConfigV1Alpha1 {
cfg := NewRegistryMirrorConfigV1Alpha1("registry.k8s.io")
cfg.RegistrySkipFallback = new(true)
cfg.RegistryEndpoints = []RegistryEndpoint{
{
EndpointURL: meta.URL{URL: ensure.Value(url.Parse("https://my-private-registry.local:5000"))},
},
{
EndpointURL: meta.URL{URL: ensure.Value(url.Parse("http://my-harbor/v2/registry-k8s.io/"))},
EndpointOverridePath: new(true),
},
}
return cfg
}
// Clone implements config.Document interface.
func (s *RegistryMirrorConfigV1Alpha1) Clone() config.Document {
return s.DeepCopy()
}
// Name implements config.Document interface.
func (s *RegistryMirrorConfigV1Alpha1) Name() string {
return s.MetaName
}
// Validate implements config.Validator interface.
func (s *RegistryMirrorConfigV1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]string, error) {
var (
errs error
warnings []string
)
if s.MetaName == "" {
errs = errors.Join(errs, errors.New("name must be specified"))
}
for idx, ep := range s.RegistryEndpoints {
if ep.EndpointURL.URL == nil {
errs = errors.Join(errs, fmt.Errorf("endpoints[%d].url must be specified", idx))
} else {
switch ep.EndpointURL.URL.Scheme {
case "http", "https":
default:
errs = errors.Join(errs, fmt.Errorf("endpoints[%d].url has unsupported scheme: %q", idx, ep.EndpointURL.URL.Scheme))
}
}
}
return warnings, errs
}
// Endpoints implements RegistryMirrorConfig interface.
func (s *RegistryMirrorConfigV1Alpha1) Endpoints() []config.RegistryEndpointConfig {
return xslices.Map(s.RegistryEndpoints, func(ep RegistryEndpoint) config.RegistryEndpointConfig {
return ep
})
}
// SkipFallback implements RegistryMirrorConfig interface.
func (s *RegistryMirrorConfigV1Alpha1) SkipFallback() bool {
return pointer.SafeDeref(s.RegistrySkipFallback)
}
// Endpoint implements RegistryEndpointConfig interface.
func (ep RegistryEndpoint) Endpoint() string {
return ep.EndpointURL.String()
}
// OverridePath implements RegistryEndpointConfig interface.
func (ep RegistryEndpoint) OverridePath() bool {
return pointer.SafeDeref(ep.EndpointOverridePath)
}