Skip to content

Commit

Permalink
add sleepcycle to stress #30
Browse files Browse the repository at this point in the history
  • Loading branch information
akyriako committed Oct 31, 2024
1 parent 98da028 commit f9cfde1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Image URL to use all building/pushing image targets
DOCKER_HUB_NAME ?= $(shell docker info | sed '/Username:/!d;s/.* //')
DOCKER_HUB_NAME ?= akyriako78#$(shell docker info | sed '/Username:/!d;s/.* //')
# sleepcycles
IMG_TAG ?= 0.2.8-dev.4
#IMG_TAG ?= $(shell git rev-parse --short HEAD)
Expand Down
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: /rekuberate-io-sleepcycles
newTag: 0.2.8-dev.2
newName: akyriako78/rekuberate-io-sleepcycles
newTag: 0.2.8-dev.4
2 changes: 1 addition & 1 deletion config/samples/core_v1alpha1_sleepcycle_app_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ spec:
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-3
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
80 changes: 59 additions & 21 deletions config/samples/stress/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/rand"
"flag"
"fmt"
"go.uber.org/zap"
Expand All @@ -15,10 +16,13 @@ import (
"k8s.io/utils/pointer"
"os"
"path/filepath"
"strings"
)

var logger *zap.Logger

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func main() {
var kubeconfig *string
home := homedir.HomeDir()
Expand All @@ -29,12 +33,18 @@ func main() {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}

purge := flag.Bool("purge", false, "(optional) remove all deployments false, provision deployments true")
purge := flag.Bool("purge", false, "(optional) false: remove all deployments , true: provision deployments")
deployments := flag.Int("deployments", 10, "(optional) number of deployments")
sleepcycle := flag.String("sleepcycle", "", "target sleepcycle")
namespace := flag.String("namespace", apiv1.NamespaceDefault, "(optional) target kubernetes namespace")

flag.Parse()

if sleepcycle == nil || *sleepcycle == "" {
flag.Usage()
os.Exit(1)
}

logger, _ = zap.NewProduction()
defer logger.Sync()

Expand All @@ -52,19 +62,21 @@ func main() {
gracefulExitCode := 0

if *purge {
for i := 0; i < *deployments; i++ {
err := deleteDeployment(deploymentsClient, fmt.Sprintf("whoami-%d", i))
if err != nil {
gracefulExitCode = 1
logger.Error(err.Error())
}
err := deleteDeployments(
deploymentsClient,
*deployments,
*sleepcycle,
)
if err != nil {
gracefulExitCode = 1
logger.Error(err.Error())
}

os.Exit(gracefulExitCode)
}

for i := 0; i < *deployments; i++ {
deploymentSpec := getManifest(i)
deploymentSpec := getManifest(*sleepcycle)

err := createDeployment(deploymentsClient, deploymentSpec)
if err != nil {
Expand All @@ -84,34 +96,47 @@ func createDeployment(deploymentsClient v1.DeploymentInterface, deploymentSpec *
return err
}

//logger.Info(fmt.Sprintf("Created deployment %q.\n", deployment.GetObjectMeta().GetName()))

return nil
}

func deleteDeployment(deploymentsClient v1.DeploymentInterface, deploymentName string) error {
logger.Info(fmt.Sprintf("deleting deployment %q", deploymentName))
func deleteDeployments(deploymentsClient v1.DeploymentInterface, count int, sleepcycle string) error {
logger.Info(fmt.Sprintf("attempting to delete %d deployments", count))

deletePolicy := metav1.DeletePropagationForeground
if err := deploymentsClient.Delete(context.TODO(), deploymentName, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
}); err != nil {
return err
listOptions := metav1.ListOptions{
LabelSelector: "rekuberate.io/sleepcycle=" + sleepcycle,
}
deploymentsList, err := deploymentsClient.List(context.TODO(), listOptions)
if err != nil {
logger.Error("getting deployments list failed")
}

if len(deploymentsList.Items) < count {
count = len(deploymentsList.Items)
}

for i := 0; i < count; i++ {
deletePolicy := metav1.DeletePropagationForeground
if err := deploymentsClient.Delete(context.TODO(), deploymentsList.Items[i].Name, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
}); err != nil {
return err
}

logger.Info(fmt.Sprintf("deleted deployment: %s", deploymentsList.Items[i].Name))
}
//logger.Info("Deleted deployment.")

return nil
}

func getManifest(idx int) *appsv1.Deployment {
appName := fmt.Sprintf("whoami-%v", idx)
func getManifest(sleepcycle string) *appsv1.Deployment {
appName := fmt.Sprintf("whoami-%s", strings.ToLower(generateSecureRandomString(5)))

deploymentSpec := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Labels: map[string]string{
"app": appName,
"rekuberate.io/sleepcycle": "sleepcycle-app-2",
"rekuberate.io/sleepcycle": sleepcycle,
},
},
Spec: appsv1.DeploymentSpec{
Expand Down Expand Up @@ -141,3 +166,16 @@ func getManifest(idx int) *appsv1.Deployment {

return deploymentSpec
}

func generateSecureRandomString(length int) string {
result := make([]byte, length)
_, err := rand.Read(result)
if err != nil {
os.Exit(1)
}

for i := range result {
result[i] = letters[int(result[i])%len(letters)]
}
return string(result)
}

0 comments on commit f9cfde1

Please sign in to comment.