Skip to content

Commit

Permalink
feat: add random scaling gvk
Browse files Browse the repository at this point in the history
Signed-off-by: r3drun3 <[email protected]>
  • Loading branch information
R3DRUN3 committed Jan 21, 2024
1 parent fc00aaa commit a88c21a
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 13 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ resources:
kind: NodeTainter
path: stackzoo.io/khaos/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: stackzoo.io
group: khaos
kind: RandomScaling
path: stackzoo.io/khaos/api/v1alpha1
version: v1alpha1
version: "3"
126 changes: 113 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Currently, Khaos does not implement *cronjobs*; any scheduling of Khaos Custom R
## Supported features
- [X] Delete pods
- [X] Random scaling pod replicas
- [x] Delete cluster nodes
- [X] Delete secrets
- [X] Delete configmaps
Expand Down Expand Up @@ -89,18 +90,19 @@ Install and list all the available operator's CRDs with the following command:
make manifests && make install && kubectl get crds

NAME CREATED AT
apiserveroverloads.khaos.stackzoo.io 2024-01-17T07:35:17Z
commandinjections.khaos.stackzoo.io 2024-01-17T07:35:17Z
configmapdestroyers.khaos.stackzoo.io 2024-01-17T07:35:17Z
consumenamespaceresources.khaos.stackzoo.io 2024-01-17T07:35:17Z
containerresourcechaos.khaos.stackzoo.io 2024-01-17T07:35:17Z
cordonnodes.khaos.stackzoo.io 2024-01-17T07:35:17Z
eventsentropies.khaos.stackzoo.io 2024-01-17T07:35:17Z
nodedestroyers.khaos.stackzoo.io 2024-01-17T07:35:17Z
nodetainters.khaos.stackzoo.io 2024-01-17T07:35:17Z
poddestroyers.khaos.stackzoo.io 2024-01-17T07:35:17Z
podlabelchaos.khaos.stackzoo.io 2024-01-17T07:35:17Z
secretdestroyers.khaos.stackzoo.io 2024-01-17T07:35:17Z
apiserveroverloads.khaos.stackzoo.io 2024-01-21T15:29:36Z
commandinjections.khaos.stackzoo.io 2024-01-21T15:29:36Z
configmapdestroyers.khaos.stackzoo.io 2024-01-21T15:29:36Z
consumenamespaceresources.khaos.stackzoo.io 2024-01-21T15:29:36Z
containerresourcechaos.khaos.stackzoo.io 2024-01-21T15:29:36Z
cordonnodes.khaos.stackzoo.io 2024-01-21T15:29:36Z
eventsentropies.khaos.stackzoo.io 2024-01-21T15:29:36Z
nodedestroyers.khaos.stackzoo.io 2024-01-21T15:29:36Z
nodetainters.khaos.stackzoo.io 2024-01-21T15:29:36Z
poddestroyers.khaos.stackzoo.io 2024-01-21T15:29:36Z
podlabelchaos.khaos.stackzoo.io 2024-01-21T15:29:36Z
randomscalings.khaos.stackzoo.io 2024-01-21T15:29:36Z
secretdestroyers.khaos.stackzoo.io 2024-01-21T15:29:36Z
```

In order to run the operator on your cluster (current context - i.e. whatever cluster `kubectl cluster-info` shows) run:
Expand Down Expand Up @@ -191,7 +193,7 @@ nginx-deployment-7bf8c77b5b-gsprh 0/1 Terminating 0 33s
nginx-deployment-7bf8c77b5b-gsprh 0/1 Terminating 0 33s
nginx-deployment-7bf8c77b5b-gsprh 0/1 Terminating 0 33s
```
1. Our operator shows the reconciliation logic's logs:
2. Our operator shows the reconciliation logic's logs:
```console
2023-11-28T14:07:18+01:00 INFO Reconciling PodDestroyer: default/nginx-destroyer {"controller": "poddestroyer", "controllerGroup": "khaos.stackzoo.io", "controllerKind": "PodDestroyer", "PodDestroyer": {"name":"nginx-destroyer","namespace":"default"}, "namespace": "default", "name": "nginx-destroyer", "reconcileID": "1e16a7d2-825a-4b46-b4e5-ac1228bc1c36"}
2023-11-28T14:07:18+01:00 INFO Selector: {map[app:nginx] []} {"controller": "poddestroyer", "controllerGroup": "khaos.stackzoo.io", "controllerKind": "PodDestroyer", "PodDestroyer": {"name":"nginx-destroyer","namespace":"default"}, "namespace": "default", "name": "nginx-destroyer", "reconcileID": "1e16a7d2-825a-4b46-b4e5-ac1228bc1c36"}
Expand Down Expand Up @@ -241,6 +243,104 @@ The `status` spec tells you how many pods have been successfully destroyed.



<details>
<summary>RANDOM SCALING POD REPLICAS</summary>


Apply an example deployment:


```console
kubectl apply -f examples/random-scaling-test-deployment.yaml
```

Retrieve our deployment's pods in the default namespace:
```console
kubectl get pods
NAME READY STATUS RESTARTS AGE
random-scaling-deployment-56c5d5bb74-pcgm6 1/1 Running 0 52s
random-scaling-deployment-56c5d5bb74-rw4sp 1/1 Running 0 52s
random-scaling-deployment-56c5d5bb74-tpvxb 1/1 Running 0 52s
```

Now apply the following `RandomScaling` manifest:

```yaml
apiVersion: khaos.stackzoo.io/v1alpha1
kind: RandomScaling
metadata:
name: example-randomscaling-2
spec:
deployment: random-scaling-deployment
minReplicas: 2
maxReplicas: 13
```

```console
kubectl apply -f examples/random-scaling.yaml
```

This will scale our deployment by randomly picking a number between `minReplicas` and `maxReplicas`.


Check again our pods:
```console
kubectl get pods
NAME READY STATUS RESTARTS AGE
random-scaling-deployment-56c5d5bb74-2tcss 1/1 Running 0 2m49s
random-scaling-deployment-56c5d5bb74-8c5gf 1/1 Running 0 2m49s
random-scaling-deployment-56c5d5bb74-bjpkc 1/1 Running 0 2m49s
random-scaling-deployment-56c5d5bb74-cctcz 1/1 Running 0 2m49s
random-scaling-deployment-56c5d5bb74-pcgm6 1/1 Running 0 5m44s
random-scaling-deployment-56c5d5bb74-rw4sp 1/1 Running 0 5m44s
random-scaling-deployment-56c5d5bb74-tpvxb 1/1 Running 0 5m44s
```

You can notice that there are 4 more pods!

Our operator shows the reconciliation logic's logs:
```console
2024-01-21T17:47:32+01:00 INFO Starting reconcile for random scaling - deployment random-scaling-deployment {"controller": "randomscaling", "controllerGroup": "khaos.stackzoo.io", "controllerKind": "RandomScaling", "RandomScaling": {"name":"example-randomscaling","namespace":"default"}, "namespace": "default", "name": "example-randomscaling", "reconcileID": "4cda6061-a893-470a-8a43-1a222256d987"}
2024-01-21T17:47:32+01:00 INFO RandomReplicas 7 {"controller": "randomscaling", "controllerGroup": "khaos.stackzoo.io", "controllerKind": "RandomScaling", "RandomScaling": {"name":"example-randomscaling","namespace":"default"}, "namespace": "default", "name": "example-randomscaling", "reconcileID": "4cda6061-a893-470a-8a43-1a222256d987"}
```

Now we can inspect the status of our PodDestroyer object:
```console
kubectl get randomscaling example-randomscaling -o yaml
```

This will retrieve our resource in `yaml` format:
```yaml
apiVersion: khaos.stackzoo.io/v1alpha1
kind: RandomScaling
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"khaos.stackzoo.io/v1alpha1","kind":"RandomScaling","metadata":{"annotations":{},"name":"example-randomscaling","namespace":"default"},"spec":{"deployment":"random-scaling-deployment","maxReplicas":10,"minReplicas":1}}
creationTimestamp: "2024-01-21T16:46:54Z"
generation: 5
name: example-randomscaling
namespace: default
resourceVersion: "1865"
uid: 4197f351-4557-4033-b996-fd5f0a8e25fc
spec:
deployment: random-scaling-deployment
maxReplicas: 10
minReplicas: 1
status:
operationResult: true
```

The `status` spec tells you that the last trigger has been succesfully completed.


</details>




<details>
<summary>DELETE NODES</summary>

Expand Down
53 changes: 53 additions & 0 deletions api/v1alpha1/randomscaling_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// RandomScalingSpec defines the desired state of RandomScaling
type RandomScalingSpec struct {
// Deployment is the name of the deployment to scale randomly
Deployment string `json:"deployment"`

// MinReplicas is the minimum number of replicas for the deployment
MinReplicas int32 `json:"minReplicas"`

// MaxReplicas is the maximum number of replicas for the deployment
MaxReplicas int32 `json:"maxReplicas"`
}

// RandomScalingStatus defines the observed state of RandomScaling
type RandomScalingStatus struct {
// OperationResult indicates whether the scaling operation was successful
OperationResult bool `json:"operationResult"`
// DeploymentResourceVersion stores the resource version of the related deployment
DeploymentResourceVersion string `json:"deploymentResourceVersion,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// RandomScaling is the Schema for the randomscalings API
type RandomScaling struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec RandomScalingSpec `json:"spec,omitempty"`
Status RandomScalingStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// RandomScalingList contains a list of RandomScaling
type RandomScalingList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []RandomScaling `json:"items"`
}

func init() {
SchemeBuilder.Register(&RandomScaling{}, &RandomScalingList{})
}
89 changes: 89 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "NodeTainter")
os.Exit(1)
}
if err = (&controller.RandomScalingReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RandomScaling")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Loading

0 comments on commit a88c21a

Please sign in to comment.