forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
64 lines (57 loc) · 1.97 KB
/
formatter.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
58
59
60
61
62
63
64
package kontainerdriver
import (
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
client "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
)
type Format struct {
ClusterIndexer cache.Indexer
}
func NewFormatter(manangement *config.ScaledContext) types.Formatter {
clusterInformer := manangement.Management.Clusters("").Controller().Informer()
// use an indexer instead of expensive k8s api calls
clusterInformer.AddIndexers(map[string]cache.IndexFunc{
clusterByKontainerDriverKey: clusterByKontainerDriver,
})
format := Format{
ClusterIndexer: clusterInformer.GetIndexer(),
}
return format.Formatter
}
const clusterByKontainerDriverKey = "clusterbyKontainerDriver"
func clusterByKontainerDriver(obj interface{}) ([]string, error) {
cluster, ok := obj.(*v3.Cluster)
if !ok {
return []string{}, nil
}
return []string{cluster.Status.Driver}, nil
}
func (f *Format) Formatter(request *types.APIContext, resource *types.RawResource) {
state, ok := resource.Values["state"].(string)
if ok {
if state == "active" {
resource.AddAction(request, "deactivate")
}
if state == "inactive" {
resource.AddAction(request, "activate")
}
}
// if cluster driver is a built-in, delete removal link from UI
if builtIn, _ := resource.Values[client.KontainerDriverFieldBuiltIn].(bool); builtIn {
delete(resource.Links, "remove")
}
resName := resource.Values[client.KontainerDriverFieldName]
// resName will be nil when first added
if resName != nil {
clustersWithKontainerDriver, err := f.ClusterIndexer.ByIndex(clusterByKontainerDriverKey, resName.(string))
if err != nil {
logrus.Warnf("failed to determine if kontainer driver %v was in use by a cluster : %v", resName.(string), err)
} else if len(clustersWithKontainerDriver) != 0 {
// if cluster driver in use, delete removal link from UI
delete(resource.Links, "remove")
}
}
}