github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/store.go (about) 1 /* 2 Copyright 2016-2017 Mirantis 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 metadata 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io" 23 24 "github.com/jonboulle/clockwork" 25 26 "github.com/Mirantis/virtlet/pkg/metadata/types" 27 "github.com/Mirantis/virtlet/pkg/network" 28 ) 29 30 // PodSandboxMetadata contains methods of a single Pod sandbox 31 type PodSandboxMetadata interface { 32 // GetID returns ID of the pod sandbox managed by this object 33 GetID() string 34 35 // Retrieve loads from DB and returns pod sandbox data bound to the object 36 Retrieve() (*types.PodSandboxInfo, error) 37 38 // Save allows to create/modify/delete pod sandbox instance bound to the object. 39 // Supplied handler gets current PodSandboxInfo value (nil if doesn't exist) and returns new structure 40 // value to be saved or nil to delete. If error value is returned from the handler, the transaction is 41 // rolled back and returned error becomes the result of the function 42 Save(func(*types.PodSandboxInfo) (*types.PodSandboxInfo, error)) error 43 } 44 45 // SandboxStore contains methods to operate on Pod sandboxes 46 type SandboxStore interface { 47 // PodSandbox returns interface instance which manages pod sandbox with given ID 48 PodSandbox(podID string) PodSandboxMetadata 49 50 // ListPodSandboxes returns list of pod sandboxes that match given filter 51 ListPodSandboxes(filter *types.PodSandboxFilter) ([]PodSandboxMetadata, error) 52 } 53 54 // ContainerMetadata contains methods of a single container (VM) 55 type ContainerMetadata interface { 56 // GetID returns ID of the container managed by this object 57 GetID() string 58 59 // Retrieve loads from DB and returns container data bound to the object 60 Retrieve() (*types.ContainerInfo, error) 61 62 // Save allows to create/modify/delete container data bound to the object. 63 // Supplied handler gets current ContainerInfo value (nil if doesn't exist) and returns new structure 64 // value to be saved or nil to delete. If error value is returned from the handler, the transaction is 65 // rolled back and returned error becomes the result of the function 66 Save(func(*types.ContainerInfo) (*types.ContainerInfo, error)) error 67 } 68 69 // ContainerStore contains methods to operate on containers (VMs) 70 type ContainerStore interface { 71 // Container returns interface instance which manages container with given ID 72 Container(containerID string) ContainerMetadata 73 74 // ListPodContainers returns a list of containers that belong to the pod with given ID value 75 ListPodContainers(podID string) ([]ContainerMetadata, error) 76 77 // ImagesInUse returns a set of images in use by containers in the store. 78 // The keys of the returned map are image names and the values are always true. 79 ImagesInUse() (map[string]bool, error) 80 } 81 82 // Store provides single interface for metadata storage implementation 83 type Store interface { 84 SandboxStore 85 ContainerStore 86 io.Closer 87 } 88 89 // NewPodSandboxInfo is a factory function for PodSandboxInfo instances 90 func NewPodSandboxInfo(config *types.PodSandboxConfig, csnData interface{}, state types.PodSandboxState, clock clockwork.Clock) (*types.PodSandboxInfo, error) { 91 var csn *network.ContainerSideNetwork 92 switch csnData.(type) { 93 case string: 94 data := csnData.(string) 95 if len(data) > 0 { 96 if err := json.Unmarshal([]byte(data), &csn); err != nil { 97 return nil, err 98 } 99 } 100 case []byte: 101 data := csnData.([]byte) 102 if len(data) > 0 { 103 if err := json.Unmarshal((data), &csn); err != nil { 104 return nil, err 105 } 106 } 107 case *network.ContainerSideNetwork: 108 csn = csnData.(*network.ContainerSideNetwork) 109 case nil: 110 break 111 default: 112 return nil, fmt.Errorf("CSN data in unknown format: %v", csnData) 113 } 114 115 return &types.PodSandboxInfo{ 116 Config: config, 117 CreatedAt: clock.Now().UnixNano(), 118 State: state, 119 ContainerSideNetwork: csn, 120 }, nil 121 }