|
| 1 | +/* |
| 2 | +Copyright 2024 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 state |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + "k8s.io/apimachinery/pkg/api/resource" |
| 28 | + "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" |
| 29 | +) |
| 30 | + |
| 31 | +const testCheckpoint = "pod_status_manager_state" |
| 32 | + |
| 33 | +func newTestStateCheckpoint(t *testing.T) *stateCheckpoint { |
| 34 | + testingDir := getTestDir(t) |
| 35 | + cache := NewStateMemory() |
| 36 | + checkpointManager, err := checkpointmanager.NewCheckpointManager(testingDir) |
| 37 | + require.NoError(t, err, "failed to create checkpoint manager") |
| 38 | + checkpointName := "pod_state_checkpoint" |
| 39 | + sc := &stateCheckpoint{ |
| 40 | + cache: cache, |
| 41 | + checkpointManager: checkpointManager, |
| 42 | + checkpointName: checkpointName, |
| 43 | + } |
| 44 | + return sc |
| 45 | +} |
| 46 | + |
| 47 | +func getTestDir(t *testing.T) string { |
| 48 | + testingDir, err := os.MkdirTemp("", "pod_resource_allocation_state_test") |
| 49 | + require.NoError(t, err, "failed to create temp dir") |
| 50 | + t.Cleanup(func() { |
| 51 | + if err := os.RemoveAll(testingDir); err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + }) |
| 55 | + return testingDir |
| 56 | +} |
| 57 | + |
| 58 | +func verifyPodResourceAllocation(t *testing.T, expected, actual *PodResourceAllocation, msgAndArgs string) { |
| 59 | + for podUID, containerResourceList := range *expected { |
| 60 | + require.Equal(t, len(containerResourceList), len((*actual)[podUID]), msgAndArgs) |
| 61 | + for containerName, resourceList := range containerResourceList { |
| 62 | + for name, quantity := range resourceList.Requests { |
| 63 | + require.True(t, quantity.Equal((*actual)[podUID][containerName].Requests[name]), msgAndArgs) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func Test_stateCheckpoint_storeState(t *testing.T) { |
| 70 | + type args struct { |
| 71 | + podResourceAllocation PodResourceAllocation |
| 72 | + } |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + args args |
| 77 | + }{} |
| 78 | + suffix := []string{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "n", "u", "m", "k", "M", "G", "T", "P", "E", ""} |
| 79 | + factor := []string{"1", "0.1", "0.03", "10", "100", "512", "1000", "1024", "700", "10000"} |
| 80 | + for _, fact := range factor { |
| 81 | + for _, suf := range suffix { |
| 82 | + if (suf == "E" || suf == "Ei") && (fact == "1000" || fact == "10000") { |
| 83 | + // when fact is 1000 or 10000, suffix "E" or "Ei", the quantity value is overflow |
| 84 | + // see detail https://github.com/kubernetes/apimachinery/blob/95b78024e3feada7739b40426690b4f287933fd8/pkg/api/resource/quantity.go#L301 |
| 85 | + continue |
| 86 | + } |
| 87 | + tests = append(tests, struct { |
| 88 | + name string |
| 89 | + args args |
| 90 | + }{ |
| 91 | + name: fmt.Sprintf("resource - %s%s", fact, suf), |
| 92 | + args: args{ |
| 93 | + podResourceAllocation: PodResourceAllocation{ |
| 94 | + "pod1": { |
| 95 | + "container1": { |
| 96 | + Requests: v1.ResourceList{ |
| 97 | + v1.ResourceCPU: resource.MustParse(fmt.Sprintf("%s%s", fact, suf)), |
| 98 | + v1.ResourceMemory: resource.MustParse(fmt.Sprintf("%s%s", fact, suf)), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + testDir := getTestDir(t) |
| 110 | + originalSC, err := NewStateCheckpoint(testDir, testCheckpoint) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + err = originalSC.SetPodResourceAllocation(tt.args.podResourceAllocation) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + actual := originalSC.GetPodResourceAllocation() |
| 117 | + verifyPodResourceAllocation(t, &tt.args.podResourceAllocation, &actual, "stored pod resource allocation is not equal to original pod resource allocation") |
| 118 | + |
| 119 | + newSC, err := NewStateCheckpoint(testDir, testCheckpoint) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + actual = newSC.GetPodResourceAllocation() |
| 123 | + verifyPodResourceAllocation(t, &tt.args.podResourceAllocation, &actual, "restored pod resource allocation is not equal to original pod resource allocation") |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func Test_stateCheckpoint_formatUpgraded(t *testing.T) { |
| 129 | + // Based on the PodResourceAllocationInfo struct, it's mostly possible that new field will be added |
| 130 | + // in struct PodResourceAllocationInfo, rather than in struct PodResourceAllocationInfo.AllocationEntries. |
| 131 | + // Emulate upgrade scenario by pretending that `ResizeStatusEntries` is a new field. |
| 132 | + // The checkpoint content doesn't have it and that shouldn't prevent the checkpoint from being loaded. |
| 133 | + sc := newTestStateCheckpoint(t) |
| 134 | + |
| 135 | + // prepare old checkpoint, ResizeStatusEntries is unset, |
| 136 | + // pretend that the old checkpoint is unaware for the field ResizeStatusEntries |
| 137 | + const checkpointContent = `{"data":"{\"allocationEntries\":{\"pod1\":{\"container1\":{\"requests\":{\"cpu\":\"1Ki\",\"memory\":\"1Ki\"}}}}}","checksum":1555601526}` |
| 138 | + expectedPodResourceAllocationInfo := &PodResourceAllocationInfo{ |
| 139 | + AllocationEntries: map[string]map[string]v1.ResourceRequirements{ |
| 140 | + "pod1": { |
| 141 | + "container1": { |
| 142 | + Requests: v1.ResourceList{ |
| 143 | + v1.ResourceCPU: resource.MustParse("1Ki"), |
| 144 | + v1.ResourceMemory: resource.MustParse("1Ki"), |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + ResizeStatusEntries: map[string]v1.PodResizeStatus{}, |
| 150 | + } |
| 151 | + checkpoint := &Checkpoint{} |
| 152 | + err := checkpoint.UnmarshalCheckpoint([]byte(checkpointContent)) |
| 153 | + require.NoError(t, err, "failed to unmarshal checkpoint") |
| 154 | + |
| 155 | + err = sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint) |
| 156 | + require.NoError(t, err, "failed to create old checkpoint") |
| 157 | + |
| 158 | + err = sc.restoreState() |
| 159 | + require.NoError(t, err, "failed to restore state") |
| 160 | + |
| 161 | + actualPodResourceAllocationInfo := &PodResourceAllocationInfo{} |
| 162 | + actualPodResourceAllocationInfo.AllocationEntries = sc.cache.GetPodResourceAllocation() |
| 163 | + actualPodResourceAllocationInfo.ResizeStatusEntries = sc.cache.GetResizeStatus() |
| 164 | + require.NoError(t, err, "failed to get pod resource allocation info") |
| 165 | + require.Equal(t, expectedPodResourceAllocationInfo, actualPodResourceAllocationInfo, "pod resource allocation info is not equal") |
| 166 | +} |
0 commit comments