/
mcagent_appconfig.go
277 lines (251 loc) · 11.9 KB
/
mcagent_appconfig.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
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2021, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package mcagent
import (
"fmt"
"reflect"
"strings"
oamv1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
clustersv1alpha1 "github.com/verrazzano/verrazzano/application-operator/apis/clusters/v1alpha1"
"github.com/verrazzano/verrazzano/application-operator/constants"
"github.com/verrazzano/verrazzano/application-operator/controllers/clusters"
vzconst "github.com/verrazzano/verrazzano/pkg/constants"
vzstring "github.com/verrazzano/verrazzano/pkg/string"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// Synchronize MultiClusterApplicationConfiguration objects to the local cluster
func (s *Syncer) syncMCApplicationConfigurationObjects(namespace string) error {
// Get all the MultiClusterApplicationConfiguration objects from the admin cluster
allAdminMCAppConfigs := clustersv1alpha1.MultiClusterApplicationConfigurationList{}
listOptions := &client.ListOptions{Namespace: namespace}
err := s.AdminClient.List(s.Context, &allAdminMCAppConfigs, listOptions)
// When placements are changed a forbidden error can be returned. In this case,
// we want to fall through and delete orphaned resources.
if err != nil && !apierrors.IsNotFound(err) && !apierrors.IsForbidden(err) {
return err
}
for _, mcAppConfig := range allAdminMCAppConfigs.Items {
if s.isThisCluster(mcAppConfig.Spec.Placement) {
// Synchronize the components referenced by the application
err := s.syncComponentList(mcAppConfig)
if err != nil {
s.Log.Errorw(fmt.Sprintf("Failed syncing components referenced by object: %v", err),
"MultiClusterApplicationConfiguration",
types.NamespacedName{Namespace: mcAppConfig.Namespace, Name: mcAppConfig.Name})
}
// Synchronize the MultiClusterApplicationConfiguration even if there were errors
// handling the application components. For compatibility with v1.0.0 it is valid
// for none of the OAM Components to be found because they may all be wrapped in
// an MultiClusterComponent resource.
_, err = s.createOrUpdateMCAppConfig(mcAppConfig)
if err != nil {
s.Log.Errorw(fmt.Sprintf("Failed syncing object: %c", err),
"MultiClusterApplicationConfiguration",
types.NamespacedName{Namespace: mcAppConfig.Namespace, Name: mcAppConfig.Name})
}
}
}
// Delete orphaned MultiClusterApplicationConfiguration resources.
// Get the list of MultiClusterApplicationConfiguration resources on the
// local cluster and compare to the list received from the admin cluster.
// The admin cluster is the source of truth.
allLocalMCAppConfigs := clustersv1alpha1.MultiClusterApplicationConfigurationList{}
err = s.LocalClient.List(s.Context, &allLocalMCAppConfigs, listOptions)
if err != nil {
s.Log.Errorf("Failed to list MultiClusterApplicationConfiguration on local cluster: %v", err)
return nil
}
for i, mcAppConfig := range allLocalMCAppConfigs.Items {
// Delete each MultiClusterApplicationConfiguration object that is not on the admin cluster or no longer placed on this cluster
if !s.appConfigPlacedOnCluster(&allAdminMCAppConfigs, mcAppConfig.Name, mcAppConfig.Namespace) {
err := s.LocalClient.Delete(s.Context, &allLocalMCAppConfigs.Items[i])
if err != nil {
s.Log.Errorf("Failed to delete MultiClusterApplicationConfiguration with name %q in namespace %q: %v", mcAppConfig.Name, mcAppConfig.Namespace, err)
}
}
}
// Delete OAM components no longer associated with any MultiClusterApplicationConfiguration
err = s.deleteOrphanedComponents(namespace)
if err != nil {
s.Log.Errorf("Failed deleting orphaned OAM Components in namespace %q: %v", namespace, err)
}
return nil
}
func (s *Syncer) createOrUpdateMCAppConfig(mcAppConfig clustersv1alpha1.MultiClusterApplicationConfiguration) (controllerutil.OperationResult, error) {
var mcAppConfigNew clustersv1alpha1.MultiClusterApplicationConfiguration
mcAppConfigNew.Namespace = mcAppConfig.Namespace
mcAppConfigNew.Name = mcAppConfig.Name
// Create or update on the local cluster
return controllerutil.CreateOrUpdate(s.Context, s.LocalClient, &mcAppConfigNew, func() error {
mutateMCAppConfig(mcAppConfig, &mcAppConfigNew)
return nil
})
}
func mutateMCAppConfig(mcAppConfig clustersv1alpha1.MultiClusterApplicationConfiguration, mcAppConfigNew *clustersv1alpha1.MultiClusterApplicationConfiguration) {
mcAppConfigNew.Spec.Placement = mcAppConfig.Spec.Placement
mcAppConfigNew.Spec.Template = mcAppConfig.Spec.Template
mcAppConfigNew.Labels = mcAppConfig.Labels
// Mark the MC app config we synced from Admin cluster with verrazzano-managed=true, to
// distinguish from any (though unlikely) that the user might have created directly
if mcAppConfigNew.Labels == nil {
mcAppConfigNew.Labels = map[string]string{}
}
mcAppConfigNew.Labels[vzconst.VerrazzanoManagedLabelKey] = constants.LabelVerrazzanoManagedDefault
}
// appConfigPlacedOnCluster returns boolean indicating if the list contains the object with the specified name and namespace and the placement
// includes the local cluster
func (s *Syncer) appConfigPlacedOnCluster(mcAdminList *clustersv1alpha1.MultiClusterApplicationConfigurationList, name string, namespace string) bool {
for _, item := range mcAdminList.Items {
if item.Name == name && item.Namespace == namespace {
return s.isThisCluster(item.Spec.Placement)
}
}
return false
}
func (s *Syncer) updateMultiClusterAppConfigStatus(name types.NamespacedName, newCond clustersv1alpha1.Condition, newClusterStatus clustersv1alpha1.ClusterLevelStatus) error {
var fetched clustersv1alpha1.MultiClusterApplicationConfiguration
err := s.AdminClient.Get(s.Context, name, &fetched)
if err != nil {
return err
}
fetched.Status.Conditions = append(fetched.Status.Conditions, newCond)
clusters.SetClusterLevelStatus(&fetched.Status, newClusterStatus)
return s.AdminClient.Status().Update(s.Context, &fetched)
}
// syncComponentList - Synchronize the list of OAM Components contained in the MultiClusterApplicationConfiguration
func (s *Syncer) syncComponentList(mcAppConfig clustersv1alpha1.MultiClusterApplicationConfiguration) error {
var errorStrings []string
// Loop through the component list and get them one at a time.
for _, component := range mcAppConfig.Spec.Template.Spec.Components {
objectKey := types.NamespacedName{Name: component.ComponentName, Namespace: mcAppConfig.Namespace}
oamComp := &oamv1alpha2.Component{}
err := s.AdminClient.Get(s.Context, objectKey, oamComp)
if err != nil {
// If the OAM component object is not found then we check if the MultiClusterComponent object exists.
if apierrors.IsNotFound(err) {
mcComp := &clustersv1alpha1.MultiClusterComponent{}
errmc := s.LocalClient.Get(s.Context, objectKey, mcComp)
// Return the OAM component not found error if we fail to get the MultiClusterComponent
// with the same name.
if errmc != nil {
return err
}
// MulticlusterComponent object found so nothing to do
continue
} else {
return err
}
}
_, err = s.createOrUpdateComponent(*oamComp, mcAppConfig.Name)
if err != nil {
errorStrings = append(errorStrings, err.Error())
}
}
// Check if any errors were collected while processing the list
if len(errorStrings) > 0 {
return fmt.Errorf(strings.Join(errorStrings, "\n"))
}
return nil
}
// createOrUpdateComponent - create or update an OAM Component
func (s *Syncer) createOrUpdateComponent(srcComp oamv1alpha2.Component, mcAppConfigName string) (controllerutil.OperationResult, error) {
var oamComp oamv1alpha2.Component
oamComp.Namespace = srcComp.Namespace
oamComp.Name = srcComp.Name
return controllerutil.CreateOrUpdate(s.Context, s.LocalClient, &oamComp, func() error {
s.mutateComponent(s.ManagedClusterName, mcAppConfigName, srcComp, &oamComp)
return nil
})
}
// mutateComponent mutates the OAM component to reflect the contents of the parent MultiClusterComponent
func (s *Syncer) mutateComponent(managedClusterName string, mcAppConfigName string, component oamv1alpha2.Component, componentNew *oamv1alpha2.Component) {
// Initialize the labels field
if componentNew.Labels == nil {
componentNew.Labels = make(map[string]string)
if component.Labels != nil {
componentNew.Labels = component.Labels
}
}
// Add the name of this MultiClusterApplicationConfiguration to the label list
componentNew.Labels[mcAppConfigsLabel] = vzstring.AppendToCommaSeparatedString(componentNew.Labels[mcAppConfigsLabel], mcAppConfigName)
componentNew.Labels[managedClusterLabel] = managedClusterName
// Mark the component synced from Admin cluster with verrazzano-managed=true, to distinguish
// those directly created by user on managed cluster
componentNew.Labels[vzconst.VerrazzanoManagedLabelKey] = constants.LabelVerrazzanoManagedDefault
componentNew.Spec = component.Spec
componentNew.Annotations = component.Annotations
}
// deleteOrphanedComponents - delete OAM components that are no longer associated with any MultiClusterApplicationConfigurations.
// Also update the contents of the mcAppConfigsLabel for a component if the list of applications it is shared by has changed.
func (s *Syncer) deleteOrphanedComponents(namespace string) error {
// Only process OAM components that were synced to the local system
labels := &metav1.LabelSelector{
MatchLabels: map[string]string{
managedClusterLabel: s.ManagedClusterName,
},
}
selector, err := metav1.LabelSelectorAsSelector(labels)
if err != nil {
return err
}
listOptions := &client.ListOptions{Namespace: namespace, LabelSelector: selector}
oamCompList := &oamv1alpha2.ComponentList{}
err = s.LocalClient.List(s.Context, oamCompList, listOptions)
if err != nil {
return err
}
// Nothing to do if no OAM components found
if len(oamCompList.Items) == 0 {
return nil
}
// Get the list of MultiClusterApplicationConfiguration objects
listOptions2 := &client.ListOptions{Namespace: namespace}
mcAppConfigList := clustersv1alpha1.MultiClusterApplicationConfigurationList{}
err = s.LocalClient.List(s.Context, &mcAppConfigList, listOptions2)
if err != nil {
return err
}
// Process the list of OAM Components checking to see if they are part of any MultiClusterApplicationConfiguration
for i, oamComp := range oamCompList.Items {
// Don't delete OAM component objects that have a MultiClusterComponent objects. These will be deleted
// when syncing MultiClusterComponent objects.
mcComp := &clustersv1alpha1.MultiClusterComponent{}
objectKey := types.NamespacedName{Name: oamComp.Name, Namespace: namespace}
err = s.LocalClient.Get(s.Context, objectKey, mcComp)
if err == nil {
continue
}
var actualAppConfigs []string
// Loop through the MultiClusterApplicationConfiguration objects checking for a reference
for _, mcAppConfig := range mcAppConfigList.Items {
for _, component := range mcAppConfig.Spec.Template.Spec.Components {
// If we get a match, maintain a list of applications this OAM component is shared by
if component.ComponentName == oamComp.Name {
actualAppConfigs = append(actualAppConfigs, mcAppConfig.Name)
}
}
}
if len(actualAppConfigs) == 0 {
// Delete the orphaned OAM Component
s.Log.Debugf("Deleting orphaned OAM Component %s in namespace %s", oamComp.Name, oamComp.Namespace)
err = s.LocalClient.Delete(s.Context, &oamCompList.Items[i])
if err != nil {
return err
}
} else {
// Has the list of applications this component is associated with changed? If so update the label.
if !reflect.DeepEqual(strings.Split(oamComp.Labels[mcAppConfigsLabel], ","), actualAppConfigs) {
oamComp.Labels[mcAppConfigsLabel] = strings.Join(actualAppConfigs, ",")
err = s.LocalClient.Update(s.Context, &oamCompList.Items[i])
if err != nil {
return err
}
}
}
}
return nil
}