ceph-csi/vendor/k8s.io/kubernetes/cmd/kubemark/hollow-node.go

211 lines
6.7 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2015 The Kubernetes Authors.
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 main
import (
2018-03-06 22:33:18 +00:00
goflag "flag"
2018-01-09 18:57:14 +00:00
"fmt"
2018-03-06 22:33:18 +00:00
"math/rand"
"os"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
2018-11-26 18:23:56 +00:00
"k8s.io/klog"
2018-01-09 18:57:14 +00:00
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
2018-03-06 22:33:18 +00:00
utilflag "k8s.io/apiserver/pkg/util/flag"
"k8s.io/apiserver/pkg/util/logs"
2018-01-09 18:57:14 +00:00
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
"k8s.io/kubernetes/pkg/api/legacyscheme"
_ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
cadvisortest "k8s.io/kubernetes/pkg/kubelet/cadvisor/testing"
"k8s.io/kubernetes/pkg/kubelet/cm"
"k8s.io/kubernetes/pkg/kubelet/dockershim"
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
"k8s.io/kubernetes/pkg/kubemark"
fakeiptables "k8s.io/kubernetes/pkg/util/iptables/testing"
fakesysctl "k8s.io/kubernetes/pkg/util/sysctl/testing"
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
2018-03-06 22:33:18 +00:00
"k8s.io/kubernetes/pkg/version/verflag"
2018-01-09 18:57:14 +00:00
fakeexec "k8s.io/utils/exec/testing"
)
type HollowNodeConfig struct {
2018-11-26 18:23:56 +00:00
KubeconfigPath string
KubeletPort int
KubeletReadOnlyPort int
Morph string
NodeName string
ServerPort int
ContentType string
UseRealProxier bool
ProxierSyncPeriod time.Duration
ProxierMinSyncPeriod time.Duration
2018-01-09 18:57:14 +00:00
}
const (
maxPods = 110
podsPerCore = 0
)
2018-11-26 18:23:56 +00:00
// TODO(#45650): Refactor hollow-node into hollow-kubelet and hollow-proxy
// and make the config driven.
2018-01-09 18:57:14 +00:00
var knownMorphs = sets.NewString("kubelet", "proxy")
func (c *HollowNodeConfig) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.KubeconfigPath, "kubeconfig", "/kubeconfig/kubeconfig", "Path to kubeconfig file.")
fs.IntVar(&c.KubeletPort, "kubelet-port", 10250, "Port on which HollowKubelet should be listening.")
fs.IntVar(&c.KubeletReadOnlyPort, "kubelet-read-only-port", 10255, "Read-only port on which Kubelet is listening.")
fs.StringVar(&c.NodeName, "name", "fake-node", "Name of this Hollow Node.")
fs.IntVar(&c.ServerPort, "api-server-port", 443, "Port on which API server is listening.")
fs.StringVar(&c.Morph, "morph", "", fmt.Sprintf("Specifies into which Hollow component this binary should morph. Allowed values: %v", knownMorphs.List()))
fs.StringVar(&c.ContentType, "kube-api-content-type", "application/vnd.kubernetes.protobuf", "ContentType of requests sent to apiserver.")
fs.BoolVar(&c.UseRealProxier, "use-real-proxier", true, "Set to true if you want to use real proxier inside hollow-proxy.")
2018-11-26 18:23:56 +00:00
fs.DurationVar(&c.ProxierSyncPeriod, "proxier-sync-period", 30*time.Second, "Period that proxy rules are refreshed in hollow-proxy.")
fs.DurationVar(&c.ProxierMinSyncPeriod, "proxier-min-sync-period", 0, "Minimum period that proxy rules are refreshed in hollow-proxy.")
2018-01-09 18:57:14 +00:00
}
func (c *HollowNodeConfig) createClientConfigFromFile() (*restclient.Config, error) {
clientConfig, err := clientcmd.LoadFromFile(c.KubeconfigPath)
if err != nil {
return nil, fmt.Errorf("error while loading kubeconfig from file %v: %v", c.KubeconfigPath, err)
}
config, err := clientcmd.NewDefaultClientConfig(*clientConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("error while creating kubeconfig: %v", err)
}
config.ContentType = c.ContentType
config.QPS = 10
config.Burst = 20
return config, nil
}
func main() {
2018-11-26 18:23:56 +00:00
rand.Seed(time.Now().UnixNano())
2018-03-06 22:33:18 +00:00
command := newHollowNodeCommand()
// TODO: once we switch everything over to Cobra commands, we can go back to calling
// utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
// normalize func and add the go flag set by hand.
pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
// utilflag.InitFlags()
logs.InitLogs()
defer logs.FlushLogs()
if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
// newControllerManagerCommand creates a *cobra.Command object with default parameters
func newHollowNodeCommand() *cobra.Command {
s := &HollowNodeConfig{}
cmd := &cobra.Command{
Use: "kubemark",
Long: "kubemark",
Run: func(cmd *cobra.Command, args []string) {
verflag.PrintAndExitIfRequested()
run(s)
},
}
s.addFlags(cmd.Flags())
return cmd
}
2018-01-09 18:57:14 +00:00
2018-03-06 22:33:18 +00:00
func run(config *HollowNodeConfig) {
2018-01-09 18:57:14 +00:00
if !knownMorphs.Has(config.Morph) {
2018-11-26 18:23:56 +00:00
klog.Fatalf("Unknown morph: %v. Allowed values: %v", config.Morph, knownMorphs.List())
2018-01-09 18:57:14 +00:00
}
// create a client to communicate with API server.
clientConfig, err := config.createClientConfigFromFile()
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Fatalf("Failed to create a ClientConfig: %v. Exiting.", err)
2018-01-09 18:57:14 +00:00
}
client, err := clientset.NewForConfig(clientConfig)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Fatalf("Failed to create a ClientSet: %v. Exiting.", err)
2018-01-09 18:57:14 +00:00
}
if config.Morph == "kubelet" {
cadvisorInterface := &cadvisortest.Fake{
NodeName: config.NodeName,
}
containerManager := cm.NewStubContainerManager()
fakeDockerClientConfig := &dockershim.ClientConfig{
DockerEndpoint: libdocker.FakeDockerEndpoint,
EnableSleep: true,
WithTraceDisabled: true,
}
hollowKubelet := kubemark.NewHollowKubelet(
config.NodeName,
client,
cadvisorInterface,
fakeDockerClientConfig,
config.KubeletPort,
config.KubeletReadOnlyPort,
containerManager,
maxPods,
podsPerCore,
)
hollowKubelet.Run()
}
if config.Morph == "proxy" {
client, err := clientset.NewForConfig(clientConfig)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Fatalf("Failed to create API Server client: %v", err)
2018-01-09 18:57:14 +00:00
}
iptInterface := fakeiptables.NewFake()
sysctl := fakesysctl.NewFake()
execer := &fakeexec.FakeExec{}
eventBroadcaster := record.NewBroadcaster()
recorder := eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: "kube-proxy", Host: config.NodeName})
hollowProxy, err := kubemark.NewHollowProxyOrDie(
config.NodeName,
2018-11-26 18:23:56 +00:00
client,
2018-01-09 18:57:14 +00:00
client.CoreV1(),
iptInterface,
sysctl,
execer,
eventBroadcaster,
recorder,
config.UseRealProxier,
2018-11-26 18:23:56 +00:00
config.ProxierSyncPeriod,
config.ProxierMinSyncPeriod,
2018-01-09 18:57:14 +00:00
)
if err != nil {
2018-11-26 18:23:56 +00:00
klog.Fatalf("Failed to create hollowProxy instance: %v", err)
2018-01-09 18:57:14 +00:00
}
hollowProxy.Run()
}
}