Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slirp hook sidecar #10272

Merged
merged 7 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ container_bundle(
"$(container_prefix)/$(image_prefix)example-hook-sidecar:$(container_tag)": "//cmd/example-hook-sidecar:example-hook-sidecar-image",
"$(container_prefix)/$(image_prefix)example-disk-mutation-hook-sidecar:$(container_tag)": "//cmd/example-disk-mutation-hook-sidecar:example-disk-mutation-hook-sidecar-image",
"$(container_prefix)/$(image_prefix)example-cloudinit-hook-sidecar:$(container_tag)": "//cmd/example-cloudinit-hook-sidecar:example-cloudinit-hook-sidecar-image",
"$(container_prefix)/$(image_prefix)network-slirp-binding:$(container_tag)": "//cmd/network-slirp-binding:network-slirp-binding-image",
# container-disk images
"$(container_prefix)/$(image_prefix)alpine-container-disk-demo:$(container_tag)": "//containerimages:alpine-container-disk-image",
"$(container_prefix)/$(image_prefix)cirros-container-disk-demo:$(container_tag)": "//containerimages:cirros-container-disk-image",
Expand Down Expand Up @@ -329,6 +330,15 @@ container_push(
tag = "$(container_tag)",
)

container_push(
name = "push-network-slirp-binding",
format = "Docker",
image = "//cmd/network-slirp-binding:network-slirp-binding-image",
registry = "$(container_prefix)",
repository = "$(image_prefix)network-slirp-binding",
tag = "$(container_tag)",
)

container_push(
name = "push-example-hook-sidecar",
format = "Docker",
Expand Down
48 changes: 48 additions & 0 deletions cmd/network-slirp-binding/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "kubevirt.io/kubevirt/cmd/network-slirp-binding",
visibility = ["//visibility:private"],
deps = [
"//cmd/network-slirp-binding/dns:go_default_library",
"//cmd/network-slirp-binding/server:go_default_library",
"//pkg/hooks:go_default_library",
"//pkg/hooks/info:go_default_library",
"//pkg/hooks/v1alpha2:go_default_library",
"//staging/src/kubevirt.io/client-go/log:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
)

go_binary(
name = "network-slirp-binding",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

load(
"@io_bazel_rules_docker//container:container.bzl",
"container_image",
)

container_image(
name = "version-container",
base = "//:passwd-image",
directory = "/",
files = ["//:get-version"],
)

container_image(
name = "network-slirp-binding-image",
architecture = select({
"@io_bazel_rules_go//go/platform:linux_arm64": "arm64",
"//conditions:default": "amd64",
}),
base = ":version-container",
directory = "/",
entrypoint = ["/network-slirp-binding"],
files = [":network-slirp-binding"],
visibility = ["//visibility:public"],
)
50 changes: 50 additions & 0 deletions cmd/network-slirp-binding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# KubeVirt Network Slirp Binding Plugin

## Summary

Slirp network binding plugin configures VMs Slirp interface using Kubevirts hook sidecar interface.

It will be used by Kubevirt to offload slirp networking configuration.

> _NOTE_:
> Slirp network binding is supported for pod network interfaces only.

# How to use

Register the `slirp` binding plugin with its sidecar image:

```yaml
apiVersion: kubevirt.io/v1
kind: KubeVirt
metadata:
name: kubevirt
namespace: kubevirt
spec:
configuration:
network:
binding:
slirp:
sidecarImage: registry:5000/kubevirt/network-slirp-binding:devel
...
```

In the VM spec, set interface to use `slirp` binding plugin:

```yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachineInstance
metadata:
name: vmi-slirp
spec:
domain:
devices:
interfaces:
- name: slirp
binding:
name: slirp
...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 dots?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 dots are fine, he doesn't need to give the full yaml here, just the relevant parts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snippet suppose to show the relevant part, three dots indicate there could be more content at that position.

networks:
- name: slirp-net
pod: {}
...
```
24 changes: 24 additions & 0 deletions cmd/network-slirp-binding/callback/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["callback.go"],
importpath = "kubevirt.io/kubevirt/cmd/network-slirp-binding/callback",
visibility = ["//visibility:public"],
deps = ["//pkg/virt-launcher/virtwrap/api:go_default_library"],
)

go_test(
name = "go_default_test",
srcs = [
"callback_suite_test.go",
"callback_test.go",
],
deps = [
":go_default_library",
"//pkg/virt-launcher/virtwrap/api:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
"//vendor/github.com/onsi/ginkgo/v2:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
],
)
58 changes: 58 additions & 0 deletions cmd/network-slirp-binding/callback/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 2023 Red Hat, Inc.
*
*/

package callback

import (
"encoding/xml"
"fmt"

domainschema "kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
)

// TODO: move to Kubevirt domain API package
const libvirtDomainQemuSchema = "http://libvirt.org/schemas/domain/qemu/1.0"

type DomainSpecMutator interface {
Mutate(*domainschema.DomainSpec) (*domainschema.DomainSpec, error)
}

func OnDefineDomain(domainXML []byte, domSpecMutator DomainSpecMutator) ([]byte, error) {
domainSpec := &domainschema.DomainSpec{
// Unmarshalling domain spec makes the XML namespace attribute empty.
// Some domain parameters requires namespace to be defined.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unmarshalling makes the ns empty?

Copy link
Contributor Author

@ormergi ormergi Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt find the root cause, from what I managed to understand its due to the name of the XmlNs attribute XML tag name:
XmlNS string `xml:"xmlns:qemu,attr,omitempty"
If I remove the : (xmlnsqemu), it unmarshals XmlNs correctly.
I have opened an issue at golang/go repo with a reproducer
golang/go#62075

It seems to be well known issue in the code-base because the schema test initialize the XmlNs attribute before asserting, although I didn't find an explanation/doc for it yet.

It seems there are problems with go XML serialization in general
golang/go#13400

// e.g: https://libvirt.org/drvqemu.html#pass-through-of-arbitrary-qemu-commands
XmlNS: libvirtDomainQemuSchema,
}
if err := xml.Unmarshal(domainXML, domainSpec); err != nil {
return nil, fmt.Errorf("failed to unmarshal given domain spec: %v", err)
}

updatedDomainSpec, err := domSpecMutator.Mutate(domainSpec)
if err != nil {
return nil, err
}

updatedDomainSpecXML, err := xml.Marshal(updatedDomainSpec)
if err != nil {
return nil, fmt.Errorf("failed to marshal updated domain spec: %v", err)
}

return updatedDomainSpecXML, nil
}
30 changes: 30 additions & 0 deletions cmd/network-slirp-binding/callback/callback_suite_test.go
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 2023 Red Hat, Inc.
*
*/

package callback_test

import (
"testing"

"kubevirt.io/client-go/testutils"
)

func TestCallback(t *testing.T) {
testutils.KubeVirtTestSuiteSetup(t)
}
92 changes: 92 additions & 0 deletions cmd/network-slirp-binding/callback/callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 2023 Red Hat, Inc.
*
*/

package callback_test

import (
"encoding/xml"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

domainschema "kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"

"kubevirt.io/kubevirt/cmd/network-slirp-binding/callback"
)

var _ = Describe("hook callback handler", func() {
Context("on define domain", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this additional later? If it has no functional usage, consider removing it and embedding the text in the upper or lower layers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by additional later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant "layer". Why do you need the Context?

It("should fail given empty byte slice stream", func() {
_, err := callback.OnDefineDomain([]byte{}, mutatorStub{})
Expect(err).To(HaveOccurred())
})

It("should fail given invalid domain XML", func() {
_, err := callback.OnDefineDomain([]byte("invalid-domain-xml"), mutatorStub{})
Expect(err).To(HaveOccurred())
})

It("should fail when domain spec mutator fails", func() {
domain := domainschema.NewMinimalDomain("test")
originalDomainSpecXML, err := xml.Marshal(domain.Spec)
Expect(err).ToNot(HaveOccurred())

expectedErr := fmt.Errorf("test error")
domSpecMutator := mutatorStub{failMutate: expectedErr}

_, err = callback.OnDefineDomain(originalDomainSpecXML, domSpecMutator)
Expect(err).To(Equal(expectedErr))
})

It("given no-op mutator, domain spec should not change", func() {
domain := domainschema.NewMinimalDomain("test")
domainSpecXML, err := xml.Marshal(domain.Spec)
Expect(err).ToNot(HaveOccurred())
domSpecMutator := mutatorStub{domSpec: &domain.Spec}

Expect(callback.OnDefineDomain(domainSpecXML, domSpecMutator)).To(Equal(domainSpecXML))
})

It("domain spec should mutate successfully", func() {
domain := domainschema.NewMinimalDomain("test")
domainSpecXML, err := xml.Marshal(domain.Spec)
Expect(err).ToNot(HaveOccurred())

mutatedDomainSpec := domain.Spec.DeepCopy()
mutatedDomainSpec.Devices.Interfaces = append(mutatedDomainSpec.Devices.Interfaces,
domainschema.Interface{Alias: domainschema.NewUserDefinedAlias("test")})
domSpecMutator := mutatorStub{domSpec: mutatedDomainSpec}

mutatedDomainSpecXML, err := xml.Marshal(mutatedDomainSpec)
Expect(err).ToNot(HaveOccurred())

Expect(callback.OnDefineDomain(domainSpecXML, domSpecMutator)).To(Equal(mutatedDomainSpecXML))
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

  • Showing that a mutation is possible (just replacing the whole thing with something else is enough).
  • Showing the behavior when there is no input (empty byte stream).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

})

type mutatorStub struct {
domSpec *domainschema.DomainSpec
failMutate error
}

func (s mutatorStub) Mutate(_ *domainschema.DomainSpec) (*domainschema.DomainSpec, error) {
return s.domSpec, s.failMutate
}
12 changes: 12 additions & 0 deletions cmd/network-slirp-binding/dns/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["resolvconf.go"],
importpath = "kubevirt.io/kubevirt/cmd/network-slirp-binding/dns",
visibility = ["//visibility:public"],
deps = [
"//pkg/network/dns:go_default_library",
"//staging/src/kubevirt.io/client-go/log:go_default_library",
],
)
50 changes: 50 additions & 0 deletions cmd/network-slirp-binding/dns/resolvconf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 2023 Red Hat, Inc.
*
*/

package dns

import (
"fmt"
"os"
"strings"

"kubevirt.io/client-go/log"

"kubevirt.io/kubevirt/pkg/network/dns"
)

const resolvConf = "/etc/resolv.conf"

func ReadResolvConfSearchDomains() ([]string, error) {

// #nosec No risk for path injection. resolvConf is static "/etc/resolve.conf"
resolvConfRaw, err := os.ReadFile(resolvConf)
if err != nil {
return nil, fmt.Errorf("failed to read resolv.conf at %q: %v", resolvConf, err)
}

searchDomains, err := dns.ParseSearchDomains(string(resolvConfRaw))
if err != nil {
return nil, fmt.Errorf("failed to parse search domains out of %q: %v", resolvConf, err)
}

log.Log.Infof("Found search domains in %s: %s", resolvConf, strings.Join(searchDomains, " "))

return searchDomains, nil
}