@@ -947,6 +947,10 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
947
947
}
948
948
}
949
949
950
+ // Clean up deployments in case some become orphaned
951
+ if cleanupErr := a .cleanupOrphanedDeployments (logger , out ); cleanupErr == nil {
952
+ logger .WithField ("strategy" , out .Spec .InstallStrategy .StrategyName ).Infof ("clean up deployments successful" )
953
+ }
950
954
case v1alpha1 .CSVPhaseSucceeded :
951
955
installer , strategy , _ := a .parseStrategiesAndUpdateStatus (out )
952
956
if strategy == nil {
@@ -1447,6 +1451,7 @@ func (a *Operator) ensureDeploymentAnnotations(logger *logrus.Entry, csv *v1alph
1447
1451
return utilerrors .NewAggregate (updateErrs )
1448
1452
}
1449
1453
1454
+ << << << < HEAD
1450
1455
// ensureLabels merges a label set with a CSV's labels and attempts to update the CSV if the merged set differs from the CSV's original labels.
1451
1456
func (a * Operator ) ensureLabels (in * v1alpha1.ClusterServiceVersion , labelSets ... labels.Set ) (* v1alpha1.ClusterServiceVersion , error ) {
1452
1457
csvLabelSet := labels .Set (in .GetLabels ())
@@ -1464,4 +1469,44 @@ func (a *Operator) ensureLabels(in *v1alpha1.ClusterServiceVersion, labelSets ..
1464
1469
out .SetLabels (merged )
1465
1470
out , err := a .client .OperatorsV1alpha1 ().ClusterServiceVersions (out .GetNamespace ()).Update (out )
1466
1471
return out , err
1472
+ == == == =
1473
+ // Clean up orphaned deployments after reinstalling deployments process
1474
+ func (a * Operator ) cleanupOrphanedDeployments (logger * logrus .Entry , csv * v1alpha1 .ClusterServiceVersion ) error {
1475
+ // Extract the InstallStrategy for the deployment
1476
+ strategy , err := a .resolver .UnmarshalStrategy (csv .Spec .InstallStrategy )
1477
+ if err != nil {
1478
+ logger .Warn ("could not parse install strategy while cleaning up CSV deployment" )
1479
+ return nil
1480
+ }
1481
+
1482
+ // Assume the strategy is for a deployment
1483
+ strategyDetailsDeployment , ok := strategy .(* install.StrategyDetailsDeployment )
1484
+ if ! ok {
1485
+ logger .Warnf ("could not cast install strategy as type %T" , strategyDetailsDeployment )
1486
+ return nil
1487
+ }
1488
+
1489
+ depNames := map [string ]string {}
1490
+ for _ , dep := range strategyDetailsDeployment .DeploymentSpecs {
1491
+ depNames [dep .Name ] = dep .Name
1492
+ }
1493
+ // Get existing deployments in CSV's namespace and owned by CSV
1494
+ existingDeployments , err := a .lister .AppsV1 ().DeploymentLister ().Deployments (csv .GetNamespace ()).List (ownerutil .CSVOwnerSelector (csv ))
1495
+ if err != nil {
1496
+ return err
1497
+ }
1498
+
1499
+ // compare existing deployments to deployments in CSV's spec to see if any need to be deleted
1500
+ for _ , d := range existingDeployments {
1501
+ if _ , exists := depNames [d .GetName ()]; ! exists {
1502
+ logger .Infof ("found an orphaned deployment %s in namespace %s" , d .GetName (), csv .GetNamespace ())
1503
+ if err := a .OpClient .DeleteDeployment (csv .GetNamespace (), d .GetName (), & metav1.DeleteOptions {}); err != nil {
1504
+ logger .Warnf ("error cleaning up deployment %s" , d .GetName ())
1505
+ return err
1506
+ }
1507
+ }
1508
+ }
1509
+
1510
+ return nil
1511
+ >> >> >> > fix (deployment ): Clean up orphaned deployments
1467
1512
}
0 commit comments