Skip to content

Commit

Permalink
add more flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanzele committed Feb 2, 2024
1 parent 72aabe8 commit 1292d44
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 36 deletions.
4 changes: 3 additions & 1 deletion cmd/simulator/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ It's a comprehensive approach to maintaining a clean and efficient simulation en
pterm.Success.Println("kubernetes client initialized successfully!")

pterm.Info.Println("initializing kubernetes resource manager...")
manager := k8s.NewManager(client, &k8s.ManagerConfig{})
manager := k8s.NewManager(client, &k8s.ManagerConfig{Namespace: config.Namespace})
pterm.Success.Println("kubernetes resource manager initialized successfully!")

// clean section
Expand Down Expand Up @@ -132,5 +132,7 @@ It's a comprehensive approach to maintaining a clean and efficient simulation en
}

func NewCleanCmd() *cobra.Command {
cleanCmd.Flags().StringVarP(&config.Namespace, "namespace", "n", config.Namespace, "namespace in which to create simulation resources")

return cleanCmd
}
71 changes: 43 additions & 28 deletions cmd/simulator/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"sync"
"time"
Expand Down Expand Up @@ -48,6 +49,41 @@ The process is designed to mimic real-world Kubernetes environments for testing
}
pterm.Success.Println("kubernetes client initialized successfully!")

pterm.Info.Println("initializing kubernetes resource manager...")
managerConfig := k8s.ManagerConfig{
Namespace: config.Namespace,
RandomEnvVars: config.RandomEnvVars,
PodRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.PodCreatorFrequency,
Requests: config.PodCreatorRequests,
Limit: config.PodCreatorLimit,
},
NodeRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.NodeCreatorFrequency,
Requests: config.NodeCreatorRequests,
Limit: config.NodeCreatorLimit,
},
JobRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.JobCreatorFrequency,
Requests: config.JobCreatorRequests,
Limit: config.JobCreatorLimit,
},
}
manager := k8s.NewManager(client, &managerConfig)
pterm.Success.Println("kubernetes resource manager initialized successfully!")

pterm.Info.Println("initializing namespaces")
if err = k8s.CreateNamespaceIfNeed(cmd.Context(), client, config.Namespace, slog.Default()); err != nil {
pterm.Error.Printf("error checking should namespace %s be created: %v\n", config.Namespace, err)
os.Exit(1)
}

if err = k8s.CreateNamespaceIfNeed(cmd.Context(), client, config.SimulatorNamespace, slog.Default()); err != nil {
pterm.Error.Printf("error checking should namespace %s be created: %v\n", config.Namespace, err)
os.Exit(1)
}
pterm.Info.Println("namespaces initialized")

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

Expand All @@ -56,7 +92,7 @@ The process is designed to mimic real-world Kubernetes environments for testing
err = runRemote(cmd.Context(), client)
} else {
pterm.Success.Println("running simulation from local machine")
err = runLocal(cmd.Context(), client)
err = runLocal(cmd.Context(), manager)
}
if err != nil {
pterm.Error.Printf("failed to run simulation: %v", err)
Expand Down Expand Up @@ -86,51 +122,29 @@ func runRemote(ctx context.Context, client kubernetes.Interface) error {
"--no-gui",
"--verbose",
}
pterm.Info.Println("creating simulator job...")
job := simulator.NewSimulatorJob(args)
_, err := client.BatchV1().Jobs("default").Create(ctx, job, metav1.CreateOptions{})
_, err := client.BatchV1().Jobs(config.SimulatorNamespace).Create(ctx, job, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("failed to create simulator job: %v", err)
}

pterm.Info.Println("waiting for simulator job pod to become ready...")
if err := k8s.WaitForJobPodsReady(ctx, client, config.Namespace, job.Name, config.DefaultPollTimeout); err != nil {
if err := k8s.WaitForJobPodsReady(ctx, client, config.SimulatorNamespace, job.Name, config.DefaultPollTimeout); err != nil {
return fmt.Errorf("failed to wait for simulator job pods to become ready: %v", err)
}

pterm.Info.Println("streaming simulator job pod logs...")
if err := k8s.WatchJobPodLogs(ctx, client, config.Namespace, job.Name, os.Stdout); err != nil {
if err := k8s.WatchJobPodLogs(ctx, client, config.SimulatorNamespace, job.Name, os.Stdout); err != nil {
return fmt.Errorf("failed to watch simulator job pod logs: %v", err)
}

return nil
}

func runLocal(ctx context.Context, client kubernetes.Interface) error {
func runLocal(ctx context.Context, manager *k8s.Manager) error {
pterm.Success.Println("kubernetes client initialized successfully!")

pterm.Info.Println("initializing kubernetes resource manager...")
managerConfig := k8s.ManagerConfig{
Namespace: config.Namespace,
RandomEnvVars: config.RandomEnvVars,
PodRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.PodCreatorFrequency,
Requests: config.PodCreatorRequests,
Limit: config.PodCreatorLimit,
},
NodeRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.NodeCreatorFrequency,
Requests: config.NodeCreatorRequests,
Limit: config.NodeCreatorLimit,
},
JobRateLimiterConfig: k8s.RateLimiterConfig{
Frequency: config.JobCreatorFrequency,
Requests: config.JobCreatorRequests,
Limit: config.JobCreatorLimit,
},
}
manager := k8s.NewManager(client, &managerConfig)
pterm.Success.Println("kubernetes resource manager initialized successfully!")

// run section
blip()
pterm.DefaultSection.Println("run")
Expand Down Expand Up @@ -162,6 +176,7 @@ func NewRunCmd() *cobra.Command {
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")
runCmd.Flags().StringVar(&config.SimulatorNamespace, "simulator-namespace", config.SimulatorNamespace, "namespace in which to create simulator resources")

return runCmd
}
1 change: 1 addition & 0 deletions cmd/simulator/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func printKWOKConfig() {
WithTextStyle(pterm.NewStyle(pterm.FgLightCyan)).
WithItems([]pterm.BulletListItem{
{Level: 1, Text: "kwok namespace = " + config.KWOKNamespace},
{Level: 1, Text: "kwok namespace = " + config.Namespace},
{Level: 1, Text: "kubeconfig = " + config.Kubeconfig},
{Level: 1, Text: "qps = " + fmt.Sprintf("%f", config.QPS)},
{Level: 1, Text: "burst = " + fmt.Sprintf("%d", config.Burst)},
Expand Down
6 changes: 4 additions & 2 deletions cmd/simulator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
// KWOKNamespace is the namespace in which kwok-operator is expected or installed.
KWOKNamespace = "kube-system"
// Namespace is the namespace in which pods should be created.
Namespace = "default"
Namespace = "simulator"
// SimulatorNamespace is the namespace in which simulator pods should be created.
SimulatorNamespace = "default"
// PodCreatorFrequency is the frequency at which the pod creator should be invoked.
PodCreatorFrequency = 1 * time.Second
// PodCreatorRequests is the number of requests that should be made to the pod creator in each iteration.
Expand Down Expand Up @@ -54,7 +56,7 @@ var (
// SimulatorTag is the tag used for the simulator.
SimulatorTag = "latest"
// RandomEnvVars configures whether the simulator should use random envvars.
RandomEnvVars = true
RandomEnvVars = false
// DefaultEnvVarsType is the default envvar type which are generated when creating fake pods.
DefaultEnvVarsType = "medium"
)
26 changes: 26 additions & 0 deletions internal/k8s/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log/slog"
"time"

k8serrors "k8s.io/apimachinery/pkg/api/errors"

batchv1 "k8s.io/api/batch/v1"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -318,6 +320,30 @@ func waitFor(ctx context.Context, client kubernetes.Interface, labelSelector str
)
}

// CreateNamespaceIfNeed creates the provided namespace if it does not exist.
func CreateNamespaceIfNeed(ctx context.Context, client kubernetes.Interface, namespace string, logger *slog.Logger) error {
logger.Info("checking does namespace exist", "namespace", namespace)
_, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
logger.Info("namespace does not exist, creating namespace", "namespace", namespace)
_, err = client.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}, metav1.CreateOptions{})
if err != nil {
return err
}
} else {
return err
}
} else {
logger.Info("namespace already exists", "namespace", namespace)
}
return nil
}

func (m *Manager) Metrics() (nodeCreationMetrics, podCreationMetrics, jobCreationMetrics ratelimiter.Metrics) {
nodeCreationMetrics = m.rateLimitedNodeCreator.Metrics()
podCreationMetrics = m.rateLimitedPodCreator.Metrics()
Expand Down
2 changes: 1 addition & 1 deletion internal/simulator/data/stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ spec:
- Running
delay:
durationMilliseconds: 600000
jitterDurationMilliseconds: 90000
jitterDurationMilliseconds: 900000
---
apiVersion: kwok.x-k8s.io/v1alpha1
kind: Stage
Expand Down
9 changes: 5 additions & 4 deletions internal/simulator/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
defaultTTLSecondsAfterFinished = 300
defaultTTLSecondsAfterFinished = 150
)

func NewSimulatorJob(args []string) *batchv1.Job {
Expand All @@ -33,9 +33,10 @@ func NewSimulatorJob(args []string) *batchv1.Job {
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "simulator",
Image: util.CreateImageString(config.SimulatorImage, config.SimulatorTag),
Args: fullArgs,
ImagePullPolicy: corev1.PullAlways,
Name: "simulator",
Image: util.CreateImageString(config.SimulatorImage, config.SimulatorTag),
Args: fullArgs,
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions internal/simulator/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func createClusterRole(ctx context.Context, clientset kubernetes.Interface) erro
Resources: []string{"pods", "pods/log"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"namespaces"},
Verbs: []string{"create", "delete", "get", "list", "watch"},
},
{
APIGroups: []string{"batch"},
Resources: []string{"jobs"},
Expand Down

0 comments on commit 1292d44

Please sign in to comment.