Skip to content

Commit

Permalink
容器更新检测增加忽略及缓存配置
Browse files Browse the repository at this point in the history
  • Loading branch information
donknap committed Dec 31, 2024
1 parent 539e849 commit 1bc7675
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
50 changes: 42 additions & 8 deletions app/application/http/controller/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/types/image"
"github.com/docker/go-units"
"github.com/donknap/dpanel/app/application/logic"
logic2 "github.com/donknap/dpanel/app/common/logic"
"github.com/donknap/dpanel/common/accessor"
"github.com/donknap/dpanel/common/dao"
"github.com/donknap/dpanel/common/entity"
Expand All @@ -27,6 +28,7 @@ import (
"log/slog"
"os"
"strings"
"time"
)

type Image struct {
Expand Down Expand Up @@ -538,13 +540,31 @@ func (self Image) UpdateTitle(http *gin.Context) {

func (self Image) CheckUpgrade(http *gin.Context) {
type ParamsValidate struct {
Tag string `json:"tag" binding:"required"`
Md5 string `json:"md5" binding:"required"`
Tag string `json:"tag" binding:"required"`
Md5 string `json:"md5" binding:"required"`
CacheKey string `json:"cacheKey" binding:"required"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
if containerCheckUpgrade, err := new(logic2.Setting).GetValue(logic2.SettingGroupCheckContainerUpgrade, params.CacheKey); err == nil {
if containerCheckUpgrade.Value.ContainerUpgrade.IgnoreUpgrade {
self.JsonResponseWithoutError(http, gin.H{
"upgrade": false,
"digest": "",
"ignore": true,
})
return
}
if time.Now().Before(containerCheckUpgrade.Value.ContainerUpgrade.ExpireTime) {
self.JsonResponseWithoutError(http, gin.H{
"upgrade": containerCheckUpgrade.Value.ContainerUpgrade.Upgrade,
"digest": containerCheckUpgrade.Value.ContainerUpgrade.Digest,
})
return
}
}
imageInfo, _, err := docker.Sdk.Client.ImageInspectWithRaw(docker.Sdk.Ctx, params.Md5)
if err != nil {
self.JsonResponseWithError(http, err, 500)
Expand All @@ -553,6 +573,8 @@ func (self Image) CheckUpgrade(http *gin.Context) {
_ = notice.Message{}.Info("imageCheckUpgrade", "tag", params.Tag)

digest := ""
upgrade := false

registryList, exists, username, password := logic.Image{}.GetRegistryList(params.Tag)
for _, s := range registryList {
option := make([]registry.Option, 0)
Expand All @@ -566,11 +588,8 @@ func (self Image) CheckUpgrade(http *gin.Context) {
if !function.InArrayWalk(imageInfo.RepoDigests, func(i string) bool {
return strings.HasSuffix(i, digest)
}) {
self.JsonResponseWithoutError(http, gin.H{
"upgrade": true,
"digest": digest,
})
return
upgrade = true
break
}
}
if err != nil {
Expand All @@ -581,8 +600,23 @@ func (self Image) CheckUpgrade(http *gin.Context) {
self.JsonResponseWithError(http, err, 500)
return
}

_ = logic2.Setting{}.Save(&entity.Setting{
GroupName: logic2.SettingGroupCheckContainerUpgrade,
Name: params.CacheKey,
Value: &accessor.SettingValueOption{
ContainerUpgrade: &accessor.CheckContainerUpgrade{
ExpireTime: time.Now().Add(24 * time.Hour),
Upgrade: upgrade,
Digest: digest,
IgnoreDigest: "",
IgnoreUpgrade: false,
},
},
})

self.JsonResponseWithoutError(http, gin.H{
"upgrade": false,
"upgrade": upgrade,
"digest": digest,
})
return
Expand Down
4 changes: 4 additions & 0 deletions app/common/logic/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var (
SettingGroupUserFounder = "founder"
)

var (
SettingGroupCheckContainerUpgrade = "checkContainerUpgrade"
)

type Setting struct {
}

Expand Down
23 changes: 16 additions & 7 deletions common/accessor/setting_value_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type SettingValueOption struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ServerIp string `json:"serverIp,omitempty"`
RequestTimeout int `json:"requestTimeout,omitempty"`
Docker map[string]*docker.Client `json:"docker,omitempty"`
DiskUsage *DiskUsage `json:"diskUsage,omitempty"`
TwoFa *TwoFa `json:"twoFa,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ServerIp string `json:"serverIp,omitempty"`
RequestTimeout int `json:"requestTimeout,omitempty"`
Docker map[string]*docker.Client `json:"docker,omitempty"`
DiskUsage *DiskUsage `json:"diskUsage,omitempty"`
TwoFa *TwoFa `json:"twoFa,omitempty"`
ContainerUpgrade *CheckContainerUpgrade `json:"containerUpgrade,omitempty"`
}

type DiskUsage struct {
Expand All @@ -27,3 +28,11 @@ type TwoFa struct {
Email string `json:"email,omitempty"`
QrCode string `json:"qrCode,omitempty"`
}

type CheckContainerUpgrade struct {
ExpireTime time.Time `json:"expreTime,omitempty"`
Upgrade bool `json:"upgrade,omitempty"`
Digest string `json:"digest,omitempty"`
IgnoreDigest string `json:"ignoreDigest,omitempty"` // 忽略本次
IgnoreUpgrade bool `json:"ignoreUpgrade,omitempty"` // 永久忽略
}

0 comments on commit 1bc7675

Please sign in to comment.