From a1553bdf0bb383381ce476bfe2ad5b16c68057a1 Mon Sep 17 00:00:00 2001 From: Justin SB Date: Sat, 15 Aug 2020 20:15:28 -0400 Subject: [PATCH] TaskDependentResource: support preview when the task isn't ready This is needed because otherwise if we try to diff a computed field, we can't read the value. --- upup/pkg/fi/changes.go | 5 +++++ upup/pkg/fi/resources.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/upup/pkg/fi/changes.go b/upup/pkg/fi/changes.go index c26c737adb221..e876a16cc4dc7 100644 --- a/upup/pkg/fi/changes.go +++ b/upup/pkg/fi/changes.go @@ -116,6 +116,11 @@ func equalFieldValues(a, e reflect.Value) bool { if ok && (e.Kind() == reflect.Ptr || e.Kind() == reflect.Interface) && !e.IsNil() { eResource, ok := e.Interface().(Resource) if ok { + if hasIsReady, ok := eResource.(HasIsReady); ok { + if !hasIsReady.IsReady() { + return false + } + } same, err := ResourcesMatch(aResource, eResource) if err != nil { klog.Fatalf("error while comparing resources: %v", err) diff --git a/upup/pkg/fi/resources.go b/upup/pkg/fi/resources.go index 5d25e55337484..b304b74f9fe2e 100644 --- a/upup/pkg/fi/resources.go +++ b/upup/pkg/fi/resources.go @@ -30,6 +30,11 @@ type Resource interface { Open() (io.Reader, error) } +// HasIsReady is implemented by Resources that are derived (and thus may not be ready at comparison time) +type HasIsReady interface { + IsReady() bool +} + type TemplateResource interface { Resource Curry(args []string) TemplateResource @@ -264,10 +269,11 @@ type TaskDependentResource struct { var _ Resource = &TaskDependentResource{} var _ HasDependencies = &TaskDependentResource{} +var _ HasIsReady = &TaskDependentResource{} func (r *TaskDependentResource) Open() (io.Reader, error) { if r.Resource == nil { - return nil, fmt.Errorf("resource opened before it is ready") + return nil, fmt.Errorf("resource opened before it is ready (task=%v)", r.Task) } return r.Resource.Open() } @@ -275,3 +281,8 @@ func (r *TaskDependentResource) Open() (io.Reader, error) { func (r *TaskDependentResource) GetDependencies(tasks map[string]Task) []Task { return []Task{r.Task} } + +// IsReady implements HasIsReady::IsReady +func (r *TaskDependentResource) IsReady() bool { + return r.Resource != nil +}