From 5edd9e423f3dbb18494a58eedfce02a245b7353a Mon Sep 17 00:00:00 2001 From: Ilya Lesikov Date: Fri, 6 Dec 2024 12:42:28 +0300 Subject: [PATCH] fix: initial resource status should be "unknown" instead of "created" Signed-off-by: Ilya Lesikov --- .../dyntracker/dynamic_absence_tracker.go | 6 ++++++ .../dyntracker/dynamic_presence_tracker.go | 12 +++++++++++- .../dyntracker/dynamic_readiness_tracker.go | 18 ++++++++++++++++++ .../dyntracker/statestore/resource_state.go | 2 +- .../dyntracker/statestore/resource_status.go | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/trackers/dyntracker/dynamic_absence_tracker.go b/pkg/trackers/dyntracker/dynamic_absence_tracker.go index 73578e8..ad5dece 100644 --- a/pkg/trackers/dyntracker/dynamic_absence_tracker.go +++ b/pkg/trackers/dyntracker/dynamic_absence_tracker.go @@ -98,6 +98,12 @@ func (t *DynamicAbsenceTracker) Track(ctx context.Context) error { return false, fmt.Errorf("get resource %q: %w", resourceHumanID, err) } + t.taskState.RWTransaction(func(ats *statestore.AbsenceTaskState) { + ats.ResourceState().RWTransaction(func(rs *statestore.ResourceState) { + rs.SetStatus(statestore.ResourceStatusCreated) + }) + }) + return false, nil }); err != nil { return fmt.Errorf("poll resource %q: %w", resourceHumanID, err) diff --git a/pkg/trackers/dyntracker/dynamic_presence_tracker.go b/pkg/trackers/dyntracker/dynamic_presence_tracker.go index 4c0d991..3b119c5 100644 --- a/pkg/trackers/dyntracker/dynamic_presence_tracker.go +++ b/pkg/trackers/dyntracker/dynamic_presence_tracker.go @@ -93,7 +93,17 @@ func (t *DynamicPresenceTracker) Track(ctx context.Context) error { if err := wait.PollImmediate(t.pollPeriod, t.timeout, func() (bool, error) { if _, err := resourceClient.Get(ctx, name, metav1.GetOptions{}); err != nil { - if apierrors.IsResourceExpired(err) || apierrors.IsGone(err) || err == io.EOF || err == io.ErrUnexpectedEOF || apierrors.IsNotFound(err) { + if apierrors.IsResourceExpired(err) || apierrors.IsGone(err) || err == io.EOF || err == io.ErrUnexpectedEOF { + return false, nil + } + + if apierrors.IsNotFound(err) { + t.taskState.RWTransaction(func(pts *statestore.PresenceTaskState) { + pts.ResourceState().RWTransaction(func(rs *statestore.ResourceState) { + rs.SetStatus(statestore.ResourceStatusDeleted) + }) + }) + return false, nil } diff --git a/pkg/trackers/dyntracker/dynamic_readiness_tracker.go b/pkg/trackers/dyntracker/dynamic_readiness_tracker.go index 03bdaa4..09b9c61 100644 --- a/pkg/trackers/dyntracker/dynamic_readiness_tracker.go +++ b/pkg/trackers/dyntracker/dynamic_readiness_tracker.go @@ -237,6 +237,7 @@ func (t *DynamicReadinessTracker) trackDeployment(ctx context.Context, tracker * t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handlePodsFromDeploymentStatus(&status, ts) t.handleDeploymentStatus(&status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -370,6 +371,7 @@ func (t *DynamicReadinessTracker) trackStatefulSet(ctx context.Context, tracker t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handlePodsFromStatefulSetStatus(&status, ts) t.handleStatefulSetStatus(&status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -489,6 +491,7 @@ func (t *DynamicReadinessTracker) trackDaemonSet(ctx context.Context, tracker *d t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handlePodsFromDaemonSetStatus(&status, ts) t.handleDaemonSetStatus(&status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -608,6 +611,7 @@ func (t *DynamicReadinessTracker) trackJob(ctx context.Context, tracker *job.Tra t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handlePodsFromJobStatus(&status, ts) t.handleJobStatus(&status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -726,6 +730,7 @@ func (t *DynamicReadinessTracker) trackCanary(ctx context.Context, tracker *cana ) t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handleCanaryStatus(&status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -811,6 +816,7 @@ func (t *DynamicReadinessTracker) trackGeneric(ctx context.Context, tracker *gen ) t.taskState.RWTransaction(func(ts *statestore.ReadinessTaskState) { t.handleGenericResourceStatus(status, ts) + t.setRootResourceCreated(ts) abort, abortErr = t.handleTaskStateStatus(ts) }) @@ -1273,6 +1279,18 @@ func (t *DynamicReadinessTracker) handleTaskStateStatus(taskState *statestore.Re return abort, abortErr } +func (t *DynamicReadinessTracker) setRootResourceCreated(taskState *statestore.ReadinessTaskState) { + rss := taskState.ResourceStates() + + if len(rss) == 0 { + return + } + + rss[0].RWTransaction(func(rs *statestore.ResourceState) { + rs.SetStatus(statestore.ResourceStatusCreated) + }) +} + func setReplicasAttribute(resourceState *statestore.ResourceState, replicas int) { attributes := resourceState.Attributes() diff --git a/pkg/trackers/dyntracker/statestore/resource_state.go b/pkg/trackers/dyntracker/statestore/resource_state.go index c5b88c4..d2acfb1 100644 --- a/pkg/trackers/dyntracker/statestore/resource_state.go +++ b/pkg/trackers/dyntracker/statestore/resource_state.go @@ -24,7 +24,7 @@ func NewResourceState(name, namespace string, groupVersionKind schema.GroupVersi name: name, namespace: namespace, groupVersionKind: groupVersionKind, - status: ResourceStatusCreated, + status: ResourceStatusUnknown, errors: make(map[string][]*Error), } } diff --git a/pkg/trackers/dyntracker/statestore/resource_status.go b/pkg/trackers/dyntracker/statestore/resource_status.go index 178b6ec..14c201c 100644 --- a/pkg/trackers/dyntracker/statestore/resource_status.go +++ b/pkg/trackers/dyntracker/statestore/resource_status.go @@ -3,6 +3,7 @@ package statestore type ResourceStatus string const ( + ResourceStatusUnknown ResourceStatus = "unknown" ResourceStatusReady ResourceStatus = "ready" ResourceStatusCreated ResourceStatus = "created" ResourceStatusDeleted ResourceStatus = "deleted"