/
actions.go
169 lines (143 loc) · 5.44 KB
/
actions.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
package controllers
import (
"context"
"fmt"
ranv1alpha1 "github.com/openshift-kni/cluster-group-upgrades-operator/api/v1alpha1"
utils "github.com/openshift-kni/cluster-group-upgrades-operator/controllers/utils"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
clusterv1 "open-cluster-management.io/api/cluster/v1"
)
// takeActionsBeforeEnable takes the required actions before starting upgrade
// returns: error/nil
func (r *ClusterGroupUpgradeReconciler) takeActionsBeforeEnable(
ctx context.Context, clusterGroupUpgrade *ranv1alpha1.ClusterGroupUpgrade) error {
actionsBeforeEnable := clusterGroupUpgrade.Spec.Actions.BeforeEnable
// Add/delete cluster labels
if actionsBeforeEnable.AddClusterLabels != nil || actionsBeforeEnable.DeleteClusterLabels != nil {
clusters := r.getClustersListFromRemediationPlan(clusterGroupUpgrade)
r.Log.Info("[actions]", "clusterList", clusters)
labels := map[string]map[string]string{
"add": actionsBeforeEnable.AddClusterLabels,
"delete": actionsBeforeEnable.DeleteClusterLabels,
}
for _, c := range clusters {
err := r.manageClusterLabels(ctx, c, labels)
if err != nil {
return err
}
}
}
return nil
}
func (r *ClusterGroupUpgradeReconciler) takeActionsAfterCompletion(
ctx context.Context, clusterGroupUpgrade *ranv1alpha1.ClusterGroupUpgrade, cluster string) error {
clusterState := ranv1alpha1.ClusterState{
Name: cluster, State: utils.ClusterRemediationComplete}
clusterGroupUpgrade.Status.Clusters = append(clusterGroupUpgrade.Status.Clusters, clusterState)
actionsAfterCompletion := clusterGroupUpgrade.Spec.Actions.AfterCompletion
// Add/delete cluster labels
if actionsAfterCompletion.AddClusterLabels != nil || actionsAfterCompletion.DeleteClusterLabels != nil {
labels := map[string]map[string]string{
"add": actionsAfterCompletion.AddClusterLabels,
"delete": actionsAfterCompletion.DeleteClusterLabels,
}
err := r.manageClusterLabels(ctx, cluster, labels)
if err != nil {
return err
}
}
return nil
}
// manageClusterLabels adds/deletes the cluster labels for selected clusters
func (r *ClusterGroupUpgradeReconciler) manageClusterLabels(
ctx context.Context, cluster string, labels map[string]map[string]string) error {
managedCluster := &clusterv1.ManagedCluster{}
if err := r.Get(ctx, types.NamespacedName{Name: cluster}, managedCluster); err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
if err := r.addClusterLabels(ctx, managedCluster, labels["add"]); err != nil {
return fmt.Errorf("fail to add labels for cluster %s: %v", managedCluster.Name, err)
}
if err := r.deleteClusterLabels(ctx, managedCluster, labels["delete"]); err != nil {
return fmt.Errorf("fail to delete labels for cluster %s: %v", managedCluster.Name, err)
}
return nil
}
// Add cluster labels
func (r *ClusterGroupUpgradeReconciler) addClusterLabels(
ctx context.Context, cluster *clusterv1.ManagedCluster, labels map[string]string) error {
if len(labels) == 0 {
return nil
}
currentLabels := cluster.GetLabels()
if currentLabels == nil {
currentLabels = make(map[string]string)
}
for key, value := range labels {
currentLabels[key] = value
}
cluster.SetLabels(currentLabels)
//nolint:revive
if err := r.Update(ctx, cluster); err != nil {
return err
}
return nil
}
// Delete cluster labels
func (r *ClusterGroupUpgradeReconciler) deleteClusterLabels(
ctx context.Context, cluster *clusterv1.ManagedCluster, labels map[string]string) error {
if len(labels) == 0 {
return nil
}
currentLabels := cluster.GetLabels()
if currentLabels == nil {
return nil
}
for key, value := range labels {
currentLabelValue, found := currentLabels[key]
if found && currentLabelValue == value {
delete(currentLabels, key)
}
}
cluster.SetLabels(currentLabels)
//nolint:revive
if err := r.Update(ctx, cluster); err != nil {
return err
}
return nil
}
func (r *ClusterGroupUpgradeReconciler) deleteResources(
ctx context.Context, clusterGroupUpgrade *ranv1alpha1.ClusterGroupUpgrade) error {
labels := map[string]string{"openshift-cluster-group-upgrades/clusterGroupUpgrade": clusterGroupUpgrade.Name}
err := utils.DeletePlacementRules(ctx, r.Client, clusterGroupUpgrade.Namespace, labels)
if err != nil {
return fmt.Errorf("failed to delete PlacementRules for CGU %s: %v", clusterGroupUpgrade.Name, err)
}
clusterGroupUpgrade.Status.PlacementRules = nil
err = utils.DeletePlacementBindings(ctx, r.Client, clusterGroupUpgrade.Namespace, labels)
if err != nil {
return fmt.Errorf("failed to delete PlacementBindings for CGU %s: %v", clusterGroupUpgrade.Name, err)
}
clusterGroupUpgrade.Status.PlacementBindings = nil
err = utils.DeletePolicies(ctx, r.Client, clusterGroupUpgrade.Namespace, labels)
if err != nil {
return fmt.Errorf("failed to delete Policies for CGU %s: %v", clusterGroupUpgrade.Name, err)
}
clusterGroupUpgrade.Status.CopiedPolicies = nil
clusters := r.getClustersListFromRemediationPlan(clusterGroupUpgrade)
r.Log.Info("[actions]", "clusterList", clusters)
err = utils.DeleteAllMultiCloudObjects(ctx, r.Client, clusterGroupUpgrade, clusters)
if err != nil {
return fmt.Errorf("failed to delete MultiCloud objects for CGU %s: %v", clusterGroupUpgrade.Name, err)
}
err = r.jobAndViewFinalCleanup(ctx, clusterGroupUpgrade)
if err != nil {
return fmt.Errorf("failed to delete precaching objects for CGU %s: %v", clusterGroupUpgrade.Name, err)
}
clusterGroupUpgrade.Status.SafeResourceNames = nil
return nil
}