From ca5a050e7d4d92d8cb0fc90d4578c866bc7db36d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Appr=C3=A9derisse=20Benjamin?= Date: Fri, 26 Feb 2021 09:57:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Abort=20-=20fix=20workflow=20run?= =?UTF-8?q?ning=20forever=20(#6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close https://github.com/kintoproj/kinto-core/issues/14 --- kinto-build/internal/build/argo/argo.go | 30 ++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/kinto-build/internal/build/argo/argo.go b/kinto-build/internal/build/argo/argo.go index bea065d..3a6f6bc 100644 --- a/kinto-build/internal/build/argo/argo.go +++ b/kinto-build/internal/build/argo/argo.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1" "github.com/argoproj/argo/pkg/client/clientset/versioned" - "github.com/argoproj/argo/workflow/util" + argoUtil "github.com/argoproj/argo/workflow/util" "github.com/kintohub/utils-go/klog" utilsGoServer "github.com/kintohub/utils-go/server" "github.com/kintoproj/kinto-build/internal/build" @@ -13,6 +13,7 @@ import ( "github.com/minio/minio-go/v6" "golang.org/x/net/context" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -315,14 +316,33 @@ func enrichTemplatesWithNodePoolInfo(templates []v1alpha1.Template, selectorValu } func (c *BuildClient) AbortRelease(ctx context.Context, buildId string) *utilsGoServer.Error { + // we check if workflow exists and if it is already finished + wf, err := c.argoClient.ArgoprojV1alpha1().Workflows(config.ArgoWorkflowNamespace).Get(buildId, v1.GetOptions{}) + + if err != nil { + if errors.IsNotFound(err) { + klog.Infof("Abort - Workflow not found %s", buildId) + return nil + } else { + klog.ErrorfWithErr(err, "Abort - Error getting workflow %s", buildId) + return utilsGoServer.NewInternalErrorWithErr(fmt.Sprintf("Abort - Error getting workflow %s", buildId), err) + } + } + + // TODO we must send back the status to the core to set the right state in the config map + if wf.Status.FinishTime() != nil { + klog.Infof("Abort - Workflow %s already finished with status %s", buildId, wf.Status.Phase) + return nil + } + + // stop workflow // https://github.com/argoproj/argo/blob/release-2.8/workflow/util/util.go#L808-L835 - err := util.StopWorkflow( + err = argoUtil.StopWorkflow( c.argoClient.ArgoprojV1alpha1().Workflows(config.ArgoWorkflowNamespace), nil, buildId, "", "") if err != nil { - klog.ErrorfWithErr(err, "Error stopping workflow %s.%s", buildId, config.ArgoWorkflowNamespace) - return utilsGoServer.NewInternalErrorWithErr( - fmt.Sprintf("Error stopping workflow %s.%s", buildId, config.ArgoWorkflowNamespace), err) + klog.ErrorfWithErr(err, "Abort - Error stopping workflow %s", buildId) + return utilsGoServer.NewInternalErrorWithErr(fmt.Sprintf("Abort - Error stopping workflow %s", buildId), err) } return nil