-
Notifications
You must be signed in to change notification settings - Fork 27
/
manager.go
264 lines (235 loc) · 8.86 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package bundle
import (
"context"
"fmt"
"os"
"time"
"github.com/go-logr/logr"
"golang.org/x/mod/semver"
api "github.com/aws/eks-anywhere-packages/api/v1alpha1"
"github.com/aws/eks-anywhere-packages/pkg/authenticator"
"github.com/aws/eks-anywhere-packages/pkg/config"
)
//go:generate mockgen -source manager.go -destination=mocks/manager.go -package=mocks Manager
type Manager interface {
// ProcessBundle returns true if there are changes
ProcessBundle(ctx context.Context, newBundle *api.PackageBundle) (bool, error)
// ProcessBundleController process the bundle controller
ProcessBundleController(ctx context.Context, pbc *api.PackageBundleController) error
}
type bundleManager struct {
log logr.Logger
bundleClient Client
registryClient RegistryClient
targetClient authenticator.TargetClusterClient
config config.Config
}
func NewBundleManager(log logr.Logger, registryClient RegistryClient, bundleClient Client, targetClient authenticator.TargetClusterClient, config config.Config) *bundleManager {
return &bundleManager{
log: log,
bundleClient: bundleClient,
registryClient: registryClient,
targetClient: targetClient,
config: config,
}
}
var _ Manager = (*bundleManager)(nil)
func (m bundleManager) ProcessBundle(_ context.Context, newBundle *api.PackageBundle) (bool, error) {
if newBundle.Namespace != api.PackageNamespace {
if newBundle.Status.State != api.PackageBundleStateIgnored {
newBundle.Spec.DeepCopyInto(&newBundle.Status.Spec)
newBundle.Status.State = api.PackageBundleStateIgnored
m.log.V(6).Info("update", "bundle", newBundle.Name, "state", newBundle.Status.State)
return true, nil
}
return false, nil
}
if !m.isCompatibleWith(newBundle) {
if newBundle.Status.State != api.PackageBundleStateUpgradeRequired {
newBundle.Spec.DeepCopyInto(&newBundle.Status.Spec)
newBundle.Status.State = api.PackageBundleStateUpgradeRequired
m.log.V(6).Info("update", "bundle", newBundle.Name, "state", newBundle.Status.State)
return true, nil
}
return false, nil
}
if !newBundle.IsValidVersion() {
if newBundle.Status.State != api.PackageBundleStateInvalid {
newBundle.Spec.DeepCopyInto(&newBundle.Status.Spec)
newBundle.Status.State = api.PackageBundleStateInvalid
m.log.V(6).Info("update", "bundle", newBundle.Name, "state", newBundle.Status.State)
return true, nil
}
return false, nil
}
if newBundle.Status.State != api.PackageBundleStateAvailable {
newBundle.Spec.DeepCopyInto(&newBundle.Status.Spec)
newBundle.Status.State = api.PackageBundleStateAvailable
m.log.V(6).Info("update", "bundle", newBundle.Name, "state", newBundle.Status.State)
return true, nil
}
return false, nil
}
func (m *bundleManager) isCompatibleWith(bundle *api.PackageBundle) bool {
currentVersion := m.config.BuildInfo.Version
return currentVersion == config.DEVELOPMENT || semver.Compare(currentVersion, bundle.Spec.MinVersion) >= 0
}
func (m *bundleManager) hasBundleNamed(bundles []api.PackageBundle, bundleName string) bool {
for _, b := range bundles {
if b.Name == bundleName {
return true
}
}
return false
}
func (m *bundleManager) ProcessBundleController(ctx context.Context, pbc *api.PackageBundleController) error {
info, err := m.targetClient.GetServerVersion(ctx, pbc.Name)
if err != nil {
m.log.Error(err, "Unable to get server version")
if pbc.Status.State == api.BundleControllerStateActive || pbc.Status.State == "" {
pbc.Status.Detail = err.Error()
pbc.Status.State = api.BundleControllerStateDisconnected
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
}
return nil
}
if err := m.targetClient.Initialize(ctx, os.Getenv("CLUSTER_NAME")); err != nil {
m.log.Error(err, "failed to intialize cluster client of management cluster")
}
config, _ := m.targetClient.ToRESTConfig()
auth, _ := authenticator.NewECRSecret(config)
// Once we've fully removed ecr-token-refresher usage from all OS we can remove the below check.
if err := auth.AddSecretToAllNamespace(ctx); err != nil {
} else {
time.Sleep(3 * time.Second)
}
latestBundle, err := m.registryClient.LatestBundle(ctx, pbc.GetBundleURI(), info.Major, info.Minor, pbc.Name)
if err != nil {
m.log.Error(err, "Unable to get latest bundle")
if pbc.Status.State == api.BundleControllerStateActive || pbc.Status.State == "" {
pbc.Status.State = api.BundleControllerStateDisconnected
pbc.Status.Detail = err.Error()
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
}
return err
}
allBundles, err := m.bundleClient.GetBundleList(ctx)
if err != nil {
return fmt.Errorf("getting bundle list: %s", err)
}
if !m.hasBundleNamed(allBundles, latestBundle.Name) {
err = m.bundleClient.CreateBundle(ctx, latestBundle)
if err != nil {
return err
}
}
latestBundleIsCurrentBundle := latestBundle.Name == pbc.Spec.ActiveBundle
if pbc.Spec.ActiveBundle == "" {
pbc.Status.State = ""
}
switch pbc.Status.State {
case api.BundleControllerStateActive:
err = m.bundleClient.CreateClusterConfigMap(ctx, pbc.Name)
if err != nil {
return fmt.Errorf("creating configmap for %s: %s", pbc.Name, err)
}
err = m.targetClient.CreateClusterNamespace(ctx, pbc.GetName())
if err != nil {
return fmt.Errorf("creating workload cluster namespace eksa-packages for %s: %s", pbc.Name, err)
}
if pbc.GetName() != os.Getenv("CLUSTER_NAME") {
secret, err := m.bundleClient.GetSecret(ctx, "aws-secret")
if err != nil {
return fmt.Errorf("getting aws secret eksa-packages:%s", err)
}
if secret != nil {
err = m.targetClient.ApplySecret(ctx, secret)
if err != nil {
return fmt.Errorf("creating workload cluster secret aws-secret:%s", err)
}
}
}
if len(pbc.Spec.ActiveBundle) > 0 {
if !m.hasBundleNamed(allBundles, pbc.Spec.ActiveBundle) {
activeBundle, err := m.registryClient.DownloadBundle(ctx, pbc.GetActiveBundleURI(), pbc.Name)
if err != nil {
m.log.Error(err, "Active bundle download failed", "bundle", pbc.Spec.ActiveBundle)
return nil
}
m.log.Info("Bundle downloaded", "bundle", pbc.Spec.ActiveBundle)
err = m.bundleClient.CreateBundle(ctx, activeBundle)
if err != nil {
m.log.Error(err, "Recreate active bundle failed", "bundle", pbc.Spec.ActiveBundle)
return nil
}
m.log.Info("Bundle created", "bundle", pbc.Spec.ActiveBundle)
}
}
if latestBundleIsCurrentBundle {
break
}
pbc.Status.State = api.BundleControllerStateUpgradeAvailable
m.log.V(6).Info("update", "PackageBundleController", pbc.Name, "state", pbc.Status.State)
pbc.Status.Detail = latestBundle.Name + " available"
pbc.Spec.DeepCopyInto(&pbc.Status.Spec)
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
case api.BundleControllerStateUpgradeAvailable:
if !latestBundleIsCurrentBundle {
if pbc.Status.Detail != latestBundle.Name+" available" {
pbc.Status.Detail = latestBundle.Name + " available"
pbc.Spec.DeepCopyInto(&pbc.Status.Spec)
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s detail to %s: %s", pbc.Name, pbc.Status.Detail, err)
}
}
break
}
pbc.Status.State = api.BundleControllerStateActive
m.log.V(6).Info("update", "PackageBundleController", pbc.Name, "state", pbc.Status.State)
pbc.Status.Detail = ""
pbc.Spec.DeepCopyInto(&pbc.Status.Spec)
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
case api.BundleControllerStateDisconnected:
pbc.Status.State = api.BundleControllerStateActive
m.log.V(6).Info("update", "PackageBundleController", pbc.Name, "state", pbc.Status.State)
pbc.Status.Detail = ""
pbc.Spec.DeepCopyInto(&pbc.Status.Spec)
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
default:
if pbc.Spec.ActiveBundle != "" {
pbc.Status.State = api.BundleControllerStateActive
m.log.V(6).Info("update", "PackageBundleController", pbc.Name, "state", pbc.Status.State)
pbc.Status.Detail = ""
pbc.Spec.DeepCopyInto(&pbc.Status.Spec)
err = m.bundleClient.SaveStatus(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s status to %s: %s", pbc.Name, pbc.Status.State, err)
}
} else {
pbc.Spec.ActiveBundle = latestBundle.Name
m.log.V(6).Info("update", "PackageBundleController", pbc.Name, "activeBundle", pbc.Spec.ActiveBundle)
pbc.Status.Detail = ""
err = m.bundleClient.Save(ctx, pbc)
if err != nil {
return fmt.Errorf("updating %s activeBundle to %s: %s", pbc.Name, pbc.Spec.ActiveBundle, err)
}
}
}
return nil
}