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 05618b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .github/scripts/release/generate_moduletemplate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ cat template.yaml
echo "ModuleTemplate created successfully"

rm -f module-config-for-e2e.yaml
rm -f template.yaml
rm -f template-operator.yaml
rm -f template.yaml
rm -f default-sample-cr.yaml
echo "Temporary files removed successfully"
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))
}
}
40 changes: 16 additions & 24 deletions internal/manifest/status/state.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
package status

import (
"errors"

"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 05618b9

Please sign in to comment.