-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yaroslavborbat <[email protected]>
- Loading branch information
1 parent
9b4e2f1
commit 1576840
Showing
5 changed files
with
262 additions
and
32 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
images/virtualization-artifact/pkg/controller/moduleconfig/cidrs_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
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 moduleconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/netip" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" | ||
) | ||
|
||
type cidrsValidator struct { | ||
client client.Client | ||
} | ||
|
||
func newCIDRsValidator(client client.Client) *cidrsValidator { | ||
return &cidrsValidator{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (v cidrsValidator) ValidateCreate(ctx context.Context, mc *mcapi.ModuleConfig) (admission.Warnings, error) { | ||
return v.validate(ctx, mc) | ||
} | ||
|
||
func (v cidrsValidator) ValidateUpdate(ctx context.Context, _, newMC *mcapi.ModuleConfig) (admission.Warnings, error) { | ||
return v.validate(ctx, newMC) | ||
} | ||
|
||
func (v cidrsValidator) validate(ctx context.Context, mc *mcapi.ModuleConfig) (admission.Warnings, error) { | ||
CIDRs, err := parseCIDRs(mc.Spec.Settings) | ||
if err != nil { | ||
return admission.Warnings{}, err | ||
} | ||
|
||
err = checkOverlapsCIDRs(CIDRs) | ||
if err != nil { | ||
return admission.Warnings{}, err | ||
} | ||
|
||
err = v.checkNodeSubnets(ctx, CIDRs) | ||
if err != nil { | ||
return admission.Warnings{}, err | ||
} | ||
|
||
return admission.Warnings{}, nil | ||
} | ||
|
||
func (v cidrsValidator) checkNodeSubnets(ctx context.Context, excludedPrefixes []netip.Prefix) error { | ||
nodes := &corev1.NodeList{} | ||
err := v.client.List(ctx, nodes) | ||
if err != nil { | ||
return fmt.Errorf("error listing nodes: %w", err) | ||
} | ||
return checkNodeAddressesOverlap(nodes.Items, excludedPrefixes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
images/virtualization-artifact/pkg/controller/moduleconfig/util.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
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 moduleconfig | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
mcapi "github.com/deckhouse/virtualization-controller/pkg/controller/moduleconfig/api" | ||
) | ||
|
||
func isEqualCIDRs(a, b netip.Prefix) bool { | ||
return a.Addr() == b.Addr() && a.Bits() == b.Bits() | ||
} | ||
|
||
func checkOverlapsCIDRs(prefixes []netip.Prefix) error { | ||
for i := 0; i < len(prefixes); i++ { | ||
for j := i + 1; j < len(prefixes); j++ { | ||
if prefixes[i].Overlaps(prefixes[j]) { | ||
return fmt.Errorf("overlapping CIDRs %v and %v", prefixes[i], prefixes[j]) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func checkNodeAddressesOverlap(nodes []corev1.Node, excludedPrefixes []netip.Prefix) error { | ||
for _, node := range nodes { | ||
for _, address := range node.Status.Addresses { | ||
if address.Type == corev1.NodeInternalIP || address.Type == corev1.NodeExternalIP { | ||
ip, err := netip.ParseAddr(address.Address) | ||
if err != nil { | ||
return fmt.Errorf("error parsing node IP: %w", err) | ||
} | ||
|
||
for _, prefix := range excludedPrefixes { | ||
if prefix.Contains(ip) { | ||
return fmt.Errorf("node address %s may overlap with subnet %s", ip, prefix) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func parseCIDRs(settings mcapi.SettingsValues) ([]netip.Prefix, error) { | ||
raw := settings[virtualMachineCIDRs].([]interface{}) | ||
CIDRs, err := convertToStringSlice(raw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
parseCIDRs := make([]netip.Prefix, len(CIDRs)) | ||
for i, validateCIDR := range CIDRs { | ||
parsedCIDR, err := netip.ParsePrefix(validateCIDR) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse CIDR %s: %w", validateCIDR, err) | ||
} | ||
parseCIDRs[i] = parsedCIDR | ||
} | ||
return parseCIDRs, nil | ||
} | ||
|
||
func convertToStringSlice(input []interface{}) ([]string, error) { | ||
result := make([]string, len(input)) | ||
for i, v := range input { | ||
strVal, ok := v.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to convert value %v to a string", v) | ||
} | ||
result[i] = strVal | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters