|
| 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