Skip to content

Commit

Permalink
Merge branch 'main' into reduce-utils-size
Browse files Browse the repository at this point in the history
  • Loading branch information
Racer159 authored Jul 6, 2023
2 parents ed1ba9d + dae4961 commit abb75fa
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ zarf tools wait-for { KIND | PROTOCOL } { NAME | SELECTOR | URI } { CONDITION |
zarf tools wait-for svc zarf-docker-registry exists -n zarf # wait for service zarf-docker-registry in namespace zarf to exist
zarf tools wait-for svc zarf-docker-registry -n zarf # same as above, except exists is the default condition
zarf tools wait-for crd addons.k3s.cattle.io # wait for crd addons.k3s.cattle.io to exist
zarf tools wait-for sts test-sts '{.status.availableReplicas}'=23 # wait for statefulset test-sts to have 23 available replicas
Wait for network endpoints:
zarf tools wait-for http localhost:8080 200 # wait for a 200 response from http://localhost:8080
Expand Down
2 changes: 1 addition & 1 deletion docs/3-create-a-zarf-package/4-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ Must be one of:
 
<blockquote>

**Description:** The condition to wait for; defaults to exist
**Description:** The condition or jsonpath state to wait for; defaults to exist

| | |
| -------- | -------- |
Expand Down
2 changes: 1 addition & 1 deletion docs/gen-cli-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ for FILE in `find docs/2-the-zarf-cli/100-cli-commands -name "*.md"`
do
sed -i.bak 's/^##/#/g' ${FILE}
sed -i.bak '2s/^/<!-- Auto-generated by docs\/gen-cli-docs.sh -->\n/' ${FILE}
sed -i.bak ':a;N;$!ba;s/\n$//' ${FILE}
truncate -s -1 ${FILE}
rm ${FILE}.bak
done
159 changes: 2 additions & 157 deletions src/cmd/tools/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
package tools

import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/spf13/cobra"

// Import to initialize client auth plugins.
Expand Down Expand Up @@ -50,160 +44,11 @@ var waitForCmd = &cobra.Command{
condition = args[2]
}

// Handle network endpoints.
switch kind {
case "http", "https", "tcp":
waitForNetworkEndpoint(kind, identifier, condition, timeout)
return
}

// Get the Zarf executable path.
zarfBinPath, err := utils.GetFinalExecutablePath()
if err != nil {
message.Fatal(err, lang.CmdToolsWaitForErrZarfPath)
}

// If the identifier contains an equals sign, convert to a label selector.
identifierMsg := fmt.Sprintf("/%s", identifier)
if strings.ContainsRune(identifier, '=') {
identifierMsg = fmt.Sprintf(" with label `%s`", identifier)
identifier = fmt.Sprintf("-l %s", identifier)
}

// Set the timeout for the wait-for command.
expired := time.After(timeout)

// Set the custom message for optional namespace.
namespaceMsg := ""
if waitNamespace != "" {
namespaceMsg = fmt.Sprintf(" in namespace %s", waitNamespace)
}

// Setup the spinner messages.
conditionMsg := fmt.Sprintf("Waiting for %s%s%s to be %s.", kind, identifierMsg, namespaceMsg, condition)
existMsg := fmt.Sprintf("Waiting for %s%s%s to exist.", kind, identifierMsg, namespaceMsg)
spinner := message.NewProgressSpinner(existMsg)
defer spinner.Stop()

for {
// Delay the check for 1 second
time.Sleep(time.Second)

select {
case <-expired:
message.Fatal(nil, lang.CmdToolsWaitForErrTimeout)

default:
spinner.Updatef(existMsg)
// Check if the resource exists.
args := []string{"tools", "kubectl", "get", "-n", waitNamespace, kind, identifier}
if stdout, stderr, err := exec.Cmd(zarfBinPath, args...); err != nil {
message.Debug(stdout, stderr, err)
continue
}

// If only checking for existence, exit here.
switch condition {
case "", "exist", "exists":
spinner.Success()
return
}

spinner.Updatef(conditionMsg)
// Wait for the resource to meet the given condition.
args = []string{"tools", "kubectl", "wait", "-n", waitNamespace,
kind, identifier, "--for", "condition=" + condition,
"--timeout=" + waitTimeout}

// If there is an error, log it and try again.
if stdout, stderr, err := exec.Cmd(zarfBinPath, args...); err != nil {
message.Debug(stdout, stderr, err)
continue
}

// And just like that, success!
spinner.Successf(conditionMsg)
return
}
}
// Execute the wait command.
utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout)
},
}

func waitForNetworkEndpoint(resource, name, condition string, timeout time.Duration) {
// Set the timeout for the wait-for command.
expired := time.After(timeout)

// Setup the spinner messages.
condition = strings.ToLower(condition)
if condition == "" {
condition = "success"
}
spinner := message.NewProgressSpinner("Waiting for network endpoint %s://%s to respond %s.", resource, name, condition)
defer spinner.Stop()

delay := 100 * time.Millisecond

for {
// Delay the check for 100ms the first time and then 1 second after that.
time.Sleep(delay)
delay = time.Second

select {
case <-expired:
message.Fatal(nil, lang.CmdToolsWaitForErrTimeout)

default:
switch resource {

case "http", "https":
// Handle HTTP and HTTPS endpoints.
url := fmt.Sprintf("%s://%s", resource, name)

// Default to checking for a 2xx response.
if condition == "success" {
// Try to get the URL and check the status code.
resp, err := http.Get(url)

// If the status code is not in the 2xx range, try again.
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
message.Debug(err)
continue
}

// Success, break out of the swtich statement.
break
}

// Convert the condition to an int and check if it's a valid HTTP status code.
code, err := strconv.Atoi(condition)
if err != nil || http.StatusText(code) == "" {
message.Fatalf(err, lang.CmdToolsWaitForErrConditionString, condition)
}

// Try to get the URL and check the status code.
resp, err := http.Get(url)
if err != nil || resp.StatusCode != code {
message.Debug(err)
continue
}

default:
// Fallback to any generic protocol using net.Dial
conn, err := net.Dial(resource, name)
if err != nil {
message.Debug(err)
continue
}
defer conn.Close()
}

// Yay, we made it!
spinner.Success()
return
}
}
}

func init() {
toolsCmd.AddCommand(waitForCmd)
waitForCmd.Flags().StringVar(&waitTimeout, "timeout", "5m", lang.CmdToolsWaitForFlagTimeout)
Expand Down
1 change: 1 addition & 0 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ const (
zarf tools wait-for svc zarf-docker-registry exists -n zarf # wait for service zarf-docker-registry in namespace zarf to exist
zarf tools wait-for svc zarf-docker-registry -n zarf # same as above, except exists is the default condition
zarf tools wait-for crd addons.k3s.cattle.io # wait for crd addons.k3s.cattle.io to exist
zarf tools wait-for sts test-sts '{.status.availableReplicas}'=23 # wait for statefulset test-sts to have 23 available replicas
Wait for network endpoints:
zarf tools wait-for http localhost:8080 200 # wait for a 200 response from http://localhost:8080
Expand Down
Loading

0 comments on commit abb75fa

Please sign in to comment.