-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
83 lines (72 loc) · 2.26 KB
/
client.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
package client
import (
"context"
"fmt"
kdeploypb "github.com/autom8ter/kdeploy/gen/grpc/go"
"github.com/autom8ter/kdeploy/internal/logger"
"github.com/autom8ter/kubego"
"github.com/graphikDB/generic"
"github.com/graphikDB/trigger"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)
type authCtx string
const (
userInfo authCtx = "userinfo"
)
type Manager struct {
kclient *kubego.Kube
iclient *kubego.Istio
jwtCache generic.Cache
logger *logger.Logger
rootUsers []string
requestAuthorizers []*trigger.Decision
userInfoEndpoint string
}
func New(kclient *kubego.Kube, iclient *kubego.Istio, logger *logger.Logger, rootUsers []string, userInfoEndpoint string, authorizers []*trigger.Decision) *Manager {
return &Manager{
kclient: kclient,
iclient: iclient,
jwtCache: generic.NewCache(1 * time.Minute),
logger: logger,
rootUsers: rootUsers,
userInfoEndpoint: userInfoEndpoint,
requestAuthorizers: authorizers,
}
}
func (m *Manager) L() *logger.Logger {
return m.logger
}
func (m *Manager) getStatus(ctx context.Context, namespace, name string) (*kdeploypb.AppStatus, error) {
var replicas []*kdeploypb.Replica
pods, err := m.kclient.Pods(namespace).List(ctx, v1.ListOptions{
TypeMeta: v1.TypeMeta{},
LabelSelector: fmt.Sprintf("kdeploy.app = %s", name),
})
if err != nil {
return nil, err
}
for _, pod := range pods.Items {
replicas = append(replicas, &kdeploypb.Replica{
Phase: string(pod.Status.Phase),
Condition: string(pod.Status.Conditions[0].Status),
Reason: pod.Status.Reason,
})
}
return &kdeploypb.AppStatus{Replicas: replicas}, nil
}
func (r *Manager) GetUserInfo(ctx context.Context) map[string]interface{} {
if ctx.Value(userInfo) == nil {
return map[string]interface{}{}
}
return ctx.Value(userInfo).(map[string]interface{})
}
func (r *Manager) SetUserInfo(ctx context.Context, userInfoData map[string]interface{}) context.Context {
return context.WithValue(ctx, userInfo, userInfoData)
}
func (r *Manager) GetJWTHash(hash string) (interface{}, bool) {
return r.jwtCache.Get(hash)
}
func (r *Manager) SetJWTHash(hash string, userInfo map[string]interface{}) {
r.jwtCache.Set(hash, userInfo, 1*time.Hour)
}