Skip to content

Commit

Permalink
Merge pull request koderover#550 from AllenShen/bugfix/get_service_te…
Browse files Browse the repository at this point in the history
…mplate

return service name to client after chart imported
  • Loading branch information
landylee007 authored Nov 5, 2021
2 parents 2124717 + 893cae3 commit bbf5199
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/service/handler/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func CreateOrUpdateHelmService(c *gin.Context) {
}
args.CreatedBy = ctx.Username

ctx.Err = svcservice.CreateOrUpdateHelmService(c.Query("productName"), args, ctx.Logger)
ctx.Resp, ctx.Err = svcservice.CreateOrUpdateHelmService(c.Query("productName"), args, ctx.Logger)
}

func CreateOrUpdateBulkHelmServices(c *gin.Context) {
Expand Down
67 changes: 35 additions & 32 deletions pkg/microservice/aslan/core/service/service/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"time"

"github.com/27149chen/afero"
"github.com/hashicorp/go-multierror"
"github.com/otiai10/copy"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -243,34 +242,34 @@ func prepareChartTemplateData(templateName string, logger *zap.SugaredLogger) (*
}, nil
}

func CreateOrUpdateHelmService(projectName string, args *HelmServiceCreationArgs, logger *zap.SugaredLogger) error {
func CreateOrUpdateHelmService(projectName string, args *HelmServiceCreationArgs, logger *zap.SugaredLogger) (*BulkHelmServiceCreationResponse, error) {
switch args.Source {
case LoadFromRepo, LoadFromPublicRepo:
return CreateOrUpdateHelmServiceFromGitRepo(projectName, args, logger)
case LoadFromChartTemplate:
return CreateOrUpdateHelmServiceFromChartTemplate(projectName, args, logger)
default:
return fmt.Errorf("invalid source")
return nil, fmt.Errorf("invalid source")
}
}

func CreateOrUpdateHelmServiceFromChartTemplate(projectName string, args *HelmServiceCreationArgs, logger *zap.SugaredLogger) error {
func CreateOrUpdateHelmServiceFromChartTemplate(projectName string, args *HelmServiceCreationArgs, logger *zap.SugaredLogger) (*BulkHelmServiceCreationResponse, error) {
templateArgs, ok := args.CreateFrom.(*CreateFromChartTemplate)
if !ok {
return fmt.Errorf("invalid argument")
return nil, fmt.Errorf("invalid argument")
}

templateChartInfo, err := prepareChartTemplateData(templateArgs.TemplateName, logger)
if err != nil {
return err
return nil, err
}

var values [][]byte
if len(templateChartInfo.DefaultValuesYAML) > 0 {
//render variables
renderedYaml, err := renderVariablesToYaml(string(templateChartInfo.DefaultValuesYAML), projectName, args.Name, templateArgs.Variables)
if err != nil {
return err
return nil, err
}
values = append(values, []byte(renderedYaml))
}
Expand All @@ -288,29 +287,29 @@ func CreateOrUpdateHelmServiceFromChartTemplate(projectName string, args *HelmSe
// remove old files
if err = os.RemoveAll(to); err != nil {
logger.Errorf("Failed to remove dir %s, err: %s", to, err)
return err
return nil, err
}
if err = copy.Copy(from, to); err != nil {
logger.Errorf("Failed to copy file from %s to %s, err: %s", from, to, err)
return err
return nil, err
}

merged, err := yamlutil.Merge(values)
if err != nil {
logger.Errorf("Failed to merge values, err: %s", err)
return err
return nil, err
}

if err = os.WriteFile(filepath.Join(to, setting.ValuesYaml), merged, 0644); err != nil {
logger.Errorf("Failed to write values, err: %s", err)
return err
return nil, err
}

fsTree := os.DirFS(config.LocalServicePath(projectName, args.Name))
ServiceS3Base := config.ObjectStorageServicePath(projectName, args.Name)
if err = fsservice.ArchiveAndUploadFilesToS3(fsTree, args.Name, ServiceS3Base, logger); err != nil {
logger.Errorf("Failed to upload files for service %s in project %s, err: %s", args.Name, projectName, err)
return err
return nil, err
}

svc, err := createOrUpdateHelmService(
Expand All @@ -332,7 +331,7 @@ func CreateOrUpdateHelmServiceFromChartTemplate(projectName string, args *HelmSe
)
if err != nil {
logger.Errorf("Failed to create service %s in project %s, error: %s", args.Name, projectName, err)
return err
return nil, err
}

compareHelmVariable([]*templatemodels.RenderChart{
Expand All @@ -342,7 +341,9 @@ func CreateOrUpdateHelmServiceFromChartTemplate(projectName string, args *HelmSe
},
}, projectName, args.CreatedBy, logger)

return nil
return &BulkHelmServiceCreationResponse{
SuccessServices: []string{args.Name},
}, nil
}

func getCodehostType(repoArgs *CreateFromRepo, repoLink string) (string, *poetry.CodeHost, error) {
Expand All @@ -357,59 +358,62 @@ func getCodehostType(repoArgs *CreateFromRepo, repoLink string) (string, *poetry
return ch.Type, ch, nil
}

func CreateOrUpdateHelmServiceFromGitRepo(projectName string, args *HelmServiceCreationArgs, log *zap.SugaredLogger) error {
func CreateOrUpdateHelmServiceFromGitRepo(projectName string, args *HelmServiceCreationArgs, log *zap.SugaredLogger) (*BulkHelmServiceCreationResponse, error) {
var err error
var repoLink string
repoArgs, ok := args.CreateFrom.(*CreateFromRepo)
if !ok {
publicArgs, ok := args.CreateFrom.(*CreateFromPublicRepo)
if !ok {
return fmt.Errorf("invalid argument")
return nil, fmt.Errorf("invalid argument")
}

repoArgs, err = PublicRepoToPrivateRepoArgs(publicArgs)
if err != nil {
log.Errorf("Failed to parse repo args %+v, err: %s", publicArgs, err)
return err
return nil, err
}

repoLink = publicArgs.RepoLink
} else {

}

response := &BulkHelmServiceCreationResponse{}
source, codehostInfo, err := getCodehostType(repoArgs, repoLink)
if err != nil {
log.Errorf("Failed to get source form repo data %+v, err: %s", *repoArgs, err.Error())
return err
return nil, err
}

helmRenderCharts := make([]*templatemodels.RenderChart, 0, len(repoArgs.Paths))
var errs *multierror.Error

var wg wait.Group
var mux sync.RWMutex
for _, p := range repoArgs.Paths {
filePath := strings.TrimLeft(p, "/")
wg.Start(func() {
var finalErr error

var (
serviceName string
chartVersion string
valuesYAML []byte
finalErr error
)
defer func() {
mux.Lock()
if finalErr != nil {
mux.Lock()
errs = multierror.Append(errs, finalErr)
mux.Unlock()
response.FailedServices = append(response.FailedServices, &FailedService{
Path: filePath,
Error: finalErr.Error(),
})
} else {
response.SuccessServices = append(response.SuccessServices, serviceName)
}
mux.Unlock()
}()

log.Infof("Loading chart under path %s", filePath)

var (
serviceName string
chartVersion string
valuesYAML []byte
)

fsTree, err := fsservice.DownloadFilesFromSource(
&fsservice.DownloadFromSourceArgs{CodehostID: repoArgs.CodehostID, Owner: repoArgs.Owner, Repo: repoArgs.Repo, Path: filePath, Branch: repoArgs.Branch, RepoLink: repoLink},
func(chartTree afero.Fs) (string, error) {
Expand Down Expand Up @@ -476,8 +480,7 @@ func CreateOrUpdateHelmServiceFromGitRepo(projectName string, args *HelmServiceC
wg.Wait()

compareHelmVariable(helmRenderCharts, projectName, args.CreatedBy, log)

return errs.ErrorOrNil()
return response, nil
}

func CreateOrUpdateBulkHelmService(projectName string, args *BulkHelmServiceCreationArgs, logger *zap.SugaredLogger) (*BulkHelmServiceCreationResponse, error) {
Expand Down

0 comments on commit bbf5199

Please sign in to comment.