forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubevirt#11511 from EdDev/admitters-net-move
virt-api, admitter: Refactor & move network validation admitters to the network package
- Loading branch information
Showing
9 changed files
with
236 additions
and
85 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2024 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package admitter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"kubevirt.io/client-go/testutils" | ||
) | ||
|
||
func TestAdmitter(t *testing.T) { | ||
testutils.KubeVirtTestSuiteSetup(t) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2024 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package admitter | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sfield "k8s.io/apimachinery/pkg/util/validation/field" | ||
|
||
"kubevirt.io/kubevirt/pkg/network/vmispec" | ||
|
||
v1 "kubevirt.io/api/core/v1" | ||
) | ||
|
||
type slirpClusterConfigChecker interface { | ||
IsSlirpInterfaceEnabled() bool | ||
} | ||
|
||
func ValidateSlirpBinding( | ||
field *k8sfield.Path, | ||
spec *v1.VirtualMachineInstanceSpec, | ||
configChecker slirpClusterConfigChecker) (causes []metav1.StatusCause) { | ||
for idx, ifaceSpec := range spec.Domain.Devices.Interfaces { | ||
if ifaceSpec.Slirp == nil { | ||
continue | ||
} | ||
net := vmispec.LookupNetworkByName(spec.Networks, ifaceSpec.Name) | ||
if net == nil { | ||
continue | ||
} | ||
|
||
if net.Pod == nil { | ||
causes = append(causes, metav1.StatusCause{ | ||
Type: metav1.CauseTypeFieldValueInvalid, | ||
Message: "Slirp interface only implemented with pod network", | ||
Field: field.Child("domain", "devices", "interfaces").Index(idx).Child("name").String(), | ||
}) | ||
} else if !configChecker.IsSlirpInterfaceEnabled() { | ||
causes = append(causes, metav1.StatusCause{ | ||
Type: metav1.CauseTypeFieldValueInvalid, | ||
Message: "Slirp interface is not enabled in kubevirt-config", | ||
Field: field.Child("domain", "devices", "interfaces").Index(idx).Child("name").String(), | ||
}) | ||
} | ||
} | ||
return causes | ||
} |
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,96 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2024 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package admitter_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
k8sfield "k8s.io/apimachinery/pkg/util/validation/field" | ||
|
||
v1 "kubevirt.io/api/core/v1" | ||
|
||
"kubevirt.io/kubevirt/pkg/network/admitter" | ||
) | ||
|
||
var _ = Describe("Validate interface with SLIRP binding", func() { | ||
It("should be rejected if not enabled in the Kubevirt CR", func() { | ||
vmi := v1.VirtualMachineInstance{} | ||
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{{ | ||
Name: "default", | ||
InterfaceBindingMethod: v1.InterfaceBindingMethod{ | ||
Slirp: &v1.InterfaceSlirp{}, | ||
}, | ||
}} | ||
vmi.Spec.Networks = []v1.Network{{ | ||
Name: "default", | ||
NetworkSource: v1.NetworkSource{Pod: &v1.PodNetwork{}}, | ||
}} | ||
|
||
config := stubSlirpClusterConfigChecker{} | ||
causes := admitter.ValidateSlirpBinding(k8sfield.NewPath("fake"), &vmi.Spec, config) | ||
Expect(causes).To(HaveLen(1)) | ||
Expect(causes[0].Message).To(Equal("Slirp interface is not enabled in kubevirt-config")) | ||
}) | ||
|
||
It("should be rejected without a pod network", func() { | ||
vmi := v1.VirtualMachineInstance{} | ||
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{{ | ||
Name: "default", | ||
InterfaceBindingMethod: v1.InterfaceBindingMethod{ | ||
Slirp: &v1.InterfaceSlirp{}, | ||
}, | ||
}} | ||
vmi.Spec.Networks = []v1.Network{{ | ||
Name: "default", | ||
NetworkSource: v1.NetworkSource{Multus: &v1.MultusNetwork{}}, | ||
}} | ||
|
||
config := stubSlirpClusterConfigChecker{enabled: true} | ||
causes := admitter.ValidateSlirpBinding(k8sfield.NewPath("fake"), &vmi.Spec, config) | ||
Expect(causes).To(HaveLen(1)) | ||
Expect(causes[0].Message).To(Equal("Slirp interface only implemented with pod network")) | ||
}) | ||
|
||
It("should be accepted with a pod network when SLIRP is enabled in the Kubevirt CR", func() { | ||
vmi := v1.VirtualMachineInstance{} | ||
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{{ | ||
Name: "default", | ||
InterfaceBindingMethod: v1.InterfaceBindingMethod{ | ||
Slirp: &v1.InterfaceSlirp{}, | ||
}, | ||
}} | ||
vmi.Spec.Networks = []v1.Network{{ | ||
Name: "default", | ||
NetworkSource: v1.NetworkSource{Pod: &v1.PodNetwork{}}, | ||
}} | ||
|
||
config := stubSlirpClusterConfigChecker{enabled: true} | ||
Expect(admitter.ValidateSlirpBinding(k8sfield.NewPath("fake"), &vmi.Spec, config)).To(BeEmpty()) | ||
}) | ||
}) | ||
|
||
type stubSlirpClusterConfigChecker struct { | ||
enabled bool | ||
} | ||
|
||
func (s stubSlirpClusterConfigChecker) IsSlirpInterfaceEnabled() bool { | ||
return s.enabled | ||
} |
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
Oops, something went wrong.