Skip to content

Commit 51801d8

Browse files
committed
Add automatic cache cleanup on version change
Track the CLI version in a version.json file in the cache directory. When a version mismatch is detected, quit running daemons and clear stale cache entries (preserving logs) to prevent issues from leftover files after an upgrade. Signed-off-by: Thomas Hallgren <thomas@tada.se>
1 parent 93f96f1 commit 51801d8

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

CHANGELOG.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ items:
5252
the need for elevated privileges when using Telepresence. Currently available for amd64 architecture only
5353
due to dependency constraints.
5454
docs: install/client
55+
- type: feature
56+
title: Automatic cache cleanup on version change
57+
body: >-
58+
Telepresence now tracks its version in a version.json file in the cache directory. When the CLI detects
59+
that the major.minor version differs from the running binary, it automatically quits running daemons and
60+
clears stale cache entries (preserving logs). This prevents issues caused by leftover cache files from a
61+
previous version. Patch and pre-release version changes do not trigger a cache cleanup.
5562
- version: 2.26.2
5663
date: (TBD)
5764
notes:

docs/release-notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ New Linux package installers (.deb for Debian/Ubuntu and .rpm for Fedora/RHEL) a
2020
A new Windows installer (.exe) is now available that installs Telepresence with the root daemon configured as a Windows service. The installer bundles WinFSP and SSHFS-Win dependencies for volume mount support, adds Telepresence to the system PATH, and optionally installs the TelepresenceDaemon service. This eliminates the need for elevated privileges when using Telepresence. Currently available for amd64 architecture only due to dependency constraints.
2121
</div>
2222

23+
## <div style="display:flex;"><img src="images/feature.png" alt="feature" style="width:30px;height:fit-content;"/><div style="display:flex;margin-left:7px;">Automatic cache cleanup on version change</div></div>
24+
<div style="margin-left: 15px">
25+
26+
Telepresence now tracks its version in a version.json file in the cache directory. When the CLI detects that the major.minor version differs from the running binary, it automatically quits running daemons and clears stale cache entries (preserving logs). This prevents issues caused by leftover cache files from a previous version. Patch and pre-release version changes do not trigger a cache cleanup.
27+
</div>
28+
2329
## Version 2.26.2
2430
## <div style="display:flex;"><img src="images/bugfix.png" alt="bugfix" style="width:30px;height:fit-content;"/><div style="display:flex;margin-left:7px;">Fix HTTP intercepts via telepresence compose failing when httpFilters or httpPaths are set</div></div>
2531
<div style="margin-left: 15px">

docs/release-notes.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ New Linux package installers (.deb for Debian/Ubuntu and .rpm for Fedora/RHEL) a
2626
A new Windows installer (.exe) is now available that installs Telepresence with the root daemon configured as a Windows service. The installer bundles WinFSP and SSHFS-Win dependencies for volume mount support, adds Telepresence to the system PATH, and optionally installs the TelepresenceDaemon service. This eliminates the need for elevated privileges when using Telepresence. Currently available for amd64 architecture only due to dependency constraints.
2727
</Body>
2828
</Note>
29+
<Note>
30+
<Title type="feature">Automatic cache cleanup on version change</Title>
31+
<Body>
32+
Telepresence now tracks its version in a version.json file in the cache directory. When the CLI detects that the major.minor version differs from the running binary, it automatically quits running daemons and clears stale cache entries (preserving logs). This prevents issues caused by leftover cache files from a previous version. Patch and pre-release version changes do not trigger a cache cleanup.
33+
</Body>
34+
</Note>
2935
## Version 2.26.2
3036
<Note>
3137
<Title type="bugfix">Fix HTTP intercepts via telepresence compose failing when httpFilters or httpPaths are set</Title>

pkg/client/cli/cmd/telepresence.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ func Telepresence(ctx context.Context, args []string) *cobra.Command {
3333
Args: OnlySubcommands,
3434
Short: "Connect your workstation to a Kubernetes cluster",
3535
Long: longHelp,
36-
PersistentPreRunE: output.SetFormat,
36+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
37+
if err := output.SetFormat(cmd, args); err != nil {
38+
return err
39+
}
40+
return ensureCacheVersion(cmd, args)
41+
},
3742
RunE: RunSubcommands,
3843
SilenceErrors: true, // main() will handle it after .ExecuteContext() returns
3944
SilenceUsage: true, // our FlagErrorFunc will handle it

pkg/client/cli/cmd/versioncache.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"io/fs"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/blang/semver/v4"
10+
"github.com/spf13/cobra"
11+
12+
"github.com/telepresenceio/telepresence/v2/pkg/client/cache"
13+
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/connect"
14+
"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
15+
"github.com/telepresenceio/telepresence/v2/pkg/version"
16+
)
17+
18+
type versionFile struct {
19+
Version semver.Version `json:"version"`
20+
}
21+
22+
func ensureCacheVersion(cmd *cobra.Command, _ []string) error {
23+
ctx := cmd.Context()
24+
var vf versionFile
25+
majorMinorMatch := false
26+
if err := cache.LoadFromUserCache(ctx, &vf, "version.json"); err == nil {
27+
majorMinorMatch = vf.Version.Major == version.Structured.Major && vf.Version.Minor == version.Structured.Minor
28+
if majorMinorMatch {
29+
return nil
30+
}
31+
}
32+
33+
// Quit all daemons (best effort, ignore errors).
34+
connect.Quit(ctx)
35+
36+
// Clear cache except logs.
37+
cacheDir := filelocation.AppUserCacheDir(ctx)
38+
entries, err := os.ReadDir(cacheDir)
39+
if err != nil && !errors.Is(err, fs.ErrNotExist) {
40+
return err
41+
}
42+
for _, entry := range entries {
43+
if entry.Name() == "logs" {
44+
continue
45+
}
46+
os.RemoveAll(filepath.Join(cacheDir, entry.Name()))
47+
}
48+
49+
return cache.SaveToUserCache(ctx, &versionFile{Version: version.Structured}, "version.json", cache.Public)
50+
}

0 commit comments

Comments
 (0)