From aa110a9c99b9608f6ba5d3a96814adfd2b427779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=90=AF=E8=88=AA?= Date: Thu, 16 Nov 2023 17:25:00 +0800 Subject: [PATCH] fix: simultaneous build of multiple source code components failed --- builder/build/build.go | 85 ++++++----- builder/build/code_build.go | 11 +- builder/exector/build_from_sourcecode_run.go | 142 +++++++++---------- builder/exector/exector.go | 51 ++++--- cmd/builder/option/option.go | 4 +- 5 files changed, 141 insertions(+), 152 deletions(-) diff --git a/builder/build/build.go b/builder/build/build.go index d6d167d8b4..4361e29047 100644 --- a/builder/build/build.go +++ b/builder/build/build.go @@ -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 @@ -124,14 +123,14 @@ 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() @@ -139,7 +138,7 @@ func GetBuild(lang code.Lang) (Build, error) { 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) @@ -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) diff --git a/builder/build/code_build.go b/builder/build/code_build.go index 65540ff88c..de1014f098 100644 --- a/builder/build/code_build.go +++ b/builder/build/code_build.go @@ -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) @@ -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 { @@ -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 @@ -543,7 +540,7 @@ func (s *slugBuild) setImagePullSecretsForPod(pod *corev1.Pod) { } } -//ErrorBuild build error +// ErrorBuild build error type ErrorBuild struct { Code int } diff --git a/builder/exector/build_from_sourcecode_run.go b/builder/exector/build_from_sourcecode_run.go index 166ff6d2c0..b7b43879d1 100644 --- a/builder/exector/build_from_sourcecode_run.go +++ b/builder/exector/build_from_sourcecode_run.go @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 { @@ -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), @@ -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 } diff --git a/builder/exector/exector.go b/builder/exector/exector.go index c46471ac98..32a74c3d6d 100644 --- a/builder/exector/exector.go +++ b/builder/exector/exector.go @@ -49,16 +49,16 @@ import ( workermodel "github.com/goodrain/rainbond/worker/discover/model" ) -//MetricTaskNum task number +// MetricTaskNum task number var MetricTaskNum float64 -//MetricErrorTaskNum error run task number +// MetricErrorTaskNum error run task number var MetricErrorTaskNum float64 -//MetricBackTaskNum back task number +// MetricBackTaskNum back task number var MetricBackTaskNum float64 -//Manager 任务执行管理器 +// Manager 任务执行管理器 type Manager interface { GetMaxConcurrentTask() float64 GetCurrentConcurrentTask() float64 @@ -69,7 +69,7 @@ type Manager interface { GetImageClient() sources.ImageClient } -//NewManager new manager +// NewManager new manager func NewManager(conf option.Config, mqc mqclient.MQClient) (Manager, error) { imageClient, err := sources.NewImageClient(conf.ContainerRuntime, conf.RuntimeEndpoint, time.Second*3) if err != nil { @@ -123,7 +123,6 @@ func NewManager(conf option.Config, mqc mqclient.MQClient) (Manager, error) { BuildKitImage: conf.BuildKitImage, BuildKitArgs: strings.Split(conf.BuildKitArgs, "&"), BuildKitCache: conf.BuildKitCache, - BuildSharedCache: conf.BuildSharedCache, KubeClient: kubeClient, EtcdCli: etcdCli, mqClient: mqc, @@ -140,7 +139,6 @@ type exectorManager struct { BuildKitImage string BuildKitArgs []string BuildKitCache bool - BuildSharedCache bool KubeClient kubernetes.Interface EtcdCli *clientv3.Client tasks chan *pb.TaskMessage @@ -154,7 +152,7 @@ type exectorManager struct { imageClient sources.ImageClient } -//TaskWorker worker interface +// TaskWorker worker interface type TaskWorker interface { Run(timeout time.Duration) error GetLogger() event.Logger @@ -166,27 +164,27 @@ type TaskWorker interface { var workerCreaterList = make(map[string]func([]byte, *exectorManager) (TaskWorker, error)) -//RegisterWorker register worker creator +// RegisterWorker register worker creator func RegisterWorker(name string, fun func([]byte, *exectorManager) (TaskWorker, error)) { workerCreaterList[name] = fun } -//ErrCallback do not handle this task +// ErrCallback do not handle this task var ErrCallback = fmt.Errorf("callback task to mq") func (e *exectorManager) SetReturnTaskChan(re func(*pb.TaskMessage)) { e.callback = re } -//TaskType: -//build_from_image build app from docker image -//build_from_source_code build app from source code -//build_from_market_slug build app from app market by download slug -//service_check check service source info -//plugin_image_build build plugin from image -//plugin_dockerfile_build build plugin from dockerfile -//share-slug share app with slug -//share-image share app with image +// TaskType: +// build_from_image build app from docker image +// build_from_source_code build app from source code +// build_from_market_slug build app from app market by download slug +// service_check check service source info +// plugin_image_build build plugin from image +// plugin_dockerfile_build build plugin from dockerfile +// share-slug share app with slug +// share-image share app with image func (e *exectorManager) AddTask(task *pb.TaskMessage) error { if e.callback != nil && task.Arch != "" && task.Arch != runtime.GOARCH { e.callback(task) @@ -297,7 +295,7 @@ func (e *exectorManager) exec(task *pb.TaskMessage) error { return nil } -//buildFromImage build app from docker image +// buildFromImage build app from docker image func (e *exectorManager) buildFromImage(task *pb.TaskMessage) { i := NewImageBuildItem(task.TaskBody) i.ImageClient = e.imageClient @@ -345,15 +343,14 @@ func (e *exectorManager) buildFromImage(task *pb.TaskMessage) { } } -//buildFromSourceCode build app from source code -//support git repository +// buildFromSourceCode build app from source code +// support git repository func (e *exectorManager) buildFromSourceCode(task *pb.TaskMessage) { i := NewSouceCodeBuildItem(task.TaskBody) i.ImageClient = e.imageClient i.BuildKitImage = e.BuildKitImage i.BuildKitArgs = e.BuildKitArgs i.BuildKitCache = e.BuildKitCache - i.BuildSharedCache = e.BuildSharedCache i.KubeClient = e.KubeClient i.RbdNamespace = e.cfg.RbdNamespace i.RbdRepoName = e.cfg.RbdRepoName @@ -409,7 +406,7 @@ func (e *exectorManager) buildFromSourceCode(task *pb.TaskMessage) { } } -//buildFromMarketSlug build app from market slug +// buildFromMarketSlug build app from market slug func (e *exectorManager) buildFromMarketSlug(task *pb.TaskMessage) { eventID := gjson.GetBytes(task.TaskBody, "event_id").String() logger := event.GetManager().GetLogger(eventID) @@ -458,7 +455,7 @@ func (e *exectorManager) buildFromMarketSlug(task *pb.TaskMessage) { } -//rollingUpgradeTaskBody upgrade message body type +// rollingUpgradeTaskBody upgrade message body type type rollingUpgradeTaskBody struct { TenantID string `json:"tenant_id"` ServiceID string `json:"service_id"` @@ -507,7 +504,7 @@ func (e *exectorManager) sendAction(tenantID, serviceID, eventID, newVersion, ac return nil } -//slugShare share app of slug +// slugShare share app of slug func (e *exectorManager) slugShare(task *pb.TaskMessage) { i, err := NewSlugShareItem(task.TaskBody, e.EtcdCli) if err != nil { @@ -547,7 +544,7 @@ func (e *exectorManager) slugShare(task *pb.TaskMessage) { }() } -//imageShare share app of docker image +// imageShare share app of docker image func (e *exectorManager) imageShare(task *pb.TaskMessage) { i, err := NewImageShareItem(task.TaskBody, e.imageClient, e.EtcdCli) if err != nil { diff --git a/cmd/builder/option/option.go b/cmd/builder/option/option.go index 80a347c948..409a803626 100644 --- a/cmd/builder/option/option.go +++ b/cmd/builder/option/option.go @@ -39,7 +39,6 @@ type Config struct { MysqlConnectionInfo string BuildKitImage string BuildKitArgs string - BuildSharedCache bool BuildKitCache bool DBType string PrometheusMetricPath string @@ -110,8 +109,7 @@ func (a *Builder) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.ContainerRuntime, "container-runtime", sources.ContainerRuntimeContainerd, "container runtime, support docker and containerd") fs.StringVar(&a.RuntimeEndpoint, "runtime-endpoint", sources.RuntimeEndpointContainerd, "container runtime endpoint") fs.StringVar(&a.BuildKitArgs, "buildkit-args", "", "buildkit build image container args config,need '&' split") - fs.BoolVar(&a.BuildKitCache, "buildkit-cache", true, "whether to enable the buildkit image cache") - fs.BoolVar(&a.BuildSharedCache, "build-shared-cache", true, "build shared cache") + fs.BoolVar(&a.BuildKitCache, "buildkit-cache", false, "whether to enable the buildkit image cache") fs.IntVar(&a.KeepCount, "keep-count", 5, "default number of reserved copies for images") fs.IntVar(&a.CleanInterval, "clean-interval", 60, "clean image interval,default 60 minute")