-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathoptions.go
More file actions
192 lines (167 loc) · 4.15 KB
/
options.go
File metadata and controls
192 lines (167 loc) · 4.15 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package localregistry
import (
"fmt"
"path"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
var (
RegistryName = "registry"
RegistryImage = "registry:2.8.1"
BuildKitImage = "moby/buildkit:master-rootless"
RegistryPort = 5000
RegistryDefaultStorage = "5Gi"
)
type Options struct {
Name string
Namespace string
LocalBuild bool
RegistryImage string
BuildKitImage string
Port int
StorageEnabled bool
StorageSize string
StorageClassName string
Resources *corev1.ResourceRequirements
Annotations map[string]string
}
func getID(o Options) string {
return path.Join(o.Namespace, o.Name)
}
func NewDefaultOptions() Options {
return Options{
Name: RegistryName,
Namespace: "",
LocalBuild: false,
RegistryImage: RegistryImage,
BuildKitImage: BuildKitImage,
Port: RegistryPort,
StorageEnabled: false,
StorageSize: RegistryDefaultStorage,
StorageClassName: "",
Resources: nil,
Annotations: map[string]string{},
}
}
func (o Options) WithName(name string) Options {
newOptions := o
if name != "" {
newOptions.Name = name
}
return newOptions
}
func (o Options) WithNamespace(namespace string) Options {
newOptions := o
if namespace != "" {
newOptions.Namespace = namespace
}
return newOptions
}
func (o Options) WithImage(image string) Options {
newOptions := o
if image != "" {
newOptions.RegistryImage = image
}
return newOptions
}
func (o Options) WithBuildKitImage(image string) Options {
newOptions := o
if image != "" {
newOptions.BuildKitImage = image
}
return newOptions
}
func (o Options) WithPort(port *int) Options {
newOptions := o
if port != nil {
newOptions.Port = *port
}
return newOptions
}
func (o Options) WithLocalBuild(localbuild bool) Options {
newOptions := o
newOptions.LocalBuild = localbuild
return newOptions
}
func (o Options) EnableStorage() Options {
newOptions := o
newOptions.StorageEnabled = true
return newOptions
}
func (o Options) WithStorageClassName(storageClassName string) Options {
newOptions := o
if storageClassName != "" {
newOptions.StorageClassName = storageClassName
}
return newOptions
}
func (o Options) WithStorageSize(storageSize string) Options {
newOptions := o
if storageSize != "" {
newOptions.StorageSize = storageSize
}
return newOptions
}
func (o Options) WithResources(resources *latest.PodResources) Options {
if resources == nil {
return o
}
// helper converts a map[string]string -> corev1.ResourceList
toList := func(src map[string]string) (corev1.ResourceList, error) {
if len(src) == 0 {
return nil, nil
}
dst := corev1.ResourceList{}
for k, v := range src {
q, err := resource.ParseQuantity(v)
if err != nil {
return nil, fmt.Errorf("invalid quantity %q for %s: %w", v, k, err)
}
dst[corev1.ResourceName(k)] = q
}
return dst, nil
}
reqs, err := toList(resources.Requests)
if reqs == nil || err != nil {
return o
}
lims, err := toList(resources.Limits)
if lims == nil || err != nil {
return o
}
o.Resources = &corev1.ResourceRequirements{
Requests: reqs,
Limits: lims,
}
return o
}
func (o Options) WithAnnotations(annotations map[string]string) Options {
newOptions := o
if annotations != nil {
newOptions.Annotations = annotations
}
return newOptions
}
func (o Options) WithLocalRegistryConfig(config *latest.LocalRegistryConfig) Options {
newOptions := o
if config != nil {
newOptions = newOptions.
WithName(config.Name).
WithNamespace(config.Namespace).
WithImage(config.Image).
WithBuildKitImage(config.BuildKitImage).
WithPort(config.Port).
WithResources(config.Resources).
WithAnnotations(config.Annotations).
WithLocalBuild(config.LocalBuild)
if config.Persistence != nil && config.Persistence.Enabled != nil &&
*config.Persistence.Enabled {
newOptions = newOptions.
EnableStorage().
WithStorageClassName(config.Persistence.StorageClassName).
WithStorageSize(config.Persistence.Size)
}
}
return newOptions
}