-
Notifications
You must be signed in to change notification settings - Fork 464
/
disjoint.go
107 lines (83 loc) · 4.69 KB
/
disjoint.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package cidr
import (
"fmt"
"k8s.io/apimachinery/pkg/util/validation/field"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
)
// ValidateNetworkDisjointedness validates that the given <seedNetworks> and <k8sNetworks> are disjoint.
func ValidateNetworkDisjointedness(fldPath *field.Path, shootNodes, shootPods, shootServices, seedNodes *string, seedPods, seedServices string, workerless bool) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateOverlapWithSeed(fldPath.Child("nodes"), shootNodes, "node", false, seedNodes, seedPods, seedServices)...)
allErrs = append(allErrs, validateOverlapWithSeed(fldPath.Child("services"), shootServices, "service", true, seedNodes, seedPods, seedServices)...)
allErrs = append(allErrs, validateOverlapWithSeed(fldPath.Child("pods"), shootPods, "pod", !workerless, seedNodes, seedPods, seedServices)...)
return allErrs
}
func validateOverlapWithSeed(fldPath *field.Path, shootNetwork *string, networkType string, networkRequired bool, seedNodes *string, seedPods, seedServices string) field.ErrorList {
allErrs := field.ErrorList{}
if shootNetwork != nil {
if NetworksIntersect(seedServices, *shootNetwork) {
allErrs = append(allErrs, field.Invalid(fldPath, *shootNetwork, fmt.Sprintf("shoot %s network intersects with seed service network", networkType)))
}
if NetworksIntersect(seedPods, *shootNetwork) {
allErrs = append(allErrs, field.Invalid(fldPath, *shootNetwork, fmt.Sprintf("shoot %s network intersects with seed pod network", networkType)))
}
if seedNodes != nil && NetworksIntersect(*seedNodes, *shootNetwork) {
allErrs = append(allErrs, field.Invalid(fldPath, *shootNetwork, fmt.Sprintf("shoot %s network intersects with seed node network", networkType)))
}
if NetworksIntersect(v1beta1constants.DefaultVPNRange, *shootNetwork) {
allErrs = append(allErrs, field.Invalid(fldPath, *shootNetwork, fmt.Sprintf("shoot %s network intersects with default vpn network (%s)", networkType, v1beta1constants.DefaultVPNRange)))
}
if NetworksIntersect(v1beta1constants.DefaultVPNRangeV6, *shootNetwork) {
allErrs = append(allErrs, field.Invalid(fldPath, *shootNetwork, fmt.Sprintf("shoot %s network intersects with default vpn network (%s)", networkType, v1beta1constants.DefaultVPNRangeV6)))
}
} else if networkRequired {
allErrs = append(allErrs, field.Required(fldPath, networkType+"s is required"))
}
return allErrs
}
// ValidateShootNetworkDisjointedness validates that the given shoot network is disjoint.
func ValidateShootNetworkDisjointedness(fldPath *field.Path, shootNodes, shootPods, shootServices *string, workerless bool) field.ErrorList {
var (
allErrs = field.ErrorList{}
pathServices = fldPath.Child("services")
pathPods = fldPath.Child("pods")
)
if shootPods != nil && shootServices != nil {
if NetworksIntersect(*shootPods, *shootServices) {
allErrs = append(allErrs, field.Invalid(pathServices, *shootServices, "shoot pod network intersects with shoot service network"))
}
if shootNodes != nil && NetworksIntersect(*shootPods, *shootNodes) {
allErrs = append(allErrs, field.Invalid(pathPods, *shootPods, "shoot pod network intersects with shoot node network"))
}
if shootNodes != nil && NetworksIntersect(*shootServices, *shootNodes) {
allErrs = append(allErrs, field.Invalid(pathServices, *shootServices, "shoot service network intersects with shoot node network"))
}
} else if shootPods != nil {
if shootNodes != nil && NetworksIntersect(*shootPods, *shootNodes) {
allErrs = append(allErrs, field.Invalid(pathPods, *shootPods, "shoot pod network intersects with shoot node network"))
}
allErrs = append(allErrs, field.Required(pathServices, "shoot service network is required"))
} else if shootServices != nil {
if shootNodes != nil && NetworksIntersect(*shootServices, *shootNodes) {
allErrs = append(allErrs, field.Invalid(pathServices, *shootServices, "shoot service network intersects with shoot node network"))
}
if !workerless {
allErrs = append(allErrs, field.Required(pathPods, "shoot pod network is required"))
}
} else {
if !workerless {
allErrs = append(allErrs, field.Required(pathPods, "shoot pod network is required"))
}
allErrs = append(allErrs, field.Required(pathServices, "shoot service network is required"))
}
return allErrs
}
// NetworksIntersect returns true if the given network CIDRs intersect.
func NetworksIntersect(cidr1, cidr2 string) bool {
c1 := NewCIDR(cidr1, field.NewPath(""))
c2 := NewCIDR(cidr2, field.NewPath(""))
return c1.ValidateOverlap(c2).ToAggregate() == nil
}