|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 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 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package managed |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + "k8s.io/apimachinery/pkg/api/resource" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + pinnedManagementEnabled bool |
| 31 | + pinnedManagementFilename = "/etc/kubernetes/openshift-workload-pinning" |
| 32 | + WorkloadsAnnotationPrefix = "target.workload.openshift.io/" |
| 33 | + WorkloadsCapacitySuffix = "workload.openshift.io/cores" |
| 34 | + ContainerAnnotationFormat = "resources.workload.openshift.io/%v" |
| 35 | +) |
| 36 | + |
| 37 | +type WorkloadContainerAnnotation struct { |
| 38 | + CpuShares uint64 `json:"cpushares"` |
| 39 | +} |
| 40 | + |
| 41 | +func NewWorkloadContainerAnnotation(cpushares uint64) WorkloadContainerAnnotation { |
| 42 | + return WorkloadContainerAnnotation{ |
| 43 | + CpuShares: cpushares, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (w WorkloadContainerAnnotation) Serialize() ([]byte, error) { |
| 48 | + return json.Marshal(w) |
| 49 | +} |
| 50 | + |
| 51 | +func init() { |
| 52 | + readEnablementFile() |
| 53 | +} |
| 54 | + |
| 55 | +func readEnablementFile() { |
| 56 | + if _, err := os.Stat(pinnedManagementFilename); err == nil { |
| 57 | + pinnedManagementEnabled = true |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func IsEnabled() bool { |
| 62 | + return pinnedManagementEnabled |
| 63 | +} |
| 64 | + |
| 65 | +/// IsPodManaged returns true and the name of the workload if enabled. |
| 66 | +/// returns true, workload name, and the annotation payload. |
| 67 | +func IsPodManaged(pod *v1.Pod) (bool, string, string) { |
| 68 | + if pod.ObjectMeta.Annotations == nil { |
| 69 | + return false, "", "" |
| 70 | + } |
| 71 | + for annotation, value := range pod.ObjectMeta.Annotations { |
| 72 | + if strings.HasPrefix(annotation, WorkloadsAnnotationPrefix) { |
| 73 | + return true, strings.TrimPrefix(annotation, WorkloadsAnnotationPrefix), value |
| 74 | + } |
| 75 | + } |
| 76 | + return false, "", "" |
| 77 | +} |
| 78 | + |
| 79 | +// ModifyStaticPodForPinnedManagement will modify a pod for pod management |
| 80 | +func ModifyStaticPodForPinnedManagement(pod *v1.Pod) (*v1.Pod, string, error) { |
| 81 | + pod = pod.DeepCopy() |
| 82 | + enabled, workloadName, value := IsPodManaged(pod) |
| 83 | + if !enabled { |
| 84 | + return nil, "", nil |
| 85 | + } |
| 86 | + if pod.Annotations == nil { |
| 87 | + pod.Annotations = make(map[string]string) |
| 88 | + } |
| 89 | + pod.Annotations[fmt.Sprintf("%v%v", WorkloadsAnnotationPrefix, workloadName)] = value |
| 90 | + if err := updateContainers(workloadName, pod); err != nil { |
| 91 | + return nil, "", err |
| 92 | + } |
| 93 | + return pod, workloadName, nil |
| 94 | +} |
| 95 | + |
| 96 | +func GenerateResourceName(workloadName string) v1.ResourceName { |
| 97 | + return v1.ResourceName(fmt.Sprintf("%v.%v", workloadName, WorkloadsCapacitySuffix)) |
| 98 | +} |
| 99 | + |
| 100 | +func updateContainers(workloadName string, pod *v1.Pod) error { |
| 101 | + updateContainer := func(container *v1.Container) error { |
| 102 | + if container.Resources.Requests == nil { |
| 103 | + return fmt.Errorf("managed container %v does not have Resource.Requests", container.Name) |
| 104 | + } |
| 105 | + if _, ok := container.Resources.Requests[v1.ResourceCPU]; !ok { |
| 106 | + return fmt.Errorf("managed container %v does not have cpu requests", container.Name) |
| 107 | + } |
| 108 | + if _, ok := container.Resources.Requests[v1.ResourceMemory]; !ok { |
| 109 | + return fmt.Errorf("managed container %v does not have memory requests", container.Name) |
| 110 | + } |
| 111 | + if container.Resources.Limits == nil { |
| 112 | + container.Resources.Limits = v1.ResourceList{} |
| 113 | + } |
| 114 | + cpuRequest := container.Resources.Requests[v1.ResourceCPU] |
| 115 | + cpuRequestInMilli := cpuRequest.MilliValue() |
| 116 | + |
| 117 | + containerAnnotation := NewWorkloadContainerAnnotation(MilliCPUToShares(cpuRequestInMilli)) |
| 118 | + jsonAnnotation, _ := containerAnnotation.Serialize() |
| 119 | + containerNameKey := fmt.Sprintf(ContainerAnnotationFormat, container.Name) |
| 120 | + |
| 121 | + newCPURequest := resource.NewMilliQuantity(cpuRequestInMilli*1000, cpuRequest.Format) |
| 122 | + |
| 123 | + pod.Annotations[containerNameKey] = string(jsonAnnotation) |
| 124 | + container.Resources.Requests[GenerateResourceName(workloadName)] = *newCPURequest |
| 125 | + container.Resources.Limits[GenerateResourceName(workloadName)] = *newCPURequest |
| 126 | + |
| 127 | + delete(container.Resources.Requests, v1.ResourceCPU) |
| 128 | + return nil |
| 129 | + } |
| 130 | + for idx := range pod.Spec.Containers { |
| 131 | + if err := updateContainer(&pod.Spec.Containers[idx]); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + } |
| 135 | + for idx := range pod.Spec.InitContainers { |
| 136 | + if err := updateContainer(&pod.Spec.InitContainers[idx]); err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + } |
| 140 | + return nil |
| 141 | +} |
0 commit comments