Skip to content

Commit

Permalink
fix: simultaneous build of multiple source code components failed (#1780
Browse files Browse the repository at this point in the history
)

Co-authored-by: 张启航 <zhangqh@goodrain.com>
  • Loading branch information
ZhangSetSail and 张启航 committed Nov 16, 2023
1 parent 599a080 commit 5ed9076
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 152 deletions.
85 changes: 42 additions & 43 deletions builder/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,65 +54,64 @@ func init() {

var buildcreaters map[code.Lang]CreaterBuild

//Build app build pack
// Build app build pack
type Build interface {
Build(*Request) (*Response, error)
}

//CreaterBuild CreaterBuild
// CreaterBuild CreaterBuild
type CreaterBuild func() (Build, error)

//MediumType Build output medium type
// MediumType Build output medium type
type MediumType string

//ImageMediumType image type
// ImageMediumType image type
var ImageMediumType MediumType = "image"

//SlugMediumType slug type
// SlugMediumType slug type
var SlugMediumType MediumType = "slug"

//ImageBuildNetworkModeHost use host network mode during docker build
// ImageBuildNetworkModeHost use host network mode during docker build
var ImageBuildNetworkModeHost = "host"

//Response build result
// Response build result
type Response struct {
MediumPath string
MediumType MediumType
}

//Request build input
// Request build input
type Request struct {
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
BuildSharedCache bool
RbdNamespace string
GRDataPVCName string
CachePVCName string
CacheMode string
CachePath string
TenantID string
SourceDir string
CacheDir string
TGZDir string
RepositoryURL string
CodeSouceInfo sources.CodeSourceInfo
Branch string
ServiceAlias string
ServiceID string
DeployVersion string
Runtime string
ServerType string
Commit Commit
Lang code.Lang
BuildEnvs map[string]string
Logger event.Logger
ImageClient sources.ImageClient
KubeClient kubernetes.Interface
ExtraHosts []string
HostAlias []HostAlias
Ctx context.Context
Arch string
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
RbdNamespace string
GRDataPVCName string
CachePVCName string
CacheMode string
CachePath string
TenantID string
SourceDir string
CacheDir string
TGZDir string
RepositoryURL string
CodeSouceInfo sources.CodeSourceInfo
Branch string
ServiceAlias string
ServiceID string
DeployVersion string
Runtime string
ServerType string
Commit Commit
Lang code.Lang
BuildEnvs map[string]string
Logger event.Logger
ImageClient sources.ImageClient
KubeClient kubernetes.Interface
ExtraHosts []string
HostAlias []HostAlias
Ctx context.Context
Arch string
}

// HostAlias holds the mapping between IP and hostnames that will be injected as an entry in the
Expand All @@ -124,22 +123,22 @@ type HostAlias struct {
Hostnames []string `json:"hostnames,omitempty" protobuf:"bytes,2,rep,name=hostnames"`
}

//Commit Commit
// Commit Commit
type Commit struct {
User string
Message string
Hash string
}

//GetBuild GetBuild
// GetBuild GetBuild
func GetBuild(lang code.Lang) (Build, error) {
if fun, ok := buildcreaters[lang]; ok {
return fun()
}
return slugBuilder()
}

//CreateImageName create image name
// CreateImageName create image name
func CreateImageName(serviceID, deployversion string) string {
imageName := strings.ToLower(fmt.Sprintf("%s/%s:%s", builder.REGISTRYDOMAIN, serviceID, deployversion))
component, err := db.GetManager().TenantServiceDao().GetServiceByID(serviceID)
Expand All @@ -158,7 +157,7 @@ func CreateImageName(serviceID, deployversion string) string {
return strings.ToLower(fmt.Sprintf("%s/%s:%s", builder.REGISTRYDOMAIN, workloadName, deployversion))
}

//GetTenantRegistryAuthSecrets GetTenantRegistryAuthSecrets
// GetTenantRegistryAuthSecrets GetTenantRegistryAuthSecrets
func GetTenantRegistryAuthSecrets(ctx context.Context, tenantID string, kcli kubernetes.Interface) map[string]types.AuthConfig {
auths := make(map[string]types.AuthConfig)
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(tenantID)
Expand Down
11 changes: 4 additions & 7 deletions builder/build/code_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (s *slugBuild) writeRunDockerfile(sourceDir, packageName string, envs map[s
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), []byte(result), 0755)
}

//buildRunnerImage Wrap slug in the runner image
// buildRunnerImage Wrap slug in the runner image
func (s *slugBuild) buildRunnerImage(slugPackage string) (string, error) {
imageName := CreateImageName(s.re.ServiceID, s.re.DeployVersion)
cacheDir := path.Join(path.Dir(slugPackage), "."+s.re.DeployVersion)
Expand Down Expand Up @@ -169,8 +169,8 @@ func (s *slugBuild) getSourceCodeTarFile(re *Request) (string, error) {
return sourceTarFile, nil
}

//stopPreBuildJob Stops previous build tasks for the same component
//The same component retains only one build task to perform
// stopPreBuildJob Stops previous build tasks for the same component
// The same component retains only one build task to perform
func (s *slugBuild) stopPreBuildJob(re *Request) error {
jobList, err := jobc.GetJobController().GetServiceJobs(re.ServiceID)
if err != nil {
Expand All @@ -189,9 +189,6 @@ func (s *slugBuild) createVolumeAndMount(re *Request, sourceTarFileName string,
lazyloading := sourceTarFileName == ""
sourceTarPath := strings.TrimPrefix(sourceTarFileName, "/cache/")
cacheSubPath := strings.TrimPrefix(re.CacheDir, "/cache/")
if s.re.BuildSharedCache {
cacheSubPath = path.Join("build/cache", re.Lang.String())
}

hostPathType := corev1.HostPathDirectoryOrCreate
unset := corev1.HostPathUnset
Expand Down Expand Up @@ -543,7 +540,7 @@ func (s *slugBuild) setImagePullSecretsForPod(pod *corev1.Pod) {
}
}

//ErrorBuild build error
// ErrorBuild build error
type ErrorBuild struct {
Code int
}
Expand Down
142 changes: 70 additions & 72 deletions builder/exector/build_from_sourcecode_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,51 @@ import (
"k8s.io/client-go/kubernetes"
)

//SourceCodeBuildItem SouceCodeBuildItem
// SourceCodeBuildItem SouceCodeBuildItem
type SourceCodeBuildItem struct {
Namespace string `json:"namespace"`
TenantName string `json:"tenant_name"`
GRDataPVCName string `json:"gr_data_pvc_name"`
CachePVCName string `json:"cache_pvc_name"`
CacheMode string `json:"cache_mode"`
CachePath string `json:"cache_path"`
ServiceAlias string `json:"service_alias"`
Action string `json:"action"`
Arch string `json:"arch"`
DestImage string `json:"dest_image"`
Logger event.Logger `json:"logger"`
EventID string `json:"event_id"`
CacheDir string `json:"cache_dir"`
TGZDir string `json:"tgz_dir"`
ImageClient sources.ImageClient
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
BuildSharedCache bool
KubeClient kubernetes.Interface
RbdNamespace string
RbdRepoName string
TenantID string
ServiceID string
DeployVersion string
Lang string
Runtime string
BuildEnvs map[string]string
CodeSouceInfo sources.CodeSourceInfo
RepoInfo *sources.RepostoryBuildInfo
commit Commit
Configs map[string]gjson.Result `json:"configs"`
Ctx context.Context
FailCause string
Namespace string `json:"namespace"`
TenantName string `json:"tenant_name"`
GRDataPVCName string `json:"gr_data_pvc_name"`
CachePVCName string `json:"cache_pvc_name"`
CacheMode string `json:"cache_mode"`
CachePath string `json:"cache_path"`
ServiceAlias string `json:"service_alias"`
Action string `json:"action"`
Arch string `json:"arch"`
DestImage string `json:"dest_image"`
Logger event.Logger `json:"logger"`
EventID string `json:"event_id"`
CacheDir string `json:"cache_dir"`
TGZDir string `json:"tgz_dir"`
ImageClient sources.ImageClient
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
KubeClient kubernetes.Interface
RbdNamespace string
RbdRepoName string
TenantID string
ServiceID string
DeployVersion string
Lang string
Runtime string
BuildEnvs map[string]string
CodeSouceInfo sources.CodeSourceInfo
RepoInfo *sources.RepostoryBuildInfo
commit Commit
Configs map[string]gjson.Result `json:"configs"`
Ctx context.Context
FailCause string
}

//Commit code Commit
// Commit code Commit
type Commit struct {
Hash string
Author string
Message string
}

//NewSouceCodeBuildItem create
// NewSouceCodeBuildItem create
func NewSouceCodeBuildItem(in []byte) *SourceCodeBuildItem {
eventID := gjson.GetBytes(in, "event_id").String()
logger := event.GetManager().GetLogger(eventID)
Expand Down Expand Up @@ -131,7 +130,7 @@ func NewSouceCodeBuildItem(in []byte) *SourceCodeBuildItem {
return scb
}

//Run Run
// Run Run
func (i *SourceCodeBuildItem) Run(timeout time.Duration) error {
// 1.clone
// 2.check dockerfile/ source_code
Expand Down Expand Up @@ -337,36 +336,35 @@ func (i *SourceCodeBuildItem) codeBuild() (*build.Response, error) {
return nil, err
}
buildReq := &build.Request{
BuildKitImage: i.BuildKitImage,
BuildKitArgs: i.BuildKitArgs,
BuildKitCache: i.BuildKitCache,
BuildSharedCache: i.BuildSharedCache,
RbdNamespace: i.RbdNamespace,
SourceDir: i.RepoInfo.GetCodeBuildAbsPath(),
CacheDir: i.CacheDir,
TGZDir: i.TGZDir,
RepositoryURL: i.RepoInfo.RepostoryURL,
CodeSouceInfo: i.CodeSouceInfo,
ServiceAlias: i.ServiceAlias,
ServiceID: i.ServiceID,
TenantID: i.TenantID,
ServerType: i.CodeSouceInfo.ServerType,
Runtime: i.Runtime,
Branch: i.CodeSouceInfo.Branch,
DeployVersion: i.DeployVersion,
Commit: build.Commit{User: i.commit.Author, Message: i.commit.Message, Hash: i.commit.Hash},
Lang: code.Lang(i.Lang),
BuildEnvs: i.BuildEnvs,
Logger: i.Logger,
ImageClient: i.ImageClient,
KubeClient: i.KubeClient,
HostAlias: hostAlias,
Ctx: i.Ctx,
GRDataPVCName: i.GRDataPVCName,
CachePVCName: i.CachePVCName,
CacheMode: i.CacheMode,
CachePath: i.CachePath,
Arch: i.Arch,
BuildKitImage: i.BuildKitImage,
BuildKitArgs: i.BuildKitArgs,
BuildKitCache: i.BuildKitCache,
RbdNamespace: i.RbdNamespace,
SourceDir: i.RepoInfo.GetCodeBuildAbsPath(),
CacheDir: i.CacheDir,
TGZDir: i.TGZDir,
RepositoryURL: i.RepoInfo.RepostoryURL,
CodeSouceInfo: i.CodeSouceInfo,
ServiceAlias: i.ServiceAlias,
ServiceID: i.ServiceID,
TenantID: i.TenantID,
ServerType: i.CodeSouceInfo.ServerType,
Runtime: i.Runtime,
Branch: i.CodeSouceInfo.Branch,
DeployVersion: i.DeployVersion,
Commit: build.Commit{User: i.commit.Author, Message: i.commit.Message, Hash: i.commit.Hash},
Lang: code.Lang(i.Lang),
BuildEnvs: i.BuildEnvs,
Logger: i.Logger,
ImageClient: i.ImageClient,
KubeClient: i.KubeClient,
HostAlias: hostAlias,
Ctx: i.Ctx,
GRDataPVCName: i.GRDataPVCName,
CachePVCName: i.CachePVCName,
CacheMode: i.CacheMode,
CachePath: i.CachePath,
Arch: i.Arch,
}
res, err := codeBuild.Build(buildReq)
return res, err
Expand All @@ -387,7 +385,7 @@ func (i *SourceCodeBuildItem) getHostAlias() (hostAliasList []build.HostAlias, e
return
}

//IsDockerfile CheckDockerfile
// IsDockerfile CheckDockerfile
func (i *SourceCodeBuildItem) IsDockerfile() bool {
filepath := path.Join(i.RepoInfo.GetCodeBuildAbsPath(), "Dockerfile")
_, err := os.Stat(filepath)
Expand Down Expand Up @@ -423,7 +421,7 @@ func (i *SourceCodeBuildItem) prepare() error {
return nil
}

//UpdateVersionInfo Update build application service version info
// UpdateVersionInfo Update build application service version info
func (i *SourceCodeBuildItem) UpdateVersionInfo(vi *dbmodel.VersionInfo) error {
version, err := db.GetManager().VersionInfoDao().GetVersionByDeployVersion(i.DeployVersion, i.ServiceID)
if err != nil {
Expand Down Expand Up @@ -452,7 +450,7 @@ func (i *SourceCodeBuildItem) UpdateVersionInfo(vi *dbmodel.VersionInfo) error {
return nil
}

//UpdateBuildVersionInfo update service build version info to db
// UpdateBuildVersionInfo update service build version info to db
func (i *SourceCodeBuildItem) UpdateBuildVersionInfo(res *build.Response) error {
vi := &dbmodel.VersionInfo{
DeliveredType: string(res.MediumType),
Expand All @@ -474,7 +472,7 @@ func (i *SourceCodeBuildItem) UpdateBuildVersionInfo(res *build.Response) error
return nil
}

//UpdateCheckResult UpdateCheckResult
// UpdateCheckResult UpdateCheckResult
func (i *SourceCodeBuildItem) UpdateCheckResult(result *dbmodel.CodeCheckResult) error {
return nil
}

0 comments on commit 5ed9076

Please sign in to comment.