Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status condition from unstructured object #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions status/condition_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package status

import (
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -237,3 +239,58 @@ func (c ConditionSet) findUnhealthyDependents() []Condition {
})
return conditions
}

// unstructuredAdapter is an adapter for the status.Object interface. unstructuredAdapter
// makes the assumption that status conditions are found on status.conditions path.
type unstructuredAdapter struct {
*unstructured.Unstructured
}

// FromUnstructured makes the assumption that the status conditions are found on status.conditions
// path. If they are not found then we return nil.
func FromUnstructured(u *unstructured.Unstructured) (Object, error) {
c, found, err := unstructured.NestedSlice(u.Object, "status", "conditions")
if err != nil || !found {
return nil, fmt.Errorf("unable to determine status conditions: %w", err)
}
// Validating status conditions are of type metav1.Condition. If not, return an error.
for _, condition := range c {
var newCondition Condition
cond := condition.(map[string]interface{})
jsonStr, err := json.Marshal(cond)
if err != nil {
return nil, err
}
if err = json.Unmarshal(jsonStr, &newCondition); err != nil {
return nil, err
}
}
return &unstructuredAdapter{Unstructured: u}, nil
}

func (u *unstructuredAdapter) GetConditions() []Condition {
conditions, _, _ := unstructured.NestedSlice(u.Object, "status", "conditions")
return lo.Map(conditions, func(condition interface{}, _ int) Condition {
var newCondition Condition
cond := condition.(map[string]interface{})
jsonStr, _ := json.Marshal(cond)
json.Unmarshal(jsonStr, &newCondition)
return newCondition
})
}

func (u *unstructuredAdapter) SetConditions(conditions []Condition) {
unstructured.SetNestedSlice(u.Object, lo.Map(conditions, func(condition Condition, _ int) interface{} {
var b map[string]interface{}
j, _ := json.Marshal(&condition)
json.Unmarshal(j, &b)
return b
}), "status", "conditions")
}

func (u *unstructuredAdapter) StatusConditions() ConditionSet {
conditionTypes := lo.Map(u.GetConditions(), func(condition Condition, _ int) string {
return condition.Type
})
return NewReadyConditions(conditionTypes...).For(u)
}
61 changes: 61 additions & 0 deletions status/condition_set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package status_test

import (
"github.com/awslabs/operatorpkg/status"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -105,4 +107,63 @@ var _ = Describe("Conditions", func() {
Expect(testObject.StatusConditions().IsTrue(ConditionTypeFoo, ConditionTypeBaz)).To(BeTrue())
Expect(testObject.StatusConditions().IsTrue(ConditionTypeFoo, ConditionTypeBar, ConditionTypeBaz)).To(BeTrue())
})
It("should return nil when status conditions are not found on the expected path", func() {
testObject := &unstructured.Unstructured{Object: map[string]interface{}{
"invalid": map[string]interface{}{
"invalid": "invalid",
},
}}
obj, err := status.FromUnstructured(testObject)
Expect(obj).To(BeNil())
Expect(err).To(HaveOccurred())
})
It("should validate status condition on unstructured object status is false", func() {
conditionObj, err := status.FromUnstructured(createUnstructuredStatusConditions("False"))
Expect(err).To(BeNil())
Expect(conditionObj).ToNot(BeNil())
Expect(conditionObj.StatusConditions().IsTrue(status.ConditionReady)).To(BeFalse())
})
It("should validate status condition on unstructured object status is true", func() {
conditionObj, err := status.FromUnstructured(createUnstructuredStatusConditions("True"))
Expect(err).To(BeNil())
Expect(conditionObj).ToNot(BeNil())
Expect(conditionObj.StatusConditions().IsTrue(status.ConditionReady)).To(BeTrue())
})
It("should set condition on unstructured object", func() {
testObject := &unstructured.Unstructured{Object: map[string]interface{}{
"status": map[string]interface{}{
"conditions": []interface{}{},
},
}}
conditions := []status.Condition{
{
Type: status.ConditionSucceeded,
Status: metav1.ConditionFalse,
Reason: "reason",
Message: "message",
},
}
conditionObj, err := status.FromUnstructured(testObject)
Expect(err).ToNot(HaveOccurred())
conditionObj.SetConditions(conditions)
c, found, err := unstructured.NestedSlice(testObject.Object, "status", "conditions")
Expect(err).To(BeNil())
Expect(found).To(BeTrue())
Expect(len(c)).To(BeEquivalentTo(1))
})
})

func createUnstructuredStatusConditions(status string) *unstructured.Unstructured {
return &unstructured.Unstructured{Object: map[string]interface{}{
"status": map[string]interface{}{
"conditions": []interface{}{
map[string]interface{}{
"type": "Ready",
"status": status,
"message": "message",
"reason": "reason",
},
},
},
}}
}