-
Notifications
You must be signed in to change notification settings - Fork 193
/
validate.go
207 lines (188 loc) · 7.04 KB
/
validate.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*-
* Copyright (c) 2016-2021, F5 Networks, Inc.
*
* 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 controller
import (
"errors"
"fmt"
cisapiv1 "github.com/F5Networks/k8s-bigip-ctlr/v3/config/apis/cis/v1"
log "github.com/F5Networks/k8s-bigip-ctlr/v3/pkg/vlogger"
"k8s.io/apimachinery/pkg/util/intstr"
)
func (ctlr *Controller) checkValidVirtualServer(
vsResource *cisapiv1.VirtualServer,
) bool {
vsNamespace := vsResource.ObjectMeta.Namespace
vsName := vsResource.ObjectMeta.Name
vkey := fmt.Sprintf("%s/%s", vsNamespace, vsName)
crInf, ok := ctlr.getNamespacedCRInformer(vsNamespace)
if !ok {
log.Warningf("%v Informer not found for namespace: %v", ctlr.getMultiClusterLog(), vsNamespace)
return false
}
// Check if the virtual exists and valid for us.
_, virtualFound, _ := crInf.vsInformer.GetIndexer().GetByKey(vkey)
if !virtualFound {
log.Infof("VirtualServer %s is invalid", vsName)
return false
}
// Check if HTTPTraffic is set for insecure VS
if vsResource.Spec.TLSProfileName == "" && vsResource.Spec.HTTPTraffic != "" {
log.Warningf("HTTPTraffic not allowed to be set for insecure VirtualServer: %v", vsName)
return false
}
bindAddr := vsResource.Spec.VirtualServerAddress
if ctlr.ipamHandler == nil {
// This ensures that pool-only mode only logs the message below the first
// time we see a config.
if bindAddr == "" {
log.Infof("No IP was specified for the virtual server %s", vsName)
return false
}
} else {
ipamLabel := vsResource.Spec.IPAMLabel
if ipamLabel == "" && bindAddr == "" {
log.Infof("No ipamLabel was specified for the virtual server %s", vsName)
return false
}
}
for _, pool := range vsResource.Spec.Pools {
if pool.MultiClusterServices == nil {
continue
}
for _, mcs := range pool.MultiClusterServices {
err := ctlr.checkValidExtendedService(mcs)
if err != nil {
// In case of invalid extendedServiceReference, just log the error and proceed
log.Warningf("[MultiCluster] invalid extendedServiceReference: %v for VS: %s: %v", mcs, vsName, err)
continue
}
}
}
return true
}
func (ctlr *Controller) checkValidTransportServer(
tsResource *cisapiv1.TransportServer,
) bool {
vsNamespace := tsResource.ObjectMeta.Namespace
vsName := tsResource.ObjectMeta.Name
vkey := fmt.Sprintf("%s/%s", vsNamespace, vsName)
crInf, ok := ctlr.getNamespacedCRInformer(vsNamespace)
if !ok {
log.Warningf("%v Informer not found for namespace: %v", ctlr.getMultiClusterLog(), vsNamespace)
return false
}
// Check if the virtual exists and valid for us.
_, virtualFound, _ := crInf.tsInformer.GetIndexer().GetByKey(vkey)
if !virtualFound {
log.Infof("TransportServer %s is invalid", vsName)
return false
}
bindAddr := tsResource.Spec.VirtualServerAddress
var err string
if ctlr.ipamHandler == nil {
// This ensures that pool-only mode only logs the message below the first
// time we see a config.
if bindAddr == "" {
err = fmt.Sprintf("No IP was specified for the transport server %s", vsName)
ctlr.updateResourceStatus(TransportServer, tsResource, "", "", errors.New(err))
return false
}
} else {
ipamLabel := tsResource.Spec.IPAMLabel
if ipamLabel == "" && bindAddr == "" {
err = fmt.Sprintf("No ipamLabel was specified for the transport server %s", vsName)
ctlr.updateResourceStatus(TransportServer, tsResource, "", "", errors.New(err))
return false
}
}
if tsResource.Spec.Type == "" {
tsResource.Spec.Type = "tcp"
} else if !(tsResource.Spec.Type == "udp" || tsResource.Spec.Type == "tcp" || tsResource.Spec.Type == "sctp") {
err = fmt.Sprintf("Invalid type value for transport server %s. Supported values are tcp, udp and sctp only", vsName)
ctlr.updateResourceStatus(TransportServer, tsResource, "", "", errors.New(err))
return false
}
if tsResource.Spec.Pool.MultiClusterServices != nil {
for _, mcs := range tsResource.Spec.Pool.MultiClusterServices {
err := ctlr.checkValidExtendedService(mcs)
if err != nil {
// In case of invalid extendedServiceReference, just log the error and proceed
log.Warningf("[MultiCluster] invalid extendedServiceReference: %v for TS: %s: %v", mcs, vsName, err)
continue
}
}
}
return true
}
func (ctlr *Controller) checkValidIngressLink(
il *cisapiv1.IngressLink,
) bool {
ilNamespace := il.ObjectMeta.Namespace
ilName := il.ObjectMeta.Name
ilkey := fmt.Sprintf("%s/%s", ilNamespace, ilName)
crInf, ok := ctlr.getNamespacedCRInformer(ilNamespace)
if !ok {
log.Warningf("%v Informer not found for namespace: %v", ctlr.getMultiClusterLog(), ilNamespace)
return false
}
// Check if the virtual exists and valid for us.
_, virtualFound, _ := crInf.ilInformer.GetIndexer().GetByKey(ilkey)
if !virtualFound {
log.Infof("IngressLink %s is invalid", ilName)
return false
}
bindAddr := il.Spec.VirtualServerAddress
var err string
if ctlr.ipamHandler == nil {
if bindAddr == "" {
err = fmt.Sprintf("No IP was specified for ingresslink %s", ilName)
ctlr.updateResourceStatus(IngressLink, il, "", "", errors.New(err))
return false
}
} else {
ipamLabel := il.Spec.IPAMLabel
if ipamLabel == "" && bindAddr == "" {
err = fmt.Sprintf("No ipamLabel was specified for the il server %s", ilName)
ctlr.updateResourceStatus(IngressLink, il, "", "", errors.New(err))
return false
}
}
return true
}
// checkValidExtendedService checks if extended service is valid or not
func (ctlr *Controller) checkValidExtendedService(mcs cisapiv1.MultiClusterServiceReference) error {
// Check if cis running in multiCluster mode
if ctlr.multiClusterMode == "" {
return fmt.Errorf("CIS is not running in multiCluster mode")
}
// Check if all required parameters are specified
if mcs.SvcName == "" || mcs.Namespace == "" || mcs.ClusterName == "" || mcs.ServicePort == (intstr.IntOrString{}) {
return fmt.Errorf("some of the mandatory parameters (clusterName/namespace/serviceName/port) are missing")
}
if mcs.ClusterName != "" {
// Check if cluster config is provided for the cluster where the service is running
if _, ok := ctlr.multiClusterConfigs.ClusterConfigs[mcs.ClusterName]; !ok {
return fmt.Errorf("cluster config for the cluster %s is not provided in DeployConfig CR", mcs.ClusterName)
}
}
if ctlr.multiClusterConfigs != nil && mcs.ClusterName == ctlr.multiClusterConfigs.LocalClusterName ||
mcs.ClusterName == ctlr.multiClusterConfigs.HAPairClusterName {
// Check if the service is running in any of the HA clusters
return fmt.Errorf("service is running in HA cluster, currently CIS doesn't support services running in " +
"HA clusters to be defined in extendedServiceReference")
}
return nil
}