-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathallclusters.go
93 lines (83 loc) · 2.78 KB
/
allclusters.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package scanuserpermissions
import (
"context"
"fmt"
"github.com/AliyunContainerService/ack-ram-tool/pkg/ctl/rbac/binding"
"github.com/AliyunContainerService/ack-ram-tool/pkg/log"
"github.com/AliyunContainerService/ack-ram-tool/pkg/openapi"
"github.com/AliyunContainerService/ack-ram-tool/pkg/types"
"github.com/briandowns/spinner"
"k8s.io/client-go/kubernetes"
"time"
)
func scanAllClusters(ctx context.Context, openAPIClient openapi.ClientInterface) error {
log.Logger.Info("start to scan users and bindings for all clusters")
clusters, accounts, err := GetAllClustersAndAccountsWithSpin(ctx, openAPIClient)
if err != nil {
return err
}
for _, cluster := range clusters {
clusterId := cluster.ClusterId
log.Logger.Infof("---- %s (%s) ----", clusterId, cluster.Name)
logger := log.Named(clusterId)
if cluster.State.NoActiveApiServer() {
logger.Errorf("invalid cluster state (%s), skip it", cluster.State)
continue
}
clusterCtx := log.IntoContext(ctx, logger)
if err := scanOneClusterWithAccounts(clusterCtx, openAPIClient, clusterId, accounts); err != nil {
logger.Errorf("scan bindings for cluster %s failed: %s", clusterId, err)
}
}
return nil
}
func scanOneClusterWithAccounts(ctx context.Context, openAPIClient openapi.ClientInterface,
clusterId string, accounts map[int64]types.Account) error {
logger := log.FromContext(ctx)
logger.Infof("start to scan bindings for cluster %s", clusterId)
spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
spin.Start()
var bindings []binding.Binding
var err error
func() {
defer spin.Stop()
var kubeClient kubernetes.Interface
kubeClient, err = getKubeClient(ctx, openAPIClient, clusterId)
if err != nil {
return
}
bindings, err = GetClusterBindings(ctx, kubeClient)
if err != nil {
return
}
}()
if err != nil {
return err
}
if opts.userId == 0 && !opts.allUsers {
logger.Warn("by default, only deleted users are included. Use the --all-users flag to include all users")
}
fmt.Printf("ClusterId: %s\n", clusterId)
outputTable(bindings, accounts)
return nil
}
func GetAllClustersAndAccountsWithSpin(ctx context.Context,
openAPIClient openapi.ClientInterface) ([]types.Cluster, map[int64]types.Account, error) {
log.Logger.Info("start to get all clusters, users and roles")
spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
spin.Start()
defer spin.Stop()
return getAllClustersAndAccounts(ctx, openAPIClient)
}
func getAllClustersAndAccounts(ctx context.Context,
openAPIClient openapi.ClientInterface) ([]types.Cluster, map[int64]types.Account, error) {
clusters, err := openAPIClient.ListClustersV1(ctx)
if err != nil {
return nil, nil, err
}
accounts, err := binding.ListAccounts(ctx, openAPIClient)
if err != nil {
return nil, nil, err
}
return clusters, accounts, nil
}