Skip to content

Commit

Permalink
add more flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanzele committed Jan 29, 2024
1 parent 6a2d4b7 commit b18dd6b
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 38 deletions.
12 changes: 11 additions & 1 deletion cmd/simulator/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/dejanzele/batch-simulator/internal/simulator/resources"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

Expand Down Expand Up @@ -46,6 +48,9 @@ The process is designed to mimic real-world Kubernetes environments for testing
}
pterm.Success.Println("kubernetes client initialized successfully!")

pterm.Info.Printf("setting the default env vars type to %s type", config.DefaultEnvVarsType)
resources.SetDefaultEnvVarsType(config.DefaultEnvVarsType)

if config.Remote {
pterm.Success.Println("running simulation in remote Kubernetes cluster")
err = runRemote(cmd.Context(), client)
Expand Down Expand Up @@ -75,6 +80,8 @@ func runRemote(ctx context.Context, client kubernetes.Interface) error {
"--job-creator-frequency", config.JobCreatorFrequency.String(),
"--job-creator-requests", fmt.Sprintf("%d", config.JobCreatorRequests),
"--job-creator-limit", fmt.Sprintf("%d", config.JobCreatorLimit),
"--random-env-vars", fmt.Sprintf("%t", config.RandomEnvVars),
"--default-env-vars-type", config.DefaultEnvVarsType,
"--namespace", config.Namespace,
"--no-gui",
"--verbose",
Expand Down Expand Up @@ -103,7 +110,8 @@ func runLocal(ctx context.Context, client kubernetes.Interface) error {

pterm.Info.Println("initializing kubernetes resource manager...")
managerConfig := k8s.ManagerConfig{
Namespace: config.Namespace,
Namespace: config.Namespace,
RandomEnvVars: config.RandomEnvVars,
PodRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.PodCreatorFrequency,
Requests: config.PodCreatorRequests,
Expand Down Expand Up @@ -152,6 +160,8 @@ func NewRunCmd() *cobra.Command {
runCmd.Flags().StringVarP(&config.Namespace, "namespace", "n", config.Namespace, "namespace in which to create simulation resources")
runCmd.Flags().BoolVarP(&config.Remote, "remote", "r", config.Remote, "run the simulator in a Kubernetes cluster")
runCmd.Flags().IntVar(&config.PodSpecSize, "pod-spec-size", config.PodSpecSize, "size of the pod spec in bytes")
runCmd.Flags().BoolVar(&config.RandomEnvVars, "random-env-vars", config.RandomEnvVars, "use random env vars")
runCmd.Flags().StringVar(&config.DefaultEnvVarsType, "default-env-vars-type", config.DefaultEnvVarsType, "default env vars type")

return runCmd
}
8 changes: 7 additions & 1 deletion cmd/simulator/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import "time"
import (
"time"
)

var (
// QPS configures maximum queries per second to use while talking with Kubernetes API.
Expand Down Expand Up @@ -51,4 +53,8 @@ var (
SimulatorImage = "dpejcev/batchsim"
// SimulatorTag is the tag used for the simulator.
SimulatorTag = "latest"
// RandomEnvVars configures whether the simulator should use random envvars.
RandomEnvVars = true
// DefaultEnvVarsType is the default envvar type which are generated when creating fake pods.
DefaultEnvVarsType = "medium"
)
33 changes: 33 additions & 0 deletions hack/fake-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: fake-deployment
namespace: default
spec:
replicas: 5000
selector:
matchLabels:
app: fake-pod
template:
metadata:
labels:
app: fake-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: In
values:
- kwok
# A taints was added to an automatically created Node.
# You can remove taints of Node or add this tolerations.
tolerations:
- key: "kwok.x-k8s.io/node"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: fake-container
image: fake-image
6 changes: 4 additions & 2 deletions internal/k8s/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type ManagerConfig struct {
Namespace string
// Logger is the logger that should be used by the Manager.
Logger *slog.Logger
// RandomEnvVars is used to determine whether random environment variables should be added to created Pods or Jobs.
RandomEnvVars bool
// PodRateLimiterConfig is the configuration for the rate limited PodCreator.
PodRateLimiterConfig RateLimiterConfig
// NodeRateLimiterConfig is the configuration for the rate limited NodeCreator.
Expand Down Expand Up @@ -80,14 +82,14 @@ func NewManager(client kubernetes.Interface, cfg *ManagerConfig) *Manager {
defaultedConfig.NodeRateLimiterConfig.Limit,
nodeExecutor,
)
podExecutor := executor.NewPodCreator(client, defaultedConfig.Namespace)
podExecutor := executor.NewPodCreator(client, defaultedConfig.Namespace, defaultedConfig.RandomEnvVars)
podRateLimiter := ratelimiter.New[*corev1.Pod](
defaultedConfig.PodRateLimiterConfig.Frequency,
defaultedConfig.PodRateLimiterConfig.Requests,
defaultedConfig.PodRateLimiterConfig.Limit,
podExecutor,
)
jobExecutor := executor.NewJobCreator(client, defaultedConfig.Namespace)
jobExecutor := executor.NewJobCreator(client, defaultedConfig.Namespace, defaultedConfig.RandomEnvVars)
jobRateLimiter := ratelimiter.New[*batchv1.Job](
defaultedConfig.JobRateLimiterConfig.Frequency,
defaultedConfig.JobRateLimiterConfig.Requests,
Expand Down
12 changes: 8 additions & 4 deletions internal/ratelimiter/executor/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ type kubernetesExecutor struct {
// PodCreator is used to create Pods.
type PodCreator struct {
kubernetesExecutor
randomEnvVars bool
}

func NewPodCreator(client kubernetes.Interface, namespace string) *PodCreator {
func NewPodCreator(client kubernetes.Interface, namespace string, randomEnvVars bool) *PodCreator {
return &PodCreator{
kubernetesExecutor: kubernetesExecutor{
client: client,
namespace: namespace,
},
randomEnvVars: randomEnvVars,
}
}

Expand All @@ -44,7 +46,7 @@ func (c *PodCreator) Identifier() string {
// Execute creates a Pod.
func (c *PodCreator) Execute(ctx context.Context) error {
name := fmt.Sprintf("fake-pod-%s", util.RandomRFC1123Name(16))
item := resources.NewFakePod(name, c.namespace)
item := resources.NewFakePod(name, c.namespace, c.randomEnvVars)
_, err := c.client.CoreV1().Pods(c.namespace).Create(ctx, item, metav1.CreateOptions{})
if err != nil {
return ratelimiter.NewCreateError(err, "v1", "Pod", item)
Expand Down Expand Up @@ -86,14 +88,16 @@ var _ ratelimiter.Executor[*corev1.Node] = &NodeCreator{}

type JobCreator struct {
kubernetesExecutor
randomEnvVars bool
}

func NewJobCreator(client kubernetes.Interface, namespace string) *JobCreator {
func NewJobCreator(client kubernetes.Interface, namespace string, randomEnvVars bool) *JobCreator {
return &JobCreator{
kubernetesExecutor: kubernetesExecutor{
client: client,
namespace: namespace,
},
randomEnvVars: randomEnvVars,
}
}

Expand All @@ -105,7 +109,7 @@ func (c *JobCreator) Identifier() string {
// Execute creates a Node.
func (c *JobCreator) Execute(ctx context.Context) error {
name := fmt.Sprintf("fake-job-%s", util.RandomRFC1123Name(16))
item := resources.NewFakeJob(name, c.namespace)
item := resources.NewFakeJob(name, c.namespace, c.randomEnvVars)
_, err := c.client.BatchV1().Jobs(c.namespace).Create(ctx, item, metav1.CreateOptions{})
if err != nil {
return ratelimiter.NewCreateError(err, "batch/v1", "Job", item)
Expand Down
12 changes: 6 additions & 6 deletions internal/ratelimiter/executor/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestNewPodCreator(t *testing.T) {
t.Parallel()

creator := NewPodCreator(fake.NewSimpleClientset(), "test")
creator := NewPodCreator(fake.NewSimpleClientset(), "test", false)
assert.Equal(t, "test", creator.namespace)
assert.NotNil(t, creator.client)
}
Expand All @@ -31,7 +31,7 @@ func TestPodCreator(t *testing.T) {
t.Parallel()

fakeClient := fake.NewSimpleClientset()
executor := NewPodCreator(fakeClient, "default")
executor := NewPodCreator(fakeClient, "default", false)

ctx := context.Background()
if err := executor.Execute(ctx); err != nil {
Expand All @@ -57,7 +57,7 @@ func TestPodCreator(t *testing.T) {
PrependReactor("create", "pods", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &corev1.Pod{}, errors.New("error creating pod")
})
executor := NewPodCreator(fakeClient, "default")
executor := NewPodCreator(fakeClient, "default", false)

ctx := context.Background()
err := executor.Execute(ctx)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestNodeCreator(t *testing.T) {
func TestNewJobCreator(t *testing.T) {
t.Parallel()

creator := NewJobCreator(fake.NewSimpleClientset(), "test")
creator := NewJobCreator(fake.NewSimpleClientset(), "test", false)
assert.Equal(t, "test", creator.namespace)
assert.NotNil(t, creator.client)
}
Expand All @@ -139,7 +139,7 @@ func TestJobCreator(t *testing.T) {
t.Parallel()

fakeClient := fake.NewSimpleClientset()
executor := NewJobCreator(fakeClient, "default")
executor := NewJobCreator(fakeClient, "default", false)

ctx := context.Background()
if err := executor.Execute(ctx); err != nil {
Expand All @@ -165,7 +165,7 @@ func TestJobCreator(t *testing.T) {
PrependReactor("create", "jobs", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &batchv1.Job{}, errors.New("error creating job")
})
executor := NewJobCreator(fakeClient, "default")
executor := NewJobCreator(fakeClient, "default", false)

ctx := context.Background()
err := executor.Execute(ctx)
Expand Down
4 changes: 2 additions & 2 deletions internal/simulator/data/stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ spec:
values:
- Running
delay:
durationMilliseconds: 10800000
jitterDurationMilliseconds: 21600000
durationMilliseconds: 600000
jitterDurationMilliseconds: 90000
---
apiVersion: kwok.x-k8s.io/v1alpha1
kind: Stage
Expand Down
Loading

0 comments on commit b18dd6b

Please sign in to comment.