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

🏃 Follow-up to condition patch utils #3109

Merged
Merged
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
2 changes: 0 additions & 2 deletions util/conditions/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (p Patch) Apply(source Setter) error {
}
// otherwise, the source is already as intended.
// NOTE: We are preserving LastTransitionTime from the source in order to avoid altering the existing value.
// XXX Set(source, sourceCondition)
continue
}
// If the condition does not exists on the source, add the new target condition.
Expand All @@ -120,7 +119,6 @@ func (p Patch) Apply(source Setter) error {
}
// Otherwise the source is already as intended.
// NOTE: We are preserving LastTransitionTime from the source in order to avoid altering the existing value.
// XXX Set(source, sourceCondition)
continue
}

Expand Down
15 changes: 10 additions & 5 deletions util/conditions/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ limitations under the License.
package conditions

import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// UnstructuredGetter return a Getter object that can read conditions from an Unstructured object.
Expand Down Expand Up @@ -59,19 +57,26 @@ func (c *unstructuredWrapper) GetConditions() clusterv1.Conditions {
}

// SetConditions set the conditions into an Unstructured object.
//
// NOTE: Due to the constraints of JSON-unmarshal, this operation is to be considered best effort.
// In more details:
// - Errors during JSON-unmarshal are ignored and a empty collection list is returned.
// - It's not possible to detect if the object has an empty condition list or if it does not implement conditions;
// in both cases the operation returns an empty slice is returned.
func (c *unstructuredWrapper) SetConditions(conditions clusterv1.Conditions) {
v := make([]interface{}, 0, len(conditions))
for i := range conditions {
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&conditions[i])
if err != nil {
klog.Error(fmt.Sprintf("Failed to convert condition on %s/%s: %v", c.GetObjectKind().GroupVersionKind().Kind, c.GetName(), err))
log.Log.Error(err, "Failed to convert Condition to unstructured map. This error shouldn't have occurred, please file an issue.", "groupVersionKind", c.GroupVersionKind(), "name", c.GetName(), "namespace", c.GetNamespace())
continue
}
v = append(v, m)
}
// unstructured.SetNestedField returns an error only if value cannot be set because one of
// the nesting levels is not a map[string]interface{}; this is not the case so the error should never happen here.
err := unstructured.SetNestedField(c.Unstructured.Object, v, "status", "conditions")
if err != nil {
klog.Error(fmt.Sprintf("Failed to set conditions on %s/%s: %v", c.GetObjectKind().GroupVersionKind().Kind, c.GetName(), err))
log.Log.Error(err, "Failed to set Conditions on unstructured object. This error shouldn't have occurred, please file an issue.", "groupVersionKind", c.GroupVersionKind(), "name", c.GetName(), "namespace", c.GetNamespace())
}
}