Skip to content

Commit d92bc1b

Browse files
author
Xin Li
authored
enhance fetch csv method for delete (IBM#507)
1 parent c1eacb3 commit d92bc1b

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

controllers/common/fetch.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ package common
1818

1919
import (
2020
"context"
21+
"strings"
22+
"time"
2123

2224
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2325
"k8s.io/apimachinery/pkg/api/errors"
26+
"k8s.io/apimachinery/pkg/runtime"
2427
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/apimachinery/pkg/util/wait"
2529
"k8s.io/klog"
2630
"sigs.k8s.io/controller-runtime/pkg/client"
2731

@@ -166,6 +170,70 @@ func FetchClusterServiceVersion(c client.Client, sub *olmv1alpha1.Subscription)
166170
return csv, nil
167171
}
168172

173+
// FetchClusterServiceVersionForDelete fetch the ClusterServiceVersion from the subscription for delete
174+
func FetchClusterServiceVersionForDelete(c client.Client, sub *olmv1alpha1.Subscription) (*olmv1alpha1.ClusterServiceVersion, error) {
175+
csvName := ""
176+
csvNamespace := sub.Namespace
177+
178+
if sub.Status.InstallPlanRef != nil {
179+
ip, err := FetchInstallPlan(c, sub.Status.InstallPlanRef.Name, sub.Status.InstallPlanRef.Namespace)
180+
if err != nil {
181+
return nil, err
182+
}
183+
for _, step := range ip.Status.Plan {
184+
if step.Resource.Kind == "ClusterServiceVersion" && strings.HasPrefix(step.Resource.Name, sub.Name) {
185+
csvName = step.Resource.Name
186+
}
187+
}
188+
} else {
189+
klog.Warningf("The Installplan for Subscription %s is not ready. Will check it again", sub.Name)
190+
return nil, nil
191+
}
192+
193+
if csvName == "" {
194+
return nil, nil
195+
}
196+
197+
csv := &olmv1alpha1.ClusterServiceVersion{}
198+
csvKey := types.NamespacedName{
199+
Name: csvName,
200+
Namespace: csvNamespace,
201+
}
202+
if err := getObjectWithRetry(c, csvKey, csv); err != nil {
203+
return nil, err
204+
}
205+
return csv, nil
206+
}
207+
208+
func FetchInstallPlan(c client.Client, name, namespace string) (*olmv1alpha1.InstallPlan, error) {
209+
installPlan := &olmv1alpha1.InstallPlan{}
210+
installPlanKey := types.NamespacedName{
211+
Namespace: namespace,
212+
Name: name,
213+
}
214+
if err := getObjectWithRetry(c, installPlanKey, installPlan); err != nil {
215+
return nil, err
216+
}
217+
return installPlan, nil
218+
}
219+
220+
// Get object with retry
221+
func getObjectWithRetry(c client.Client, key types.NamespacedName, obj runtime.Object) error {
222+
if err := wait.PollImmediate(time.Second*5, time.Second*15, func() (bool, error) {
223+
if err := c.Get(context.TODO(), key, obj); err != nil {
224+
if errors.IsNotFound(err) {
225+
return false, nil
226+
}
227+
klog.Errorf("failed to get %s with %s: %v", obj.GetObjectKind(), key.String(), err)
228+
return false, err
229+
}
230+
return true, nil
231+
}); err != nil {
232+
return err
233+
}
234+
return nil
235+
}
236+
169237
// GetOperatorNamespace returns the operator namespace based on the install mode
170238
func GetOperatorNamespace(installMode, namespace string) string {
171239
if installMode == apiv1alpha1.InstallModeCluster {

controllers/operandrequest_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
// +kubebuilder:docs-gen:collapse=Imports
3434

35-
var _ = Describe("OperandRegistry controller", func() {
35+
var _ = Describe("OperandRequest controller", func() {
3636
const (
3737
name = "ibm-cloudpak-name"
3838
namespace = "ibm-cloudpak"
@@ -97,7 +97,7 @@ var _ = Describe("OperandRegistry controller", func() {
9797

9898
Expect(k8sClient.Create(ctx, request)).Should(Succeed())
9999

100-
By("Checking status of the OperandRegquest")
100+
By("Checking status of the OperandRequest")
101101
Eventually(func() operatorv1alpha1.ClusterPhase {
102102
requestInstance := &operatorv1alpha1.OperandRequest{}
103103
Expect(k8sClient.Get(ctx, requestKey, requestInstance)).Should(Succeed())

controllers/reconcile_operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (r *OperandRequestReconciler) deleteSubscription(operandName string, reques
193193
return nil
194194
}
195195

196-
csv, err := fetch.FetchClusterServiceVersion(r.Client, sub)
196+
csv, err := fetch.FetchClusterServiceVersionForDelete(r.Client, sub)
197197
// If can't get CSV, requeue the request
198198
if err != nil {
199199
return err

controllers/testutil/test_data.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,32 @@ func InstallPlanStatus() olmv1alpha1.InstallPlanStatus {
287287
return olmv1alpha1.InstallPlanStatus{
288288
Phase: olmv1alpha1.InstallPlanPhaseComplete,
289289
CatalogSources: []string{},
290+
Plan: []*olmv1alpha1.Step{
291+
{
292+
Resolving: "etcd-csv.v0.0.1",
293+
Resource: olmv1alpha1.StepResource{
294+
CatalogSource: "community-operators",
295+
CatalogSourceNamespace: "openshift-marketplace",
296+
Group: "operators.coreos.com",
297+
Version: "v1alpha1",
298+
Kind: "ClusterServiceVersion",
299+
Name: "etcd-csv.v0.0.1",
300+
},
301+
Status: olmv1alpha1.StepStatusPresent,
302+
},
303+
{
304+
Resolving: "jenkins-csv.v0.0.1",
305+
Resource: olmv1alpha1.StepResource{
306+
CatalogSource: "community-operators",
307+
CatalogSourceNamespace: "openshift-marketplace",
308+
Group: "operators.coreos.com",
309+
Version: "v1alpha1",
310+
Kind: "ClusterServiceVersion",
311+
Name: "jenkins-csv.v0.0.1",
312+
},
313+
Status: olmv1alpha1.StepStatusCreated,
314+
},
315+
},
290316
}
291317
}
292318

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/onsi/gomega v1.9.0
1010
github.com/operator-framework/api v0.3.10
1111
k8s.io/api v0.18.2
12+
k8s.io/apiextensions-apiserver v0.18.2
1213
k8s.io/apimachinery v0.18.2
1314
k8s.io/client-go v0.18.2
1415
k8s.io/klog v1.0.0

0 commit comments

Comments
 (0)