Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] JSONPath feature #1873

Merged
merged 15 commits into from
Jul 6, 2023
Merged
11 changes: 10 additions & 1 deletion src/cmd/tools/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
var (
waitTimeout string
waitNamespace string
waitType string
)

var waitForCmd = &cobra.Command{
Expand All @@ -50,6 +51,13 @@ var waitForCmd = &cobra.Command{
condition = args[2]
}

// Check if waitType is JSONPath or condition
if utils.IsJSONPathWaitType(condition) {
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
waitType = "jsonpath="
} else {
waitType = "condition="
}

// Handle network endpoints.
switch kind {
case "http", "https", "tcp":
Expand Down Expand Up @@ -83,6 +91,7 @@ var waitForCmd = &cobra.Command{
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 {
Expand Down Expand Up @@ -112,7 +121,7 @@ var waitForCmd = &cobra.Command{
spinner.Updatef(conditionMsg)
// Wait for the resource to meet the given condition.
args = []string{"tools", "kubectl", "wait", "-n", waitNamespace,
kind, identifier, "--for", "condition=" + condition,
kind, identifier, "--for", waitType + condition,
"--timeout=" + waitTimeout}

// If there is an error, log it and try again.
Expand Down
9 changes: 9 additions & 0 deletions src/pkg/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ package utils
import (
"fmt"
"regexp"
"strings"
"time"
)

func IsJSONPathWaitType(condition string) bool {
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
if condition[0] != '{' || !strings.Contains(condition, "=") || !strings.Contains(condition, "}") {
return false
}

return true
}

// Unique returns a new slice with only unique elements.
func Unique[T comparable](s []T) (r []T) {
exists := make(map[T]bool)
Expand Down
46 changes: 46 additions & 0 deletions src/pkg/utils/misc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package utils provides generic helper functions.
package utils

import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type TestIsJSONPathWaitTypeSuite struct {
suite.Suite
*require.Assertions
waitTypes testWaitTypes
}

type testWaitTypes struct {
jsonPathType []string
conditionType []string
}

func (suite *TestIsJSONPathWaitTypeSuite) SetupSuite() {
suite.Assertions = require.New(suite.T())

suite.waitTypes.jsonPathType = []string{
"{.status.availableReplicas}=1",
"{.status.containerStatuses[0].ready}=true",
"{.spec.containers[0].ports[0].containerPort}=80",
"{.spec.nodeName}=knode0",
}
suite.waitTypes.conditionType = []string{
"Ready",
"delete",
"",
}
}

func (suite *TestIsJSONPathWaitTypeSuite) Test_0_IsJSONPathWaitType() {
for _, waitType := range suite.waitTypes.conditionType {
suite.True(IsJSONPathWaitType(waitType), "Expected %s to be a JSONPath wait type", waitType)
}
for _, waitType := range suite.waitTypes.jsonPathType {
suite.False(IsJSONPathWaitType(waitType), "Expected %s not to be a JSONPath wait type", waitType)
}
}