Skip to content

Commit 1ab9243

Browse files
author
Moritz Clasmeier
committed
Merge branch 'mc/simplify-teardown' into mc/kind-e2e
2 parents 84fe568 + db22948 commit 1ab9243

1 file changed

Lines changed: 64 additions & 172 deletions

File tree

internal/deployer/deployer.go

Lines changed: 64 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -40,70 +40,6 @@ var (
4040

4141
// AdminUsername is the default admin username for StackRox Central
4242
AdminUsername = "admin"
43-
44-
// TODO(#91): at some point this will get out of date. If we filter by the app.../part-of
45-
// label anyway, then maybe we should just delete all resource kinds present on cluster?
46-
// also we should use the fully-qualified types
47-
allInstallableCentralResourceKinds = []string{
48-
"applications",
49-
"clusterroles",
50-
"configmaps",
51-
"deployments",
52-
"destinationrules",
53-
"endpoints",
54-
"endpointslices",
55-
"horizontalpodautoscalers",
56-
"networkpolicys",
57-
"leases",
58-
"persistentvolumes",
59-
"persistentvolumeclaims",
60-
"pods",
61-
"podsecuritypolicys",
62-
"prometheusrules",
63-
"roles",
64-
"rolebindings",
65-
"replicasets",
66-
"routes",
67-
"secrets",
68-
"services",
69-
"serviceaccounts",
70-
"servicemonitors",
71-
"storageclasses",
72-
}
73-
74-
allInstallableSecuredClusterResourceKinds = []string{
75-
"clusterroles",
76-
"clusterrolebindings",
77-
"configmaps",
78-
"consoleplugins",
79-
"controllerrevisions",
80-
"daemonsets",
81-
"deployments",
82-
"endpoints",
83-
"endpointslices",
84-
"destinationrules",
85-
"horizontalpodautoscalers",
86-
"networkpolicys",
87-
"leases",
88-
"persistentvolumes",
89-
"persistentvolumeclaims",
90-
"pods",
91-
"podsecuritypolicys",
92-
"prometheusrules",
93-
"replicasets",
94-
"roles",
95-
"rolebindings",
96-
"secrets",
97-
"services",
98-
"serviceaccounts",
99-
"servicemonitors",
100-
"storageclasses",
101-
"validatingwebhookconfigurations",
102-
}
103-
)
104-
105-
const (
106-
injectedCABundleConfigMap = "injected-cabundle-stackrox-central-services"
10743
)
10844

10945
// Deployer is the base deployer for ACS
@@ -149,16 +85,6 @@ type ResourceToDelete struct {
14985
OwnerName string
15086
}
15187

152-
func (d *Deployer) filterResourceKinds(resourceKinds []string) []string {
153-
filteredResourceKinds := make([]string, 0, len(resourceKinds))
154-
for _, resourceKind := range resourceKinds {
155-
if _, ok := d.clusterResourceKinds[resourceKind]; ok {
156-
filteredResourceKinds = append(filteredResourceKinds, resourceKind)
157-
}
158-
}
159-
return filteredResourceKinds
160-
}
161-
16288
func (d *Deployer) deleteResource(ctx context.Context, namespace, resourceType, resourceName string, args ...string) error {
16389
return d.deleteResources(ctx, namespace, []string{resourceType}, append([]string{resourceName}, args...)...)
16490
}
@@ -178,60 +104,43 @@ func (d *Deployer) deleteResources(ctx context.Context, namespace string, resour
178104
return err
179105
}
180106

181-
func (d *Deployer) deleteFinalizers(ctx context.Context, namespace, resourceType, resourceName string) error {
182-
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
183-
Args: []string{
184-
"-n", namespace, "patch", resourceType, resourceName,
185-
"-p", `{"metadata":{"finalizers":null}}`,
186-
"--type=merge",
187-
},
188-
})
189-
return err
190-
}
191-
192107
// Expects that reconciliation for the RHACS operator is paused.
193-
func (d *Deployer) deleteCentralResources(ctx context.Context, wait bool) error {
108+
func (d *Deployer) deleteCentralResources(ctx context.Context) error {
194109
d.logger.Info("Deleting Central resources")
195-
var crExists bool
110+
crExists := true
196111

197-
if d.doesResourceExist(ctx, "central", "stackrox-central-services", d.centralNamespace) {
198-
crExists = true
112+
if _, err := k8s.RetrieveResourceFromCluster(ctx, d.logger, d.centralNamespace, "central", "stackrox-central-services"); err != nil {
113+
if !k8s.IsResourceNotFound(err) {
114+
return fmt.Errorf("retrieving Central CR: %w", err)
115+
}
116+
crExists = false
117+
}
199118

200-
// Trigger async deletion of the Central CR.
201-
err := d.deleteResource(ctx, d.centralNamespace, "central", "stackrox-central-services", "--wait=false")
202-
if err != nil {
203-
return fmt.Errorf("failed to asynchronously delete Central CR: %w", err)
119+
if crExists {
120+
d.logger.Info("Removing any pause-reconcile annotation from Central")
121+
if err := d.removePauseReconcileAnnotation(ctx, "central", "stackrox-central-services", d.centralNamespace); err != nil {
122+
return err
123+
}
124+
if d.verbose {
125+
d.logger.Dim("Removed any pause-reconcile annotation from Central")
204126
}
205127

206-
err = d.deleteFinalizers(ctx, d.centralNamespace, "central", "stackrox-central-services")
128+
err := d.deleteResource(ctx, d.centralNamespace, "central", "stackrox-central-services", "--wait")
207129
if err != nil {
208-
return fmt.Errorf("failed to delete finalizers on Central CR: %w", err)
130+
return err
209131
}
210-
}
211-
212-
// Pause reconciliation for other controllers, not just our RHACS operator.
213-
// This is needed to ensure that there is no race causing the Cluster Network Operator
214-
// to re-create the injected-ca-bundle ConfigMap during resource deletion.
215-
err := d.preventOtherControllersFromReconciling(ctx)
216-
if err != nil {
217-
return fmt.Errorf("failed to prevent other controllers from reconciling: %w", err)
218-
}
219-
220-
// Delete other resources by brute force.
221-
resourceKinds := d.filterResourceKinds(allInstallableCentralResourceKinds)
222-
err = d.deleteResources(ctx, d.centralNamespace, resourceKinds, "-l=app.kubernetes.io/part-of=stackrox-central-services")
223-
if err != nil {
224-
return err
132+
if d.verbose {
133+
d.logger.Dim("Deleted Central CR")
134+
}
135+
} else {
136+
d.logger.Info("Deletion of Central resources requested, but Central CR is not present anymore")
225137
}
226138

227139
for _, resource := range []ResourceToDelete{
228-
{Name: "central-db", Kind: "pvc", OwnerName: centralCrName},
229-
{Name: "central-db-backup", Kind: "pvc", OwnerName: centralCrName},
140+
{Name: "central-db", Kind: "pvc"},
141+
{Name: "central-db-backup", Kind: "pvc"},
230142
{Name: "admin-password", Kind: "secret"},
231143
{Name: "scanner-db-password", Kind: "secret", OwnerName: centralCrName},
232-
// In case the Cluster Network Operator has succeeded in re-creating the injectedCABundleConfigMap
233-
// after our operator has already deleted it.
234-
{Name: injectedCABundleConfigMap, Kind: "configmap"},
235144
} {
236145
d.logger.Dimf("Attempting to delete %s/%s", resource.Kind, resource.Name)
237146
if resource.OwnerName != "" {
@@ -255,66 +164,41 @@ func (d *Deployer) deleteCentralResources(ctx context.Context, wait bool) error
255164
}
256165
}
257166

258-
if crExists {
259-
// Now delete the Central CR synchronously.
260-
err := d.deleteResource(ctx, d.centralNamespace, "central", "stackrox-central-services")
261-
if err != nil {
262-
return fmt.Errorf("failed to delete Central CR: %w", err)
263-
}
264-
}
265-
266167
return nil
267168
}
268169

269-
func (d *Deployer) preventOtherControllersFromReconciling(ctx context.Context) error {
270-
return d.preventCABundleInjection(ctx)
271-
}
272-
273-
func (d *Deployer) preventCABundleInjection(ctx context.Context) error {
274-
configMapName := injectedCABundleConfigMap
275-
276-
d.logger.Info("Removing CNO label from injected-cabundle ConfigMap to prevent CNO from injecting the CA bundle during cleanup")
277-
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
278-
Args: []string{
279-
"label", "configmap", configMapName, "-n", d.centralNamespace,
280-
"config.openshift.io/inject-trusted-cabundle-",
281-
},
282-
IgnoreErrors: true,
283-
})
284-
285-
if err != nil {
286-
d.logger.Warningf("Failed to remove CNO label from %s: %v", configMapName, err)
287-
}
288-
289-
return nil
290-
}
291-
292-
func (d *Deployer) deleteSecuredClusterResources(ctx context.Context, wait bool) error {
170+
func (d *Deployer) deleteSecuredClusterResources(ctx context.Context) error {
293171
d.logger.Info("Deleting SecuredCluster resources")
294-
var crExists bool
172+
crExists := true
295173

296-
if d.doesResourceExist(ctx, "securedcluster", "stackrox-secured-cluster-services", d.sensorNamespace) {
297-
crExists = true
174+
if _, err := k8s.RetrieveResourceFromCluster(ctx, d.logger, d.sensorNamespace, "securedcluster", "stackrox-secured-cluster-services"); err != nil {
175+
if !k8s.IsResourceNotFound(err) {
176+
return fmt.Errorf("retrieving SecuredCluster CR: %w", err)
177+
}
178+
crExists = false
179+
}
298180

299-
// Trigger async deletion of the SecuredCluster CR.
300-
err := d.deleteResource(ctx, d.sensorNamespace, "securedcluster", "stackrox-secured-cluster-services", "--wait=false")
301-
if err != nil {
181+
if crExists {
182+
d.logger.Info("Removing any pause-reconcile annotation from SecuredCluster")
183+
if err := d.removePauseReconcileAnnotation(ctx, "securedcluster", "stackrox-secured-cluster-services", d.sensorNamespace); err != nil {
302184
return err
303185
}
186+
if d.verbose {
187+
d.logger.Dim("Removed any pause-reconcile annotation from SecuredCluster")
188+
}
304189

305-
err = d.deleteFinalizers(ctx, d.sensorNamespace, "securedcluster", "stackrox-secured-cluster-services")
190+
err := d.deleteResource(ctx, d.sensorNamespace, "securedcluster", "stackrox-secured-cluster-services", "--wait")
306191
if err != nil {
307-
return fmt.Errorf("failed to delete finalizers on SecuredCluster CR: %w", err)
192+
return err
308193
}
194+
if d.verbose {
195+
d.logger.Dim("Deleted SecuredCluster CR")
196+
}
197+
} else {
198+
d.logger.Info("Deletion of SecuredCluster resources requested, but SecuredCluster CR is not present anymore")
309199
}
310200

311-
// In the meantime, delete other resources by brute force.
312-
resourceKinds := d.filterResourceKinds(allInstallableSecuredClusterResourceKinds)
313-
err := d.deleteResources(ctx, d.sensorNamespace, resourceKinds, "-l=app.kubernetes.io/part-of=stackrox-secured-cluster-services")
314-
if err != nil {
315-
return err
316-
}
317-
201+
// Delete resources, which are treated special.
318202
for _, resource := range []ResourceToDelete{
319203
{Name: "cluster-registration-secret", Kind: "secret"},
320204
// We need to make sure that don't accidentally delete a scanner-db-password belonging to the central CR,
@@ -342,14 +226,6 @@ func (d *Deployer) deleteSecuredClusterResources(ctx context.Context, wait bool)
342226
}
343227
}
344228

345-
if crExists {
346-
// Now delete the SecuredCluster CR synchronously.
347-
err := d.deleteResource(ctx, d.sensorNamespace, "securedcluster", "stackrox-secured-cluster-services")
348-
if err != nil {
349-
return fmt.Errorf("failed to delete SecuredCluster CR: %w", err)
350-
}
351-
}
352-
353229
return nil
354230
}
355231

@@ -791,7 +667,7 @@ func (d *Deployer) teardownCentral(ctx context.Context) error {
791667
}
792668

793669
d.logger.Info("⏳ Waiting for Central resources to be fully deleted...")
794-
err := d.deleteCentralResources(ctx, true)
670+
err := d.deleteCentralResources(ctx)
795671
if err != nil {
796672
return fmt.Errorf("failed to delete Central resources: %w", err)
797673
}
@@ -816,7 +692,7 @@ func (d *Deployer) teardownSecuredCluster(ctx context.Context) error {
816692
}
817693

818694
d.logger.Info("⏳ Waiting for SecuredCluster resources to be fully deleted...")
819-
err := d.deleteSecuredClusterResources(ctx, true)
695+
err := d.deleteSecuredClusterResources(ctx)
820696
if err != nil {
821697
return fmt.Errorf("failed to delete SecuredCluster resources: %w", err)
822698
}
@@ -1033,6 +909,22 @@ func (d *Deployer) addPauseReconcileAnnotation(ctx context.Context, resourceType
1033909
return nil
1034910
}
1035911

912+
func (d *Deployer) removePauseReconcileAnnotation(ctx context.Context, resourceType, resourceName, namespace string) error {
913+
_, err := d.runKubectl(ctx, k8s.KubectlOptions{
914+
Args: []string{
915+
"annotate", resourceType, resourceName,
916+
"-n", namespace,
917+
fmt.Sprintf("%s-", pauseReconcileAnnotationKey),
918+
},
919+
IgnoreErrors: true,
920+
})
921+
if err != nil {
922+
return fmt.Errorf("failed to remove pause-reconcile annotation: %w", err)
923+
}
924+
925+
return nil
926+
}
927+
1036928
func (d *Deployer) SetDeployOperator(deployOperator bool) {
1037929
d.shouldDeployOperator = deployOperator
1038930
}

0 commit comments

Comments
 (0)