github.com/google/cadvisor@v0.49.1/storage/storage.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package storage 16 17 import ( 18 "fmt" 19 "sort" 20 21 info "github.com/google/cadvisor/info/v1" 22 ) 23 24 type StorageDriver interface { 25 AddStats(cInfo *info.ContainerInfo, stats *info.ContainerStats) error 26 27 // Close will clear the state of the storage driver. The elements 28 // stored in the underlying storage may or may not be deleted depending 29 // on the implementation of the storage driver. 30 Close() error 31 } 32 33 type StorageDriverFunc func() (StorageDriver, error) 34 35 var registeredPlugins = map[string](StorageDriverFunc){} 36 37 func RegisterStorageDriver(name string, f StorageDriverFunc) { 38 registeredPlugins[name] = f 39 } 40 41 func New(name string) (StorageDriver, error) { 42 if name == "" { 43 return nil, nil 44 } 45 f, ok := registeredPlugins[name] 46 if !ok { 47 return nil, fmt.Errorf("unknown backend storage driver: %s", name) 48 } 49 return f() 50 } 51 52 func ListDrivers() []string { 53 drivers := make([]string, 0, len(registeredPlugins)) 54 for name := range registeredPlugins { 55 drivers = append(drivers, name) 56 } 57 sort.Strings(drivers) 58 return drivers 59 }