Adjusting implementation of delay-days to those suggested in PR review comments, to perform necessary checks in update.go. Added some comments where useful for understanding existing functionality.

This commit is contained in:
Peter Wilhelm 2023-12-21 19:28:25 -05:00
parent 6bd125d137
commit 8bcf748eb0
2 changed files with 46 additions and 38 deletions

View file

@ -328,21 +328,6 @@ func (client dockerClient) IsContainerStale(container t.Container, params t.Upda
return client.HasNewImage(ctx, container, params)
}
// Date strings sometimes vary in how many digits after the decimal point are present. This function
// standardizes them by removing the milliseconds.
func truncateMilliseconds(dateString string) string {
// Find the position of the dot (.) in the date string
dotIndex := strings.Index(dateString, ".")
// If the dot is found, truncate the string before the dot
if dotIndex != -1 {
return dateString[:dotIndex] + "Z"
}
// If the dot is not found, return the original string
return dateString
}
func (client dockerClient) HasNewImage(ctx context.Context, container t.Container, params t.UpdateParams) (hasNew bool, latestImage t.ImageID, err error) {
currentImageID := t.ImageID(container.ContainerInfo().ContainerJSONBase.Image)
imageName := container.ImageName()
@ -358,26 +343,6 @@ func (client dockerClient) HasNewImage(ctx context.Context, container t.Containe
return false, currentImageID, nil
}
// Disabled by default
if params.DelayDays > 0 {
// Define the layout string for the date format without milliseconds
layout := "2006-01-02T15:04:05Z"
newImageDate, error := time.Parse(layout, truncateMilliseconds(newImageInfo.Created))
if error != nil {
log.Errorf("Error parsing Created date (%s) for container %s latest label. Error: %s", newImageInfo.Created, container.Name(), error)
return false, currentImageID, nil
} else {
requiredDays := params.DelayDays
diffDays := int(time.Since(newImageDate).Hours() / 24)
if diffDays < requiredDays {
log.Infof("New image found for %s that was created %d day(s) ago but update delayed until %d day(s) after creation", container.Name(), diffDays, requiredDays)
return false, currentImageID, nil
}
}
}
log.Infof("Found new %s image (%s)", imageName, newImageID.ShortID())
return true, newImageID, nil
}