fix: change failed_when execute time (#2586)

* fix: change failed_when execute time

Signed-off-by: joyceliu <joyceliu@yunify.com>

* doc: move pkg/util to pkg/controllers/util

Signed-off-by: joyceliu <joyceliu@yunify.com>

---------

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-05-22 18:00:22 +08:00 committed by GitHub
parent 4c9256cae4
commit d979c92066
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 8 additions and 18 deletions

View File

@ -38,7 +38,7 @@ import (
"github.com/kubesphere/kubekey/v4/cmd/controller-manager/app/options"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/util"
"github.com/kubesphere/kubekey/v4/pkg/controllers/util"
)
const (

View File

@ -19,7 +19,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/util"
"github.com/kubesphere/kubekey/v4/pkg/controllers/util"
)
const (

View File

@ -26,7 +26,7 @@ import (
"github.com/kubesphere/kubekey/v4/cmd/controller-manager/app/options"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/util"
"github.com/kubesphere/kubekey/v4/pkg/controllers/util"
)
// InventoryReconciler reconciles a Inventory object

View File

@ -28,8 +28,8 @@ import (
"github.com/kubesphere/kubekey/v4/cmd/controller-manager/app/options"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/controllers/util"
"github.com/kubesphere/kubekey/v4/pkg/converter"
"github.com/kubesphere/kubekey/v4/pkg/util"
)
// KKClusterReconciler reconciles a KKCluster object

View File

@ -31,7 +31,7 @@ import (
"github.com/kubesphere/kubekey/v4/cmd/controller-manager/app/options"
_const "github.com/kubesphere/kubekey/v4/pkg/const"
"github.com/kubesphere/kubekey/v4/pkg/util"
"github.com/kubesphere/kubekey/v4/pkg/controllers/util"
)
// KKMachineReconciler reconciles a KKMachine object.

View File

@ -325,11 +325,6 @@ func (e *taskExecutor) executeModule(ctx context.Context, task *kkcorev1alpha1.T
return
}
// Check if task should fail based on failed_when conditions
if skip := e.dealFailedWhen(had, stdout, stderr); skip {
return
}
// Execute the actual module with the prepared context
*stdout, *stderr = modules.FindModule(task.Spec.Module.Name)(ctx, modules.ExecOptions{
Args: e.task.Spec.Module.Args,
@ -339,6 +334,8 @@ func (e *taskExecutor) executeModule(ctx context.Context, task *kkcorev1alpha1.T
Playbook: *e.playbook,
LogOutput: e.logOutput,
})
e.dealFailedWhen(had, stderr)
}
// dealLoop parses the loop specification into a slice of items to iterate over.
@ -380,24 +377,17 @@ func (e *taskExecutor) dealWhen(had map[string]any, stdout, stderr *string) bool
// dealFailedWhen evaluates the "failed_when" conditions for a task to determine if it should fail.
// Returns true if the task should be marked as failed, false if it should proceed.
func (e *taskExecutor) dealFailedWhen(had map[string]any, stdout, stderr *string) bool {
func (e *taskExecutor) dealFailedWhen(had map[string]any, stderr *string) {
if len(e.task.Spec.FailedWhen) > 0 {
ok, err := tmpl.ParseBool(had, e.task.Spec.FailedWhen...)
if err != nil {
klog.V(5).ErrorS(err, "validate failed_when condition error", "task", ctrlclient.ObjectKeyFromObject(e.task))
*stderr = fmt.Sprintf("parse failed_when condition error: %v", err)
return true
}
if ok {
*stdout = modules.StdoutFalse
*stderr = "reach failed_when, failed"
return true
}
}
return false
}
// dealRegister handles storing task output in a registered variable if specified.