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
8 changes: 6 additions & 2 deletions api/v1beta1/pod_replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

package v1beta1

import "strconv"
import (
"strconv"

chaostypes "github.com/DataDog/chaos-controller/types"
)

// PodReplacementSpec represents a pod replacement disruption
type PodReplacementSpec struct {
Expand All @@ -27,7 +31,7 @@ func (s *PodReplacementSpec) Validate() error {
// GenerateArgs generates injection or cleanup pod arguments for the given spec
func (s *PodReplacementSpec) GenerateArgs() []string {
args := []string{
"pod-replacement",
chaostypes.DisruptionKindPodReplacement,
"inject",
}

Expand Down
56 changes: 36 additions & 20 deletions cli/injector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ func initConfig() {
// assign to the pointer to level the new value to persist it after this method
disruptionArgs.Level = chaostypes.DisruptionLevel(disruptionLevelRaw)

// check if we're running pod-replacement command which doesn't need containers
isPodReplacement := false

for _, arg := range os.Args {
if arg == chaostypes.DisruptionKindPodReplacement {
isPodReplacement = true
break
}
}

switch disruptionArgs.Level {
case chaostypes.DisruptionLevelPod:
// check for container ID flag
Expand All @@ -231,33 +241,39 @@ func initConfig() {
return
}

for containerName, containerID := range disruptionArgs.TargetContainers {
// retrieve container info
ctn, err := container.New(containerID, containerName)
if err != nil {
log.Fatalw("can't create container object", tags.ErrorKey, err)
if !isPodReplacement {
// Pod replacement operates at the pod level and doesn't need container information
for containerName, containerID := range disruptionArgs.TargetContainers {
// retrieve container info
ctn, err := container.New(containerID, containerName)
if err != nil {
log.Fatalw("can't create container object", tags.ErrorKey, err)

return
}
return
}

log.Infow("injector targeting container", tags.ContainerIDKey, containerID, tags.ContainerNameKey, containerName)
log.Infow("injector targeting container", tags.ContainerIDKey, containerID, tags.ContainerNameKey, containerName)

pid := ctn.PID()
pid := ctn.PID()

// keep pid for later if this is a chaos handler container
if disruptionArgs.OnInit && ctn.Name() == chaosInitContName {
handlerPID = pid
}
// keep pid for later if this is a chaos handler container
if disruptionArgs.OnInit && ctn.Name() == chaosInitContName {
handlerPID = pid
}

ctns = append(ctns, ctn)
pids = append(pids, pid)
}
ctns = append(ctns, ctn)
pids = append(pids, pid)
}
} else {
// check for pod IP flag
if disruptionArgs.TargetPodIP == "" {
log.Fatal("--target-pod-ip flag must be passed when --level=pod")

// check for pod IP flag
if disruptionArgs.TargetPodIP == "" {
log.Fatal("--target-pod-ip flag must be passed when --level=pod")
return
}

return
pids = []uint32{1}
ctns = []container.Container{nil}
}
case chaostypes.DisruptionLevelNode:
pids = []uint32{1}
Expand Down
6 changes: 4 additions & 2 deletions cli/injector/pod_replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ package main
import (
"strconv"

"github.com/spf13/cobra"

"github.com/DataDog/chaos-controller/api/v1beta1"
"github.com/DataDog/chaos-controller/injector"
"github.com/DataDog/chaos-controller/o11y/tags"
"github.com/spf13/cobra"
chaostypes "github.com/DataDog/chaos-controller/types"
)

var podReplacementCmd = &cobra.Command{
Use: "pod-replacement",
Use: chaostypes.DisruptionKindPodReplacement,
Short: "Pod replacement subcommands",
Run: injectAndWait,
PreRun: func(cmd *cobra.Command, args []string) {
Expand Down
24 changes: 24 additions & 0 deletions examples/demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ spec:
requests:
memory: 16Mi
cpu: 10m
## Create a second container to ensure the pod replace is working as expected
- name: storage-monitor-2
image: alpine:latest
command: ["/bin/sh"]
args:
- -c
- |
echo "Storage monitoring container started"
# Create a test file to demonstrate persistence
echo "demo-storage-$(date)" > /mnt/shared/storage-info.txt
# Keep container running and periodically update the file
while true; do
echo "$(date): Storage is available" >> /mnt/shared/heartbeat.log
sleep 30
done
volumeMounts:
- mountPath: /mnt/shared
name: shared-storage
resources:
limits:
memory: 16Mi
cpu: 10m
requests:
memory: 16Mi
volumeClaimTemplates:
- metadata:
name: shared-storage
Expand Down