This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
/
migrate.go
227 lines (192 loc) · 6.11 KB
/
migrate.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Copyright 2021 GramLabs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sfio
import (
"net/url"
"strings"
"github.com/thestormforge/konjure/pkg/filters"
redskyv1alpha1 "github.com/thestormforge/optimize-controller/api/v1alpha1"
redskyv1beta1 "github.com/thestormforge/optimize-controller/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// ExperimentMigrationFilter is a KYAML filter for perform experiment migration.
type ExperimentMigrationFilter struct {
}
// Filter applies migration changes to all recognized experiment nodes in the supplied list.
func (f *ExperimentMigrationFilter) Filter(node *yaml.RNode) (*yaml.RNode, error) {
return node.Pipe(
yaml.Tee(
filters.FilterOne(&filters.ResourceMetaFilter{
Group: redskyv1alpha1.GroupVersion.Group,
Version: redskyv1alpha1.GroupVersion.Version,
Kind: "Experiment",
}),
yaml.FilterFunc(f.MigrateExperimentV1alpha1),
),
yaml.Tee(
filters.FilterOne(&filters.ResourceMetaFilter{
Group: redskyv1beta1.GroupVersion.Group,
Version: redskyv1beta1.GroupVersion.Version,
Kind: "Experiment",
}),
yaml.FilterFunc(f.MigrateExperimentV1beta1),
),
)
}
// MigrateExperimentV1beta1 converts a resource node from a v1beta1 Experiment to the latest format.
func (f *ExperimentMigrationFilter) MigrateExperimentV1beta1(node *yaml.RNode) (*yaml.RNode, error) {
return node.Pipe()
}
// MigrateExperimentV1alpha1 converts a resource node from a v1alpha1 Experiment to a v1beta1 Experiment.
func (f *ExperimentMigrationFilter) MigrateExperimentV1alpha1(node *yaml.RNode) (*yaml.RNode, error) {
return node.Pipe(
// Remove the "# trial", "# job", "# pod" comments if present
yaml.Tee(
yaml.Lookup("spec"), ClearFieldComment("template", "# trial"),
yaml.Lookup("spec"), ClearFieldComment("template", "# job"),
yaml.Lookup("spec"), ClearFieldComment("template", "# pod"),
),
// Rename the template fields
yaml.Tee(
yaml.Lookup("spec"), RenameField("template", "trialTemplate"),
yaml.Lookup("spec"), RenameField("template", "jobTemplate"),
),
// Migrate the parameters
yaml.Tee(
yaml.Lookup("spec", "parameters"), yaml.FilterFunc(f.migrateParametersV1alpha1),
),
// Migrate the metrics
yaml.Tee(
yaml.Lookup("spec", "metrics"), yaml.FilterFunc(f.migrateMetricsV1alpha1),
),
// Finally, set the apiVersion
yaml.Tee(
yaml.SetField("apiVersion", yaml.NewStringRNode(redskyv1beta1.GroupVersion.String())),
),
)
}
func (f *ExperimentMigrationFilter) migrateParametersV1alpha1(node *yaml.RNode) (*yaml.RNode, error) {
elements, err := node.Elements()
if err != nil {
return nil, err
}
for _, e := range elements {
err := e.PipeE(
// If values is non-empty, remove min/max
yaml.Tee(
Has(yaml.Lookup("values", "-")),
yaml.Tee(yaml.Clear("min")),
yaml.Tee(yaml.Clear("max")),
),
)
if err != nil {
return nil, err
}
}
return node, nil
}
func (f *ExperimentMigrationFilter) migrateMetricsV1alpha1(node *yaml.RNode) (*yaml.RNode, error) {
elements, err := node.Elements()
if err != nil {
return nil, err
}
for _, e := range elements {
m := metric{}
if err := e.YNode().Decode(&m); err != nil {
return nil, err
}
err := e.PipeE(
// Change metric type "local" to "kubernetes"
yaml.Tee(
yaml.MatchField("type", "local"),
yaml.Set(yaml.NewStringRNode(string(redskyv1beta1.MetricKubernetes))),
),
// Change type "pods" to "" and add "target: { kind: PodList }"
yaml.Tee(
Has(yaml.MatchField("type", "pods")),
yaml.Tee(yaml.Clear("type")),
yaml.SetField("target", yaml.NewMapRNode(&map[string]string{
"kind": "PodList",
})),
),
// Set or clear the URL field
yaml.Tee(
m.setURLField("url"),
),
// Rename the selector to target (must happen AFTER setting the "url" field because we unmarshal it)
yaml.Tee(
RenameField("selector", "target"),
),
// Remove fields that no longer exist
yaml.Tee(yaml.Clear("path")),
yaml.Tee(yaml.Clear("port")),
yaml.Tee(yaml.Clear("scheme")),
)
if err != nil {
return nil, err
}
}
return node, nil
}
// metric represents the parts of a v1alpha1 metric required for producing a v1beta1 metric URL.
type metric struct {
Type string `yaml:"type"`
Scheme string `yaml:"scheme"`
Selector *metav1.LabelSelector `yaml:"selector"`
Port IntOrString `yaml:"port"`
Path string `yaml:"path"`
}
// setURLField returns a filter that will set the named field with the metric URL.
func (m *metric) setURLField(name string) yaml.Filter {
switch m.Type {
case "prometheus":
if m.Selector == nil || (len(m.Selector.MatchLabels) == 1 && m.Selector.MatchLabels["app"] == "prometheus") {
return yaml.Clear(name)
}
case "datadog":
u := url.URL{RawQuery: url.Values{"aggregator": []string{m.Scheme}}.Encode()}
return yaml.SetField(name, yaml.NewStringRNode(u.String()))
case "jsonpath":
// Don't return empty
default:
return yaml.Clear(name)
}
u := url.URL{
Scheme: m.Scheme,
Host: redskyv1alpha1.LegacyHostnamePlaceholder,
Path: m.Path,
}
if u.Scheme == "" {
u.Scheme = "http"
}
if m.Port.IntValue() > 0 {
u.Host += ":" + m.Port.String()
}
if p := strings.SplitN(m.Path, "?", 2); len(p) > 1 {
u.Path = p[0]
u.RawQuery = p[1]
}
return yaml.SetField(name, yaml.NewStringRNode(u.String()))
}
type IntOrString struct {
intstr.IntOrString
}
func (is *IntOrString) UnmarshalYAML(value *yaml.Node) error {
if value.Tag == yaml.NodeTagInt {
is.Type = intstr.Int
return value.Decode(&is.IntVal)
}
return value.Decode(&is.StrVal)
}