Skip to content

Instantly share code, notes, and snippets.

@yifan-gu
Created January 16, 2016 00:59
Show Gist options
  • Save yifan-gu/fbb911db83d785915543 to your computer and use it in GitHub Desktop.
Save yifan-gu/fbb911db83d785915543 to your computer and use it in GitHub Desktop.
rkt-kubernetes bridge CNI patch
diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go
index 713748a..a37b949 100644
--- a/pkg/kubelet/kubelet.go
+++ b/pkg/kubelet/kubelet.go
@@ -2599,7 +2599,11 @@ func (kl *Kubelet) reconcileCBR0(podCIDR string) error {
glog.V(5).Info("Shaper is nil, creating")
kl.shaper = bandwidth.NewTCShaper("cbr0")
}
- return kl.shaper.ReconcileInterface()
+ if err := kl.shaper.ReconcileInterface(); err != nil {
+ return err
+ }
+
+ return rkt.WriteBridgeNetConfig("cbr0", cidr)
}
// updateNodeStatus updates node status to master with retries.
diff --git a/pkg/kubelet/rkt/network.go b/pkg/kubelet/rkt/network.go
new file mode 100644
index 0000000..c388dcc
--- /dev/null
+++ b/pkg/kubelet/rkt/network.go
@@ -0,0 +1,73 @@
+/*
+Copyright 2015 The Kubernetes Authors All rights reserved.
+
+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 rkt
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net"
+ "os"
+ "path"
+)
+
+const (
+ defaultNetConfigFile = "k8s-cbr0.conf"
+ defaultNetworkName = "rkt.kubernetes.io"
+)
+
+type IPAM struct {
+ Type string `json:"type"`
+ Subnet string `json:"subnet"`
+ Gateway net.IP `json:"gateway"`
+}
+
+// A CNI bridge config that instructs rkt to get IPs from the 'cbr0' bridge
+// created by kubelet.
+type NetConfig struct {
+ Name string `json:"name"`
+ Type string `json:"type"`
+ BrName string `json:"bridge"`
+ IsGW bool `json:"isGateway"`
+ IPAM IPAM `json:"ipam"`
+}
+
+// WriteBridgeNetConfig creates and write the CNI bridge configure file at ${rktLocalConfigDir}/net.d/${defaultNetConfigFile}.
+// bridgeName is the name of the container bridge, e.g. 'cbr0'.
+// cidr is the CIDR block of the bridge, note that cidr.IP is the gateway of the bridge.
+func WriteBridgeNetConfig(bridgeName string, cidr *net.IPNet) error {
+ netConfig := &NetConfig{
+ Name: defaultNetworkName,
+ Type: "bridge",
+ BrName: bridgeName,
+ IsGW: true,
+ IPAM: IPAM{Type: "host-local", Subnet: cidr.String(), Gateway: cidr.IP},
+ }
+
+ data, err := json.MarshalIndent(netConfig, "", "\t")
+ if err != nil {
+ return err
+ }
+
+ // Ensure the 'net.d' dir exists.
+ dirpath := path.Join(rktLocalConfigDir, "net.d")
+ err = os.MkdirAll(dirpath, 0750)
+ if err != nil && !os.IsExist(err) {
+ return err
+ }
+
+ return ioutil.WriteFile(path.Join(dirpath, defaultNetConfigFile), data, 0640)
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment