Skip to content

Commit cc60c7d

Browse files
author
Moritz Clasmeier
committed
Moved the code for extracting conditions from unstructured the
updater tests and use Ginkgo assertions instead of production error handling.
1 parent 9e4a650 commit cc60c7d

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

pkg/internal/status/conditions.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25-
"k8s.io/apimachinery/pkg/runtime"
2625
kubeclock "k8s.io/utils/clock"
2726
)
2827

@@ -190,19 +189,3 @@ func (conditions Conditions) MarshalJSON() ([]byte, error) {
190189
})
191190
return json.Marshal(conds)
192191
}
193-
194-
func FromUnstructured(conditionsSlice []interface{}) (Conditions, error) {
195-
conditions := make(Conditions, 0, len(conditionsSlice))
196-
for _, c := range conditionsSlice {
197-
condMap, ok := c.(map[string]interface{})
198-
if !ok {
199-
continue
200-
}
201-
cond := Condition{}
202-
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(condMap, &cond); err != nil {
203-
return nil, err
204-
}
205-
conditions = append(conditions, cond)
206-
}
207-
return conditions, nil
208-
}

pkg/reconciler/internal/updater/updater_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ import (
2828
corev1 "k8s.io/api/core/v1"
2929
apierrors "k8s.io/apimachinery/pkg/api/errors"
3030
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31+
"k8s.io/apimachinery/pkg/runtime"
3132
"k8s.io/apimachinery/pkg/types"
3233
"sigs.k8s.io/controller-runtime/pkg/client"
3334
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3435
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3536
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3637

38+
"github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
3739
pkgStatus "github.com/operator-framework/helm-operator-plugins/pkg/internal/status"
3840
"github.com/operator-framework/helm-operator-plugins/pkg/reconciler/internal/conditions"
3941
)
@@ -230,8 +232,7 @@ var _ = Describe("Updater", func() {
230232
Expect(cl.Get(context.TODO(), types.NamespacedName{Namespace: "testNamespace", Name: "testDeployment"}, obj)).To(Succeed())
231233
objConditionsSlice, _, err := unstructured.NestedSlice(obj.Object, "status", "conditions")
232234
Expect(err).ToNot(HaveOccurred())
233-
objConditions, err := pkgStatus.FromUnstructured(objConditionsSlice)
234-
Expect(err).ToNot(HaveOccurred())
235+
objConditions := conditionsFromUnstructured(objConditionsSlice)
235236
// Verify both status conditions are present.
236237
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("UnknownCondition"))).To(BeTrue())
237238
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("Deployed"))).To(BeTrue())
@@ -417,3 +418,16 @@ var _ = Describe("statusFor", func() {
417418
Expect(status.Conditions.IsTrueFor(pkgStatus.ConditionType("UnknownCondition"))).To(BeTrue())
418419
})
419420
})
421+
422+
func conditionsFromUnstructured(conditionsSlice []interface{}) status.Conditions {
423+
conditions := make(status.Conditions, 0, len(conditionsSlice))
424+
for _, c := range conditionsSlice {
425+
condMap, ok := c.(map[string]interface{})
426+
Expect(ok).To(BeTrue(), "condition is not a map[string]interface{}")
427+
cond := status.Condition{}
428+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(condMap, &cond)
429+
Expect(err).ToNot(HaveOccurred(), "failed to convert status condition from unstructured")
430+
conditions = append(conditions, cond)
431+
}
432+
return conditions
433+
}

0 commit comments

Comments
 (0)