|
1 | 1 | package killer |
2 | 2 |
|
3 | 3 | import ( |
4 | | - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "time" |
5 | 8 |
|
6 | 9 | "github.com/p-program/kube-killer/core" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
7 | 13 | "k8s.io/client-go/kubernetes" |
| 14 | + "k8s.io/client-go/util/retry" |
8 | 15 | ) |
9 | 16 |
|
10 | | -func init() { |
11 | | - |
12 | | -} |
13 | | - |
14 | | -// var newDeployKillerCommand = &cobra.Command{ |
15 | | -// Use: "deploy", |
16 | | -// Short: "Kill kubernetes's deploy", |
17 | | -// Long: ``, |
18 | | -// Run: func(cmd *cobra.Command, args []string) { |
19 | | -// fmt.Print("") |
20 | | -// }} |
21 | | - |
22 | 17 | type DeploymentKiller struct { |
23 | 18 | client *kubernetes.Clientset |
24 | 19 | deleteOption metav1.DeleteOptions |
25 | 20 | dryRun bool |
26 | 21 | mafia bool |
| 22 | + half bool |
27 | 23 | namespace string |
28 | 24 | } |
29 | 25 |
|
@@ -54,6 +50,107 @@ func (k *DeploymentKiller) DryRun() *DeploymentKiller { |
54 | 50 | return k |
55 | 51 | } |
56 | 52 |
|
57 | | -func (k *DeploymentKiller) Kill() { |
| 53 | +func (k *DeploymentKiller) SetHalf() *DeploymentKiller { |
| 54 | + k.half = true |
| 55 | + return k |
| 56 | +} |
| 57 | + |
| 58 | +// Kill deletes deployments based on the mode |
| 59 | +// - If mafia is true and half is true: deletes half of the deployments randomly |
| 60 | +// - If mafia is true: deletes all deployments |
| 61 | +// - Otherwise: deletes all deployments (default behavior, similar to kubectl delete deploy) |
| 62 | +func (k *DeploymentKiller) Kill() error { |
| 63 | + if k.mafia { |
| 64 | + if k.half { |
| 65 | + return k.KillHalfDeployments() |
| 66 | + } |
| 67 | + return k.KillAllDeployments() |
| 68 | + } |
| 69 | + return k.KillAllDeployments() |
| 70 | +} |
| 71 | + |
| 72 | +// KillAllDeployments deletes all deployments in the namespace |
| 73 | +func (k *DeploymentKiller) KillAllDeployments() error { |
| 74 | + deployments, err := k.client.AppsV1().Deployments(k.namespace).List(context.TODO(), metav1.ListOptions{}) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to list deployments: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + if len(deployments.Items) == 0 { |
| 80 | + log.Info().Msgf("No deployments found in namespace %s", k.namespace) |
| 81 | + return nil |
| 82 | + } |
| 83 | + |
| 84 | + log.Info().Msgf("Found %d deployment(s) in namespace %s", len(deployments.Items), k.namespace) |
| 85 | + |
| 86 | + for _, deploy := range deployments.Items { |
| 87 | + if err := k.deleteDeployment(deploy); err != nil { |
| 88 | + log.Error().Err(err).Msgf("Failed to delete deployment %s", deploy.Name) |
| 89 | + // Continue with other deployments |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// KillHalfDeployments deletes half of the deployments randomly |
| 97 | +func (k *DeploymentKiller) KillHalfDeployments() error { |
| 98 | + deployments, err := k.client.AppsV1().Deployments(k.namespace).List(context.TODO(), metav1.ListOptions{}) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to list deployments: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + if len(deployments.Items) == 0 { |
| 104 | + log.Info().Msgf("No deployments found in namespace %s", k.namespace) |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + // Randomly shuffle the deployments list |
| 109 | + deployList := deployments.Items |
| 110 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 111 | + r.Shuffle(len(deployList), func(i, j int) { |
| 112 | + deployList[i], deployList[j] = deployList[j], deployList[i] |
| 113 | + }) |
| 114 | + |
| 115 | + // Calculate how many deployments to kill (half, rounded down) |
| 116 | + deploysToKill := len(deployList) / 2 |
| 117 | + if deploysToKill == 0 { |
| 118 | + deploysToKill = 1 // At least kill one deployment if there's only one |
| 119 | + } |
| 120 | + |
| 121 | + log.Info().Msgf("Killing %d out of %d deployment(s) in namespace %s", deploysToKill, len(deployList), k.namespace) |
| 122 | + |
| 123 | + for i := 0; i < deploysToKill; i++ { |
| 124 | + deploy := deployList[i] |
| 125 | + if err := k.deleteDeployment(deploy); err != nil { |
| 126 | + log.Error().Err(err).Msgf("Failed to delete deployment %s", deploy.Name) |
| 127 | + // Continue with other deployments |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// deleteDeployment deletes a single deployment |
| 135 | +func (k *DeploymentKiller) deleteDeployment(deploy appsv1.Deployment) error { |
| 136 | + deployName := deploy.Name |
| 137 | + |
| 138 | + if k.dryRun { |
| 139 | + log.Info().Msgf("[DRY RUN] Would delete deployment %s in namespace %s", deployName, k.namespace) |
| 140 | + return nil |
| 141 | + } |
| 142 | + |
| 143 | + log.Warn().Msgf("Deleting deployment %s in namespace %s", deployName, k.namespace) |
| 144 | + |
| 145 | + // Use retry for deletion in case of conflicts |
| 146 | + err := retry.RetryOnConflict(retry.DefaultRetry, func() error { |
| 147 | + return k.client.AppsV1().Deployments(k.namespace).Delete(context.TODO(), deployName, k.deleteOption) |
| 148 | + }) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + return fmt.Errorf("failed to delete deployment %s: %w", deployName, err) |
| 152 | + } |
58 | 153 |
|
| 154 | + log.Info().Msgf("Successfully deleted deployment %s in namespace %s", deployName, k.namespace) |
| 155 | + return nil |
59 | 156 | } |
0 commit comments