-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
labels.go
169 lines (147 loc) · 4.21 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
164
165
166
167
168
169
/*
Copyright 2019 The Skaffold Authors
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 manifest
import (
"context"
"strings"
apimachinery "k8s.io/apimachinery/pkg/runtime/schema"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
)
type ResourceSelectorLabels struct {
allowlist map[apimachinery.GroupKind]latest.ResourceFilter
denylist map[apimachinery.GroupKind]latest.ResourceFilter
}
func NewResourceSelectorLabels(allowlist map[apimachinery.GroupKind]latest.ResourceFilter, denylist map[apimachinery.GroupKind]latest.ResourceFilter) *ResourceSelectorLabels {
return &ResourceSelectorLabels{
allowlist: allowlist,
denylist: denylist,
}
}
func (rsi *ResourceSelectorLabels) allowByGroupKind(gk apimachinery.GroupKind) bool {
if _, allowed := rsi.allowlist[gk]; allowed {
// TODO(aaron-prindle) see if it makes sense to make this only use the allowlist...
if rf, disallowed := rsi.denylist[gk]; disallowed {
for _, s := range rf.Labels {
if s == ".*" {
return false
}
}
for _, s := range rf.Image {
if s == ".*" {
return false
}
}
}
return true
}
return false
}
func (rsi *ResourceSelectorLabels) allowByNavpath(gk apimachinery.GroupKind, navpath string, k string) (string, bool) {
for _, w := range ConfigConnectorResourceSelector {
if w.Matches(gk.Group, gk.Kind) {
if k != metadataField {
return "", false
}
return "labels", true
}
}
if rf, ok := rsi.denylist[gk]; ok {
for _, denypath := range rf.Labels {
if denypath == ".*" {
return "", false
}
// truncate the last part of the labels path and see if this matches
lastDot := strings.LastIndex(denypath, ".")
if lastDot == -1 {
// no dot exists in denypath, denypath is invalid
break
}
if navpath == denypath[:lastDot] {
return "", false
}
}
}
if rf, ok := rsi.allowlist[gk]; ok {
for _, allowpath := range rf.Labels {
if allowpath == ".*" {
if k != metadataField {
return "", false
}
return "labels", true
}
// truncate the last part of the labels path and see if this matches
lastDot := strings.LastIndex(allowpath, ".")
if lastDot == -1 {
// no dot exists in allowpath, allowpath is invalid
continue
}
if navpath == allowpath[:lastDot] {
return allowpath[lastDot+1:], true
}
}
}
return "", false
}
// SetLabels add labels to a list of Kubernetes manifests.
func (l *ManifestList) SetLabels(labels map[string]string, rs ResourceSelector) (ManifestList, error) {
if len(labels) == 0 {
return *l, nil
}
replacer := newLabelsSetter(labels)
updated, err := l.Visit(replacer, rs)
if err != nil {
return nil, labelSettingErr(err)
}
log.Entry(context.TODO()).Debugln("manifests with labels", updated.String())
return updated, nil
}
type labelsSetter struct {
labels map[string]string
}
func newLabelsSetter(labels map[string]string) *labelsSetter {
return &labelsSetter{
labels: labels,
}
}
func (r *labelsSetter) Visit(gk apimachinery.GroupKind, navpath string, o map[string]interface{}, k string, v interface{}, rs ResourceSelector) bool {
labelsField, ok := rs.allowByNavpath(gk, navpath, k)
if !ok {
return true
}
if len(r.labels) == 0 {
return false
}
metadata, ok := v.(map[string]interface{})
if !ok {
return true
}
l, present := metadata[labelsField]
if !present {
metadata[labelsField] = r.labels
return false
}
labels, ok := l.(map[string]interface{})
if !ok {
return true
}
for k, v := range r.labels {
_, present := labels[k]
if !present { // Don't overwrite existing labels
labels[k] = v
} else if k == "skaffold.dev/run-id" { // Always override skaffold run-id
labels[k] = v
}
}
return false
}