/
validation.go
112 lines (99 loc) · 3.48 KB
/
validation.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
// Copyright Istio 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 model
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/validation/agent"
)
// UnixAddressPrefix is the prefix used to indicate an address is for a Unix Domain socket. It is used in
// ServiceEntry.Endpoint.Address message.
const (
UnixAddressPrefix = "unix://"
PodIPAddressPrefix = "0.0.0.0"
LocalhostAddressPrefix = "127.0.0.1"
PodIPv6AddressPrefix = "::"
LocalhostIPv6AddressPrefix = "::1"
)
// Validate ensures that the service object is well-defined
func (s *Service) Validate() error {
var errs error
if len(s.Hostname) == 0 {
errs = multierror.Append(errs, fmt.Errorf("invalid empty hostname"))
}
parts := strings.Split(string(s.Hostname), ".")
for _, part := range parts {
if !labels.IsDNS1123Label(part) {
errs = multierror.Append(errs, fmt.Errorf("invalid hostname part: %q", part))
}
}
// Require at least one port
if len(s.Ports) == 0 {
errs = multierror.Append(errs, fmt.Errorf("service must have at least one declared port"))
}
// Port names can be empty if there exists only one port
for _, port := range s.Ports {
if port.Name == "" {
if len(s.Ports) > 1 {
errs = multierror.Append(errs,
fmt.Errorf("empty port names are not allowed for services with multiple ports"))
}
} else if !labels.IsDNS1123Label(port.Name) {
errs = multierror.Append(errs, fmt.Errorf("invalid name: %q", port.Name))
}
if err := agent.ValidatePort(port.Port); err != nil {
errs = multierror.Append(errs,
fmt.Errorf("invalid service port value %d for %q: %v", port.Port, port.Name, err))
}
}
return errs
}
// Validate ensures that the service instance is well-defined
func (instance *ServiceInstance) Validate() error {
var errs error
if instance.Service == nil {
errs = multierror.Append(errs, fmt.Errorf("missing service in the instance"))
} else if err := instance.Service.Validate(); err != nil {
errs = multierror.Append(errs, err)
}
if instance.Endpoint != nil {
if err := instance.Endpoint.Labels.Validate(); err != nil {
errs = multierror.Append(errs, err)
}
if err := agent.ValidatePort(int(instance.Endpoint.EndpointPort)); err != nil {
errs = multierror.Append(errs, err)
}
}
port := instance.ServicePort
if port == nil {
errs = multierror.Append(errs, fmt.Errorf("missing service port"))
} else if instance.Service != nil {
expected, ok := instance.Service.Ports.Get(port.Name)
if !ok {
errs = multierror.Append(errs, fmt.Errorf("missing service port %q", port.Name))
} else {
if expected.Port != port.Port {
errs = multierror.Append(errs,
fmt.Errorf("unexpected service port value %d, expected %d", port.Port, expected.Port))
}
if expected.Protocol != port.Protocol {
errs = multierror.Append(errs,
fmt.Errorf("unexpected service protocol %s, expected %s", port.Protocol, expected.Protocol))
}
}
}
return errs
}