github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/boskos/crds/crd_storage.go (about) 1 /* 2 Copyright 2017 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 crds 18 19 import ( 20 "k8s.io/test-infra/boskos/common" 21 "k8s.io/test-infra/boskos/storage" 22 23 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 ) 25 26 const ( 27 deleteGracePeriodSeconds = 10 28 ) 29 30 type inClusterStorage struct { 31 client ClientInterface 32 } 33 34 // NewCRDStorage creates a Custom Resource Definition persistence layer 35 func NewCRDStorage(client ClientInterface) storage.PersistenceLayer { 36 return &inClusterStorage{ 37 client: client, 38 } 39 } 40 41 func (cs *inClusterStorage) Add(i common.Item) error { 42 o := cs.client.NewObject() 43 o.FromItem(i) 44 _, err := cs.client.Create(o) 45 return err 46 } 47 48 func (cs *inClusterStorage) Delete(name string) error { 49 return cs.client.Delete(name, v1.NewDeleteOptions(deleteGracePeriodSeconds)) 50 } 51 52 func (cs *inClusterStorage) Update(i common.Item) error { 53 o, err := cs.client.Get(i.GetName()) 54 if err != nil { 55 return err 56 } 57 o.FromItem(i) 58 _, err = cs.client.Update(o) 59 return err 60 } 61 62 func (cs *inClusterStorage) Get(name string) (common.Item, error) { 63 o, err := cs.client.Get(name) 64 if err != nil { 65 return nil, err 66 } 67 return o.ToItem(), nil 68 69 } 70 71 func (cs *inClusterStorage) List() ([]common.Item, error) { 72 col, err := cs.client.List(v1.ListOptions{}) 73 if err != nil { 74 return nil, err 75 } 76 var items []common.Item 77 for _, i := range col.GetItems() { 78 items = append(items, i.ToItem()) 79 } 80 return items, nil 81 }