-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
135 lines (117 loc) · 3.51 KB
/
common.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
package controlapi
import (
"regexp"
"strings"
"github.com/docker/docker/pkg/plugingetter"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/ipamapi"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/manager/allocator"
"github.com/docker/swarmkit/manager/state/store"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var isValidDNSName = regexp.MustCompile(`^[a-zA-Z0-9](?:[-_]*[A-Za-z0-9]+)*$`)
// configs and secrets have different naming requirements from tasks and services
var isValidConfigOrSecretName = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[a-zA-Z0-9-_.]*[a-zA-Z0-9])?$`)
func buildFilters(by func(string) store.By, values []string) store.By {
filters := make([]store.By, 0, len(values))
for _, v := range values {
filters = append(filters, by(v))
}
return store.Or(filters...)
}
func filterContains(match string, candidates []string) bool {
if len(candidates) == 0 {
return true
}
for _, c := range candidates {
if c == match {
return true
}
}
return false
}
func filterContainsPrefix(match string, candidates []string) bool {
if len(candidates) == 0 {
return true
}
for _, c := range candidates {
if strings.HasPrefix(match, c) {
return true
}
}
return false
}
func filterMatchLabels(match map[string]string, candidates map[string]string) bool {
if len(candidates) == 0 {
return true
}
for k, v := range candidates {
c, ok := match[k]
if !ok {
return false
}
if v != "" && v != c {
return false
}
}
return true
}
func validateAnnotations(m api.Annotations) error {
if m.Name == "" {
return status.Errorf(codes.InvalidArgument, "meta: name must be provided")
}
if !isValidDNSName.MatchString(m.Name) {
// if the name doesn't match the regex
return status.Errorf(codes.InvalidArgument, "name must be valid as a DNS name component")
}
if len(m.Name) > 63 {
// DNS labels are limited to 63 characters
return status.Errorf(codes.InvalidArgument, "name must be 63 characters or fewer")
}
return nil
}
func validateConfigOrSecretAnnotations(m api.Annotations) error {
if m.Name == "" {
return status.Errorf(codes.InvalidArgument, "name must be provided")
} else if len(m.Name) > 64 || !isValidConfigOrSecretName.MatchString(m.Name) {
// if the name doesn't match the regex
return status.Errorf(codes.InvalidArgument,
"invalid name, only 64 [a-zA-Z0-9-_.] characters allowed, and the start and end character must be [a-zA-Z0-9]")
}
return nil
}
func validateDriver(driver *api.Driver, pg plugingetter.PluginGetter, pluginType string) error {
if driver == nil {
// It is ok to not specify the driver. We will choose
// a default driver.
return nil
}
if driver.Name == "" {
return status.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
}
// First check against the known drivers
switch pluginType {
case ipamapi.PluginEndpointType:
if strings.ToLower(driver.Name) == ipamapi.DefaultIPAM {
return nil
}
case driverapi.NetworkPluginEndpointType:
if allocator.IsBuiltInNetworkDriver(driver.Name) {
return nil
}
default:
}
if pg == nil {
return status.Errorf(codes.InvalidArgument, "plugin %s not supported", driver.Name)
}
p, err := pg.Get(driver.Name, pluginType, plugingetter.Lookup)
if err != nil {
return status.Errorf(codes.InvalidArgument, "error during lookup of plugin %s", driver.Name)
}
if p.IsV1() {
return status.Errorf(codes.InvalidArgument, "legacy plugin %s of type %s is not supported in swarm mode", driver.Name, pluginType)
}
return nil
}