7
7
appsv1 "k8s.io/api/apps/v1"
8
8
rbac "k8s.io/api/rbac/v1"
9
9
10
+ "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
10
11
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
11
12
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
12
13
)
@@ -113,14 +114,8 @@ func (i *StrategyDeploymentInstaller) Install(s Strategy) error {
113
114
return err
114
115
}
115
116
116
- if i .previousStrategy != nil {
117
- previous , ok := i .previousStrategy .(* StrategyDetailsDeployment )
118
- if ! ok {
119
- return fmt .Errorf ("couldn't parse old install %s strategy with deployment installer" , previous .GetStrategyName ())
120
- }
121
- return i .cleanupPrevious (strategy , previous )
122
- }
123
- return nil
117
+ // Clean up orphaned deployments
118
+ return i .cleanupOrphanedDeployments (strategy .DeploymentSpecs )
124
119
}
125
120
126
121
// CheckInstalled can return nil (installed), or errors
@@ -144,9 +139,15 @@ func (i *StrategyDeploymentInstaller) checkForDeployments(deploymentSpecs []Stra
144
139
depNames = append (depNames , dep .Name )
145
140
}
146
141
147
- existingDeployments , err := i .strategyClient .FindAnyDeploymentsMatchingNames (depNames )
142
+ // Check the owner is a CSV
143
+ csv , ok := i .owner .(* v1alpha1.ClusterServiceVersion )
144
+ if ! ok {
145
+ return StrategyError {Reason : StrategyErrReasonComponentMissing , Message : fmt .Sprintf ("owner %s is not a CSV" , i .owner .GetName ())}
146
+ }
147
+
148
+ existingDeployments , err := i .strategyClient .FindAnyDeploymentsMatchingLabels (ownerutil .CSVOwnerSelector (csv ))
148
149
if err != nil {
149
- return StrategyError {Reason : StrategyErrReasonComponentMissing , Message : fmt .Sprintf ("error querying for %s: %s" , depNames , err )}
150
+ return StrategyError {Reason : StrategyErrReasonComponentMissing , Message : fmt .Sprintf ("error querying existing deployments for CSV %s: %s" , csv . GetName () , err )}
150
151
}
151
152
152
153
// compare deployments to see if any need to be created/updated
@@ -181,3 +182,39 @@ func (i *StrategyDeploymentInstaller) checkForDeployments(deploymentSpecs []Stra
181
182
}
182
183
return nil
183
184
}
185
+
186
+ // Clean up orphaned deployments after reinstalling deployments process
187
+ func (i * StrategyDeploymentInstaller ) cleanupOrphanedDeployments (deploymentSpecs []StrategyDeploymentSpec ) error {
188
+ // Map of deployments
189
+ depNames := map [string ]string {}
190
+ for _ , dep := range deploymentSpecs {
191
+ depNames [dep .Name ] = dep .Name
192
+ }
193
+
194
+ // Check the owner is a CSV
195
+ csv , ok := i .owner .(* v1alpha1.ClusterServiceVersion )
196
+ if ! ok {
197
+ return fmt .Errorf ("owner %s is not a CSV" , i .owner .GetName ())
198
+ }
199
+
200
+ // Get existing deployments in CSV's namespace and owned by CSV
201
+ existingDeployments , err := i .strategyClient .FindAnyDeploymentsMatchingLabels (ownerutil .CSVOwnerSelector (csv ))
202
+ if err != nil {
203
+ return err
204
+ }
205
+
206
+ // compare existing deployments to deployments in CSV's spec to see if any need to be deleted
207
+ for _ , d := range existingDeployments {
208
+ if _ , exists := depNames [d .GetName ()]; ! exists {
209
+ if ownerutil .IsOwnedBy (d , i .owner ) {
210
+ log .Infof ("found an orphaned deployment %s in namespace %s" , d .GetName (), i .owner .GetNamespace ())
211
+ if err := i .strategyClient .DeleteDeployment (d .GetName ()); err != nil {
212
+ log .Warnf ("error cleaning up deployment %s" , d .GetName ())
213
+ return err
214
+ }
215
+ }
216
+ }
217
+ }
218
+
219
+ return nil
220
+ }
0 commit comments