github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/memory_registry.go (about) 1 /* 2 Copyright 2014 Google Inc. All rights reserved. 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 package registry 17 18 import ( 19 . "github.com/GoogleCloudPlatform/kubernetes/pkg/api" 20 ) 21 22 // An implementation of TaskRegistry and ControllerRegistry that is backed by memory 23 // Mainly used for testing. 24 type MemoryRegistry struct { 25 taskData map[string]Task 26 controllerData map[string]ReplicationController 27 serviceData map[string]Service 28 } 29 30 func MakeMemoryRegistry() *MemoryRegistry { 31 return &MemoryRegistry{ 32 taskData: map[string]Task{}, 33 controllerData: map[string]ReplicationController{}, 34 serviceData: map[string]Service{}, 35 } 36 } 37 38 func (registry *MemoryRegistry) ListTasks(labelQuery *map[string]string) ([]Task, error) { 39 result := []Task{} 40 for _, value := range registry.taskData { 41 if LabelsMatch(value, labelQuery) { 42 result = append(result, value) 43 } 44 } 45 return result, nil 46 } 47 48 func (registry *MemoryRegistry) GetTask(taskID string) (*Task, error) { 49 task, found := registry.taskData[taskID] 50 if found { 51 return &task, nil 52 } else { 53 return nil, nil 54 } 55 } 56 57 func (registry *MemoryRegistry) CreateTask(machine string, task Task) error { 58 registry.taskData[task.ID] = task 59 return nil 60 } 61 62 func (registry *MemoryRegistry) DeleteTask(taskID string) error { 63 delete(registry.taskData, taskID) 64 return nil 65 } 66 67 func (registry *MemoryRegistry) UpdateTask(task Task) error { 68 registry.taskData[task.ID] = task 69 return nil 70 } 71 72 func (registry *MemoryRegistry) ListControllers() ([]ReplicationController, error) { 73 result := []ReplicationController{} 74 for _, value := range registry.controllerData { 75 result = append(result, value) 76 } 77 return result, nil 78 } 79 80 func (registry *MemoryRegistry) GetController(controllerID string) (*ReplicationController, error) { 81 controller, found := registry.controllerData[controllerID] 82 if found { 83 return &controller, nil 84 } else { 85 return nil, nil 86 } 87 } 88 89 func (registry *MemoryRegistry) CreateController(controller ReplicationController) error { 90 registry.controllerData[controller.ID] = controller 91 return nil 92 } 93 94 func (registry *MemoryRegistry) DeleteController(controllerId string) error { 95 delete(registry.controllerData, controllerId) 96 return nil 97 } 98 99 func (registry *MemoryRegistry) UpdateController(controller ReplicationController) error { 100 registry.controllerData[controller.ID] = controller 101 return nil 102 } 103 104 func (registry *MemoryRegistry) ListServices() (ServiceList, error) { 105 var list []Service 106 for _, value := range registry.serviceData { 107 list = append(list, value) 108 } 109 return ServiceList{Items: list}, nil 110 } 111 112 func (registry *MemoryRegistry) CreateService(svc Service) error { 113 registry.serviceData[svc.ID] = svc 114 return nil 115 } 116 117 func (registry *MemoryRegistry) GetService(name string) (*Service, error) { 118 svc, found := registry.serviceData[name] 119 if found { 120 return &svc, nil 121 } else { 122 return nil, nil 123 } 124 } 125 126 func (registry *MemoryRegistry) DeleteService(name string) error { 127 delete(registry.serviceData, name) 128 return nil 129 } 130 131 func (registry *MemoryRegistry) UpdateService(svc Service) error { 132 return registry.CreateService(svc) 133 } 134 135 func (registry *MemoryRegistry) UpdateEndpoints(e Endpoints) error { 136 return nil 137 }