Skip to content

Commit

Permalink
Merge pull request #37 from rekuberate-io/30-memory-profiling-of-mana…
Browse files Browse the repository at this point in the history
…gercontroller

add deployment emulator pusher #30
  • Loading branch information
akyriako authored Nov 19, 2024
2 parents f42fce5 + 2d7103e commit 155fd24
Show file tree
Hide file tree
Showing 22 changed files with 408 additions and 540 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/lint-helm-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
- name: Run chart-testing (lint)
if: steps.list-changed.outputs.changed == 'true'
run: ct lint --target-branch ${{ github.event.repository.default_branch }}

- name: Create kind cluster
if: steps.list-changed.outputs.changed == 'true'
uses: helm/[email protected]

- name: Run chart-testing (install)
if: steps.list-changed.outputs.changed == 'true'
run: ct install --target-branch ${{ github.event.repository.default_branch }}
# - name: Run chart-testing (lint)
# if: steps.list-changed.outputs.changed == 'true'
# run: ct lint --target-branch ${{ github.event.repository.default_branch }}

# - name: Create kind cluster
# if: steps.list-changed.outputs.changed == 'true'
# uses: helm/[email protected]
#
# - name: Run chart-testing (install)
# if: steps.list-changed.outputs.changed == 'true'
# run: ct install --target-branch ${{ github.event.repository.default_branch }}
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# 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.7
IMG_TAG ?= 0.2.8-rc.0
#IMG_TAG ?= $(shell git rev-parse --short HEAD)
IMG_NAME ?= rekuberate-io-sleepcycles
IMG ?= $(DOCKER_HUB_NAME)/$(IMG_NAME):$(IMG_TAG)
# runners
#RUNNERS_IMAGE_TAG ?= 0.1.0-rc.1
RUNNERS_IMAGE_TAG ?= 0.2.0-rc.0
RUNNERS_IMG_NAME ?= rekuberate-io-sleepcycles-runners
RUNNERS_IMG ?= $(DOCKER_HUB_NAME)/$(RUNNERS_IMG_NAME)#:$(RUNNERS_IMAGE_TAG)
RUNNERS_IMG ?= $(DOCKER_HUB_NAME)/$(RUNNERS_IMG_NAME):$(RUNNERS_IMAGE_TAG)
RUNNERS_IMG_LATEST ?= $(DOCKER_HUB_NAME)/$(RUNNERS_IMG_NAME):latest
# targets
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
# CONTAINER_TOOL defines the container tool to be used for building images.
Expand Down Expand Up @@ -108,7 +109,7 @@ docker-buildx-runner: fmt vet ## Build and push docker image for the runner for
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile.runners > Dockerfile.runners.cross
- $(CONTAINER_TOOL) buildx create --name runners-builder
$(CONTAINER_TOOL) buildx use runners-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${RUNNERS_IMG} -f Dockerfile.runners.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${RUNNERS_IMG} --tag ${RUNNERS_IMG_LATEST} -f Dockerfile.runners.cross .
- $(CONTAINER_TOOL) buildx rm runners-builder
rm Dockerfile.runners.cross

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ make docker-buildx
2. Deploy the controller to the cluster using the image defined in `IMG`:

```sh
make deploy
make install && make deploy
```

and then deploy the samples:
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/sleepcycle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ type SleepCycleSpec struct {
Enabled bool `json:"enabled"`

// +optional
// +kubebuilder:default=1
// +kubebuilder:default=0
// +kubebuilder:validation:Type=integer
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=3
// +kubebuilder:validation:ExclusiveMinimum=false
// +kubebuilder:validation:ExclusiveMaximum=false
Expand Down
4 changes: 2 additions & 2 deletions charts/sleepcycles/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.2.7
version: 0.2.8
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.2.7"
appVersion: "0.2.8-rc.0"
2 changes: 1 addition & 1 deletion charts/sleepcycles/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ controllerManager:
- ALL
image:
repository: akyriako78/rekuberate-io-sleepcycles
tag: 0.2.7
tag: 0.2.8-rc.0
resources:
limits:
cpu: 500m
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/core.rekuberate.io_sleepcycles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ spec:
default: UTC
type: string
successfulJobsHistoryLimit:
default: 1
default: 0
format: int32
maximum: 3
minimum: 1
minimum: 0
type: integer
wakeup:
pattern: (^((\*\/)?([0-5]?[0-9])((\,|\-|\/)([0-5]?[0-9]))*|\*)\s+((\*\/)?((2[0-3]|1[0-9]|[0-9]|00))((\,|\-|\/)(2[0-3]|1[0-9]|[0-9]|00))*|\*)\s+((\*\/)?([1-9]|[12][0-9]|3[01])((\,|\-|\/)([1-9]|[12][0-9]|3[01]))*|\*)\s+((\*\/)?([1-9]|1[0-2])((\,|\-|\/)([1-9]|1[0-2]))*|\*|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|des))\s+((\*\/)?[0-6]((\,|\-|\/)[0-6])*|\*|00|(sun|mon|tue|wed|thu|fri|sat))\s*$)|@(annually|yearly|monthly|weekly|daily|hourly|reboot)
Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: akyriako78/rekuberate-io-sleepcycles
newTag: 0.2.7
newTag: 0.2.8-dev.7
5 changes: 3 additions & 2 deletions config/samples/core_v1alpha1_sleepcycle_app_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ apiVersion: core.rekuberate.io/v1alpha1
kind: SleepCycle
metadata:
name: sleepcycle-app-2
namespace: app-2
namespace: default
spec:
shutdown: "1/2 * * * *"
shutdownTimeZone: "Europe/Athens"
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
12 changes: 12 additions & 0 deletions config/samples/core_v1alpha1_sleepcycle_app_3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.rekuberate.io/v1alpha1
kind: SleepCycle
metadata:
name: sleepcycle-app-3
namespace: default
spec:
shutdown: "1/2 * * * *"
shutdownTimeZone: "Europe/Athens"
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
12 changes: 12 additions & 0 deletions config/samples/core_v1alpha1_sleepcycle_app_4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.rekuberate.io/v1alpha1
kind: SleepCycle
metadata:
name: sleepcycle-app-4
namespace: default
spec:
shutdown: "1/2 * * * *"
shutdownTimeZone: "Europe/Athens"
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
12 changes: 12 additions & 0 deletions config/samples/core_v1alpha1_sleepcycle_app_5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.rekuberate.io/v1alpha1
kind: SleepCycle
metadata:
name: sleepcycle-app-5
namespace: default
spec:
shutdown: "1/2 * * * *"
shutdownTimeZone: "Europe/Athens"
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
12 changes: 12 additions & 0 deletions config/samples/core_v1alpha1_sleepcycle_app_6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.rekuberate.io/v1alpha1
kind: SleepCycle
metadata:
name: sleepcycle-app-6
namespace: default
spec:
shutdown: "1/2 * * * *"
shutdownTimeZone: "Europe/Athens"
wakeup: "*/2 * * * *"
wakeupTimeZone: "Europe/Dublin"
enabled: true
runnerImage: akyriako78/rekuberate-io-sleepcycles-runners:0.2.0-dev-4
186 changes: 186 additions & 0 deletions config/samples/stress/stress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package main

import (
"context"
"crypto/rand"
"flag"
"fmt"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/kubernetes/typed/apps/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/utils/pointer"
"os"
"path/filepath"
"strings"
)

var logger *zap.Logger

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func main() {
var kubeconfig *string
home := homedir.HomeDir()

if home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}

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 func(logger *zap.Logger) {
err := logger.Sync()
if err != nil {
_ = fmt.Errorf("failed to sync zap logger: %v", err)
}
}(logger)

config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", *kubeconfig))
if err != nil {
logger.Fatal(err.Error())
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logger.Fatal(err.Error())
}

deploymentsClient := clientset.AppsV1().Deployments(*namespace)
gracefulExitCode := 0

if *purge {
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(*sleepcycle)

err := createDeployment(deploymentsClient, deploymentSpec)
if err != nil {
gracefulExitCode = 1
logger.Error(err.Error())
}
}

os.Exit(gracefulExitCode)
}

func createDeployment(deploymentsClient v1.DeploymentInterface, deploymentSpec *appsv1.Deployment) error {
logger.Info(fmt.Sprintf("creating deployment %q", deploymentSpec.GetObjectMeta().GetName()))

_, err := deploymentsClient.Create(context.TODO(), deploymentSpec, metav1.CreateOptions{})
if err != nil {
return err
}

return nil
}

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

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))
}

return nil
}

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,
},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32Ptr(2),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": appName,
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": appName,
},
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "whoami",
Image: "traefik/whoami",
},
},
},
},
},
}

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)
}
2 changes: 1 addition & 1 deletion config/samples/whoami-app-2-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: app-2
namespace: app-2
namespace: default
labels:
app: app-2
rekuberate.io/sleepcycle: sleepcycle-app-2
Expand Down
Loading

0 comments on commit 155fd24

Please sign in to comment.