Skip to content

Commit

Permalink
When pulling images check for stale cached images
Browse files Browse the repository at this point in the history
  • Loading branch information
biecho committed Nov 4, 2022
1 parent 2424217 commit 77cbcba
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
82 changes: 55 additions & 27 deletions ctriface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ import (
_ "google.golang.org/grpc/codes" //tmp
_ "google.golang.org/grpc/status" //tmp

"github.com/go-multierror/multierror"
"github.com/vhive-serverless/vhive/memory/manager"
"github.com/vhive-serverless/vhive/metrics"
"github.com/vhive-serverless/vhive/misc"
"github.com/go-multierror/multierror"

_ "github.com/davecgh/go-spew/spew" //tmp
)
Expand Down Expand Up @@ -314,42 +314,70 @@ func getImageURL(image string) string {

func (o *Orchestrator) getImage(ctx context.Context, imageName string) (*containerd.Image, error) {
image, found := o.cachedImages[imageName]
if !found {
var err error
log.Debug(fmt.Sprintf("Pulling image %s", imageName))

imageURL := getImageURL(imageName)
local, _ := isLocalDomain(imageURL)
if local {
// Pull local image using HTTP
resolver := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
Hosts: docker.ConfigureDefaultRegistries(
docker.WithPlainHTTP(docker.MatchAllHosts),
),
})
image, err = o.client.Pull(ctx, imageURL,
containerd.WithPullUnpack,
containerd.WithPullSnapshotter(o.snapshotter),
containerd.WithResolver(resolver),
)
} else {
// Pull remote image
image, err = o.client.Pull(ctx, imageURL,
containerd.WithPullUnpack,
containerd.WithPullSnapshotter(o.snapshotter),
)
}

imageUrl := getImageURL(imageName)
if !found || imageIsOutdated(image, imageUrl) {
image, err := o.pullImage(ctx, imageUrl)

if err != nil {
return &image, err
}
o.cachedImages[imageName] = image
return &image, nil
}

return &image, nil
}

func imageIsOutdated(cachedImage containerd.Image, imageUrl string) bool {
remoteImageDigest, err := remoteImageDigest(imageUrl)
return err == nil && cachedImage.Target().Digest.String() != remoteImageDigest
}

func remoteImageDigest(imageUrl string) (string, error) {
cmd := fmt.Sprintf("skopeo inspect docker://%s | jq -r '.Digest'", imageUrl)

start := time.Now()
out, err := exec.Command("bash", "-c", cmd).Output()
elapsed := time.Since(start)
log.Printf("Remote digest fetching took %s", elapsed)

return string(out), err
}

func (o *Orchestrator) pullImage(ctx context.Context, imageUrl string) (containerd.Image, error) {
log.Debugf("Pulling image %s\n", imageUrl)

local, _ := isLocalDomain(imageUrl)

if local {
return o.pullLocalImage(ctx, imageUrl)
}
return o.pullRemoteImage(ctx, imageUrl)
}

func (o *Orchestrator) pullRemoteImage(ctx context.Context, imageUrl string) (containerd.Image, error) {
return o.client.Pull(ctx, imageUrl,
containerd.WithPullUnpack,
containerd.WithPullSnapshotter(o.snapshotter),
)
}

// Pull local image using HTTP
func (o *Orchestrator) pullLocalImage(ctx context.Context, imageUrl string) (containerd.Image, error) {
resolver := docker.NewResolver(docker.ResolverOptions{
Client: http.DefaultClient,
Hosts: docker.ConfigureDefaultRegistries(
docker.WithPlainHTTP(docker.MatchAllHosts),
),
})
return o.client.Pull(ctx, imageUrl,
containerd.WithPullUnpack,
containerd.WithPullSnapshotter(o.snapshotter),
containerd.WithResolver(resolver),
)
}

func getK8sDNS() []string {
//using googleDNS as a backup
dnsIPs := []string{"8.8.8.8"}
Expand Down
4 changes: 2 additions & 2 deletions ctriface/iface_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright (c) 2020 Dmitrii Ustiugov, Plamen Petrov and EASE lab
// # Copyright (c) 2020 Dmitrii Ustiugov, Plamen Petrov and EASE lab
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,7 +41,7 @@ var (
isUPFEnabled = flag.Bool("upf", false, "Set UPF enabled")
isLazyMode = flag.Bool("lazy", false, "Set lazy serving on or off")
//nolint:deadcode,unused,varcheck
isWithCache = flag.Bool("withCache", false, "Do not drop the cache before measurements")
isWithCache = flag.Bool("withCache", false, "Do not drop the cache before measurements")
)

func TestPauseSnapResume(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/self-hosted-kind/scripts/setup-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

set -e

# Needed for 'skopeo'
echo "deb http://mirrors.kernel.org/ubuntu kinetic main universe" | sudo tee -a /etc/apt/sources.list

# Install base- and setup-dependencies
apt-get update
apt-get install --yes \
Expand Down
1 change: 0 additions & 1 deletion taps/tapManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func setupForwardRules(tapName, hostIface string) error {
}
}


conn := nftables.Conn{}

// 1. nft add table ip filter
Expand Down

0 comments on commit 77cbcba

Please sign in to comment.