Skip to content

Commit

Permalink
fix rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-kiselenko committed Apr 17, 2024
1 parent 3b55df1 commit 6dc8e3c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
8 changes: 8 additions & 0 deletions internal/storage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ 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)

// UpdatePinnedImagesList updates pinned and pause images list in imageService.
UpdatePinnedImagesList(imageList []string)
}

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

// UpdatePinnedImagesList updates pinned images list in imageService.
func (svc *imageService) UpdatePinnedImagesList(pauseImage string, pinnedImages []string) {
svc.regexForPinnedImages = CompileRegexpsForPinnedImages(pinnedImages)
}

// 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
25 changes: 17 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,9 @@ func (c *Config) Reload() error {
if err := c.ReloadPauseImage(newConfig); err != nil {
return err
}
c.ReloadPinnedImages(newConfig)
if c.ReloadPinnedImages(newConfig) {
logrus.Infof("Updated new pinned images: %+v", c.PinnedImages)
}
if err := c.ReloadRegistries(); err != nil {
return err
}
Expand All @@ -75,6 +77,7 @@ func (c *Config) Reload() error {
return err
}

cdi.GetRegistry(cdi.WithSpecDirs(newConfig.CDISpecDirs...))
return nil
}

Expand Down Expand Up @@ -145,17 +148,23 @@ 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 {
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
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ func (s *Server) startReloadWatcher(ctx context.Context) {
logrus.Errorf("Unable to reload configuration: %v", err)
continue
}
// ImageServer compiles the list with regex for both
// pinned and sandbox/pause images, we need to update them
s.StorageImageServer().UpdatePinnedImagesList(append(s.config.PinnedImages, s.config.PauseImage))
}
}()

Expand Down
12 changes: 12 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.

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

@test "reload config should update 'pinned_images'" {
# add pinned_images config
printf '[crio.image]\npinned_images = [""]\n' > "$CRIO_CONFIG_DIR"/00-default
printf '[crio.image]\npinned_images = ["quay.io/crio/hello-wasm:latest"]\n' > "$CRIO_CONFIG_DIR"/01-overwrite
# image is not pinned
crictl pull quay.io/crio/hello-wasm:latest
output=$(crictl images -o json | jq '.images[] | select(.repoTags[] == "quay.io/crio/hello-wasm:latest") | .pinned')
[ "$output" == "false" ]
reload_crio
# image becomes pinned
wait_for_log 'Updated new pinned images'
output=$(crictl images -o json | jq '.images[] | select(.repoTags[] == "quay.io/crio/hello-wasm:latest") | .pinned')
[ "$output" == "true" ]
# flush pinned_images list
printf '[crio.image]\npinned_images = [""]\n' > "$CRIO_CONFIG_DIR"/03-overwrite
reload_crio
wait_for_log 'Updated new pinned images'
output=$(crictl images -o json | jq '.images[] | select(.pinned == true) | .repoTags[]')
# only pause image is pinned
[[ "$output" == *"pause"* ]]
# flush pause and pinned_images list
printf '[crio.image]\npinned_images = [""]\npause_image = "registry.k8s.io/pause:4.1"\n' > "$CRIO_CONFIG_DIR"/04-overwrite
reload_crio
output=$(crictl images -o json | jq '.images[] | select(.pinned == true)')
# no pinned images returned
[[ "$output" == "" ]]
}

0 comments on commit 6dc8e3c

Please sign in to comment.