From e1c43376ca9e425c55d0f5fb74bf62580c7a7e6d Mon Sep 17 00:00:00 2001 From: UncleGedd <42304551+UncleGedd@users.noreply.github.com> Date: Wed, 27 Mar 2024 08:40:30 -0500 Subject: [PATCH] fix: err when deploying with BubbleTea with no cluster (#527) --- .github/actions/save-logs/action.yaml | 4 +-- .github/workflows/nightly-uds-core.yaml | 7 ++--- src/cmd/uds.go | 2 +- src/pkg/bundle/common.go | 20 +++++---------- src/pkg/bundle/tui/common.go | 10 ++++++++ src/pkg/bundle/tui/deploy/handlers.go | 32 +++++++++++++++++------ src/pkg/bundle/tui/deploy/model.go | 18 +++++++------ src/pkg/bundle/tui/deploy/views.go | 34 +++++++++++-------------- 8 files changed, 72 insertions(+), 55 deletions(-) diff --git a/.github/actions/save-logs/action.yaml b/.github/actions/save-logs/action.yaml index 8d5e838a..0b0fd2f5 100644 --- a/.github/actions/save-logs/action.yaml +++ b/.github/actions/save-logs/action.yaml @@ -6,10 +6,10 @@ runs: steps: - name: Fix log permissions run: | - sudo chown $USER /tmp/uds-*.log || echo "" + sudo chown $USER /tmp/zarf-*.log || echo "" shell: bash - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 with: name: debug-log - path: /tmp/uds-*.log + path: /tmp/zarf-*.log diff --git a/.github/workflows/nightly-uds-core.yaml b/.github/workflows/nightly-uds-core.yaml index 074bcede..0a9b2fd3 100644 --- a/.github/workflows/nightly-uds-core.yaml +++ b/.github/workflows/nightly-uds-core.yaml @@ -31,12 +31,13 @@ jobs: run: | chmod +x build/uds - - name: Setup K3d - uses: ./.github/actions/k3d + - name: install-k3d + run: "curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash" + shell: bash - name: Deploy UDS Core bundle # renovate: datasource=github-tags depName=defenseunicorns/uds-core versioning=semver - run: build/uds deploy ghcr.io/defenseunicorns/packages/uds/bundles/k3d-core-istio-dev:0.13.1 --confirm --no-progress + run: build/uds deploy k3d-core-istio-dev:0.16.1 --confirm shell: bash - name: Validate UDS Core deployment diff --git a/src/cmd/uds.go b/src/cmd/uds.go index 964861ef..68dd4981 100644 --- a/src/cmd/uds.go +++ b/src/cmd/uds.go @@ -221,7 +221,7 @@ var logsCmd = &cobra.Command{ Use: "logs", Aliases: []string{"l"}, Short: "Display log file contents", - Run: func(cmd *cobra.Command, args []string) { + Run: func(_ *cobra.Command, _ []string) { logFilePath := filepath.Join(config.CommonOptions.CachePath, config.CachedLogs) // Open the cached log file diff --git a/src/pkg/bundle/common.go b/src/pkg/bundle/common.go index 5130f356..d6a2548e 100644 --- a/src/pkg/bundle/common.go +++ b/src/pkg/bundle/common.go @@ -16,7 +16,6 @@ import ( "github.com/defenseunicorns/uds-cli/src/config" "github.com/defenseunicorns/uds-cli/src/pkg/bundler/fetcher" "github.com/defenseunicorns/uds-cli/src/types" - "github.com/defenseunicorns/zarf/src/config/lang" "github.com/defenseunicorns/zarf/src/pkg/cluster" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/oci" @@ -264,22 +263,15 @@ func ValidateBundleSignature(bundleYAMLPath, signaturePath, publicKeyPath string return zarfUtils.CosignVerifyBlob(bundleYAMLPath, signaturePath, publicKeyPath) } -// GetDeployedPackages returns packages that have been deployed -func GetDeployedPackages() ([]zarfTypes.DeployedPackage, error) { - cluster := cluster.NewClusterOrDie() - deployedPackages, errs := cluster.GetDeployedZarfPackages() - if len(errs) > 0 { - return nil, lang.ErrUnableToGetPackages - } - return deployedPackages, nil -} - // GetDeployedPackageNames returns the names of the packages that have been deployed func GetDeployedPackageNames() []string { var deployedPackageNames []string - deployedPackages, _ := GetDeployedPackages() - for _, pkg := range deployedPackages { - deployedPackageNames = append(deployedPackageNames, pkg.Name) + c, _ := cluster.NewCluster() + if c != nil { + deployedPackages, _ := c.GetDeployedZarfPackages() + for _, pkg := range deployedPackages { + deployedPackageNames = append(deployedPackageNames, pkg.Name) + } } return deployedPackageNames } diff --git a/src/pkg/bundle/tui/common.go b/src/pkg/bundle/tui/common.go index 3cb54886..cdb47c40 100644 --- a/src/pkg/bundle/tui/common.go +++ b/src/pkg/bundle/tui/common.go @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The UDS Authors +// Package tui contains logic for the TUI operations package tui import ( @@ -10,11 +11,20 @@ import ( "github.com/charmbracelet/lipgloss" ) +const ( + // LIGHTBLUE is the common light blue color used in the TUI + LIGHTBLUE = lipgloss.Color("#4BFDEB") + + // LIGHTGRAY is the common light gray color used in the TUI + LIGHTGRAY = lipgloss.Color("#7A7A78") +) + var ( // IndentStyle is the style for indenting text IndentStyle = lipgloss.NewStyle().Padding(0, 4) ) +// Pause pauses the TUI for a short period of time func Pause() tea.Cmd { return tea.Tick(time.Millisecond*500, func(_ time.Time) tea.Msg { return nil diff --git a/src/pkg/bundle/tui/deploy/handlers.go b/src/pkg/bundle/tui/deploy/handlers.go index 5f6ebe76..64ee5795 100644 --- a/src/pkg/bundle/tui/deploy/handlers.go +++ b/src/pkg/bundle/tui/deploy/handlers.go @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The UDS Authors +// Package deploy contains the TUI logic for bundle deploys package deploy import ( @@ -14,13 +15,20 @@ import ( "github.com/defenseunicorns/uds-cli/src/config" "github.com/defenseunicorns/uds-cli/src/pkg/bundle/tui" "github.com/defenseunicorns/uds-cli/src/pkg/utils" + "github.com/defenseunicorns/zarf/src/pkg/cluster" "github.com/defenseunicorns/zarf/src/pkg/message" zarfTypes "github.com/defenseunicorns/zarf/src/types" ) -func (m *model) handleNewPackage(pkgName string, currentPkgIdx int) tea.Cmd { - // see if pkg has already been deployed - deployedPkg, _ := c.GetDeployedPackage(pkgName) +func (m *Model) handleNewPackage(pkgName string, currentPkgIdx int) tea.Cmd { + // check if pkg has already been deployed + var deployedPkg *zarfTypes.DeployedPackage + if c != nil { + deployedPkg, _ = c.GetDeployedPackage(pkgName) + } else { + // keep checking for cluster connectivity + c, _ = cluster.NewCluster() + } newPkg := pkgState{ name: pkgName, } @@ -56,7 +64,7 @@ func (m *model) handleNewPackage(pkgName string, currentPkgIdx int) tea.Cmd { ) } -func (m *model) handlePreDeploy() tea.Cmd { +func (m *Model) handlePreDeploy() tea.Cmd { cmd := func() tea.Msg { name, bundleYAML, source, err := m.bndlClient.PreDeployValidation() if err != nil { @@ -75,7 +83,7 @@ func (m *model) handlePreDeploy() tea.Cmd { return cmd } -func (m *model) handleDeploy() tea.Cmd { +func (m *Model) handleDeploy() tea.Cmd { // ensure bundle deployment is confirmed and is only being deployed once if m.confirmed && !m.deploying { // run Deploy concurrently so we can update the TUI while it runs @@ -97,7 +105,7 @@ func (m *model) handleDeploy() tea.Cmd { return nil } -func (m *model) handleDone(err error) tea.Cmd { +func (m *Model) handleDone(err error) tea.Cmd { cmds := []tea.Cmd{tea.Println(), tea.Println(m.udsTitle()), tea.Println()} m.done = true // remove the current view cmds = append(cmds, genSuccessCmds(m)...) @@ -116,7 +124,7 @@ func (m *model) handleDone(err error) tea.Cmd { return tea.Sequence(cmds...) } -func (m *model) handleDeployTick() (tea.Model, tea.Cmd) { +func (m *Model) handleDeployTick() (tea.Model, tea.Cmd) { // check if all pkgs are complete numComplete := 0 if len(m.packages) == m.totalPkgs { @@ -141,7 +149,15 @@ func (m *model) handleDeployTick() (tea.Model, tea.Cmd) { if p.complete { continue } - deployedPkg, _ := c.GetDeployedPackage(p.name) + + var deployedPkg *zarfTypes.DeployedPackage + if c != nil { + deployedPkg, _ = c.GetDeployedPackage(p.name) + } else { + // keep checking for cluster connectivity + c, _ = cluster.NewCluster() + } + // if deployedPkg is nil, the package hasn't been deployed yet if deployedPkg == nil { break diff --git a/src/pkg/bundle/tui/deploy/model.go b/src/pkg/bundle/tui/deploy/model.go index e9e98d24..da92b8e3 100644 --- a/src/pkg/bundle/tui/deploy/model.go +++ b/src/pkg/bundle/tui/deploy/model.go @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The UDS Authors +// Package deploy contains the TUI logic for bundle deploys package deploy import ( @@ -35,6 +36,7 @@ const ( ) var ( + // Program is the Bubbletea TUI main program Program *tea.Program c *cluster.Cluster logVpWidthScale = 0.9 @@ -65,7 +67,7 @@ type pkgState struct { isRemote bool } -type model struct { +type Model struct { bndlClient bndlClientShim bundleYAML string doneChan chan int @@ -86,7 +88,8 @@ type model struct { validatingBundleSpinner spinner.Model } -func InitModel(client bndlClientShim) model { +// InitModel initializes the model for the TUI +func InitModel(client bndlClientShim) Model { var confirmed bool var inProgress bool var isRemoteBundle bool @@ -120,7 +123,7 @@ func InitModel(client bndlClientShim) model { numYAMLLines := 10 yamlViewport := viewport.New(termWidth, numYAMLLines) - return model{ + return Model{ bndlClient: client, doneChan: make(chan int), errChan: make(chan error), @@ -134,13 +137,13 @@ func InitModel(client bndlClientShim) model { } } -func (m *model) Init() tea.Cmd { +func (m *Model) Init() tea.Cmd { return func() tea.Msg { return doPreDeploy } } -func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { select { case err := <-m.errChan: cmd := m.handleDone(err) @@ -273,7 +276,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } -func (m *model) View() string { +func (m *Model) View() string { if m.done { // no errors, clear the controlled Program's output return "" @@ -284,7 +287,6 @@ func (m *model) View() string { return fmt.Sprintf("\n%s\n\n%s\n%s\n\n%s\n", m.udsTitle(), m.bundleDeployProgress(), logMsg, m.logView()) } else if m.confirmed { return fmt.Sprintf("\n%s\n\n%s\n%s\n%s\n", m.udsTitle(), m.bundleDeployProgress(), logMsg, m.deployView()) - } else { - return fmt.Sprintf("%s\n", m.preDeployView()) } + return fmt.Sprintf("%s\n", m.preDeployView()) } diff --git a/src/pkg/bundle/tui/deploy/views.go b/src/pkg/bundle/tui/deploy/views.go index bcdc2b3c..55ef3357 100644 --- a/src/pkg/bundle/tui/deploy/views.go +++ b/src/pkg/bundle/tui/deploy/views.go @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-FileCopyrightText: 2023-Present The UDS Authors +// Package deploy contains the TUI logic for bundle deploys package deploy import ( @@ -16,17 +17,12 @@ import ( "github.com/goccy/go-yaml/printer" ) -const ( - LIGHTBLUE = lipgloss.Color("#4BFDEB") - LIGHTGRAY = lipgloss.Color("#7A7A78") -) - var ( termWidth int termHeight int styledCheck = lipgloss.NewStyle().Foreground(lipgloss.Color("#00FF00")).Render("✔") - lightBlueText = lipgloss.NewStyle().Foreground(LIGHTBLUE) - lightGrayText = lipgloss.NewStyle().Foreground(LIGHTGRAY) + lightBlueText = lipgloss.NewStyle().Foreground(tui.LIGHTBLUE) + lightGrayText = lipgloss.NewStyle().Foreground(tui.LIGHTGRAY) logMsg = tui.IndentStyle.Render(fmt.Sprintf("\n%s %s", lightBlueText.Render(""), lightGrayText.Render("Toggle logs"))) ) @@ -39,16 +35,16 @@ var ( }() ) -func (m *model) logView() string { +func (m *Model) logView() string { headerMsg := fmt.Sprintf("%s %s", lightBlueText.Render(m.packages[m.pkgIdx].name), lightGrayText.Render("package logs")) return tui.IndentStyle.Render( fmt.Sprintf("%s\n%s\n%s\n\n", m.logHeaderView(headerMsg), m.logViewport.View(), m.logFooterView()), ) } -func (m *model) yamlHeaderView() string { +func (m *Model) yamlHeaderView() string { upArrow := "▲ " - styledUpArrow := lipgloss.NewStyle().Foreground(LIGHTGRAY).Render(upArrow) + styledUpArrow := lipgloss.NewStyle().Foreground(tui.LIGHTGRAY).Render(upArrow) if !m.yamlViewport.AtTop() { styledUpArrow = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFF258")).Render(upArrow) } @@ -56,9 +52,9 @@ func (m *model) yamlHeaderView() string { return lipgloss.JoinHorizontal(lipgloss.Center, styledUpArrow, headerLine) } -func (m *model) yamlFooterView() string { +func (m *Model) yamlFooterView() string { downArrow := "▼ " - styledDownArrow := lipgloss.NewStyle().Foreground(LIGHTGRAY).Render(downArrow) + styledDownArrow := lipgloss.NewStyle().Foreground(tui.LIGHTGRAY).Render(downArrow) if !m.yamlViewport.AtBottom() { styledDownArrow = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFF258")).Render(downArrow) @@ -67,7 +63,7 @@ func (m *model) yamlFooterView() string { return lipgloss.JoinHorizontal(lipgloss.Center, styledDownArrow, footerLine) } -func (m *model) logHeaderView(msg string) string { +func (m *Model) logHeaderView(msg string) string { title := titleStyle.Render(msg) if msg == "" { title = "" @@ -76,12 +72,12 @@ func (m *model) logHeaderView(msg string) string { return lipgloss.JoinHorizontal(lipgloss.Center, title, headerLine) } -func (m *model) logFooterView() string { +func (m *Model) logFooterView() string { footerLine := strings.Repeat("─", max(0, m.logViewport.Width)-1) return lipgloss.JoinHorizontal(lipgloss.Center, footerLine) } -func (m *model) deployView() string { +func (m *Model) deployView() string { view := "" for _, p := range m.packages { // count number of successful components @@ -156,7 +152,7 @@ func genRemotePkgText(p pkgState, numComponentsSuccess int) string { return text } -func (m *model) preDeployView() string { +func (m *Model) preDeployView() string { header := tui.IndentStyle.Render("📦 Bundle Definition (▲ / ▼)") prompt := tui.IndentStyle.Render("❓ Deploy this bundle? (y/n)") prettyYAML := tui.IndentStyle.Render(colorPrintYAML(m.bundleYAML)) @@ -231,7 +227,7 @@ func yamlFormat(attr color.Attribute) string { } // udsTitle returns the title header for the UDS bundle -func (m *model) udsTitle() string { +func (m *Model) udsTitle() string { styledBundleName := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFF258")).Render(m.bundleName + " ") title := " UDS Bundle: " styledTitle := lipgloss.NewStyle().Margin(0, 3). @@ -243,7 +239,7 @@ func (m *model) udsTitle() string { } // genSuccessCmds generates the success or failure messages for each package -func genSuccessCmds(m *model) []tea.Cmd { +func genSuccessCmds(m *Model) []tea.Cmd { var cmds []tea.Cmd for i := 0; i < len(m.packages); i++ { successMsg := fmt.Sprintf("%s Package %s deployed\n", styledCheck, lightBlueText.Render(m.packages[i].name)) @@ -252,7 +248,7 @@ func genSuccessCmds(m *model) []tea.Cmd { return cmds } -func (m *model) bundleDeployProgress() string { +func (m *Model) bundleDeployProgress() string { styledText := lightGrayText.Render("📦 Deploying bundle package") styledPkgCounter := lightGrayText.Render(fmt.Sprintf("(%d / %d)", m.pkgIdx+1, m.totalPkgs)) msg := fmt.Sprintf("%s %s", styledText, styledPkgCounter)