Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check manifest and image update in new gateway buildfunc #624

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions pkg/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ import (

func (b generalBuilder) BuildFunc() func(ctx context.Context, c client.Client) (*client.Result, error) {
return func(ctx context.Context, c client.Client) (*client.Result, error) {
depsFiles := []string{
b.PubKeyPath,
b.ConfigFilePath,
b.ManifestFilePath,
}
isUpdated, err := b.CheckDepsFileUpdate(ctx, b.Tag, depsFiles)
if err != nil {
b.logger.Debugf("failed to check manifest update: %s", err)
}
if !isUpdated {
b.logger.Infof("manifest is not updated, skip building")
return nil, nil
}

def, err := b.compile(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to compile")
Expand Down
80 changes: 50 additions & 30 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,37 +122,10 @@ func (b generalBuilder) NumGPUs() int {
return ir.NumGPUs()
}

// Always return updated when met error
func (b generalBuilder) CheckDepsFileUpdate(ctx context.Context, tag string, deps []string) (bool, error) {
dockerClient, err := docker.NewClient(ctx)
if err != nil {
return true, err
}
image, err := dockerClient.GetImage(ctx, tag)
if err != nil {
return true, err
}
imageCreatedTime := image.Created

latestTimestamp := int64(0)
for _, dep := range deps {
file, err := os.Stat(dep)
if err != nil {
return true, err
}
modifiedtime := file.ModTime().Unix()
// Only needt o use the latest modified time
if modifiedtime > latestTimestamp {
latestTimestamp = modifiedtime
}
}
if latestTimestamp > imageCreatedTime {
return true, nil
}
return false, nil
}

func (b generalBuilder) Build(ctx context.Context) error {
if !b.checkIfNeedBuild(ctx) {
return nil
}
pw, err := progresswriter.NewPrinter(ctx, os.Stdout, b.ProgressMode)
if err != nil {
return errors.Wrap(err, "failed to create progress writer")
Expand Down Expand Up @@ -325,3 +298,50 @@ func (b generalBuilder) build(ctx context.Context, pw progresswriter.Writer) err
}
return nil
}

func (b generalBuilder) checkIfNeedBuild(ctx context.Context) bool {
depsFiles := []string{
b.PubKeyPath,
b.ConfigFilePath,
b.ManifestFilePath,
}
isUpdated, err := b.checkDepsFileUpdate(ctx, b.Tag, depsFiles)
if err != nil {
b.logger.Debugf("failed to check manifest update: %s", err)
}
if !isUpdated {
b.logger.Infof("manifest is not updated, skip building")
return false
}
return true
}

// Always return updated when met error
func (b generalBuilder) checkDepsFileUpdate(ctx context.Context, tag string, deps []string) (bool, error) {
dockerClient, err := docker.NewClient(ctx)
if err != nil {
return true, err
}
image, err := dockerClient.GetImage(ctx, tag)
if err != nil {
return true, err
}
imageCreatedTime := image.Created

latestTimestamp := int64(0)
for _, dep := range deps {
file, err := os.Stat(dep)
if err != nil {
return true, err
}
modifiedtime := file.ModTime().Unix()
// Only needt o use the latest modified time
if modifiedtime > latestTimestamp {
latestTimestamp = modifiedtime
}
}
if latestTimestamp > imageCreatedTime {
return true, nil
}
return false, nil
}