Skip to content

Commit

Permalink
added chaos cmd to peridically create & delete deployments in given t…
Browse files Browse the repository at this point in the history
…ime interval
  • Loading branch information
VineethReddy02 committed Jul 27, 2020
1 parent ab8dad3 commit 2fab8aa
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ default 1300 1300 456 250
kube-system 8 11 4 0 15 0 0 0
mock-kubelet 3500 4000 1200 400 9348 50 30 35
```


#### To create & delete periodically the deployments with provided time interval (i.e in seconds)

The below cmd would create & delete 100 deployments with replicas as 50 for every 10 seconds in scale namespace.

```
./k8s-scaler chaos d --scale 100 -r 50 --time 10 -n scale
```

#### TODO:

1. Support custom resources creation at scale. As CRD already exists in cluster. CR creation can be done using k8s-scaler by passing ```--kind``` value but custom resources spec formation needs to be taken care in k8s-scaler.
Expand Down
Binary file modified bin/k8s-scaler
Binary file not shown.
90 changes: 90 additions & 0 deletions cmd/chaos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
kube_client "github.com/VineethReddy02/k8s-scaler/pkg/kube-client"
v1 "k8s.io/api/core/v1"
"log"
"strings"

"github.com/spf13/cobra"
)

// chaosCmd represents the chaos command
var chaosCmd = &cobra.Command{
Use: "chaos",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
resourceType := args[0]
count, _ := cmd.Flags().GetInt32("scale")
replicas, _ := cmd.Flags().GetInt32("replicas")
containers, _ := cmd.Flags().GetInt32("containers")
config, _ := rootCmd.PersistentFlags().GetString("kubeconfig")
namespace, _ := cmd.Flags().GetString("namespace")
namespaces, _ := cmd.Flags().GetString("exclude-namespaces")
excludeNamespaces := strings.Split(namespaces, ",")
nodeselector, _ := cmd.Flags().GetString("node-selector")
toleration, _ := cmd.Flags().GetString("toleration")
timeInterval, _ := cmd.Flags().GetInt32("time")
if nodeselector != "" {
ns := strings.Split(nodeselector, "=")
kube_client.Config.NodeSelector = map[string]string{ns[0]:ns[1]}
} else {
kube_client.Config.NodeSelector = nil
}
if toleration != "" {
ts := strings.Split(toleration, "=")
toleration := &v1.Toleration{
Key: ts[0],
Operator: "Equal",
Value: ts[1],
Effect: "NoSchedule",
}
kube_client.Config.Tolerations = append(kube_client.Config.Tolerations, *toleration)
}else {
kube_client.Config.Tolerations = nil
}

kubeClient := kube_client.NewKubeClient()
clientInfo := kubeClient.GetKubeClient(config)
kubeClient.Client = clientInfo
if resourceType == "deployments" || resourceType == "d" {
kubeClient.CreateChaosWithDeployments(count, replicas, containers, namespace, excludeNamespaces, timeInterval)
} else {
log.Fatal("k8s-scaler only creating chaos with deployments")
}
},
}

func init() {
rootCmd.AddCommand(chaosCmd)

chaosCmd.Flags().Int32P("scale", "s", 1, "number of instances.")
chaosCmd.Flags().Int32P("replicas", "r", 1, "number of replicas per instance.")
chaosCmd.Flags().Int32P("containers", "c", 1, "number of containers per pod.")
chaosCmd.Flags().StringP("namespace", "n", "", "specify the namespace")
chaosCmd.Flags().String("node-selector", "", "specify the node selector as map key=value")
chaosCmd.Flags().StringP("toleration", "t", "", "specify the toleration for a specific node as map key=value")
chaosCmd.Flags().StringP("exclude-namespaces", "e", "", "specify namespaces that needs to be excluded during creation.")
chaosCmd.Flags().Int32P("time", "", 300, "time interval between creation & deletion, default to 5 minutes.")
}
13 changes: 13 additions & 0 deletions pkg/kube-client/chaos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kube_client

import "time"

func (ctx *KubeClient) CreateChaosWithDeployments(count, replicas, containers int32, namespace string, excludeNamespaces []string, timeInterval int32) {
ticker := time.NewTicker(time.Duration(timeInterval) * time.Second)
for {
ctx.CreateDeployments(count, replicas, containers, namespace, excludeNamespaces)
<-ticker.C
ctx.DeleteDeployments(count, namespace, excludeNamespaces)
<-ticker.C
}
}

0 comments on commit 2fab8aa

Please sign in to comment.