forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
labels.go
163 lines (145 loc) · 5.8 KB
/
labels.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
package template
import (
"fmt"
"reflect"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kmeta "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
configapi "github.com/openshift/origin/pkg/config/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// MergeInto flags
const (
OverwriteExistingDstKey = 1 << iota
ErrorOnExistingDstKey
ErrorOnDifferentDstKeyValue
)
// addReplicationControllerNestedLabels adds new label(s) to a nested labels of a single ReplicationController object
func addReplicationControllerNestedLabels(obj *kapi.ReplicationController, labels labels.Set) error {
if obj.Spec.Template.Labels == nil {
obj.Spec.Template.Labels = make(map[string]string)
}
if err := MergeInto(obj.Spec.Template.Labels, labels, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.ReplicationController.Spec.Template: %v", err)
}
if err := MergeInto(obj.Spec.Template.Labels, obj.Spec.Selector, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.ReplicationController.Spec.Template: %v", err)
}
// Selector and Spec.Template.Labels must be equal
if obj.Spec.Selector == nil {
obj.Spec.Selector = make(map[string]string)
}
if err := MergeInto(obj.Spec.Selector, obj.Spec.Template.Labels, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.ReplicationController.Spec.Selector: %v", err)
}
return nil
}
// addDeploymentNestedLabels adds new label(s) to a nested labels of a single Deployment object
func addDeploymentNestedLabels(obj *deployapi.Deployment, labels labels.Set) error {
if obj.ControllerTemplate.Template.Labels == nil {
obj.ControllerTemplate.Template.Labels = make(map[string]string)
}
if err := MergeInto(obj.ControllerTemplate.Template.Labels, labels, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.Deployment.ControllerTemplate.Template: %v", err)
}
return nil
}
// addDeploymentConfigNestedLabels adds new label(s) to a nested labels of a single DeploymentConfig object
func addDeploymentConfigNestedLabels(obj *deployapi.DeploymentConfig, labels labels.Set) error {
if obj.Template.ControllerTemplate.Template.Labels == nil {
obj.Template.ControllerTemplate.Template.Labels = make(map[string]string)
}
if err := MergeInto(obj.Template.ControllerTemplate.Template.Labels, labels, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.DeploymentConfig.Template.ControllerTemplate.Template: %v", err)
}
return nil
}
// AddObjectLabels adds new label(s) to a single runtime.Object
func AddObjectLabels(obj runtime.Object, labels labels.Set) error {
if labels == nil {
// Nothing to add
return nil
}
accessor, err := kmeta.Accessor(obj)
if err != nil {
return err
}
metaLabels := accessor.Labels()
if metaLabels == nil {
metaLabels = make(map[string]string)
}
if err := MergeInto(metaLabels, labels, ErrorOnDifferentDstKeyValue); err != nil {
return fmt.Errorf("unable to add labels to Template.%s: %v", accessor.Kind(), err)
}
accessor.SetLabels(metaLabels)
// Handle nested Labels
switch objType := obj.(type) {
case *kapi.ReplicationController:
return addReplicationControllerNestedLabels(objType, labels)
case *deployapi.Deployment:
return addDeploymentNestedLabels(objType, labels)
case *deployapi.DeploymentConfig:
return addDeploymentConfigNestedLabels(objType, labels)
}
return nil
}
// AddConfigLabels adds new label(s) to all resources defined in the given Config.
func AddConfigLabels(c *configapi.Config, labels labels.Set) errs.ValidationErrorList {
itemErrors := errs.ValidationErrorList{}
for i, in := range c.Items {
if err := AddObjectLabels(in, labels); err != nil {
reportError(&itemErrors, i, *errs.NewFieldInvalid("labels", err, fmt.Sprintf("error applying labels %v to %v", labels, in)))
}
}
return itemErrors.Prefix("Config")
}
// MergeInto merges items from a src map into a dst map.
// Returns an error when the maps are not of the same type.
// Flags:
// - ErrorOnExistingDstKey
// When set: Return an error if any of the dst keys is already set.
// - ErrorOnDifferentDstKeyValue
// When set: Return an error if any of the dst keys is already set
// to a different value than src key.
// - OverwriteDstKey
// When set: Overwrite existing dst key value with src key value.
func MergeInto(dst, src interface{}, flags int) error {
dstVal := reflect.ValueOf(dst)
srcVal := reflect.ValueOf(src)
if dstVal.Kind() != reflect.Map {
return fmt.Errorf("dst is not a valid map: %v", dstVal.Kind())
}
if srcVal.Kind() != reflect.Map {
return fmt.Errorf("src is not a valid map: %v", srcVal.Kind())
}
if dstTyp, srcTyp := dstVal.Type(), srcVal.Type(); !dstTyp.AssignableTo(srcTyp) {
return fmt.Errorf("type mismatch, can't assign '%v' to '%v'", srcTyp, dstTyp)
}
if dstVal.IsNil() {
return fmt.Errorf("dst value is nil")
}
if srcVal.IsNil() {
// Nothing to merge
return nil
}
for _, k := range srcVal.MapKeys() {
if dstVal.MapIndex(k).IsValid() {
if flags&ErrorOnExistingDstKey != 0 {
return fmt.Errorf("dst key already set (ErrorOnExistingDstKey=1), '%v'='%v'", k, dstVal.MapIndex(k))
}
if dstVal.MapIndex(k).String() != srcVal.MapIndex(k).String() {
if flags&ErrorOnDifferentDstKeyValue != 0 {
return fmt.Errorf("dst key already set to a different value (ErrorOnDifferentDstKeyValue=1), '%v'='%v'", k, dstVal.MapIndex(k))
}
if flags&OverwriteExistingDstKey != 0 {
dstVal.SetMapIndex(k, srcVal.MapIndex(k))
}
}
} else {
dstVal.SetMapIndex(k, srcVal.MapIndex(k))
}
}
return nil
}