kuberuntime_container_windows.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kuberuntime
  15. import (
  16. "fmt"
  17. "github.com/docker/docker/pkg/sysinfo"
  18. "k8s.io/api/core/v1"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  21. kubefeatures "k8s.io/kubernetes/pkg/features"
  22. kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
  23. "k8s.io/kubernetes/pkg/securitycontext"
  24. )
  25. // applyPlatformSpecificContainerConfig applies platform specific configurations to runtimeapi.ContainerConfig.
  26. func (m *kubeGenericRuntimeManager) applyPlatformSpecificContainerConfig(config *runtimeapi.ContainerConfig, container *v1.Container, pod *v1.Pod, uid *int64, username string) error {
  27. windowsConfig, err := m.generateWindowsContainerConfig(container, pod, uid, username)
  28. if err != nil {
  29. return err
  30. }
  31. config.Windows = windowsConfig
  32. return nil
  33. }
  34. // generateWindowsContainerConfig generates windows container config for kubelet runtime v1.
  35. // Refer https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/cri-windows.md.
  36. func (m *kubeGenericRuntimeManager) generateWindowsContainerConfig(container *v1.Container, pod *v1.Pod, uid *int64, username string) (*runtimeapi.WindowsContainerConfig, error) {
  37. wc := &runtimeapi.WindowsContainerConfig{
  38. Resources: &runtimeapi.WindowsContainerResources{},
  39. SecurityContext: &runtimeapi.WindowsContainerSecurityContext{},
  40. }
  41. cpuRequest := container.Resources.Requests.Cpu()
  42. cpuLimit := container.Resources.Limits.Cpu()
  43. isolatedByHyperv := kubeletapis.ShouldIsolatedByHyperV(pod.Annotations)
  44. if !cpuLimit.IsZero() {
  45. // Note that sysinfo.NumCPU() is limited to 64 CPUs on Windows due to Processor Groups,
  46. // as only 64 processors are available for execution by a given process. This causes
  47. // some oddities on systems with more than 64 processors.
  48. // Refer https://msdn.microsoft.com/en-us/library/windows/desktop/dd405503(v=vs.85).aspx.
  49. cpuMaximum := 10000 * cpuLimit.MilliValue() / int64(sysinfo.NumCPU()) / 1000
  50. if isolatedByHyperv {
  51. cpuCount := int64(cpuLimit.MilliValue()+999) / 1000
  52. wc.Resources.CpuCount = cpuCount
  53. if cpuCount != 0 {
  54. cpuMaximum = cpuLimit.MilliValue() / cpuCount * 10000 / 1000
  55. }
  56. }
  57. // ensure cpuMaximum is in range [1, 10000].
  58. if cpuMaximum < 1 {
  59. cpuMaximum = 1
  60. } else if cpuMaximum > 10000 {
  61. cpuMaximum = 10000
  62. }
  63. wc.Resources.CpuMaximum = cpuMaximum
  64. }
  65. cpuShares := milliCPUToShares(cpuLimit.MilliValue(), isolatedByHyperv)
  66. if cpuShares == 0 {
  67. cpuShares = milliCPUToShares(cpuRequest.MilliValue(), isolatedByHyperv)
  68. }
  69. wc.Resources.CpuShares = cpuShares
  70. memoryLimit := container.Resources.Limits.Memory().Value()
  71. if memoryLimit != 0 {
  72. wc.Resources.MemoryLimitInBytes = memoryLimit
  73. }
  74. // setup security context
  75. effectiveSc := securitycontext.DetermineEffectiveSecurityContext(pod, container)
  76. // RunAsUser only supports int64 from Kubernetes API, but Windows containers only support username.
  77. if effectiveSc.RunAsUser != nil {
  78. return nil, fmt.Errorf("run as uid (%d) is not supported on Windows", *effectiveSc.RunAsUser)
  79. }
  80. if username != "" {
  81. wc.SecurityContext.RunAsUsername = username
  82. }
  83. if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.WindowsGMSA) &&
  84. effectiveSc.WindowsOptions != nil &&
  85. effectiveSc.WindowsOptions.GMSACredentialSpec != nil {
  86. wc.SecurityContext.CredentialSpec = *effectiveSc.WindowsOptions.GMSACredentialSpec
  87. }
  88. return wc, nil
  89. }