Skip to content

Commit

Permalink
Invalidate imageCache if the pinned image from config exists
Browse files Browse the repository at this point in the history
Signed-off-by: roman-kiselenko <roman.kiselenko.dev@gmail.com>
  • Loading branch information
roman-kiselenko committed Apr 8, 2024
1 parent f63de87 commit 51deda1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions internal/storage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ type ImageServer interface {
// CandidatesForPotentiallyShortImageName resolves an image name into a set of fully-qualified image names (domain/repo/image:tag|@digest).
// It will only return an empty slice if err != nil.
CandidatesForPotentiallyShortImageName(systemContext *types.SystemContext, imageName string) ([]RegistryImageReference, error)

// UpdateImageCache invalidate imageCache if the pinned image from config exists;
// this method doesn't change the Pinned attribute, only removes the image item.
UpdateImageCache(configPinnedImage []string) error
}

func parseImageNames(image *storage.Image) (someName *RegistryImageReference, tags []reference.NamedTagged, digests []reference.Canonical, err error) {
Expand Down Expand Up @@ -897,6 +901,26 @@ func (st nativeStorageTransport) ResolveReference(ref types.ImageReference) (typ
return istorage.ResolveReference(ref)
}

// UpdateImageCache invalidate imageCache if the pinned image from config exists;
// this method doesn't change the Pinned attribute, only removes the image item.
func (svc *imageService) UpdateImageCache(configPinnedImage []string) error {
images, err := svc.store.Images()
if err != nil {
return err
}
for i := range images {
img := &images[i]
for _, name := range img.Names {
if FilterPinnedImage(name, CompileRegexpsForPinnedImages(configPinnedImage)) {
svc.imageCacheLock.Lock()
delete(svc.imageCache, img.ID)
svc.imageCacheLock.Unlock()
}
}
}
return nil
}

// FilterPinnedImage checks if the given image needs to be pinned
// and excluded from kubelet's image GC.
func FilterPinnedImage(image string, pinnedImages []*regexp.Regexp) bool {
Expand Down
15 changes: 14 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func (s *Server) startReloadWatcher(ctx context.Context) {
for {
// Block until the signal is received
<-ch
if err := s.config.Reload(); err != nil {
if err := s.reloadConfigByHUP(); err != nil {
logrus.Errorf("Unable to reload configuration: %v", err)
continue
}
Expand All @@ -583,6 +583,19 @@ func (s *Server) startReloadWatcher(ctx context.Context) {
log.Infof(ctx, "Registered SIGHUP reload watcher")
}

// reloadConfigByHUP reload the config and UpdateImageCache
func (s *Server) reloadConfigByHUP() error {
if err := s.config.Reload(); err != nil {
return err
}
if len(s.config.PinnedImages) > 0 {
if err := s.StorageImageServer().UpdateImageCache(s.config.PinnedImages); err != nil {
return err
}
}
return nil
}

func useDefaultUmask() {
const defaultUmask = 0o022
oldUmask := unix.Umask(defaultUmask)
Expand Down
14 changes: 14 additions & 0 deletions test/mocks/criostorage/criostorage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51deda1

Please sign in to comment.