-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutator.go
88 lines (76 loc) · 2.21 KB
/
mutator.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
package v2
import (
"errors"
"fmt"
"net/url"
"path"
"strings"
)
const (
// MutatorsResource is the name of this resource type
MutatorsResource = "mutators"
)
// StorePrefix returns the path prefix to this resource in the store
func (m *Mutator) StorePrefix() string {
return MutatorsResource
}
// URIPath returns the path component of a mutator URI.
func (m *Mutator) URIPath() string {
return path.Join(URLPrefix, "namespaces", url.PathEscape(m.Namespace), MutatorsResource, url.PathEscape(m.Name))
}
// Validate returns an error if the mutator does not pass validation tests.
func (m *Mutator) Validate() error {
if err := ValidateName(m.Name); err != nil {
return errors.New("mutator name " + err.Error())
}
if m.Command == "" {
return errors.New("mutator command must be set")
}
if m.Namespace == "" {
return errors.New("namespace must be set")
}
return nil
}
// Update updates m with selected fields. Returns non-nil error if any of the
// selected fields are unsupported.
func (m *Mutator) Update(from *Mutator, fields ...string) error {
for _, f := range fields {
switch f {
case "Command":
m.Command = from.Command
case "Timeout":
m.Timeout = from.Timeout
case "EnvVars":
m.EnvVars = append(m.EnvVars[0:0], from.EnvVars...)
case "RuntimeAssets":
m.RuntimeAssets = append(m.RuntimeAssets[0:0], from.RuntimeAssets...)
default:
return fmt.Errorf("unsupported field: %q", f)
}
}
return nil
}
// NewMutator creates a new Mutator.
func NewMutator(meta ObjectMeta) *Mutator {
return &Mutator{ObjectMeta: meta}
}
// FixtureMutator returns a Mutator fixture for testing.
func FixtureMutator(name string) *Mutator {
return &Mutator{
Command: "command",
ObjectMeta: NewObjectMeta(name, "default"),
}
}
// MutatorFields returns a set of fields that represent that resource
func MutatorFields(r Resource) map[string]string {
resource := r.(*Mutator)
return map[string]string{
"mutator.name": resource.ObjectMeta.Name,
"mutator.namespace": resource.ObjectMeta.Namespace,
"mutator.runtime_assets": strings.Join(resource.RuntimeAssets, ","),
}
}
// SetNamespace sets the namespace of the resource.
func (m *Mutator) SetNamespace(namespace string) {
m.Namespace = namespace
}