Skip to content

Commit

Permalink
Updates pinned images list on config reload
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 17, 2024
1 parent f63de87 commit 7ee109b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/storage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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 @@ -897,6 +900,11 @@ func (st nativeStorageTransport) ResolveReference(ref types.ImageReference) (typ
return istorage.ResolveReference(ref)
}

// UpdatePinnedImagesList updates pinned images list in imageService.
func (svc *imageService) UpdatePinnedImagesList(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
19 changes: 13 additions & 6 deletions pkg/config/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/containers/image/v5/pkg/sysregistriesv2"
"github.com/cri-o/cri-o/internal/log"
Expand All @@ -20,7 +21,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 @@ -143,17 +144,23 @@ func (c *Config) ReloadPauseImage(newConfig *Config) error {
}

// ReloadPinnedImages updates the PinnedImages with the provided `newConfig`.
// The method print log in case of any updates.
func (c *Config) ReloadPinnedImages(newConfig *Config) {
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
}

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

// ReloadRegistries reloads the registry configuration from the Configs
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.

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

@test "reload config should update 'pinned_images' and 'pause_image' is pinned automatically" {
OPTION="pinned_images"
EXAMPLE_IMAGE=quay.io/crio/hello-wasm:latest
# add pinned_images config
printf '[crio.image]\npinned_images = [""]\n' > "$CRIO_CONFIG_DIR"/00-default
printf '[crio.image]\npinned_images = ["%s"]\n' $EXAMPLE_IMAGE > "$CRIO_CONFIG_DIR"/01-overwrite
# image is not pinned
output=$(crictl images -o json | jq ".images[] | select(.repoTags[] == \"$EXAMPLE_IMAGE\") |.pinned")
[ "$output" == "false" ]
reload_crio
# image becomes pinned
expect_log_success $OPTION $EXAMPLE_IMAGE
output=$(crictl images -o json | jq ".images[] | select(.repoTags[] == \"$EXAMPLE_IMAGE\") |.pinned")
[ "$output" == "true" ]
# flush pinned_images list
printf '[crio.image]\npinned_images = [""]\n' > "$CRIO_CONFIG_DIR"/03-overwrite
reload_crio
expect_log_success $OPTION $EXAMPLE_IMAGE
output=$(crictl images -o json | jq '.images[] | select(.pinned == true) | .repoTags[]')
# only pause image is pinned
[[ "$output" == *"pause"* ]]
# flush pinned_images list and override pause image
printf '[crio.image]\npinned_images = [""]\npause_image = "%s"\n' $EXAMPLE_IMAGE > "$CRIO_CONFIG_DIR"/04-overwrite
reload_crio
output=$(crictl images -o json | jq '.images[] | select(.pinned == true)')
# pause image is pinned
[[ "$output" == *"wasm"* ]]
}

0 comments on commit 7ee109b

Please sign in to comment.