Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions chaoslib/litmus/pod-dns-chaos/helper/dnschaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"bytes"
"context"
"fmt"
"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/telemetry"
"github.com/palantir/stacktrace"
"go.opentelemetry.io/otel"
"os"
"os/exec"
"os/signal"
Expand All @@ -16,6 +12,11 @@ import (
"syscall"
"time"

"github.com/litmuschaos/litmus-go/pkg/cerrors"
"github.com/litmuschaos/litmus-go/pkg/telemetry"
"github.com/palantir/stacktrace"
"go.opentelemetry.io/otel"

clients "github.com/litmuschaos/litmus-go/pkg/clients"
"github.com/litmuschaos/litmus-go/pkg/events"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/pod-dns-chaos/types"
Expand Down
7 changes: 7 additions & 0 deletions chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ import (
func PrepareAndInjectChaos(ctx context.Context, experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {
ctx, span := otel.Tracer(telemetry.TracerName).Start(ctx, "PreparePodDNSFault")
defer span.End()

// Validate the experiment target hostnames
var err error
experimentsDetails.TargetHostNames, err = stringutils.ParseHostnames(experimentsDetails.TargetHostNames)
if err != nil {
return stacktrace.Propagate(err, "failed to parse target hostnames")
}
// Get the target pod details for the chaos execution
// if the target pod is not defined it will derive the random target pod list using pod affected percentage
if experimentsDetails.TargetPods == "" && chaosDetails.AppDetail == nil {
Expand Down
41 changes: 41 additions & 0 deletions pkg/utils/stringutils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package stringutils
import (
"math/rand"
"time"
"strings"
"fmt"
"encoding/json"
)

const (
Expand Down Expand Up @@ -33,3 +36,41 @@ func RandStringBytesMask(n int, src rand.Source) string {

return string(b)
}

func ParseHostnames(input string) (string, error) {

if(input == "") {
return input, nil
}

var parsed []string

// Try JSON parse first
if(strings.HasPrefix(input, "[") && strings.HasSuffix(input, "]")) {
err := json.Unmarshal([]byte(input), &parsed)
if err == nil {
// Input was valid JSON list of strings
return input, nil
} else {
return "", fmt.Errorf("failed to parse input as JSON: %v", err)
}
}


// If not in JSON format, fallback to comma-separated logic
parts := strings.Split(input, ",")
for part := range parts {
part = strings.TrimSpace(part)
if part != "" {
parsed = append(parsed, part)
}
}

// Rebuild as JSON array of strings
jsonBytes, err := json.Marshal(parsed)
if err != nil {
return "", fmt.Errorf("failed to format names: %v", err)
}

return string(jsonBytes), nil
}
Loading