2015-03-19 23:14:13 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-03-19 23:14:13 +00:00
|
|
|
|
|
|
|
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 network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-09 00:51:57 +00:00
|
|
|
"net"
|
2015-03-19 23:14:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2016-02-01 22:30:47 +00:00
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-09 21:59:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-09 21:00:41 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2015-10-14 05:18:37 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2015-09-10 22:48:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/validation"
|
2015-03-19 23:14:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultPluginName = "kubernetes.io/no-op"
|
|
|
|
|
2016-01-27 04:02:59 +00:00
|
|
|
// Called when the node's Pod CIDR is known when using the
|
|
|
|
// controller manager's --allocate-node-cidrs=true option
|
|
|
|
const NET_PLUGIN_EVENT_POD_CIDR_CHANGE = "pod-cidr-change"
|
|
|
|
const NET_PLUGIN_EVENT_POD_CIDR_CHANGE_DETAIL_CIDR = "pod-cidr"
|
|
|
|
|
2015-03-19 23:14:13 +00:00
|
|
|
// Plugin is an interface to network plugins for the kubelet
|
|
|
|
type NetworkPlugin interface {
|
|
|
|
// Init initializes the plugin. This will be called exactly once
|
|
|
|
// before any other methods are called.
|
|
|
|
Init(host Host) error
|
|
|
|
|
2016-01-27 04:02:59 +00:00
|
|
|
// Called on various events like:
|
|
|
|
// NET_PLUGIN_EVENT_POD_CIDR_CHANGE
|
|
|
|
Event(name string, details map[string]interface{})
|
|
|
|
|
2015-03-19 23:14:13 +00:00
|
|
|
// Name returns the plugin's name. This will be used when searching
|
|
|
|
// for a plugin by name, e.g.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// SetUpPod is the method called after the infra container of
|
|
|
|
// the pod has been created but before the other containers of the
|
|
|
|
// pod are launched.
|
2015-12-24 23:46:56 +00:00
|
|
|
SetUpPod(namespace string, name string, podInfraContainerID kubecontainer.DockerID) error
|
2015-03-19 23:14:13 +00:00
|
|
|
|
|
|
|
// TearDownPod is the method called before a pod's infra container will be deleted
|
2015-12-24 23:46:56 +00:00
|
|
|
TearDownPod(namespace string, name string, podInfraContainerID kubecontainer.DockerID) error
|
2015-06-09 00:51:57 +00:00
|
|
|
|
|
|
|
// Status is the method called to obtain the ipv4 or ipv6 addresses of the container
|
2015-12-24 23:46:56 +00:00
|
|
|
Status(namespace string, name string, podInfraContainerID kubecontainer.DockerID) (*PodNetworkStatus, error)
|
2015-06-09 00:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PodNetworkStatus stores the network status of a pod (currently just the primary IP address)
|
2015-12-10 08:57:27 +00:00
|
|
|
// This struct represents version "v1beta1"
|
2015-06-09 00:51:57 +00:00
|
|
|
type PodNetworkStatus struct {
|
2015-09-09 21:59:11 +00:00
|
|
|
unversioned.TypeMeta `json:",inline"`
|
2015-06-09 00:51:57 +00:00
|
|
|
|
|
|
|
// IP is the primary ipv4/ipv6 address of the pod. Among other things it is the address that -
|
|
|
|
// - kube expects to be reachable across the cluster
|
|
|
|
// - service endpoints are constructed with
|
|
|
|
// - will be reported in the PodStatus.PodIP field (will override the IP reported by docker)
|
|
|
|
IP net.IP `json:"ip" description:"Primary IP address of the pod"`
|
2015-03-19 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Host is an interface that plugins can use to access the kubelet.
|
|
|
|
type Host interface {
|
|
|
|
// Get the pod structure by its name, namespace
|
|
|
|
GetPodByName(namespace, name string) (*api.Pod, bool)
|
|
|
|
|
|
|
|
// GetKubeClient returns a client interface
|
2016-02-01 22:30:47 +00:00
|
|
|
GetKubeClient() clientset.Interface
|
2015-09-09 21:00:41 +00:00
|
|
|
|
|
|
|
// GetContainerRuntime returns the container runtime that implements the containers (e.g. docker/rkt)
|
|
|
|
GetRuntime() kubecontainer.Runtime
|
2015-03-19 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitNetworkPlugin inits the plugin that matches networkPluginName. Plugins must have unique names.
|
|
|
|
func InitNetworkPlugin(plugins []NetworkPlugin, networkPluginName string, host Host) (NetworkPlugin, error) {
|
|
|
|
if networkPluginName == "" {
|
|
|
|
// default to the no_op plugin
|
|
|
|
plug := &noopNetworkPlugin{}
|
|
|
|
return plug, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginMap := map[string]NetworkPlugin{}
|
|
|
|
|
|
|
|
allErrs := []error{}
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
name := plugin.Name()
|
2015-09-10 22:48:28 +00:00
|
|
|
if !validation.IsQualifiedName(name) {
|
2015-03-19 23:14:13 +00:00
|
|
|
allErrs = append(allErrs, fmt.Errorf("network plugin has invalid name: %#v", plugin))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := pluginMap[name]; found {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("network plugin %q was registered more than once", name))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pluginMap[name] = plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
chosenPlugin := pluginMap[networkPluginName]
|
|
|
|
if chosenPlugin != nil {
|
|
|
|
err := chosenPlugin.Init(host)
|
|
|
|
if err != nil {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("Network plugin %q failed init: %v", networkPluginName, err))
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infof("Loaded network plugin %q", networkPluginName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("Network plugin %q not found.", networkPluginName))
|
|
|
|
}
|
|
|
|
|
2015-10-14 05:18:37 +00:00
|
|
|
return chosenPlugin, utilerrors.NewAggregate(allErrs)
|
2015-03-19 23:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnescapePluginName(in string) string {
|
|
|
|
return strings.Replace(in, "~", "/", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopNetworkPlugin struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (plugin *noopNetworkPlugin) Init(host Host) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 04:02:59 +00:00
|
|
|
func (plugin *noopNetworkPlugin) Event(name string, details map[string]interface{}) {
|
|
|
|
}
|
|
|
|
|
2015-03-19 23:14:13 +00:00
|
|
|
func (plugin *noopNetworkPlugin) Name() string {
|
|
|
|
return DefaultPluginName
|
|
|
|
}
|
|
|
|
|
2015-12-24 23:46:56 +00:00
|
|
|
func (plugin *noopNetworkPlugin) SetUpPod(namespace string, name string, id kubecontainer.DockerID) error {
|
2015-03-19 23:14:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-24 23:46:56 +00:00
|
|
|
func (plugin *noopNetworkPlugin) TearDownPod(namespace string, name string, id kubecontainer.DockerID) error {
|
2015-03-19 23:14:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-09 00:51:57 +00:00
|
|
|
|
2015-12-24 23:46:56 +00:00
|
|
|
func (plugin *noopNetworkPlugin) Status(namespace string, name string, id kubecontainer.DockerID) (*PodNetworkStatus, error) {
|
2015-06-09 00:51:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|