Skip to content

Commit feb157b

Browse files
authored
Merge pull request #16 from noderings/feat/cluster-liqo-upgrade
Add nr cluster liqo upgrade
2 parents 6bb024e + 28508d4 commit feb157b

8 files changed

Lines changed: 572 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `nr cluster liqo upgrade` upgrades an existing Liqo release to the pinned Harbor chart. It applies the chart CRDs, then runs Helm with `--reset-then-reuse-values` so the cluster id and provider values stay. It does not ask for a hypervisor token. Run it on the agent together with `nr cluster operator upgrade`.
13+
1014
## [1.0.20] - 2026-11-29
1115

1216
### Changed

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ If registration is interrupted:
7272
nr cluster register --resume --name edge-ams-01 --org-id <org-uuid>
7373
```
7474

75-
Upgrade the operator later without re-entering hypervisor tokens:
75+
Upgrade an existing agent without re-entering hypervisor tokens:
7676

7777
```bash
78+
nr cluster liqo upgrade
7879
nr cluster operator upgrade
7980
```
8081

82+
`nr cluster liqo upgrade` moves Liqo to the pinned chart and image and keeps the cluster id. The gateway restarts, so the peer can blip. `nr cluster operator upgrade` moves the hypervisor operator chart and image.
83+
8184
`nr cluster register --resume --reinstall-operator` also reuses existing Secrets unless you pass credentials (that path is for rotating tokens).
8285

8386
## Common commands
@@ -87,6 +90,7 @@ nr auth status
8790
nr cluster status --name edge-ams-01
8891
nr cluster verify --name edge-ams-01
8992
nr cluster health --name edge-ams-01
93+
nr cluster liqo upgrade
9094
nr cluster operator upgrade
9195
nr cluster deregister --name edge-ams-01
9296
```

‎docs/CONFIG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ This is not a `--hypervisor-driver` value. Register the hypervisor agent first,
107107

108108
Registration checkpoints live under `~/.nr/`. Do not copy these directories between machines.
109109

110-
Never commit tokens, kubeconfigs, or instances files to version control. If a hypervisor token leaked, rotate it and re-run `nr cluster register --resume --reinstall-operator` with the new credentials (env or instances file). Chart-only upgrades use `nr cluster operator upgrade` and do not rewrite Secrets.
110+
Never commit tokens, kubeconfigs, or instances files to version control. If a hypervisor token leaked, rotate it and re-run `nr cluster register --resume --reinstall-operator` with the new credentials (env or instances file). Chart-only upgrades use `nr cluster liqo upgrade` and `nr cluster operator upgrade`. Neither rewrites Secrets.

‎docs/TROUBLESHOOTING.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ nr cluster register --resume --name <same-name> --org-id <org-uuid>
4747

4848
Checkpoints live under `~/.nr/`. Use `nr cluster status` and `nr cluster debug` for details.
4949

50+
**Upgrade Liqo and the operator (no hypervisor tokens)**
51+
52+
```bash
53+
nr cluster liqo upgrade
54+
nr cluster operator upgrade
55+
```
56+
57+
`nr cluster liqo upgrade` reuses the Liqo cluster id and Helm values, applies the chart CRDs, and sets the image tag. It does not need `NR_API_TOKEN` or `--org-id`. The gateway restarts and the peer can show Error until it returns to Connected.
58+
5059
**Upgrade operator charts (no hypervisor tokens)**
5160

5261
```bash

‎internal/cli/cluster.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ func handleResume(cmd *cobra.Command, ctx context.Context, stateManager *state.M
919919
if err := runPostRegisterVerify(ctx, apiClient, log, installState.AgentID, registerOpts); err != nil {
920920
return err
921921
}
922-
log.Info("Tip: upgrade charts with 'nr cluster operator upgrade', or --resume --reinstall-operator")
922+
log.Info("Tip: upgrade with 'nr cluster liqo upgrade' and 'nr cluster operator upgrade', or --resume --reinstall-operator")
923923
return nil
924924
}
925925

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/noderings/cli/internal/config"
10+
"github.com/noderings/cli/internal/install"
11+
"github.com/noderings/cli/internal/logger"
12+
)
13+
14+
const (
15+
flagLiqoChart = "liqo-chart"
16+
flagLiqoChartVersion = "liqo-chart-version"
17+
flagLiqoTimeout = "timeout"
18+
)
19+
20+
var clusterLiqoCmd = &cobra.Command{
21+
Use: "liqo",
22+
Short: "Manage the Liqo Helm release on this agent",
23+
}
24+
25+
var clusterLiqoUpgradeCmd = &cobra.Command{
26+
Use: "upgrade",
27+
Short: "Upgrade the Liqo chart and images without re-peering",
28+
Long: `Upgrade the existing Liqo Helm release on this agent to the CLI-pinned build.
29+
30+
Reuses the cluster id, network CIDRs, and custom-resource list already stored
31+
on the release. Applies the chart CRDs first, then runs helm upgrade with
32+
--reset-then-reuse-values so new chart keys render. Does not uninstall Liqo,
33+
change the cluster id, or ask for a hypervisor token.
34+
35+
Does not require NR_API_TOKEN or --org-id. Run on the agent VM.
36+
37+
The WireGuard gateway restarts. The peer can show Error for a short time and
38+
then return to Connected. ` + fmt.Sprintf("This CLI pins chart %s and image %s.", config.DefaultLiqoChartVersion, config.DefaultLiqoVersion),
39+
Args: cobra.NoArgs,
40+
RunE: runClusterLiqoUpgrade,
41+
}
42+
43+
func init() {
44+
clusterCmd.AddCommand(clusterLiqoCmd)
45+
clusterLiqoCmd.AddCommand(clusterLiqoUpgradeCmd)
46+
clusterLiqoUpgradeCmd.Flags().String(flagLiqoChart, "", "Local path or OCI ref for the Liqo chart (default: Harbor OCI pin)")
47+
clusterLiqoUpgradeCmd.Flags().String(flagLiqoChartVersion, "", "Helm chart version (image tag is the same value with a leading v)")
48+
clusterLiqoUpgradeCmd.Flags().String(flagLiqoTimeout, "", "Helm --wait timeout (default 10m)")
49+
clusterLiqoUpgradeCmd.Flags().Bool(flagDryRun, false, "Print the Helm upgrade and exit")
50+
}
51+
52+
func runClusterLiqoUpgrade(cmd *cobra.Command, args []string) error {
53+
ctx := cmd.Context()
54+
if ctx == nil {
55+
ctx = context.Background()
56+
}
57+
58+
cfgLoader := config.NewLoader()
59+
cfg, err := cfgLoader.Load()
60+
if err != nil {
61+
return fmt.Errorf("load config: %w", err)
62+
}
63+
log, err := logger.NewLogger(cfg.Logging.Level, cfg.Logging.File)
64+
if err != nil {
65+
return err
66+
}
67+
68+
chart, err := cmd.Flags().GetString(flagLiqoChart)
69+
if err != nil {
70+
return err
71+
}
72+
chartVersion, err := cmd.Flags().GetString(flagLiqoChartVersion)
73+
if err != nil {
74+
return err
75+
}
76+
timeout, err := cmd.Flags().GetString(flagLiqoTimeout)
77+
if err != nil {
78+
return err
79+
}
80+
dryRun, err := cmd.Flags().GetBool(flagDryRun)
81+
if err != nil {
82+
return err
83+
}
84+
85+
if _, err := install.EnsureCLIBinDir(); err != nil {
86+
log.Warnf("Could not add ~/.nr/bin to PATH: %v", err)
87+
}
88+
kubeconfig := install.EnsureReadableKubeconfig(ctx, "", log)
89+
u, err := install.NewLiqoUpgrade(chart, chartVersion, kubeconfig, timeout)
90+
if err != nil {
91+
return err
92+
}
93+
if dryRun {
94+
_, err := fmt.Fprint(cmd.OutOrStdout(), install.LiqoUpgradeDryRun(u))
95+
return err
96+
}
97+
log.Infof("Liqo upgrade target chart %s tag %s", u.ChartVersion, u.ImageTag)
98+
return install.UpgradeLiqoRelease(ctx, u, log)
99+
}

‎internal/install/liqo_upgrade.go‎

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package install
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/noderings/cli/internal/config"
11+
)
12+
13+
const (
14+
liqoHelmRelease = "liqo"
15+
liqoHelmValueTag = "tag"
16+
liqoCRDRelDir = "charts/liqo-crds/crds"
17+
liqoPulledChartPlaceholder = "<pulled-chart>"
18+
defaultLiqoHelmTimeout = "10m"
19+
helmFlagWait = "--wait"
20+
helmFlagTimeout = "--timeout"
21+
)
22+
23+
// LiqoUpgrade is an in-place Helm upgrade of an existing provider Liqo release.
24+
// It reuses the cluster id, CIDRs, and custom-resource list already stored on the
25+
// release. It does not install Liqo, rotate peering, or ask for a hypervisor token.
26+
type LiqoUpgrade struct {
27+
Chart string
28+
ChartVersion string
29+
ImageTag string
30+
Release string
31+
Namespace string
32+
Kubeconfig string
33+
Timeout string
34+
}
35+
36+
// NewLiqoUpgrade builds an upgrade from the CLI pin. An empty chart uses the Harbor
37+
// OCI chart. chartVersion is the Helm chart version (leading v is stripped). The
38+
// image tag always keeps the v prefix.
39+
func NewLiqoUpgrade(chart, chartVersion, kubeconfig, timeout string) (LiqoUpgrade, error) {
40+
chart = strings.TrimSpace(chart)
41+
if chart == "" {
42+
chart = config.DefaultLiqoChartOCI
43+
}
44+
if strings.TrimSpace(timeout) == "" {
45+
timeout = defaultLiqoHelmTimeout
46+
}
47+
u := LiqoUpgrade{
48+
Chart: chart,
49+
ChartVersion: liqoChartVersion(chartVersion),
50+
ImageTag: liqoImageTag(chartVersion),
51+
Release: liqoHelmRelease,
52+
Namespace: config.DefaultLiqoNamespace,
53+
Kubeconfig: strings.TrimSpace(kubeconfig),
54+
Timeout: strings.TrimSpace(timeout),
55+
}
56+
return u, u.Validate()
57+
}
58+
59+
func (u LiqoUpgrade) Validate() error {
60+
if strings.TrimSpace(u.Chart) == "" {
61+
return fmt.Errorf("liqo chart is required")
62+
}
63+
if strings.TrimSpace(u.ImageTag) == "" {
64+
return fmt.Errorf("liqo image tag is required")
65+
}
66+
if strings.TrimSpace(u.Release) == "" || strings.TrimSpace(u.Namespace) == "" {
67+
return fmt.Errorf("liqo release and namespace are required")
68+
}
69+
if strings.TrimSpace(u.Timeout) == "" {
70+
return fmt.Errorf("liqo upgrade timeout is required")
71+
}
72+
if isOCIRef(u.Chart) && strings.TrimSpace(u.ChartVersion) == "" {
73+
return fmt.Errorf("liqo chart version is required for an OCI chart")
74+
}
75+
// Helm splits one --set-string value on commas, so a comma in the tag can
76+
// inject another key=value pair. Reject the separators before argv is built.
77+
if err := rejectHelmSetSeparators("liqo chart version", u.ChartVersion); err != nil {
78+
return err
79+
}
80+
if err := rejectHelmSetSeparators("liqo image tag", u.ImageTag); err != nil {
81+
return err
82+
}
83+
return nil
84+
}
85+
86+
func rejectHelmSetSeparators(field, value string) error {
87+
if strings.ContainsAny(value, ",=") {
88+
return fmt.Errorf("%s %q must not contain ',' or '='", field, value)
89+
}
90+
return nil
91+
}
92+
93+
// LiqoUpgradeArgs is the helm argv for the in-place upgrade.
94+
// --reset-then-reuse-values keeps the cluster id and provider values while
95+
// picking up chart keys added since the previous release. --reuse-values alone
96+
// leaves those keys nil and the new chart fails to render.
97+
// This is helm upgrade without --install: a missing release must fail closed.
98+
func LiqoUpgradeArgs(u LiqoUpgrade) []string {
99+
args := []string{
100+
helmCmdUpgrade, u.Release, u.Chart,
101+
helmFlagNamespace, u.Namespace,
102+
helmFlagResetThenReuseValues,
103+
helmFlagSetString, liqoHelmValueTag + "=" + u.ImageTag,
104+
helmFlagWait,
105+
helmFlagTimeout, u.Timeout,
106+
}
107+
args = appendOCIChartVersion(args, u.Chart, u.ChartVersion)
108+
return withKubeconfig(u.Kubeconfig, args)
109+
}
110+
111+
// LiqoCRDDir is the chart directory of Liqo CRD manifests. Helm does not upgrade
112+
// CRDs from a subchart on upgrade, and the new chart writes statusSync.
113+
func LiqoCRDDir(chartPath string) (string, error) {
114+
dir := filepath.Join(chartPath, liqoCRDRelDir)
115+
st, err := os.Stat(dir)
116+
if err != nil {
117+
return "", fmt.Errorf("liqo chart %s CRD directory %s: %w", chartPath, dir, err)
118+
}
119+
if !st.IsDir() {
120+
return "", fmt.Errorf("liqo chart %s CRD path %s is not a directory", chartPath, dir)
121+
}
122+
return dir, nil
123+
}
124+
125+
// LiqoUpgradeDryRun is the command sequence UpgradeLiqoRelease runs.
126+
// An OCI chart is pulled first. Helm then upgrades that extracted chart, so the
127+
// upgrade argv has no --version.
128+
func LiqoUpgradeDryRun(u LiqoUpgrade) string {
129+
chartPath := u.Chart
130+
var b strings.Builder
131+
if isOCIRef(u.Chart) {
132+
fmt.Fprintf(&b, "helm pull %s --version %s\n", u.Chart, u.ChartVersion)
133+
chartPath = liqoPulledChartPlaceholder
134+
}
135+
fmt.Fprintf(&b, "kubectl apply --server-side --force-conflicts -f %s/%s\n", chartPath, liqoCRDRelDir)
136+
fmt.Fprintf(&b, "helm %s\n", strings.Join(liqoExecutedUpgradeArgs(u, chartPath), " "))
137+
return b.String()
138+
}
139+
140+
func liqoExecutedUpgradeArgs(u LiqoUpgrade, chartPath string) []string {
141+
run := u
142+
run.Chart = chartPath
143+
return LiqoUpgradeArgs(run)
144+
}
145+
146+
// UpgradeLiqoRelease applies the chart CRDs, then upgrades the existing release.
147+
func UpgradeLiqoRelease(ctx context.Context, u LiqoUpgrade, logger Logger) error {
148+
if logger == nil {
149+
logger = noopLogger{}
150+
}
151+
if err := u.Validate(); err != nil {
152+
return err
153+
}
154+
ok, err := helmReleaseExists(ctx, u.Kubeconfig, u.Release, u.Namespace)
155+
if err != nil {
156+
return err
157+
}
158+
if !ok {
159+
return fmt.Errorf("no Liqo Helm release %s in namespace %s. Install with nr cluster register", u.Release, u.Namespace)
160+
}
161+
162+
chartPath, cleanup, err := resolveLiqoChart(ctx, u, logger)
163+
if err != nil {
164+
return err
165+
}
166+
if cleanup != nil {
167+
defer cleanup()
168+
}
169+
crdDir, err := LiqoCRDDir(chartPath)
170+
if err != nil {
171+
return err
172+
}
173+
logger.Infof("Applying Liqo CRDs from %s", crdDir)
174+
if err := runCmd(ctx, binKubectl, withKubeconfig(u.Kubeconfig, []string{
175+
kubectlCmdApply, kubectlFlagServerSide, kubectlFlagForceConflicts,
176+
kubectlFlagFilename, crdDir,
177+
})...); err != nil {
178+
return fmt.Errorf("apply liqo CRDs: %w", err)
179+
}
180+
181+
args := liqoExecutedUpgradeArgs(u, chartPath)
182+
logger.Infof("Upgrading Liqo chart %s tag=%s (reusing the existing cluster id)...", u.ChartVersion, u.ImageTag)
183+
if err := runCmd(ctx, binHelm, args...); err != nil {
184+
return fmt.Errorf("helm upgrade: %w", err)
185+
}
186+
logger.Infof("✓ Liqo chart %s / image %s", u.ChartVersion, u.ImageTag)
187+
return nil
188+
}
189+
190+
func resolveLiqoChart(ctx context.Context, u LiqoUpgrade, logger Logger) (string, func(), error) {
191+
if !isOCIRef(u.Chart) {
192+
if _, err := os.Stat(filepath.Join(u.Chart, helmChartFile)); err != nil {
193+
return "", nil, fmt.Errorf("liqo chart %s: %w", u.Chart, err)
194+
}
195+
return u.Chart, nil, nil
196+
}
197+
mgr := NewLiqoManager(nil, nil, &LiqoConfig{
198+
ChartOCI: u.Chart,
199+
ChartVersion: u.ChartVersion,
200+
}, logger)
201+
return mgr.prepareLocalChart(ctx)
202+
}
203+
204+
func liqoChartVersion(version string) string {
205+
version = strings.TrimSpace(version)
206+
if version == "" {
207+
return config.DefaultLiqoChartVersion
208+
}
209+
return strings.TrimPrefix(version, "v")
210+
}
211+
212+
func liqoImageTag(version string) string {
213+
version = strings.TrimSpace(version)
214+
if version == "" {
215+
return config.DefaultLiqoVersion
216+
}
217+
if strings.HasPrefix(version, "v") {
218+
return version
219+
}
220+
return "v" + version
221+
}

0 commit comments

Comments
 (0)