-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
142 lines (119 loc) · 3.16 KB
/
manager.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package agent
import (
"context"
"encoding/base64"
"net/url"
"sync"
clusterController "github.com/rancher/rancher/pkg/cluster/controller"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
)
type Manager struct {
ManagementConfig rest.Config
LocalConfig *rest.Config
controllers sync.Map
}
type record struct {
clusterRec *v3.Cluster
cluster *config.ClusterContext
ctx context.Context
cancel context.CancelFunc
}
func NewManager(management *config.ManagementContext) *Manager {
return &Manager{
ManagementConfig: management.RESTConfig,
LocalConfig: management.LocalConfig,
}
}
func (m *Manager) Stop(cluster *v3.Cluster) {
obj, ok := m.controllers.Load(cluster.UID)
if !ok {
return
}
logrus.Info("Stopping cluster agent for", obj.(*record).cluster.ClusterName)
obj.(*record).cancel()
m.controllers.Delete(cluster.UID)
}
func (m *Manager) Start(ctx context.Context, cluster *v3.Cluster) error {
obj, ok := m.controllers.Load(cluster.UID)
if ok {
if !m.changed(obj.(*record), cluster) {
return nil
}
m.Stop(obj.(*record).clusterRec)
}
controller, err := m.toRecord(ctx, cluster)
if controller == nil || err != nil {
return err
}
obj, loaded := m.controllers.LoadOrStore(cluster.UID, controller)
if !loaded {
go func() {
if err := m.doStart(obj.(*record)); err != nil {
m.Stop(cluster)
}
}()
}
return nil
}
func (m *Manager) changed(r *record, cluster *v3.Cluster) bool {
existing := r.clusterRec
if existing.Status.APIEndpoint != cluster.Status.APIEndpoint ||
existing.Status.ServiceAccountToken != cluster.Status.ServiceAccountToken ||
existing.Status.CACert != cluster.Status.CACert {
return true
}
return false
}
func (m *Manager) doStart(rec *record) error {
logrus.Info("Starting cluster agent for", rec.cluster.ClusterName)
if err := clusterController.Register(rec.ctx, rec.cluster); err != nil {
return err
}
return rec.cluster.Start(rec.ctx)
}
func (m *Manager) toRESTConfig(cluster *v3.Cluster) (*rest.Config, error) {
if cluster == nil {
return nil, nil
}
if cluster.Spec.Internal {
return m.LocalConfig, nil
}
if cluster.Status.APIEndpoint == "" || cluster.Status.CACert == "" || cluster.Status.ServiceAccountToken == "" {
return nil, nil
}
u, err := url.Parse(cluster.Status.APIEndpoint)
if err != nil {
return nil, err
}
caBytes, err := base64.StdEncoding.DecodeString(cluster.Status.CACert)
if err != nil {
return nil, err
}
return &rest.Config{
Host: u.Host,
Prefix: u.Path,
BearerToken: cluster.Status.ServiceAccountToken,
TLSClientConfig: rest.TLSClientConfig{
CAData: caBytes,
},
}, nil
}
func (m *Manager) toRecord(ctx context.Context, cluster *v3.Cluster) (*record, error) {
kubeConfig, err := m.toRESTConfig(cluster)
if kubeConfig == nil || err != nil {
return nil, err
}
clusterContext, err := config.NewClusterContext(m.ManagementConfig, *kubeConfig, cluster.Name)
if err != nil {
return nil, err
}
s := &record{
cluster: clusterContext,
clusterRec: cluster,
}
s.ctx, s.cancel = context.WithCancel(ctx)
return s, nil
}