From 88ddabdb85facf76865225320351dcfe246259c7 Mon Sep 17 00:00:00 2001 From: Patrick Zhao Date: Wed, 19 Jun 2024 16:51:23 +0800 Subject: [PATCH 1/2] fix service unstart after create env Signed-off-by: Patrick Zhao --- .../aslan/core/environment/service/environment.go | 10 +++++++--- pkg/microservice/aslan/core/environment/service/k8s.go | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/microservice/aslan/core/environment/service/environment.go b/pkg/microservice/aslan/core/environment/service/environment.go index 3549425bd1..a393bad400 100644 --- a/pkg/microservice/aslan/core/environment/service/environment.go +++ b/pkg/microservice/aslan/core/environment/service/environment.go @@ -2115,8 +2115,11 @@ func DeleteProduct(username, envName, productName, requestID string, isDelete bo svcNames = append(svcNames, svcName) } - // @todo fix env already deleted issue - DeleteProductServices("", requestID, envName, productName, svcNames, false, log) + // @todo fix env already deleted issue, may cause service not really deleted in k8s + err = DeleteProductServices("", requestID, envName, productName, svcNames, false, log) + if err != nil { + log.Warnf("DeleteProductServices error: %v", err) + } // Handles environment sharing related operations. err = EnsureDeleteShareEnvConfig(ctx, productInfo, istioClient) @@ -2152,7 +2155,8 @@ func DeleteProduct(username, envName, productName, requestID string, isDelete bo func DeleteProductServices(userName, requestID, envName, productName string, serviceNames []string, production bool, log *zap.SugaredLogger) (err error) { productInfo, err := commonrepo.NewProductColl().Find(&commonrepo.ProductFindOptions{Name: productName, EnvName: envName, Production: util.GetBoolPointer(production)}) if err != nil { - log.Errorf("find product error: %v", err) + err = fmt.Errorf("failed to find product, productName: %s, envName: %s, production: %v, error: %v", productName, envName, production, err) + log.Error(err) return err } if getProjectType(productName) == setting.HelmDeployType { diff --git a/pkg/microservice/aslan/core/environment/service/k8s.go b/pkg/microservice/aslan/core/environment/service/k8s.go index f67950533a..c345110bf7 100644 --- a/pkg/microservice/aslan/core/environment/service/k8s.go +++ b/pkg/microservice/aslan/core/environment/service/k8s.go @@ -687,7 +687,7 @@ func (k *K8sService) createGroup(username string, product *commonmodels.Product, updatableServiceNameList = append(updatableServiceNameList, group[i].ServiceName) go func(svc *commonmodels.ProductService) { defer wg.Done() - items, err := upsertService(prod, svc, svc, !prod.Production, informer, kubeClient, istioClient, k.log) + items, err := upsertService(prod, svc, nil, !prod.Production, informer, kubeClient, istioClient, k.log) if err != nil { lock.Lock() switch e := err.(type) { From 9f7e3dfe61952b60d2e4230b4d8958c7eed63bfe Mon Sep 17 00:00:00 2001 From: Patrick Zhao Date: Tue, 25 Jun 2024 17:00:57 +0800 Subject: [PATCH 2/2] fix restart service and create env image not change Signed-off-by: Patrick Zhao --- .../core/environment/service/environment.go | 14 ++++--- .../aslan/core/environment/service/service.go | 42 +------------------ 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/pkg/microservice/aslan/core/environment/service/environment.go b/pkg/microservice/aslan/core/environment/service/environment.go index a393bad400..70dd071917 100644 --- a/pkg/microservice/aslan/core/environment/service/environment.go +++ b/pkg/microservice/aslan/core/environment/service/environment.go @@ -674,10 +674,17 @@ func updateProductImpl(updateRevisionSvcs []string, deployStrategy map[string]st return } + curEnv, err := commonrepo.NewProductColl().Find(&commonrepo.ProductFindOptions{Name: productName, EnvName: envName}) + if err != nil { + log.Errorf("Failed to find current env %s/%s, error: %v", productName, envName, err) + service.Error = err.Error() + return + } + items, errUpsertService := upsertService( updateProd, service, - existedProd.GetServiceMap()[service.ServiceName], + curEnv.GetServiceMap()[service.ServiceName], !updateProd.Production, inf, kubeClient, istioClient, log) if errUpsertService != nil { service.Error = errUpsertService.Error() @@ -2499,11 +2506,6 @@ func upsertService(env *commonmodels.Product, newService *commonmodels.ProductSe return nil, nil } - // for newService not deployed in envs, we should not replace containers in case variables exist in containers - if prevSvc == nil { - newService.Containers = nil - } - parsedYaml, err := kube.RenderEnvService(env, newService.GetServiceRender(), newService) if err != nil { log.Errorf("Failed to render newService %s, error: %v", newService.ServiceName, err) diff --git a/pkg/microservice/aslan/core/environment/service/service.go b/pkg/microservice/aslan/core/environment/service/service.go index 7aa2a1eb45..8fd38a772a 100644 --- a/pkg/microservice/aslan/core/environment/service/service.go +++ b/pkg/microservice/aslan/core/environment/service/service.go @@ -22,7 +22,6 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" "helm.sh/helm/v3/pkg/releaseutil" - versionedclient "istio.io/client-go/pkg/clientset/versioned" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" @@ -36,7 +35,6 @@ import ( "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/kube" "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/service/repository" commontypes "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/types" - commonutil "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/util" "github.com/koderover/zadig/v2/pkg/setting" kubeclient "github.com/koderover/zadig/v2/pkg/shared/kube/client" internalresource "github.com/koderover/zadig/v2/pkg/shared/kube/resource" @@ -443,25 +441,6 @@ func RestartService(envName string, args *SvcOptArgs, production bool, log *zap. return err } - restConfig, err := kubeclient.GetRESTConfig(config.HubServerAddress(), productObj.ClusterID) - if err != nil { - return err - } - - istioClient, err := versionedclient.NewForConfig(restConfig) - if err != nil { - return err - } - - cls, err := kubeclient.GetKubeClientSet(config.HubServerAddress(), productObj.ClusterID) - if err != nil { - return e.ErrCreateEnv.AddErr(err) - } - inf, err := informer.NewInformer(productObj.ClusterID, productObj.Namespace, cls) - if err != nil { - return e.ErrCreateEnv.AddErr(err) - } - // aws secrets needs to be refreshed regs, err := commonservice.ListRegistryNamespaces("", true, log) if err != nil { @@ -496,33 +475,14 @@ func RestartService(envName string, args *SvcOptArgs, production bool, log *zap. return updater.RestartStatefulSet(productObj.Namespace, sts.Name, kubeClient) } default: - var serviceTmpl *commonmodels.Service var productService *commonmodels.ProductService - serviceObj, ok := productObj.GetServiceMap()[args.ServiceName] if !ok { return nil } - productService = serviceObj - serviceTmpl, err = repository.QueryTemplateService(&commonrepo.ServiceFindOption{ - ProductName: serviceObj.ProductName, - ServiceName: serviceObj.ServiceName, - Revision: serviceObj.Revision, - Type: setting.K8SDeployType, - }, productObj.Production) - - // for services deployed by zadig, service will be applied when restarting - if commonutil.ServiceDeployed(serviceTmpl.ServiceName, productObj.ServiceDeployStrategy) { - _, err = upsertService( - productObj, - productService, - productService, - !productObj.Production, inf, kubeClient, istioClient, log) - } else { - err = restartRelatedWorkloads(productObj, productService, kubeClient, log) - } + err = restartRelatedWorkloads(productObj, productService, kubeClient, log) log.Infof("restart resource from namespace:%s/serviceName:%s ", productObj.Namespace, args.ServiceName) if err != nil {