forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
57 lines (50 loc) · 2.03 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package kontainerdriver
import (
"fmt"
errorsutil "github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
)
type store struct {
types.Store
KontainerDriverLister v3.KontainerDriverLister
ClusterIndexer cache.Indexer
}
func NewStore(management *config.ScaledContext, s types.Store) types.Store {
clusterInformer := management.Management.Clusters("").Controller().Informer()
// use an indexer instead of expensive k8s api calls
clusterInformer.AddIndexers(map[string]cache.IndexFunc{
clusterByKontainerDriverKey: clusterByKontainerDriver,
})
kd := management.Management.KontainerDrivers("").Controller().Lister()
storeObj := store{
Store: s,
KontainerDriverLister: kd,
ClusterIndexer: clusterInformer.GetIndexer(),
}
return &storeObj
}
// Delete removes the KontainerDriver if it is not in use by a cluster
func (s *store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
//need to get the full driver since just the id is not enough see if a cluster uses it
driver, err := s.KontainerDriverLister.Get("", id)
if err != nil {
if !errors.IsNotFound(err) {
return nil, errorsutil.WithMessage(err, fmt.Sprintf("error getting kontainer driver %s", id))
}
//if driver is not found, don't return error
return nil, nil
}
clustersWithKontainerDriver, err := s.ClusterIndexer.ByIndex(clusterByKontainerDriverKey, driver.Status.DisplayName)
if err != nil {
return nil, errorsutil.WithMessage(err, fmt.Sprintf("error determing if kontainer driver [%s] was in use", driver.Status.DisplayName))
}
if len(clustersWithKontainerDriver) != 0 {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, "cluster Driver is in use by one or more clusters, delete clusters before deleting this driver")
}
return s.Store.Delete(apiContext, schema, id)
}