Skip to content

Commit

Permalink
refactor manager state update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanxin committed Nov 26, 2024
1 parent afda7cb commit e7718e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
10 changes: 8 additions & 2 deletions internal/declarative/v2/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
ErrManagerInErrorState = errors.New("manager is in error state")
ErrResourceSyncDiffInSameOCILayer = errors.New("resource syncTarget diff detected but in " +
"same oci layer, prevent sync resource to be deleted")
ErrStateRequireUpdate = errors.New("manifest state requires update")
)

const (
Expand Down Expand Up @@ -334,15 +335,20 @@ func (r *Reconciler) syncManifestState(ctx context.Context, skrClient Client, ma
}
status.ConfirmModuleCRCondition(manifest)
}
return status.SetManifestState(manifest, shared.StateDeleting)
if status.RequireManifestStateUpdateAfterSyncResource(manifest, shared.StateDeleting) {
return ErrStateRequireUpdate
}
}

managerState, err := r.checkManagerState(ctx, skrClient, target)
if err != nil {
manifest.SetStatus(manifestStatus.WithState(shared.StateError).WithErr(err))
return err
}
return status.SetManifestState(manifest, managerState)
if status.RequireManifestStateUpdateAfterSyncResource(manifest, managerState) {
return ErrStateRequireUpdate
}
return nil
}

func (r *Reconciler) RecordModuleCRWarningCondition(manifest *v1beta2.Manifest) error {
Expand Down
11 changes: 11 additions & 0 deletions internal/manifest/status/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ func ConfirmResourcesCondition(manifest *v1beta2.Manifest) {
manifest.SetStatus(status.WithOperation(resourceCondition.Message))
}
}

func ConfirmInstallationCondition(manifest *v1beta2.Manifest) {
status := manifest.GetStatus()
installationCondition := initInstallationCondition(manifest)

if !meta.IsStatusConditionTrue(status.Conditions, installationCondition.Type) {
installationCondition.Status = apimetav1.ConditionTrue
meta.SetStatusCondition(&status.Conditions, installationCondition)
manifest.SetStatus(status.WithOperation(installationCondition.Message))
}
}
38 changes: 16 additions & 22 deletions internal/manifest/status/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@ package status
import (
"errors"

Check failure on line 4 in internal/manifest/status/state.go

View workflow job for this annotation

GitHub Actions / Run 'make test'

"errors" imported and not used

Check failure on line 4 in internal/manifest/status/state.go

View workflow job for this annotation

GitHub Actions / lint

"errors" imported and not used) (typecheck)

Check failure on line 4 in internal/manifest/status/state.go

View workflow job for this annotation

GitHub Actions / lint

"errors" imported and not used (typecheck)

Check failure on line 4 in internal/manifest/status/state.go

View workflow job for this annotation

GitHub Actions / lint

"errors" imported and not used) (typecheck)

"k8s.io/apimachinery/pkg/api/meta"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
)

const waitingForResourcesMsg = "waiting for resources to become ready"

var ErrInstallationConditionRequiresUpdate = errors.New("installation condition needs an update")

func SetManifestState(manifest *v1beta2.Manifest, newState shared.State) error {
status := manifest.GetStatus()

if newState == shared.StateProcessing {
manifest.SetStatus(status.WithState(shared.StateProcessing).WithOperation(waitingForResourcesMsg))
return ErrInstallationConditionRequiresUpdate
}

installationCondition := initInstallationCondition(manifest)
if newState != status.State || !meta.IsStatusConditionTrue(status.Conditions, installationCondition.Type) {
installationCondition.Status = apimetav1.ConditionTrue
meta.SetStatusCondition(&status.Conditions, installationCondition)
const (
waitingForResourcesMsg = "waiting for resources to become ready"
resourcesAreReadyMsg = "resources are ready"
)

manifest.SetStatus(status.WithState(newState).WithOperation(installationCondition.Message))
return ErrInstallationConditionRequiresUpdate
func RequireManifestStateUpdateAfterSyncResource(manifest *v1beta2.Manifest, newState shared.State) bool {
manifestStatus := manifest.GetStatus()

if newState != manifestStatus.State {
if newState == shared.StateProcessing || newState == shared.StateError {
manifest.SetStatus(manifestStatus.WithState(newState).WithOperation(waitingForResourcesMsg))
} else {
ConfirmInstallationCondition(manifest)
manifest.SetStatus(manifestStatus.WithState(newState).WithOperation(resourcesAreReadyMsg))
}
return true
}

return nil
return false
}

0 comments on commit e7718e0

Please sign in to comment.