@@ -1116,6 +1116,7 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr
1116
1116
if err != nil {
1117
1117
return false , errors .Wrapf (err , "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s" , kind , namespace , name )
1118
1118
}
1119
+
1119
1120
resourceVersion := existingRes .GetResourceVersion ()
1120
1121
1121
1122
if ! r .CheckLabel (existingRes , map [string ]string {constant .OpreqLabel : "true" }) && (newLabels == nil || newLabels [constant .OpreqLabel ] != "true" ) {
@@ -1253,7 +1254,7 @@ func (r *Reconciler) updateK8sJob(ctx context.Context, existingK8sRes unstructur
1253
1254
}
1254
1255
newAnnotations [constant .HashedData ] = newHashedData
1255
1256
1256
- if err := r .deleteK8sResource (ctx , existingRes , namespace ); err != nil {
1257
+ if err := r .deleteK8sResource (ctx , existingRes , newLabels , namespace ); err != nil {
1257
1258
return errors .Wrap (err , "failed to update k8s resource" )
1258
1259
}
1259
1260
if err := r .createK8sResource (ctx , templatek8sRes , k8sResConfig , newLabels , newAnnotations , ownerReferences , optionalFields ); err != nil {
@@ -1312,7 +1313,7 @@ func (r *Reconciler) updateK8sRoute(ctx context.Context, existingK8sRes unstruct
1312
1313
templatek8sRes .SetName (name )
1313
1314
templatek8sRes .SetNamespace (namespace )
1314
1315
1315
- if err := r .deleteK8sResource (ctx , existingRes , namespace ); err != nil {
1316
+ if err := r .deleteK8sResource (ctx , existingRes , newLabels , namespace ); err != nil {
1316
1317
return errors .Wrap (err , "failed to delete Route for recreation" )
1317
1318
}
1318
1319
if err := r .createK8sResource (ctx , templatek8sRes , k8sResConfig , newLabels , newAnnotations , ownerReferences , optionalFields ); err != nil {
@@ -1324,7 +1325,56 @@ func (r *Reconciler) updateK8sRoute(ctx context.Context, existingK8sRes unstruct
1324
1325
return nil
1325
1326
}
1326
1327
1327
- func (r * Reconciler ) deleteK8sResource (ctx context.Context , existingK8sRes unstructured.Unstructured , namespace string ) error {
1328
+ // deleteAllK8sResource remove k8s resource base on OperandConfig
1329
+ func (r * Reconciler ) deleteAllK8sResource (ctx context.Context , csc * operatorv1alpha1.OperandConfig , operandName , namespace string ) error {
1330
+
1331
+ service := csc .GetService (operandName )
1332
+ if service == nil {
1333
+ return nil
1334
+ }
1335
+
1336
+ var k8sResourceList []operatorv1alpha1.ConfigResource
1337
+ k8sResourceList = append (k8sResourceList , service .Resources ... )
1338
+
1339
+ merr := & util.MultiErr {}
1340
+ var (
1341
+ wg sync.WaitGroup
1342
+ )
1343
+ for _ , k8sRes := range k8sResourceList {
1344
+ k8sResShouldBeDeleted := unstructured.Unstructured {
1345
+ Object : map [string ]interface {}{
1346
+ "apiVersion" : k8sRes .APIVersion ,
1347
+ "kind" : k8sRes .Kind ,
1348
+ "metadata" : map [string ]interface {}{
1349
+ "name" : k8sRes .Name ,
1350
+ },
1351
+ },
1352
+ }
1353
+ k8sNamespace := namespace
1354
+ if k8sRes .Namespace != "" {
1355
+ k8sNamespace = k8sRes .Namespace
1356
+ }
1357
+
1358
+ wg .Add (1 )
1359
+ go func () {
1360
+ defer wg .Done ()
1361
+ if err := r .deleteK8sResource (ctx , k8sResShouldBeDeleted , k8sRes .Labels , k8sNamespace ); err != nil {
1362
+ r .Mutex .Lock ()
1363
+ defer r .Mutex .Unlock ()
1364
+ merr .Add (err )
1365
+ return
1366
+ }
1367
+ }()
1368
+ }
1369
+ wg .Wait ()
1370
+
1371
+ if len (merr .Errors ) != 0 {
1372
+ return merr
1373
+ }
1374
+ return nil
1375
+ }
1376
+
1377
+ func (r * Reconciler ) deleteK8sResource (ctx context.Context , existingK8sRes unstructured.Unstructured , newLabels map [string ]string , namespace string ) error {
1328
1378
1329
1379
kind := existingK8sRes .GetKind ()
1330
1380
apiversion := existingK8sRes .GetAPIVersion ()
@@ -1346,7 +1396,13 @@ func (r *Reconciler) deleteK8sResource(ctx context.Context, existingK8sRes unstr
1346
1396
if apierrors .IsNotFound (err ) {
1347
1397
klog .V (3 ).Infof ("There is no k8s resource: %s from kind: %s" , name , kind )
1348
1398
} else {
1349
- if r .CheckLabel (k8sResShouldBeDeleted , map [string ]string {constant .OpreqLabel : "true" }) && ! r .CheckLabel (k8sResShouldBeDeleted , map [string ]string {constant .NotUninstallLabel : "true" }) {
1399
+ // If the existing k8s resources has the OpreqLabel and does not have the NotUninstallLabel, delete it
1400
+ // If the OpreqLabel is difined in OperandConfig resource, delete it
1401
+ hasOpreqLabel := r .CheckLabel (k8sResShouldBeDeleted , map [string ]string {constant .OpreqLabel : "true" })
1402
+ hasNotUninstallLabel := r .CheckLabel (k8sResShouldBeDeleted , map [string ]string {constant .NotUninstallLabel : "true" })
1403
+ opreqLabelInConfig := newLabels != nil && newLabels [constant .OpreqLabel ] == "true"
1404
+
1405
+ if (hasOpreqLabel && ! hasNotUninstallLabel ) || opreqLabelInConfig {
1350
1406
klog .V (3 ).Infof ("Deleting k8s resource: %s from kind: %s" , name , kind )
1351
1407
err := r .Delete (ctx , & k8sResShouldBeDeleted , client .PropagationPolicy (metav1 .DeletePropagationBackground ))
1352
1408
if err != nil && ! apierrors .IsNotFound (err ) {
@@ -1378,55 +1434,6 @@ func (r *Reconciler) deleteK8sResource(ctx context.Context, existingK8sRes unstr
1378
1434
return nil
1379
1435
}
1380
1436
1381
- // deleteAllK8sResource remove k8s resource base on OperandConfig
1382
- func (r * Reconciler ) deleteAllK8sResource (ctx context.Context , csc * operatorv1alpha1.OperandConfig , operandName , namespace string ) error {
1383
-
1384
- service := csc .GetService (operandName )
1385
- if service == nil {
1386
- return nil
1387
- }
1388
-
1389
- var k8sResourceList []operatorv1alpha1.ConfigResource
1390
- k8sResourceList = append (k8sResourceList , service .Resources ... )
1391
-
1392
- merr := & util.MultiErr {}
1393
- var (
1394
- wg sync.WaitGroup
1395
- )
1396
- for _ , k8sRes := range k8sResourceList {
1397
- k8sResShouldBeDeleted := unstructured.Unstructured {
1398
- Object : map [string ]interface {}{
1399
- "apiVersion" : k8sRes .APIVersion ,
1400
- "kind" : k8sRes .Kind ,
1401
- "metadata" : map [string ]interface {}{
1402
- "name" : k8sRes .Name ,
1403
- },
1404
- },
1405
- }
1406
- k8sNamespace := namespace
1407
- if k8sRes .Namespace != "" {
1408
- k8sNamespace = k8sRes .Namespace
1409
- }
1410
-
1411
- wg .Add (1 )
1412
- go func () {
1413
- defer wg .Done ()
1414
- if err := r .deleteK8sResource (ctx , k8sResShouldBeDeleted , k8sNamespace ); err != nil {
1415
- r .Mutex .Lock ()
1416
- defer r .Mutex .Unlock ()
1417
- merr .Add (err )
1418
- return
1419
- }
1420
- }()
1421
- }
1422
- wg .Wait ()
1423
-
1424
- if len (merr .Errors ) != 0 {
1425
- return merr
1426
- }
1427
- return nil
1428
- }
1429
-
1430
1437
func (r * Reconciler ) checkResAuth (ctx context.Context , verbs []string , k8sResTemplate unstructured.Unstructured ) bool {
1431
1438
kind := k8sResTemplate .GetKind ()
1432
1439
apiversion := k8sResTemplate .GetAPIVersion ()
0 commit comments