-
Notifications
You must be signed in to change notification settings - Fork 216
/
agent.go
244 lines (205 loc) · 6.31 KB
/
agent.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Package agent builds manifests for creating a managed fleet-agent. (fleetcontroller)
package agent
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/pkg/errors"
"github.com/rancher/fleet/modules/cli/pkg/client"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/config"
"github.com/rancher/fleet/pkg/durations"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
fleetns "github.com/rancher/fleet/pkg/namespace"
"github.com/rancher/wrangler/pkg/yaml"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
var (
ErrNoHostInConfig = errors.New("failed to find cluster server parameter")
)
type Options struct {
ManifestOptions
ConfigOptions
CA []byte
Host string
NoCA bool // unused
}
// AgentWithConfig writes the agent manifest to the given writer. It
// includes an updated agent token secret from the cluster. It finds or creates
// the agent config inside a configmap.
//
// This is used when importing a cluster.
func AgentWithConfig(ctx context.Context, agentNamespace, controllerNamespace, agentScope string, cg *client.Getter, output io.Writer, tokenName string, opts *Options) error {
if opts == nil {
opts = &Options{}
}
client, err := cg.Get()
if err != nil {
return err
}
objs, err := agentToken(ctx, agentNamespace, controllerNamespace, client, tokenName, opts)
if err != nil {
return err
}
agentConfig, err := agentConfig(ctx, agentNamespace, controllerNamespace, cg, &opts.ConfigOptions)
if err != nil {
return err
}
objs = append(objs, agentConfig...)
// get a fresh config from the API
cfg, err := config.Lookup(ctx, controllerNamespace, config.ManagerConfigName, client.Core.ConfigMap())
if err != nil {
return err
}
mo := opts.ManifestOptions
mo.AgentImage = cfg.AgentImage
mo.SystemDefaultRegistry = cfg.SystemDefaultRegistry
mo.AgentImagePullPolicy = cfg.AgentImagePullPolicy
mo.CheckinInterval = cfg.AgentCheckinInterval.Duration.String()
objs = append(objs, Manifest(agentNamespace, agentScope, mo)...)
data, err := yaml.Export(objs...)
if err != nil {
return err
}
_, err = output.Write(data)
return err
}
func agentToken(ctx context.Context, agentNamespace, controllerNamespace string, client *client.Client, tokenName string, opts *Options) ([]runtime.Object, error) {
token, err := getToken(ctx, controllerNamespace, tokenName, client)
if err != nil {
return nil, err
}
if opts.Host != "" {
token["apiServerURL"] = []byte(opts.Host)
}
if len(opts.CA) > 0 {
token["apiServerCA"] = opts.CA
}
return []runtime.Object{
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: agentNamespace,
},
},
// fleet-agent-bootstrap
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: config.AgentBootstrapConfigName,
Namespace: agentNamespace,
},
Data: token,
},
}, nil
}
// getToken load the import-token-local secret and
// check system registration namespace is expected
func getToken(ctx context.Context, controllerNamespace, tokenName string, client *client.Client) (map[string][]byte, error) {
secretName, err := waitForSecretName(ctx, tokenName, client)
if err != nil {
return nil, err
}
secret, err := client.Core.Secret().Get(client.Namespace, secretName, metav1.GetOptions{})
if err != nil {
return nil, err
}
values := secret.Data["values"]
if len(values) == 0 {
return nil, fmt.Errorf("failed to find \"values\" on secret %s/%s", client.Namespace, secretName)
}
data := map[string]interface{}{}
if err := yaml.Unmarshal(values, &data); err != nil {
return nil, err
}
if _, ok := data["token"]; !ok {
return nil, fmt.Errorf("failed to find token in values")
}
expectedNamespace := fleetns.SystemRegistrationNamespace(controllerNamespace)
actualNamespace := data["systemRegistrationNamespace"]
if actualNamespace != expectedNamespace {
return nil, fmt.Errorf("registration namespace (%s) from secret (%s/%s) does not match expected: %s", actualNamespace, secret.Namespace, secret.Name, expectedNamespace)
}
byteData := map[string][]byte{}
for k, v := range data {
if s, ok := v.(string); ok {
byteData[k] = []byte(s)
}
}
return byteData, nil
}
func waitForSecretName(ctx context.Context, tokenName string, client *client.Client) (string, error) {
watcher, err := startWatch(client.Namespace, client.Fleet.ClusterRegistrationToken())
if err != nil {
return "", err
}
defer func() {
watcher.Stop()
for range watcher.ResultChan() {
// drain the channel
}
}()
crt, err := client.Fleet.ClusterRegistrationToken().Get(client.Namespace, tokenName, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to lookup token %s: %w", tokenName, err)
}
if crt.Status.SecretName != "" {
return crt.Status.SecretName, nil
}
timeout := time.After(durations.AgentSecretTimeout)
for {
var event watch.Event
select {
case <-timeout:
return "", fmt.Errorf("timeout getting credential for cluster group")
case <-ctx.Done():
return "", ctx.Err()
case event = <-watcher.ResultChan():
}
if newCGT, ok := event.Object.(*fleet.ClusterRegistrationToken); ok {
if newCGT.UID != crt.UID || newCGT.Status.SecretName == "" {
continue
}
return newCGT.Status.SecretName, nil
}
}
}
func startWatch(namespace string, sa fleetcontrollers.ClusterRegistrationTokenClient) (watch.Interface, error) {
secrets, err := sa.List(namespace, metav1.ListOptions{
Limit: 1,
})
if err != nil {
return nil, err
}
return sa.Watch(namespace, metav1.ListOptions{ResourceVersion: secrets.ResourceVersion})
}
func GetCAFromConfig(rawConfig clientcmdapi.Config) ([]byte, error) {
cluster, ok := rawConfig.Clusters[rawConfig.CurrentContext]
if !ok {
for _, v := range rawConfig.Clusters {
cluster = v
break
}
}
if cluster != nil {
if len(cluster.CertificateAuthorityData) > 0 {
return cluster.CertificateAuthorityData, nil
}
return os.ReadFile(cluster.CertificateAuthority)
}
return nil, nil
}
func GetHostFromConfig(rawConfig clientcmdapi.Config) (string, error) {
cluster, ok := rawConfig.Clusters[rawConfig.CurrentContext]
if ok {
return cluster.Server, nil
}
for _, v := range rawConfig.Clusters {
return v.Server, nil
}
return "", ErrNoHostInConfig
}