@@ -22,25 +22,49 @@ import (
2222 "io"
2323 "os"
2424 "os/signal"
25+ "path/filepath"
2526 "strings"
26- "time"
2727
2828 "github.com/spf13/cobra"
29- "github.com/spf13/pflag"
3029 "golang.org/x/term"
3130
3231 "go.ate.dev/substrate/cmd/kubectl-ate/internal/install"
3332 "go.ate.dev/substrate/cmd/kubectl-ate/internal/install/tui"
3433)
3534
3635var (
37- installOpts = install.Options {Postgres : install .PostgresBundled , ObjectStore : install .ObjectStoreRustFS }
38- upgradeOpts install.Options
36+ configPath string
37+ chartDir string
38+ noTUI bool
39+ upgradeDryRun bool
3940 uninstallOpts install.Options
4041 uninstallYes bool
41- noTUI bool
4242)
4343
44+ const configHelp = `--config takes a YAML file, or - for stdin:
45+
46+ postgres: bundled # or external
47+ objectStore: rustfs # or s3, gcs
48+ microvm:
49+ enabled: true
50+ assetBucket: my-assets # for s3 or gcs
51+ timeout: 10m # each wait for workloads
52+ releases: # Helm values
53+ all: # every release except substrate-crds
54+ valuesFiles: # relative to the config file
55+ - gke.yaml
56+ values: # wins over valuesFiles
57+ image:
58+ tag: v1.0.0
59+ podcert: {} # substrate-podcert
60+ atelet: {} # substrate-atelet
61+ substrate: # substrate
62+ values:
63+ otel:
64+ endpoint: http://otel-collector:4317
65+
66+ A release's own valuesFiles and values win over the ones in all.`
67+
4468var installCmd = & cobra.Command {
4569 Use : "install" ,
4670 Short : "Install Agent Substrate from its Helm charts" ,
@@ -50,28 +74,39 @@ Installs the substrate-crds, substrate-podcert, substrate-atelet and substrate
5074Helm releases in order, and creates the CA pools and secrets they need.
5175Existing secrets are kept. Rerunning install resumes a failed install.
5276
53- By default it also installs a single-replica PostgreSQL for evaluation. For
54- anything else, create the database connection Secrets described in
55- charts/substrate/README.md and pass --postgres external .
77+ Without --config, install uses a single-replica PostgreSQL for evaluation and
78+ a RustFS for snapshots. On a terminal it asks for these choices first and
79+ prints the matching config .
5680
57- Snapshots go to a RustFS the substrate release runs. To use a bucket instead,
58- pass --object-store s3 or gcs with a values file that sets objectStore.*,
59- ateApi.snapshotLocation and, for GCS, objectStorePlugin.transferServiceAccount.
81+ For anything but evaluation, create the database connection Secrets
82+ described in charts/substrate/README.md and set postgres: external. To store
83+ snapshots in a bucket, set objectStore to s3 or gcs and set the charts'
84+ objectStore.*, ateApi.snapshotLocation and, for GCS,
85+ objectStorePlugin.transferServiceAccount values.
6086
61- -- microvm downloads the Kata and Cloud Hypervisor guest assets for each node
62- architecture, caches them under the user cache directory, uploads them to the
63- object store, and applies the microvm SandboxConfig. Upgrade restages them.
87+ microvm.enabled downloads the Kata and Cloud Hypervisor guest assets for each
88+ node architecture, caches them under the user cache directory, uploads them to
89+ the object store, and applies the microvm SandboxConfig. Upgrade restages them.
6490
65- On a terminal with no flags set, install asks for these choices first.` ,
91+ ` + configHelp ,
92+ Example : ` kubectl ate install -c install.yaml
93+ kubectl ate install -c - <<EOF
94+ microvm:
95+ enabled: true
96+ EOF` ,
6697 Args : cobra .NoArgs ,
6798 RunE : func (cmd * cobra.Command , _ []string ) error {
68- opts := installOpts
69- if wizardWanted (cmd ) {
70- if err := tui .Wizard (cmd .ErrOrStderr (), & opts ); err != nil {
99+ opts , tty , err := options (cmd )
100+ if err != nil {
101+ return err
102+ }
103+ defer closeTerminal (tty )
104+ if useTUI (cmd , tty ) && configPath == "" {
105+ if err := tui .Wizard (cmd .ErrOrStderr (), & opts .Config ); err != nil {
71106 return err
72107 }
73108 }
74- return runInstaller (cmd , opts , "install" , (* install .Installer ).Install ,
109+ return runInstaller (cmd , opts , tty , "install" , (* install .Installer ).Install ,
75110 "Agent Substrate is installed.\n Check it with: kubectl ate status" )
76111 },
77112}
@@ -84,16 +119,21 @@ kubectl ate: the certificate controller, atelet, the CRDs, then the substrate
84119release. It doesn't roll WorkerPool worker images, which
85120docs/reference/version-skew-policy.md upgrades after atelet.
86121
87- Each release keeps the values it was installed with. Values passed with -f and
88- --set are layered on top. Upgrade refuses downgrades, skipped minor releases,
89- and CRD changes that would strand stored objects.
122+ Each release keeps the values it was installed with. The config's
123+ releases values are layered on top. Upgrade also reads timeout and
124+ microvm.assetBucket from the config. It refuses downgrades, skipped minor
125+ releases, and CRD changes that would strand stored objects.
90126
91127--dry-run prints each release's manifest diff and changes nothing.` ,
92128 Args : cobra .NoArgs ,
93129 RunE : func (cmd * cobra.Command , _ []string ) error {
94- opts := upgradeOpts
95- opts .Out = cmd .OutOrStdout ()
96- return runInstaller (cmd , opts , "upgrade" , (* install .Installer ).Upgrade ,
130+ opts , tty , err := options (cmd )
131+ if err != nil {
132+ return err
133+ }
134+ defer closeTerminal (tty )
135+ opts .DryRun , opts .Out = upgradeDryRun , cmd .OutOrStdout ()
136+ return runInstaller (cmd , opts , tty , "upgrade" , (* install .Installer ).Upgrade ,
97137 "Agent Substrate is upgraded." )
98138 },
99139}
@@ -105,31 +145,88 @@ var uninstallCmd = &cobra.Command{
105145
106146Deletes every WorkerPool first, so ate-controller can finalize them, then
107147uninstalls the releases in the reverse of install order and removes the
108- bundled PostgreSQL.
148+ bundled PostgreSQL. It reads only timeout from the config.
109149
110150CRDs, CA keys, secrets, the database volume and the RustFS volume stay unless
111151--include-crds or --purge are set, so a later install keeps its identities and
112152data.` ,
113153 Args : cobra .NoArgs ,
114154 RunE : func (cmd * cobra.Command , _ []string ) error {
115- opts := uninstallOpts
155+ opts , tty , err := options (cmd )
156+ if err != nil {
157+ return err
158+ }
159+ defer closeTerminal (tty )
160+ opts .IncludeCRDs , opts .Purge = uninstallOpts .IncludeCRDs , uninstallOpts .Purge
116161 if ! uninstallYes {
117- opts .Confirm = confirmFunc (cmd )
162+ opts .Confirm = confirmFunc (cmd , tty )
118163 }
119- return runInstaller (cmd , opts , "uninstall" , (* install .Installer ).Uninstall ,
164+ return runInstaller (cmd , opts , tty , "uninstall" , (* install .Installer ).Uninstall ,
120165 "Agent Substrate is uninstalled." )
121166 },
122167}
123168
124- // confirmFunc asks on the terminal. Without one it refuses, so scripts must
125- // pass --yes. Ctrl+C cancels ctx, which ends the wait for an answer.
126- func confirmFunc (cmd * cobra.Command ) func (context.Context , string ) (bool , error ) {
169+ // options reads the config and finds the terminal to read keys and answers
170+ // from. Without --config the config is install.DefaultConfig. The terminal is
171+ // nil when there is none. Close it with closeTerminal.
172+ func options (cmd * cobra.Command ) (install.Options , * os.File , error ) {
173+ opts := install.Options {ChartDir : chartDir }
174+ var err error
175+ switch configPath {
176+ case "" :
177+ opts .Config = install .DefaultConfig ()
178+ case "-" :
179+ opts .Config , err = install .ReadConfig (cmd .InOrStdin (), "." )
180+ default :
181+ var f * os.File
182+ if f , err = os .Open (configPath ); err == nil {
183+ opts .Config , err = install .ReadConfig (f , filepath .Dir (configPath ))
184+ f .Close ()
185+ }
186+ }
187+ if err != nil {
188+ return opts , nil , fmt .Errorf ("while reading %s: %w" , configName (), err )
189+ }
190+ return opts , terminal (cmd ), nil
191+ }
192+
193+ func configName () string {
194+ if configPath == "-" {
195+ return "config from stdin"
196+ }
197+ return configPath
198+ }
199+
200+ // terminal returns stdin if it's a terminal. When the config came from stdin
201+ // it opens the controlling terminal instead. It returns nil if neither works.
202+ func terminal (cmd * cobra.Command ) * os.File {
203+ if configPath == "-" {
204+ f , err := os .OpenFile ("/dev/tty" , os .O_RDWR , 0 )
205+ if err != nil {
206+ return nil
207+ }
208+ return f
209+ }
210+ if f , ok := cmd .InOrStdin ().(* os.File ); ok && isTerminal (f ) {
211+ return f
212+ }
213+ return nil
214+ }
215+
216+ func closeTerminal (tty * os.File ) {
217+ if tty != nil && configPath == "-" {
218+ tty .Close ()
219+ }
220+ }
221+
222+ // confirmFunc asks on tty. Without one it refuses, so scripts must pass
223+ // --yes. Ctrl+C cancels ctx, which ends the wait for an answer.
224+ func confirmFunc (cmd * cobra.Command , tty * os.File ) func (context.Context , string ) (bool , error ) {
127225 return func (ctx context.Context , summary string ) (bool , error ) {
128226 w := cmd .ErrOrStderr ()
129227 fmt .Fprint (w , summary )
130- f , ok := cmd .InOrStdin ().(* os.File )
131- if ! ok || ! isTerminal (f ) {
132- return false , errors .New ("stdin isn't a terminal; pass --yes to uninstall" )
228+ if tty == nil {
229+ return false , errors .New ("no terminal to ask on; pass --yes to uninstall" )
133230 }
134231 fmt .Fprint (w , "Uninstall? [y/N] " )
135232 type line struct {
@@ -138,7 +235,7 @@ func confirmFunc(cmd *cobra.Command) func(context.Context, string) (bool, error)
138235 }
139236 read := make (chan line , 1 )
140237 go func () {
141- s , err := bufio .NewReader (f ).ReadString ('\n' )
238+ s , err := bufio .NewReader (tty ).ReadString ('\n' )
142239 read <- line {s , err }
143240 }()
144241 var l line
@@ -181,11 +278,11 @@ func newInstallerWith(cmd *cobra.Command, opts install.Options, r install.Report
181278 return install .New (opts , r )
182279}
183280
184- // runInstaller runs fn with the TUI when stderr is a terminal , and with plain
281+ // runInstaller runs fn with the TUI when useTUI allows it , and with plain
185282// lines otherwise. The TUI shows the error itself, so the returned error is
186283// marked as reported.
187- func runInstaller (cmd * cobra.Command , opts install.Options , title string , fn func (* install.Installer , context.Context ) error , summary string ) error {
188- if ! useTUI (cmd ) || opts .DryRun {
284+ func runInstaller (cmd * cobra.Command , opts install.Options , tty * os. File , title string , fn func (* install.Installer , context.Context ) error , summary string ) error {
285+ if ! useTUI (cmd , tty ) || opts .DryRun {
189286 in , err := newInstaller (cmd , opts )
190287 if err != nil {
191288 return err
@@ -194,7 +291,7 @@ func runInstaller(cmd *cobra.Command, opts install.Options, title string, fn fun
194291 }
195292 ctx , cancel := signal .NotifyContext (cmd .Context (), os .Interrupt )
196293 defer cancel ()
197- r := tui .New (cmd .ErrOrStderr (), title , cancel )
294+ r := tui .New (tty , cmd .ErrOrStderr (), title , cancel )
198295 in , err := newInstallerWith (cmd , opts , r )
199296 if err != nil {
200297 return err
@@ -206,54 +303,30 @@ func runInstaller(cmd *cobra.Command, opts install.Options, title string, fn fun
206303 return err
207304}
208305
209- // useTUI reports whether stdin and stderr are terminals and nothing asked for
210- // plain output. Helm logs at -v would interleave with the TUI.
211- func useTUI (cmd * cobra.Command ) bool {
212- return ! noTUI && verbosity == 0 && isTerminal (cmd .ErrOrStderr ()) && isTerminal (cmd .InOrStdin ())
213- }
214-
215- // wizardWanted reports whether install should ask for its options: on a
216- // terminal with no install flags set.
217- func wizardWanted (cmd * cobra.Command ) bool {
218- return useTUI (cmd ) && ! localFlagSet (cmd )
219- }
220-
221- // localFlagSet reports whether any of cmd's own flags was set. The FlagSet
222- // LocalNonPersistentFlags returns doesn't count set flags, so check each one.
223- func localFlagSet (cmd * cobra.Command ) bool {
224- set := false
225- cmd .LocalNonPersistentFlags ().VisitAll (func (f * pflag.Flag ) { set = set || f .Changed })
226- return set
306+ // useTUI reports whether there is a terminal for keys, stderr is a terminal,
307+ // and nothing asked for plain output. Helm logs at -v would interleave with
308+ // the TUI.
309+ func useTUI (cmd * cobra.Command , tty * os.File ) bool {
310+ return ! noTUI && verbosity == 0 && tty != nil && isTerminal (cmd .ErrOrStderr ())
227311}
228312
229313func isTerminal (v any ) bool {
230314 f , ok := v .(* os.File )
231315 return ok && term .IsTerminal (int (f .Fd ()))
232316}
233317
234- // addChartFlags adds the flags install and upgrade share.
235- func addChartFlags (f * pflag.FlagSet , opts * install.Options ) {
236- f .StringVar (& opts .ChartDir , "chart-dir" , "" , "Load charts from this directory instead of the ones built into kubectl ate" )
237- f .StringArrayVarP (& opts .ValuesFiles , "values" , "f" , nil , "Values file for every release except substrate-crds (repeatable)" )
238- f .StringArrayVar (& opts .Values , "set" , nil , "Set a value for every release except substrate-crds, as key=value (repeatable)" )
239- f .DurationVar (& opts .Timeout , "timeout" , 10 * time .Minute , "How long to wait for each release's workloads" )
240- }
241-
242318func init () {
243- addChartFlags (installCmd .Flags (), & installOpts )
244- installCmd .Flags ().StringVar ((* string )(& installOpts .Postgres ), "postgres" , string (install .PostgresBundled ), "PostgreSQL to use: bundled (evaluation only) or external" )
245- installCmd .Flags ().StringVar ((* string )(& installOpts .ObjectStore ), "object-store" , string (install .ObjectStoreRustFS ), "Snapshot storage: rustfs (in cluster), s3 or gcs" )
246- installCmd .Flags ().BoolVar (& installOpts .MicroVM , "microvm" , false , "Also install the microvm sandbox class: download its guest assets, stage them in the object store and apply the microvm SandboxConfig" )
247- installCmd .Flags ().StringVar (& installOpts .MicroVMAssetBucket , "microvm-asset-bucket" , "" , "Bucket for microVM assets with --object-store s3 or gcs; atelet must be able to read it" )
248- addChartFlags (upgradeCmd .Flags (), & upgradeOpts )
249- upgradeCmd .Flags ().BoolVar (& upgradeOpts .DryRun , "dry-run" , false , "Print each release's manifest diff without upgrading" )
319+ for _ , c := range []* cobra.Command {installCmd , upgradeCmd , uninstallCmd } {
320+ c .Flags ().StringVarP (& configPath , "config" , "c" , "" , "Install config file, or - for stdin" )
321+ c .Flags ().BoolVar (& noTUI , "no-tui" , false , "Print plain progress lines instead of the interactive view" )
322+ }
323+ for _ , c := range []* cobra.Command {installCmd , upgradeCmd } {
324+ c .Flags ().StringVar (& chartDir , "chart-dir" , "" , "Load charts from this directory instead of the ones built into kubectl ate" )
325+ }
326+ upgradeCmd .Flags ().BoolVar (& upgradeDryRun , "dry-run" , false , "Print each release's manifest diff without upgrading" )
250327 f := uninstallCmd .Flags ()
251328 f .BoolVar (& uninstallOpts .IncludeCRDs , "include-crds" , false , "Also delete the CRDs, with every WorkerPool, SandboxConfig and CSIDriverConfig" )
252329 f .BoolVar (& uninstallOpts .Purge , "purge" , false , "Also delete the namespaces, CA keys, secrets, database volume and trust bundles" )
253330 f .BoolVarP (& uninstallYes , "yes" , "y" , false , "Don't ask for confirmation" )
254- f .DurationVar (& uninstallOpts .Timeout , "timeout" , 10 * time .Minute , "How long to wait for each step" )
255- for _ , c := range []* cobra.Command {installCmd , upgradeCmd , uninstallCmd } {
256- c .Flags ().BoolVar (& noTUI , "no-tui" , false , "Print plain progress lines instead of the interactive view" )
257- }
258331 rootCmd .AddCommand (installCmd , upgradeCmd , uninstallCmd , statusCmd )
259332}
0 commit comments