Skip to content

Commit

Permalink
Merge pull request kubevirt#525 from vladikr/virt-dhcp
Browse files Browse the repository at this point in the history
Introduce virt-dhcp
  • Loading branch information
davidvossel authored Nov 8, 2017
2 parents b1acbfe + f167900 commit 7ae83cf
Show file tree
Hide file tree
Showing 12 changed files with 985 additions and 3 deletions.
46 changes: 46 additions & 0 deletions cmd/fake-dnsmasq-process/fake-dnsmasq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 2017 Red Hat, Inc.
*
*/

package main

import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)

func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt,
syscall.SIGQUIT,
)

fmt.Printf("Started fake dnsmasq process\n")

timeout := time.After(60 * time.Second)
select {
case <-timeout:
case <-c:
time.Sleep(2 * time.Second)
}

fmt.Printf("Exit fake dnsmasq process\n")
}
27 changes: 27 additions & 0 deletions cmd/virt-dhcp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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 2017 Red Hat, Inc.
#

FROM fedora:26

MAINTAINER "The KubeVirt Project" <[email protected]>

RUN dnf -y install dnsmasq

COPY virt-dhcp /virt-dhcp

ENTRYPOINT [ "/virt-dhcp" ]
38 changes: 38 additions & 0 deletions cmd/virt-dhcp/virt-dhcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 2017 Red Hat, Inc.
*
*/

package main

import (
"flag"

"kubevirt.io/kubevirt/pkg/log"
dhcp "kubevirt.io/kubevirt/pkg/virt-dhcp"
)

const defaultDhcpSocket = "/var/run/kubevirt/virt_dhcp_sock"

func main() {
log.InitializeLogging("virt-dhcp")
virtDhcpSocket := flag.String("virtdhcp-socket", defaultDhcpSocket, "Virt-DHCP socket.")
if *virtDhcpSocket == "" {
*virtDhcpSocket = defaultDhcpSocket
}
dhcp.Run(*virtDhcpSocket)
}
6 changes: 3 additions & 3 deletions hack/config-default.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/virt-manifest cmd/fake-qemu-process"
docker_images="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virt-manifest images/haproxy images/iscsi-demo-target-tgtd images/vm-killer images/libvirt-kubevirt images/spice-proxy cmd/virt-migrator cmd/registry-disk-v1alpha images/cirros-registry-disk-demo"
binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/virt-manifest cmd/fake-qemu-process cmd/virt-dhcp cmd/fake-dnsmasq-process"
docker_images="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virt-manifest images/haproxy images/iscsi-demo-target-tgtd images/vm-killer images/libvirt-kubevirt images/spice-proxy cmd/virt-migrator cmd/registry-disk-v1alpha images/cirros-registry-disk-demo cmd/virt-dhcp"
optional_docker_images="cmd/registry-disk-v1alpha images/fedora-atomic-registry-disk-demo"
docker_prefix=kubevirt
docker_tag=${DOCKER_TAG:-latest}
Expand All @@ -8,4 +8,4 @@ master_ip=192.168.200.2
master_port=8184
network_provider=weave
primary_nic=${primary_nic:-eth1}
primary_node_name=${primary_node_name:-master}
primary_node_name=${primary_node_name:-master}
12 changes: 12 additions & 0 deletions manifests/libvirt.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ spec:
- name: libvirt-runtime
mountPath: /var/run/libvirt
command: ["/usr/sbin/virtlogd", "-f", "/etc/libvirt/virtlogd.conf"]
- name: virt-dhcp
image: {{ docker_prefix }}/virt-dhcp:{{ docker_tag }}
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
runAsUser: 0
volumeMounts:
- name: libvirt-runtime
mountPath: /var/run/libvirt
- name: virt-share-dir
mountPath: /var/run/kubevirt
command: ["/virt-dhcp"]
volumes:
- name: libvirt-data
hostPath:
Expand Down
57 changes: 57 additions & 0 deletions pkg/virt-dhcp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package virtdhcp

import (
"fmt"
"net/rpc"

"kubevirt.io/kubevirt/pkg/log"
)

type VirtDHCPClient struct {
client *rpc.Client
}

func GetClient() (*VirtDHCPClient, error) {
conn, err := rpc.Dial("unix", DhcpSocket)
if err != nil {
log.Log.Reason(err).Errorf("client failed to connect to virt-dhcp socket: %s", DhcpSocket)
return nil, err
}

return &VirtDHCPClient{client: conn}, nil
}

func (dhcp *VirtDHCPClient) AddIP(mac string, ip string, lease int) error {
addArgs := &AddArgs{ip, mac, lease}
addReply := &DhcpReply{}

dhcp.client.Call("DHCP.AddIP", addArgs, addReply)
if addReply.Success != true {
msg := fmt.Sprintf("failed to add interface to dhcp, ip: %s, mac: %s - %s", ip, mac, addReply.Message)
return fmt.Errorf(msg)
}
return nil
}

func (dhcp *VirtDHCPClient) RemoveIP(mac string, ip string) error {
removeArgs := &RemoveArgs{ip, mac}
removeReply := &DhcpReply{}

dhcp.client.Call("DHCP.RemoveIP", removeArgs, removeReply)
if removeReply.Success != true {
msg := fmt.Sprintf("failed to remove interface from dhcp, ip: %s, mac: %s - %s", ip, mac, removeReply.Message)
return fmt.Errorf(msg)
}
return nil
}

func (dhcp *VirtDHCPClient) SetIPs(hosts *[]AddArgs, reply *DhcpReply) error {
addReply := &DhcpReply{}

dhcp.client.Call("DHCP.SetIPs", hosts, addReply)
if addReply.Success != true {
msg := fmt.Sprintf("failed to set known hosts to dhcp: %s", addReply.Message)
return fmt.Errorf(msg)
}
return nil
}
Loading

0 comments on commit 7ae83cf

Please sign in to comment.