perf: buildkit caching support configuration (#1732)

This commit is contained in:
张启航 2023-08-05 09:24:17 +08:00 committed by GitHub
parent 1f05fe51a1
commit ed3c4d3a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 42 additions and 30 deletions

View File

@ -83,6 +83,7 @@ type Response struct {
type Request struct {
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
RbdNamespace string
GRDataPVCName string
CachePVCName string

View File

@ -142,7 +142,7 @@ func (s *slugBuild) buildRunnerImage(slugPackage string) (string, error) {
return "", fmt.Errorf("pull image %s: %v", builder.RUNNERIMAGENAME, err)
}
logrus.Infof("pull image %s successfully.", builder.RUNNERIMAGENAME)
err := sources.ImageBuild(s.re.Arch, cacheDir, "", "", s.re.RbdNamespace, s.re.ServiceID, s.re.DeployVersion, s.re.Logger, "run-build", "", s.re.BuildKitImage, s.re.BuildKitArgs, s.re.KubeClient)
err := sources.ImageBuild(s.re.Arch, cacheDir, "", "", s.re.RbdNamespace, s.re.ServiceID, s.re.DeployVersion, s.re.Logger, "run-build", "", s.re.BuildKitImage, s.re.BuildKitArgs, s.re.BuildKitCache, s.re.KubeClient)
if err != nil {
s.re.Logger.Error(fmt.Sprintf("build image %s of new version failure", imageName), map[string]string{"step": "builder-exector", "status": "failure"})
logrus.Errorf("build image error: %s", err.Error())

View File

@ -141,7 +141,7 @@ func (d *dockerfileBuild) runBuildJob(re *Request, buildImageName string) error
if err != nil {
return err
}
volumes, mounts := d.createVolumeAndMount(re, secret.Name, re.ServiceID, buildKitTomlCMName)
volumes, mounts := d.createVolumeAndMount(re, secret.Name, re.ServiceID, buildKitTomlCMName, re.BuildKitCache)
podSpec.Volumes = volumes
privileged := true
container := corev1.Container{
@ -188,7 +188,7 @@ func (d *dockerfileBuild) runBuildJob(re *Request, buildImageName string) error
return nil
}
func (d *dockerfileBuild) createVolumeAndMount(re *Request, secretName, ServiceID string, buildKitTomlCMName string) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) {
func (d *dockerfileBuild) createVolumeAndMount(re *Request, secretName, ServiceID string, buildKitTomlCMName string, buildKitCache bool) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) {
hostPathType := corev1.HostPathDirectoryOrCreate
hostsFilePathType := corev1.HostPathFile
dockerfileBuildVolume := corev1.Volume{
@ -225,15 +225,6 @@ func (d *dockerfileBuild) createVolumeAndMount(re *Request, secretName, ServiceI
},
},
},
{
Name: "buildkit-db",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: fmt.Sprintf("/cache/buildkit-cache/%v", ServiceID),
Type: &hostPathType,
},
},
},
{
Name: "buildkit-secret",
VolumeSource: corev1.VolumeSource{
@ -275,10 +266,21 @@ func (d *dockerfileBuild) createVolumeAndMount(re *Request, secretName, ServiceI
Name: "buildkittoml",
MountPath: "/etc/buildkit",
},
{
}
if buildKitCache {
volumes = append(volumes, corev1.Volume{
Name: "buildkit-db",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/cache/buildkit-cache",
Type: &hostPathType,
},
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "buildkit-db",
MountPath: "/var/lib/buildkit",
},
})
}
return volumes, volumeMounts
}

View File

@ -71,7 +71,7 @@ func (d *netcoreBuild) Build(re *Request) (*Response, error) {
return nil, fmt.Errorf("write default dockerfile error:%s", err.Error())
}
// build image
err := sources.ImageBuild(re.Arch, d.sourceDir, re.CachePVCName, re.CacheMode, re.RbdNamespace, re.ServiceID, re.DeployVersion, re.Logger, "nc-build", "", re.BuildKitImage, re.BuildKitArgs, re.KubeClient)
err := sources.ImageBuild(re.Arch, d.sourceDir, re.CachePVCName, re.CacheMode, re.RbdNamespace, re.ServiceID, re.DeployVersion, re.Logger, "nc-build", "", re.BuildKitImage, re.BuildKitArgs, re.BuildKitCache, re.KubeClient)
if err != nil {
re.Logger.Error(fmt.Sprintf("build image %s failure, find log in rbd-chaos", d.buildImageName), map[string]string{"step": "builder-exector", "status": "failure"})
logrus.Errorf("build image error: %s", err.Error())

View File

@ -63,6 +63,7 @@ type SourceCodeBuildItem struct {
ImageClient sources.ImageClient
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
KubeClient kubernetes.Interface
RbdNamespace string
RbdRepoName string
@ -328,6 +329,7 @@ func (i *SourceCodeBuildItem) codeBuild() (*build.Response, error) {
buildReq := &build.Request{
BuildKitImage: i.BuildKitImage,
BuildKitArgs: i.BuildKitArgs,
BuildKitCache: i.BuildKitCache,
RbdNamespace: i.RbdNamespace,
SourceDir: i.RepoInfo.GetCodeBuildAbsPath(),
CacheDir: i.CacheDir,

View File

@ -122,6 +122,7 @@ func NewManager(conf option.Config, mqc mqclient.MQClient) (Manager, error) {
return &exectorManager{
BuildKitImage: conf.BuildKitImage,
BuildKitArgs: strings.Split(conf.BuildKitArgs, "&"),
BuildKitCache: conf.BuildKitCache,
KubeClient: kubeClient,
EtcdCli: etcdCli,
mqClient: mqc,
@ -137,6 +138,7 @@ func NewManager(conf option.Config, mqc mqclient.MQClient) (Manager, error) {
type exectorManager struct {
BuildKitImage string
BuildKitArgs []string
BuildKitCache bool
KubeClient kubernetes.Interface
EtcdCli *clientv3.Client
tasks chan *pb.TaskMessage
@ -348,6 +350,7 @@ func (e *exectorManager) buildFromSourceCode(task *pb.TaskMessage) {
i.ImageClient = e.imageClient
i.BuildKitImage = e.BuildKitImage
i.BuildKitArgs = e.BuildKitArgs
i.BuildKitCache = e.BuildKitCache
i.KubeClient = e.KubeClient
i.RbdNamespace = e.cfg.RbdNamespace
i.RbdRepoName = e.cfg.RbdRepoName

View File

@ -105,7 +105,7 @@ func (e *exectorManager) runD(t *model.BuildPluginTaskBody, logger event.Logger)
n1 := strings.Split(mm[len(mm)-1], ".")[0]
buildImageName := fmt.Sprintf(builder.REGISTRYDOMAIN+"/plugin_%s_%s:%s", n1, t.PluginID, t.DeployVersion)
logger.Info("start build image", map[string]string{"step": "builder-exector"})
err := sources.ImageBuild("", sourceDir, "", "", "rbd-system", t.PluginID, t.DeployVersion, logger, "plug-build", buildImageName, e.BuildKitImage, e.BuildKitArgs, e.KubeClient)
err := sources.ImageBuild("", sourceDir, "", "", "rbd-system", t.PluginID, t.DeployVersion, logger, "plug-build", buildImageName, e.BuildKitImage, e.BuildKitArgs, e.BuildKitCache, e.KubeClient)
if err != nil {
logger.Error(fmt.Sprintf("build image %s failure,find log in rbd-chaos", buildImageName), map[string]string{"step": "builder-exector", "status": "failure"})
logrus.Errorf("[plugin]build image error: %s", err.Error())

View File

@ -392,7 +392,7 @@ func EncodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
}
//ImageBuild use buildkit build image
func ImageBuild(arch, contextDir, cachePVCName, cacheMode, RbdNamespace, ServiceID, DeployVersion string, logger event.Logger, buildType, plugImageName, BuildKitImage string, BuildKitArgs []string, kubeClient kubernetes.Interface) error {
func ImageBuild(arch, contextDir, cachePVCName, cacheMode, RbdNamespace, ServiceID, DeployVersion string, logger event.Logger, buildType, plugImageName, BuildKitImage string, BuildKitArgs []string, BuildKitCache bool, kubeClient kubernetes.Interface) error {
// create image name
var buildImageName string
if buildType == "plug-build" {
@ -449,7 +449,7 @@ func ImageBuild(arch, contextDir, cachePVCName, cacheMode, RbdNamespace, Service
return err
}
// only support never and onfailure
volumes, volumeMounts := CreateVolumesAndMounts(ServiceID, contextDir, buildType, cacheMode, cachePVCName, buildKitTomlCMName)
volumes, volumeMounts := CreateVolumesAndMounts(ServiceID, contextDir, buildType, cacheMode, cachePVCName, buildKitTomlCMName, BuildKitCache)
podSpec.Volumes = volumes
privileged := true
// container config
@ -806,22 +806,13 @@ func PrepareBuildKitTomlCM(ctx context.Context, kubeClient kubernetes.Interface,
}
// CreateVolumesAndMounts -
func CreateVolumesAndMounts(ServiceID, contextDir, buildType, cacheMode, cachePVCName string, buildKitTomlCMName string) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) {
func CreateVolumesAndMounts(ServiceID, contextDir, buildType, cacheMode, cachePVCName string, buildKitTomlCMName string, buildKitCache bool) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) {
pathSplit := strings.Split(contextDir, "/")
subPath := strings.Join(pathSplit[2:], "/")
hostPathType := corev1.HostPathDirectoryOrCreate
hostsFilePathType := corev1.HostPathFile
// buildkit volumes volumeMounts config
volumes = []corev1.Volume{
{
Name: "buildkit-db",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: fmt.Sprintf("/cache/buildkit-cache/%v", ServiceID),
Type: &hostPathType,
},
},
},
{
Name: "buildkit-secret",
VolumeSource: corev1.VolumeSource{
@ -873,10 +864,21 @@ func CreateVolumesAndMounts(ServiceID, contextDir, buildType, cacheMode, cachePV
Name: "buildkittoml",
MountPath: "/etc/buildkit",
},
{
}
if buildKitCache {
volumes = append(volumes, corev1.Volume{
Name: "buildkit-db",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/cache/buildkit-cache",
Type: &hostPathType,
},
},
})
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "buildkit-db",
MountPath: "/var/lib/buildkit",
},
})
}
// Customize it according to how it is built volumes volumeMounts config
if buildType == "plug-build" {

View File

@ -39,6 +39,7 @@ type Config struct {
MysqlConnectionInfo string
BuildKitImage string
BuildKitArgs string
BuildKitCache bool
DBType string
PrometheusMetricPath string
EventLogServers []string
@ -106,6 +107,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")
}
//SetLog 设置log