-
Notifications
You must be signed in to change notification settings - Fork 474
/
port_mapping_checker.go
109 lines (95 loc) · 3.34 KB
/
port_mapping_checker.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
package services
import (
"fmt"
"strings"
apps_v1 "k8s.io/api/apps/v1"
core_v1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
)
type PortMappingChecker struct {
Service v1.Service
Deployments []apps_v1.Deployment
Pods []core_v1.Pod
}
func (p PortMappingChecker) Check() ([]*models.IstioCheck, bool) {
validations := make([]*models.IstioCheck, 0)
// Check Port naming for services in the service mesh
if p.hasMatchingPodsWithSidecar(p.Service) {
for portIndex, sp := range p.Service.Spec.Ports {
if _, ok := p.Service.Labels["kiali_wizard"]; ok || strings.ToLower(string(sp.Protocol)) == "udp" {
continue
} else if sp.AppProtocol != nil {
if !kubernetes.MatchPortAppProtocolWithValidProtocols(sp.AppProtocol) {
validation := models.Build("port.appprotocol.mismatch", fmt.Sprintf("spec/ports[%d]", portIndex))
validations = append(validations, &validation)
}
} else if !kubernetes.MatchPortNameWithValidProtocols(sp.Name) {
validation := models.Build("port.name.mismatch", fmt.Sprintf("spec/ports[%d]", portIndex))
validations = append(validations, &validation)
}
}
}
// Ignoring istio-system Services as some ports are used for debug purposes and not exposed in deployments
if config.IsIstioNamespace(p.Service.Namespace) {
log.Tracef("Skipping Port matching check for Service %s from Istio Namespace %s", p.Service.Name, p.Service.Namespace)
return validations, len(validations) == 0
}
if deployment := p.findMatchingDeployment(p.Service.Spec.Selector); deployment != nil {
p.matchPorts(&p.Service, deployment, &validations)
}
return validations, len(validations) == 0
}
func (p PortMappingChecker) hasMatchingPodsWithSidecar(service v1.Service) bool {
sPods := models.Pods{}
sPods.Parse(kubernetes.FilterPodsByService(&service, p.Pods))
return sPods.HasIstioSidecar()
}
func (p PortMappingChecker) findMatchingDeployment(selectors map[string]string) *apps_v1.Deployment {
if len(selectors) == 0 {
return nil
}
selector := labels.SelectorFromSet(labels.Set(selectors))
for _, d := range p.Deployments {
labelSet := labels.Set(d.Labels)
if selector.Matches(labelSet) {
return &d
}
}
return nil
}
func (p PortMappingChecker) matchPorts(service *v1.Service, deployment *apps_v1.Deployment, validations *[]*models.IstioCheck) {
Service:
for portIndex, sp := range service.Spec.Ports {
if sp.TargetPort.Type == intstr.String && sp.TargetPort.StrVal != "" {
// Check port name in this case
for _, c := range deployment.Spec.Template.Spec.Containers {
for _, cp := range c.Ports {
if cp.Name == sp.TargetPort.StrVal {
continue Service
}
}
}
} else {
portNumber := sp.Port
if sp.TargetPort.Type == intstr.Int && sp.TargetPort.IntVal > 0 {
// Check port number from here
portNumber = sp.TargetPort.IntVal
}
for _, c := range deployment.Spec.Template.Spec.Containers {
for _, cp := range c.Ports {
if cp.ContainerPort == portNumber {
continue Service
}
}
}
}
validation := models.Build("service.deployment.port.mismatch", fmt.Sprintf("spec/ports[%d]", portIndex))
*validations = append(*validations, &validation)
}
}