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 10, 2024
1 parent f63de87 commit 9e7262c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
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
27 changes: 19 additions & 8 deletions pkg/config/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Config) Reload() error {
// Reload the config
newConfig, err := DefaultConfig()
if err != nil {
return errors.New("unable to create default config")
return fmt.Errorf("unable to create default config: %w", err)
}

if _, err := os.Stat(c.singleConfigPath); !os.IsNotExist(err) {
Expand Down Expand Up @@ -51,7 +51,6 @@ func (c *Config) Reload() error {
if err := c.ReloadPauseImage(newConfig); err != nil {
return err
}
c.ReloadPinnedImages(newConfig)
if err := c.ReloadRegistries(); err != nil {
return err
}
Expand All @@ -76,6 +75,11 @@ func (c *Config) Reload() error {
return nil
}

// IsPinnedImagesUpdated return the state of pinned image updates
func (c *Config) IsPinnedImagesUpdated() bool {
return c.ReloadPinnedImages(c)
}

// logConfig logs a config set operation as with info verbosity. Please always
// use this function for setting configuration options to ensure consistent
// log outputs
Expand Down Expand Up @@ -143,17 +147,24 @@ func (c *Config) ReloadPauseImage(newConfig *Config) error {
}

// ReloadPinnedImages updates the PinnedImages with the provided `newConfig`.
func (c *Config) ReloadPinnedImages(newConfig *Config) {
func (c *Config) ReloadPinnedImages(newConfig *Config) bool {
updatedPinnedImages := make([]string, len(newConfig.PinnedImages))
updatedPinnedImageList := false

for i, image := range newConfig.PinnedImages {
if i < len(c.PinnedImages) && image == c.PinnedImages[i] {
updatedPinnedImages[i] = c.PinnedImages[i]
} else {
updatedPinnedImages[i] = image
continue
}
updatedPinnedImages[i] = image
updatedPinnedImageList = true
}
logrus.Infof("Updated new pinned images: %+v", updatedPinnedImages)
c.PinnedImages = updatedPinnedImages

if updatedPinnedImageList {
logrus.Infof("Updated new pinned images: %+v", updatedPinnedImages)
c.PinnedImages = updatedPinnedImages
}

return updatedPinnedImageList
}

// ReloadRegistries reloads the registry configuration from the Configs
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,17 @@ var _ = t.Describe("Config", func() {
sut.PinnedImages = []string{"image1", "image4", "image3"}
newConfig := &config.Config{}
newConfig.PinnedImages = []string{"image5"}
sut.ReloadPinnedImages(newConfig)
ok := sut.ReloadPinnedImages(newConfig)
Expect(ok).To(BeTrue())
Expect(sut.PinnedImages).To(Equal([]string{"image5"}))
})

It("should not update PinnedImages if they are the same as newConfig's PinnedImages", func() {
sut.PinnedImages = []string{"image1", "image2", "image3"}
newConfig := &config.Config{}
newConfig.PinnedImages = []string{"image1", "image2", "image3"}
sut.ReloadPinnedImages(newConfig)
ok := sut.ReloadPinnedImages(newConfig)
Expect(ok).To(BeFalse())
Expect(sut.PinnedImages).To(Equal([]string{"image1", "image2", "image3"}))
})
})
Expand Down
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,12 @@ func (s *Server) startReloadWatcher(ctx context.Context) {
logrus.Errorf("Unable to reload configuration: %v", err)
continue
}
if s.config.IsPinnedImagesUpdated() {
if err := s.StorageImageServer().UpdateImageCache(s.config.PinnedImages); err != nil {
logrus.Errorf("Unable to update pinned images: %v", err)
continue
}
}
}
}()

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.

14 changes: 14 additions & 0 deletions test/reload_config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,17 @@ EOF
#then
wait_for_log '"updating runtime configuration"'
}

@test "reload config should update 'pinned_images'" {
# given
cat << EOF > "$CRIO_CONFIG_DIR/99-pinned-image.conf"
[crio.image]
pinned_images = [ "quay.io/crio/hello-wasm:latest" ]
EOF

# when
reload_crio

#then
wait_for_log 'Updated new pinned images'
}

0 comments on commit 9e7262c

Please sign in to comment.