Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable grab download resume #5042

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/autopilot/constant/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
AutopilotNamespace = "k0s-autopilot"
AutopilotConfigName = AutopilotName
K0sBinaryDir = "/usr/local/bin"
K0sTempFilename = "k0s.tmp"
K0sDefaultDataDir = "/var/lib/k0s"
K0sManifestSubDir = "manifests"
K0sImagesDir = "images"
Expand Down
1 change: 0 additions & 1 deletion pkg/autopilot/controller/signal/common/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (r *downloadController) Reconcile(ctx context.Context, req cr.Request) (cr.

} else {
logger.Infof("Download of '%s' successful", manifest.URL)

// When the download is complete move the status to the success state
signalData.Status = apsigv2.NewStatus(manifest.SuccessState)
}
Expand Down
17 changes: 5 additions & 12 deletions pkg/autopilot/controller/signal/k0s/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"

apcomm "github.com/k0sproject/k0s/pkg/autopilot/common"
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
apsigpred "github.com/k0sproject/k0s/pkg/autopilot/controller/signal/common/predicate"
apsigv2 "github.com/k0sproject/k0s/pkg/autopilot/signaling/v2"
Expand Down Expand Up @@ -102,30 +102,23 @@ func (r *applyingUpdate) Reconcile(ctx context.Context, req cr.Request) (cr.Resu
}

logger := r.log.WithField("signalnode", signalNode.GetName())
logger.Info("Applying update")

var signalData apsigv2.SignalData
if err := signalData.Unmarshal(signalNode.GetAnnotations()); err != nil {
return cr.Result{}, fmt.Errorf("unable to unmarshal signal data for node='%s': %w", req.NamespacedName.Name, err)
}

// Get the filename fragment from the URL
updateURL, err := url.Parse(signalData.Command.K0sUpdate.URL)
if err != nil {
return cr.Result{}, fmt.Errorf("unable to get update request URL: %w", err)
}

// TODO: make the filename part random
updateFilename := path.Base(updateURL.Path)
updateFilenamePath := path.Join(r.k0sBinaryDir, updateFilename)
updateFilenamePath := path.Join(r.k0sBinaryDir, apconst.K0sTempFilename)

// Ensure that the expected file exists
if _, err := os.Stat(updateFilenamePath); errors.Is(err, os.ErrNotExist) {
return cr.Result{}, fmt.Errorf("unable to find update file '%s': %w", updateFilename, err)
return cr.Result{}, fmt.Errorf("unable to find update file '%s': %w", apconst.K0sTempFilename, err)
}

// Ensure that the new file is executable
if err := os.Chmod(updateFilenamePath, 0755); err != nil {
return cr.Result{}, fmt.Errorf("unable to chmod update file '%s': %w", updateFilename, err)
return cr.Result{}, fmt.Errorf("unable to chmod update file '%s': %w", apconst.K0sTempFilename, err)
}

// Perform the update atomically
Expand Down
5 changes: 4 additions & 1 deletion pkg/autopilot/controller/signal/k0s/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package k0s

import (
"crypto/sha256"
"path/filepath"

apcomm "github.com/k0sproject/k0s/pkg/autopilot/common"
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant"
apdel "github.com/k0sproject/k0s/pkg/autopilot/controller/delegate"
apsigcomm "github.com/k0sproject/k0s/pkg/autopilot/controller/signal/common"
apsigpred "github.com/k0sproject/k0s/pkg/autopilot/controller/signal/common/predicate"
Expand Down Expand Up @@ -87,7 +89,8 @@ func (b downloadManifestBuilderK0s) Build(signalNode crcli.Object, signalData ap
URL: signalData.Command.K0sUpdate.URL,
ExpectedHash: signalData.Command.K0sUpdate.Sha256,
Hasher: sha256.New(),
DownloadDir: b.k0sBinaryDir,
// Force grab to download the new bin in the same folder of the current one but with '.tmp' suffix
Filename: filepath.Join(b.k0sBinaryDir, apconst.K0sTempFilename),
},
SuccessState: Cordoning,
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/autopilot/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
ExpectedHash string
Hasher hash.Hash
DownloadDir string
Filename string
}

type downloader struct {
Expand Down Expand Up @@ -72,6 +73,15 @@ func (d *downloader) Download(ctx context.Context) error {
dlreq.SetChecksum(d.config.Hasher, expectedHash, true)
}

// We're never really resuming downloads, so disable this feature.
// This also allows to re-download the file if it's already present.
dlreq.NoResume = true

if d.config.Filename != "" {
d.logger.Infof("Setting filename to %s", d.config.Filename)
dlreq.Filename = d.config.Filename
}

client := grab.NewClient()
// Set user agent to mitigate 403 errors from GitHub
// See https://github.com/cavaliergopher/grab/issues/104
Expand Down
Loading