Skip to content

Commit

Permalink
Use configurable netmask for Calico
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Sep 29, 2017
1 parent 10a6dd4 commit d79b96b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 71 deletions.
6 changes: 6 additions & 0 deletions deploy/virtlet-ds-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ spec:
name: virtlet-config
key: loglevel
optional: true
- name: VIRTLET_CALICO_SUBNET
valueFrom:
configMapKeyRef:
name: virtlet-config
key: calico-subnet
optional: true
- name: IMAGE_REGEXP_TRANSLATION
valueFrom:
configMapKeyRef:
Expand Down
6 changes: 6 additions & 0 deletions deploy/virtlet-ds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ spec:
name: virtlet-config
key: loglevel
optional: true
- name: VIRTLET_CALICO_SUBNET
valueFrom:
configMapKeyRef:
name: virtlet-config
key: calico-subnet
optional: true
- name: IMAGE_REGEXP_TRANSLATION
valueFrom:
configMapKeyRef:
Expand Down
5 changes: 4 additions & 1 deletion docs/networking.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ spawning it as a child process.
as it tries to pass a routing configuration that cannot be passed
over DHCP. For it to work Virtlet patches Calico-provided CNI result,
replacing Calico's unreachable fake gateway with another fake gateway
with an IP address acquired from Calico IPAM.
with an IP address acquired from Calico IPAM. A proper node subnet must
be set for Calico-based virtlet installations. It's controlled by
`calico-subnet` key Virtlet configmap (denoting the number of 1s in
the netmask) and defaults to `24`.

**NOTE:** Virtlet doesn't support `hostNetwork` pod setting because it
cannot be impelemnted for VM in a meaningful way.
8 changes: 8 additions & 0 deletions examples/ubuntu-vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ metadata:
kubernetes.io/target-runtime: virtlet
VirtletCloudInitUserData: |
ssh_pwauth: True
users:
- name: testuser
gecos: User
primary-group: testuser
groups: users
lock_passwd: false
passwd: "$6$rounds=4096$wPs4Hz4tfs$a8ssMnlvH.3GX88yxXKF2cKMlVULsnydoOKgkuStTErTq2dzKZiIx9R/pPWWh5JLxzoZEx7lsSX5T2jW5WISi1"
sudo: ALL=(ALL) NOPASSWD:ALL
VirtletSSHKeys: |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCaJEcFDXEK2ZbX0ZLS1EIYFZRbDAcRfuVjpstSc0De8+sV1aiu+dePxdkuDRwqFtCyk6dEZkssjOkBXtri00MECLkir6FcH3kKOJtbJ6vy3uaJc9w1ERo+wyl6SkAh/+JTJkp7QRXj8oylW5E20LsbnA/dIwWzAF51PPwF7A7FtNg9DnwPqMkxFo1Th/buOMKbP5ZA1mmNNtmzbMpMfJATvVyiv3ccsSJKOiyQr6UG+j7sc/7jMVz5Xk34Vd0l8GwcB0334MchHckmqDB142h/NCWTr8oLakDNvkfC1YneAfAO41hDkUbxPtVBG5M/o7P4fxoqiHEX+ZLfRxDtHB53 me@localhost
spec:
Expand Down
30 changes: 15 additions & 15 deletions pkg/tapmanager/tapfdsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"net"
"os"
"strconv"
"time"

"github.com/containernetworking/cni/pkg/ns"
Expand All @@ -35,7 +37,9 @@ import (
)

const (
calicoNetType = "calico"
calicoNetType = "calico"
calicoDefaultSubnet = 24
calicoSubnetVar = "VIRTLET_CALICO_SUBNET"
)

// PodNetworkDesc contains the data that are required by TapFDSource
Expand Down Expand Up @@ -151,6 +155,7 @@ func (s *TapFDSource) GetFD(key string, data []byte) (int, []byte, error) {
if netConfig.IPs[0].Version != "4" {
return 0, nil, errors.New("IPv4 config was expected")
}
netConfig.IPs[0].Address.Mask = netmaskForCalico()
netConfig.IPs[0].Gateway = s.dummyGateway
netConfig.Routes = []*cnitypes.Route{
{
Expand Down Expand Up @@ -304,20 +309,15 @@ func fixCNIResult(netConfig *cnicurrent.Result, csn *nettools.ContainerSideNetwo
}
}

func calcNetmaskForCalico(ipA, ipB net.IP) (net.IPMask, error) {
var a, b uint
for _, v := range ipA {
a = (a << 8) + uint(v)
}
for _, v := range ipB {
b = (b << 8) + uint(v)
}
for n := 30; n >= 0; n-- {
m := (uint(1) << uint(32-n)) - 1
// avoid zero and broadcast addrs
if (a&^m) == (b&^m) && (a&m) != 0 && (b&m) != 0 && (a&m) != m && (b&m) != m {
return net.CIDRMask(n, 32), nil
func netmaskForCalico() net.IPMask {
n := calicoDefaultSubnet
subnetStr := os.Getenv(calicoSubnetVar)
if subnetStr != "" {
n, err := strconv.Atoi(subnetStr)
if err != nil || n <= 0 || n > 30 {
glog.Warningf("bad calico subnet %q, using /%d", subnetStr, calicoDefaultSubnet)
n = calicoDefaultSubnet
}
}
return nil, errors.New("addresses too different")
return net.CIDRMask(n, 32)
}
55 changes: 0 additions & 55 deletions pkg/tapmanager/tapfdsource_test.go

This file was deleted.

0 comments on commit d79b96b

Please sign in to comment.