mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #5886 from vmarmol/net-host-1
Implement Kubelet support for HostNetwork by pod source
This commit is contained in:
commit
e54cbf6e07
@ -184,6 +184,8 @@ func (s *APIServer) Run(_ []string) error {
|
|||||||
|
|
||||||
capabilities.Initialize(capabilities.Capabilities{
|
capabilities.Initialize(capabilities.Capabilities{
|
||||||
AllowPrivileged: s.AllowPrivileged,
|
AllowPrivileged: s.AllowPrivileged,
|
||||||
|
// TODO(vmarmol): Implement support for HostNetworkSources.
|
||||||
|
HostNetworkSources: []string{},
|
||||||
})
|
})
|
||||||
|
|
||||||
cloud := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
cloud := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
@ -61,6 +62,7 @@ type KubeletServer struct {
|
|||||||
DockerEndpoint string
|
DockerEndpoint string
|
||||||
RootDirectory string
|
RootDirectory string
|
||||||
AllowPrivileged bool
|
AllowPrivileged bool
|
||||||
|
HostNetworkSources string
|
||||||
RegistryPullQPS float64
|
RegistryPullQPS float64
|
||||||
RegistryBurst int
|
RegistryBurst int
|
||||||
RunOnce bool
|
RunOnce bool
|
||||||
@ -106,6 +108,7 @@ func NewKubeletServer() *KubeletServer {
|
|||||||
ImageGCHighThresholdPercent: 90,
|
ImageGCHighThresholdPercent: 90,
|
||||||
ImageGCLowThresholdPercent: 80,
|
ImageGCLowThresholdPercent: 80,
|
||||||
NetworkPluginName: "",
|
NetworkPluginName: "",
|
||||||
|
HostNetworkSources: kubelet.FileSource,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,6 +127,7 @@ func (s *KubeletServer) AddFlags(fs *pflag.FlagSet) {
|
|||||||
fs.StringVar(&s.DockerEndpoint, "docker_endpoint", s.DockerEndpoint, "If non-empty, use this for the docker endpoint to communicate with")
|
fs.StringVar(&s.DockerEndpoint, "docker_endpoint", s.DockerEndpoint, "If non-empty, use this for the docker endpoint to communicate with")
|
||||||
fs.StringVar(&s.RootDirectory, "root_dir", s.RootDirectory, "Directory path for managing kubelet files (volume mounts,etc).")
|
fs.StringVar(&s.RootDirectory, "root_dir", s.RootDirectory, "Directory path for managing kubelet files (volume mounts,etc).")
|
||||||
fs.BoolVar(&s.AllowPrivileged, "allow_privileged", s.AllowPrivileged, "If true, allow containers to request privileged mode. [default=false]")
|
fs.BoolVar(&s.AllowPrivileged, "allow_privileged", s.AllowPrivileged, "If true, allow containers to request privileged mode. [default=false]")
|
||||||
|
fs.StringVar(&s.HostNetworkSources, "host_network_sources", s.HostNetworkSources, "Comma-separated list of sources from which the Kubelet allows pods to use of host network. For all sources use \"*\" [default=\"file\"]")
|
||||||
fs.Float64Var(&s.RegistryPullQPS, "registry_qps", s.RegistryPullQPS, "If > 0, limit registry pull QPS to this value. If 0, unlimited. [default=0.0]")
|
fs.Float64Var(&s.RegistryPullQPS, "registry_qps", s.RegistryPullQPS, "If > 0, limit registry pull QPS to this value. If 0, unlimited. [default=0.0]")
|
||||||
fs.IntVar(&s.RegistryBurst, "registry_burst", s.RegistryBurst, "Maximum size of a bursty pulls, temporarily allows pulls to burst to this number, while still not exceeding registry_qps. Only used if --registry_qps > 0")
|
fs.IntVar(&s.RegistryBurst, "registry_burst", s.RegistryBurst, "Maximum size of a bursty pulls, temporarily allows pulls to burst to this number, while still not exceeding registry_qps. Only used if --registry_qps > 0")
|
||||||
fs.BoolVar(&s.RunOnce, "runonce", s.RunOnce, "If true, exit after spawning pods from local manifests or remote urls. Exclusive with --api_servers, and --enable-server")
|
fs.BoolVar(&s.RunOnce, "runonce", s.RunOnce, "If true, exit after spawning pods from local manifests or remote urls. Exclusive with --api_servers, and --enable-server")
|
||||||
@ -178,9 +182,14 @@ func (s *KubeletServer) Run(_ []string) error {
|
|||||||
cloud := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
cloud := cloudprovider.InitCloudProvider(s.CloudProvider, s.CloudConfigFile)
|
||||||
glog.Infof("Successfully initialized cloud provider: %q from the config file: %q\n", s.CloudProvider, s.CloudConfigFile)
|
glog.Infof("Successfully initialized cloud provider: %q from the config file: %q\n", s.CloudProvider, s.CloudConfigFile)
|
||||||
|
|
||||||
|
hostNetworkSources, err := kubelet.GetValidatedSources(strings.Split(s.HostNetworkSources, ","))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
kcfg := KubeletConfig{
|
kcfg := KubeletConfig{
|
||||||
Address: s.Address,
|
Address: s.Address,
|
||||||
AllowPrivileged: s.AllowPrivileged,
|
AllowPrivileged: s.AllowPrivileged,
|
||||||
|
HostNetworkSources: hostNetworkSources,
|
||||||
HostnameOverride: s.HostnameOverride,
|
HostnameOverride: s.HostnameOverride,
|
||||||
RootDirectory: s.RootDirectory,
|
RootDirectory: s.RootDirectory,
|
||||||
ConfigFile: s.Config,
|
ConfigFile: s.Config,
|
||||||
@ -321,7 +330,7 @@ func RunKubelet(kcfg *KubeletConfig) {
|
|||||||
glog.Infof("No api server defined - no events will be sent.")
|
glog.Infof("No api server defined - no events will be sent.")
|
||||||
}
|
}
|
||||||
kubelet.SetupLogging()
|
kubelet.SetupLogging()
|
||||||
kubelet.SetupCapabilities(kcfg.AllowPrivileged)
|
kubelet.SetupCapabilities(kcfg.AllowPrivileged, kcfg.HostNetworkSources)
|
||||||
|
|
||||||
credentialprovider.SetPreferredDockercfgPath(kcfg.RootDirectory)
|
credentialprovider.SetPreferredDockercfgPath(kcfg.RootDirectory)
|
||||||
|
|
||||||
@ -383,6 +392,7 @@ type KubeletConfig struct {
|
|||||||
CadvisorInterface cadvisor.Interface
|
CadvisorInterface cadvisor.Interface
|
||||||
Address util.IP
|
Address util.IP
|
||||||
AllowPrivileged bool
|
AllowPrivileged bool
|
||||||
|
HostNetworkSources []string
|
||||||
HostnameOverride string
|
HostnameOverride string
|
||||||
RootDirectory string
|
RootDirectory string
|
||||||
ConfigFile string
|
ConfigFile string
|
||||||
|
@ -24,6 +24,9 @@ import (
|
|||||||
// For now these are global. Eventually they may be per-user
|
// For now these are global. Eventually they may be per-user
|
||||||
type Capabilities struct {
|
type Capabilities struct {
|
||||||
AllowPrivileged bool
|
AllowPrivileged bool
|
||||||
|
|
||||||
|
// List of pod sources for which using host network is allowed.
|
||||||
|
HostNetworkSources []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
@ -46,7 +49,8 @@ func SetForTests(c Capabilities) {
|
|||||||
func Get() Capabilities {
|
func Get() Capabilities {
|
||||||
if capabilities == nil {
|
if capabilities == nil {
|
||||||
Initialize(Capabilities{
|
Initialize(Capabilities{
|
||||||
AllowPrivileged: false,
|
AllowPrivileged: false,
|
||||||
|
HostNetworkSources: []string{},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return *capabilities
|
return *capabilities
|
||||||
|
@ -1007,6 +1007,20 @@ const (
|
|||||||
PodInfraContainerImage = "kubernetes/pause:latest"
|
PodInfraContainerImage = "kubernetes/pause:latest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Determined whether the specified pod is allowed to use host networking
|
||||||
|
func allowHostNetwork(pod *api.Pod) (bool, error) {
|
||||||
|
podSource, err := getPodSource(pod)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
for _, source := range capabilities.Get().HostNetworkSources {
|
||||||
|
if source == podSource {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
// createPodInfraContainer starts the pod infra container for a pod. Returns the docker container ID of the newly created container.
|
// createPodInfraContainer starts the pod infra container for a pod. Returns the docker container ID of the newly created container.
|
||||||
func (kl *Kubelet) createPodInfraContainer(pod *api.Pod) (dockertools.DockerID, error) {
|
func (kl *Kubelet) createPodInfraContainer(pod *api.Pod) (dockertools.DockerID, error) {
|
||||||
var ports []api.ContainerPort
|
var ports []api.ContainerPort
|
||||||
@ -1040,7 +1054,22 @@ func (kl *Kubelet) createPodInfraContainer(pod *api.Pod) (dockertools.DockerID,
|
|||||||
if ref != nil {
|
if ref != nil {
|
||||||
kl.recorder.Eventf(ref, "pulled", "Successfully pulled image %q", container.Image)
|
kl.recorder.Eventf(ref, "pulled", "Successfully pulled image %q", container.Image)
|
||||||
}
|
}
|
||||||
id, err := kl.runContainer(pod, container, nil, "", "")
|
|
||||||
|
// Use host networking if specified and allowed.
|
||||||
|
netNamespace := ""
|
||||||
|
if pod.Spec.HostNetwork {
|
||||||
|
allowed, err := allowHostNetwork(pod)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !allowed {
|
||||||
|
return "", fmt.Errorf("pod with UID %q specified host networking, but is disallowed", pod.UID)
|
||||||
|
}
|
||||||
|
|
||||||
|
netNamespace = "host"
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := kl.runContainer(pod, container, nil, netNamespace, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/cadvisor"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/cadvisor"
|
||||||
@ -3456,3 +3457,61 @@ func TestDoNotCacheStatusForStaticPods(t *testing.T) {
|
|||||||
t.Errorf("unexpected status %#v found for static pod %q", status, podFullName)
|
t.Errorf("unexpected status %#v found for static pod %q", status, podFullName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHostNetworkAllowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t)
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
HostNetworkSources: []string{ApiserverSource, FileSource},
|
||||||
|
})
|
||||||
|
pod := &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
UID: "12345678",
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: "new",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ConfigSourceAnnotationKey: FileSource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostNetwork: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err := kubelet.createPodInfraContainer(pod)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected pod infra creation to succeed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostNetworkDisallowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t)
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
HostNetworkSources: []string{},
|
||||||
|
})
|
||||||
|
pod := &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
UID: "12345678",
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: "new",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ConfigSourceAnnotationKey: FileSource,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostNetwork: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err := kubelet.createPodInfraContainer(pod)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected pod infra creation to fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,7 +16,11 @@ limitations under the License.
|
|||||||
|
|
||||||
package kubelet
|
package kubelet
|
||||||
|
|
||||||
import "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
)
|
||||||
|
|
||||||
const ConfigSourceAnnotationKey = "kubernetes.io/config.source"
|
const ConfigSourceAnnotationKey = "kubernetes.io/config.source"
|
||||||
const ConfigMirrorAnnotationKey = "kubernetes.io/config.mirror"
|
const ConfigMirrorAnnotationKey = "kubernetes.io/config.mirror"
|
||||||
@ -64,3 +68,22 @@ type PodUpdate struct {
|
|||||||
Op PodOperation
|
Op PodOperation
|
||||||
Source string
|
Source string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets all validated sources from the specified sources.
|
||||||
|
func GetValidatedSources(sources []string) ([]string, error) {
|
||||||
|
validated := make([]string, 0, len(sources))
|
||||||
|
for _, source := range sources {
|
||||||
|
switch source {
|
||||||
|
case AllSource:
|
||||||
|
return []string{FileSource, HTTPSource, ApiserverSource}, nil
|
||||||
|
case FileSource, HTTPSource, ApiserverSource:
|
||||||
|
validated = append(validated, source)
|
||||||
|
break
|
||||||
|
case "":
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return []string{}, fmt.Errorf("unknown pod source %q", source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return validated, nil
|
||||||
|
}
|
||||||
|
44
pkg/kubelet/types_test.go
Normal file
44
pkg/kubelet/types_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. 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 kubelet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetValidatedSources(t *testing.T) {
|
||||||
|
// Empty.
|
||||||
|
sources, err := GetValidatedSources([]string{""})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, sources, 0)
|
||||||
|
|
||||||
|
// Success.
|
||||||
|
sources, err = GetValidatedSources([]string{FileSource, ApiserverSource})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, sources, 2)
|
||||||
|
|
||||||
|
// All.
|
||||||
|
sources, err = GetValidatedSources([]string{AllSource})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, sources, 3)
|
||||||
|
|
||||||
|
// Unknown source.
|
||||||
|
sources, err = GetValidatedSources([]string{"taco"})
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
@ -27,9 +27,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TODO: move this into pkg/capabilities
|
// TODO: move this into pkg/capabilities
|
||||||
func SetupCapabilities(allowPrivileged bool) {
|
func SetupCapabilities(allowPrivileged bool, hostNetworkSources []string) {
|
||||||
capabilities.Initialize(capabilities.Capabilities{
|
capabilities.Initialize(capabilities.Capabilities{
|
||||||
AllowPrivileged: allowPrivileged,
|
AllowPrivileged: allowPrivileged,
|
||||||
|
HostNetworkSources: hostNetworkSources,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user