hollow_kubelet.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubemark
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/klog"
  18. "k8s.io/utils/mount"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. clientset "k8s.io/client-go/kubernetes"
  21. internalapi "k8s.io/cri-api/pkg/apis"
  22. kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
  23. "k8s.io/kubernetes/cmd/kubelet/app/options"
  24. "k8s.io/kubernetes/pkg/kubelet"
  25. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  26. "k8s.io/kubernetes/pkg/kubelet/cadvisor"
  27. "k8s.io/kubernetes/pkg/kubelet/cm"
  28. containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  29. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  30. "k8s.io/kubernetes/pkg/util/oom"
  31. "k8s.io/kubernetes/pkg/volume"
  32. "k8s.io/kubernetes/pkg/volume/cephfs"
  33. "k8s.io/kubernetes/pkg/volume/configmap"
  34. "k8s.io/kubernetes/pkg/volume/csi"
  35. "k8s.io/kubernetes/pkg/volume/downwardapi"
  36. "k8s.io/kubernetes/pkg/volume/emptydir"
  37. "k8s.io/kubernetes/pkg/volume/fc"
  38. "k8s.io/kubernetes/pkg/volume/flocker"
  39. "k8s.io/kubernetes/pkg/volume/git_repo"
  40. "k8s.io/kubernetes/pkg/volume/glusterfs"
  41. "k8s.io/kubernetes/pkg/volume/hostpath"
  42. "k8s.io/kubernetes/pkg/volume/iscsi"
  43. "k8s.io/kubernetes/pkg/volume/local"
  44. "k8s.io/kubernetes/pkg/volume/nfs"
  45. "k8s.io/kubernetes/pkg/volume/portworx"
  46. "k8s.io/kubernetes/pkg/volume/projected"
  47. "k8s.io/kubernetes/pkg/volume/quobyte"
  48. "k8s.io/kubernetes/pkg/volume/rbd"
  49. "k8s.io/kubernetes/pkg/volume/scaleio"
  50. "k8s.io/kubernetes/pkg/volume/secret"
  51. "k8s.io/kubernetes/pkg/volume/storageos"
  52. "k8s.io/kubernetes/pkg/volume/util/hostutil"
  53. "k8s.io/kubernetes/pkg/volume/util/subpath"
  54. "k8s.io/kubernetes/test/utils"
  55. )
  56. type HollowKubelet struct {
  57. KubeletFlags *options.KubeletFlags
  58. KubeletConfiguration *kubeletconfig.KubeletConfiguration
  59. KubeletDeps *kubelet.Dependencies
  60. }
  61. func volumePlugins() []volume.VolumePlugin {
  62. allPlugins := []volume.VolumePlugin{}
  63. allPlugins = append(allPlugins, emptydir.ProbeVolumePlugins()...)
  64. allPlugins = append(allPlugins, git_repo.ProbeVolumePlugins()...)
  65. allPlugins = append(allPlugins, hostpath.ProbeVolumePlugins(volume.VolumeConfig{})...)
  66. allPlugins = append(allPlugins, nfs.ProbeVolumePlugins(volume.VolumeConfig{})...)
  67. allPlugins = append(allPlugins, secret.ProbeVolumePlugins()...)
  68. allPlugins = append(allPlugins, iscsi.ProbeVolumePlugins()...)
  69. allPlugins = append(allPlugins, glusterfs.ProbeVolumePlugins()...)
  70. allPlugins = append(allPlugins, rbd.ProbeVolumePlugins()...)
  71. allPlugins = append(allPlugins, quobyte.ProbeVolumePlugins()...)
  72. allPlugins = append(allPlugins, cephfs.ProbeVolumePlugins()...)
  73. allPlugins = append(allPlugins, downwardapi.ProbeVolumePlugins()...)
  74. allPlugins = append(allPlugins, fc.ProbeVolumePlugins()...)
  75. allPlugins = append(allPlugins, flocker.ProbeVolumePlugins()...)
  76. allPlugins = append(allPlugins, configmap.ProbeVolumePlugins()...)
  77. allPlugins = append(allPlugins, projected.ProbeVolumePlugins()...)
  78. allPlugins = append(allPlugins, portworx.ProbeVolumePlugins()...)
  79. allPlugins = append(allPlugins, scaleio.ProbeVolumePlugins()...)
  80. allPlugins = append(allPlugins, local.ProbeVolumePlugins()...)
  81. allPlugins = append(allPlugins, storageos.ProbeVolumePlugins()...)
  82. allPlugins = append(allPlugins, csi.ProbeVolumePlugins()...)
  83. return allPlugins
  84. }
  85. func NewHollowKubelet(
  86. flags *options.KubeletFlags,
  87. config *kubeletconfig.KubeletConfiguration,
  88. client *clientset.Clientset,
  89. heartbeatClient *clientset.Clientset,
  90. cadvisorInterface cadvisor.Interface,
  91. imageService internalapi.ImageManagerService,
  92. runtimeService internalapi.RuntimeService,
  93. containerManager cm.ContainerManager) *HollowKubelet {
  94. d := &kubelet.Dependencies{
  95. KubeClient: client,
  96. HeartbeatClient: heartbeatClient,
  97. RemoteRuntimeService: runtimeService,
  98. RemoteImageService: imageService,
  99. CAdvisorInterface: cadvisorInterface,
  100. Cloud: nil,
  101. OSInterface: &containertest.FakeOS{},
  102. ContainerManager: containerManager,
  103. VolumePlugins: volumePlugins(),
  104. TLSOptions: nil,
  105. OOMAdjuster: oom.NewFakeOOMAdjuster(),
  106. Mounter: &mount.FakeMounter{},
  107. Subpather: &subpath.FakeSubpath{},
  108. HostUtil: hostutil.NewFakeHostUtil(nil),
  109. }
  110. return &HollowKubelet{
  111. KubeletFlags: flags,
  112. KubeletConfiguration: config,
  113. KubeletDeps: d,
  114. }
  115. }
  116. // Starts this HollowKubelet and blocks.
  117. func (hk *HollowKubelet) Run() {
  118. if err := kubeletapp.RunKubelet(&options.KubeletServer{
  119. KubeletFlags: *hk.KubeletFlags,
  120. KubeletConfiguration: *hk.KubeletConfiguration,
  121. }, hk.KubeletDeps, false); err != nil {
  122. klog.Fatalf("Failed to run HollowKubelet: %v. Exiting.", err)
  123. }
  124. select {}
  125. }
  126. // HollowKubletOptions contains settable parameters for hollow kubelet.
  127. type HollowKubletOptions struct {
  128. NodeName string
  129. KubeletPort int
  130. KubeletReadOnlyPort int
  131. MaxPods int
  132. PodsPerCore int
  133. NodeLabels map[string]string
  134. }
  135. // Builds a KubeletConfiguration for the HollowKubelet, ensuring that the
  136. // usual defaults are applied for fields we do not override.
  137. func GetHollowKubeletConfig(opt *HollowKubletOptions) (*options.KubeletFlags, *kubeletconfig.KubeletConfiguration) {
  138. testRootDir := utils.MakeTempDirOrDie("hollow-kubelet.", "")
  139. podFilePath := utils.MakeTempDirOrDie("static-pods", testRootDir)
  140. klog.Infof("Using %s as root dir for hollow-kubelet", testRootDir)
  141. // Flags struct
  142. f := options.NewKubeletFlags()
  143. f.EnableServer = true
  144. f.RootDirectory = testRootDir
  145. f.HostnameOverride = opt.NodeName
  146. f.MinimumGCAge = metav1.Duration{Duration: 1 * time.Minute}
  147. f.MaxContainerCount = 100
  148. f.MaxPerPodContainerCount = 2
  149. f.NodeLabels = opt.NodeLabels
  150. f.ContainerRuntimeOptions.ContainerRuntime = kubetypes.RemoteContainerRuntime
  151. f.RegisterNode = true
  152. f.RegisterSchedulable = true
  153. f.ProviderID = fmt.Sprintf("kubemark://%v", opt.NodeName)
  154. // Config struct
  155. c, err := options.NewKubeletConfiguration()
  156. if err != nil {
  157. panic(err)
  158. }
  159. c.StaticPodURL = ""
  160. c.Address = "0.0.0.0" /* bind address */
  161. c.Port = int32(opt.KubeletPort)
  162. c.ReadOnlyPort = int32(opt.KubeletReadOnlyPort)
  163. c.StaticPodPath = podFilePath
  164. c.FileCheckFrequency.Duration = 20 * time.Second
  165. c.HTTPCheckFrequency.Duration = 20 * time.Second
  166. c.NodeStatusUpdateFrequency.Duration = 10 * time.Second
  167. c.NodeStatusReportFrequency.Duration = 5 * time.Minute
  168. c.SyncFrequency.Duration = 10 * time.Second
  169. c.EvictionPressureTransitionPeriod.Duration = 5 * time.Minute
  170. c.MaxPods = int32(opt.MaxPods)
  171. c.PodsPerCore = int32(opt.PodsPerCore)
  172. c.ClusterDNS = []string{}
  173. c.ImageGCHighThresholdPercent = 90
  174. c.ImageGCLowThresholdPercent = 80
  175. c.VolumeStatsAggPeriod.Duration = time.Minute
  176. c.CgroupRoot = ""
  177. c.CPUCFSQuota = true
  178. c.EnableControllerAttachDetach = false
  179. c.EnableDebuggingHandlers = true
  180. c.CgroupsPerQOS = false
  181. // hairpin-veth is used to allow hairpin packets. Note that this deviates from
  182. // what the "real" kubelet currently does, because there's no way to
  183. // set promiscuous mode on docker0.
  184. c.HairpinMode = kubeletconfig.HairpinVeth
  185. c.MaxOpenFiles = 1024
  186. c.RegistryBurst = 10
  187. c.RegistryPullQPS = 5.0
  188. c.ResolverConfig = kubetypes.ResolvConfDefault
  189. c.KubeletCgroups = "/kubelet"
  190. c.SerializeImagePulls = true
  191. c.SystemCgroups = ""
  192. c.ProtectKernelDefaults = false
  193. return f, c
  194. }