Repository: microsoft/retina
File: test/e2e/framework/scaletest/create-resources.go (Go)

Code before:

// Useful when wanting to do parameter checking, for example
// if a parameter length is known to be required less than 80 characters,
// do this here so we don't find out later on when we run the step
// when possible, try to avoid making external calls, this should be fast and simple
func (c *CreateResources) Prevalidate() error {
	err := validateDeploymentType(c.RealPodType)
	return err
}

// Primary step where test logic is executed
// Returning an error will cause the test to fail
func (c *CreateResources) Run() error {
	resources := c.getResources()

	config, err := clientcmd.BuildConfigFromFlags("", c.KubeConfigFilePath)
	if err != nil {
		return fmt.Errorf("error building kubeconfig: %w", err)
	}

	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return fmt.Errorf("error creating Kubernetes client: %w", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), timeoutCreateResourcesSeconds*time.Second)
	defer cancel()

	retrier := retry.Retrier{Attempts: defaultRetryAttempts, Delay: defaultRetryDelay}

	for _, resource := range resources {
		err := retrier.Do(ctx, func() error {
			return e2ekubernetes.CreateResource(ctx, resource, clientset)
		})
		if err != nil {
			return fmt.Errorf("error creating resource: %w", err)
		}
	}

	return nil
}

// Require for background steps
func (c *CreateResources) Stop() error {
	return nil
}

func (c *CreateResources) getResources() []runtime.Object {
	log.Println("Generating Resources")

	objs := []runtime.Object{}

Review comment (bug):
I can't find the other instance, but I also want to point out that `value * time.Second` is dangerous math to do--you should eliminate it every chance you get. The value can easily overflow as you can see here if someone tries to give you a duration: https://go.dev/play/p/tZhE9KQPdju . `time.Duration` is a first-class type and it should be passed around and used.

Code after:
// when possible, try to avoid making external calls, this should be fast and simple
func (c *CreateResources) Prevalidate() error {
	err := validateDeploymentType(c.RealPodType)
	return err
}

// Primary step where test logic is executed
// Returning an error will cause the test to fail
func (c *CreateResources) Run() error {
	resources := c.getResources()

	config, err := clientcmd.BuildConfigFromFlags("", c.KubeConfigFilePath)
	if err != nil {
		return fmt.Errorf("error building kubeconfig: %w", err)
	}

	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return fmt.Errorf("error creating Kubernetes client: %w", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Second)
	defer cancel()

	retrier := retry.Retrier{Attempts: defaultRetryAttempts, Delay: defaultRetryDelay}

	for _, resource := range resources {
		err := retrier.Do(ctx, func() error {
			return e2ekubernetes.CreateResource(ctx, resource, clientset)
		})
		if err != nil {
			return fmt.Errorf("error creating resource: %w", err)
		}
	}

	return nil
}

// Require for background steps
func (c *CreateResources) Stop() error {
	return nil
}

func (c *CreateResources) getResources() []runtime.Object {
	log.Println("Generating Resources")

	objs := []runtime.Object{}

	// kwokDeployments := c.generateDeployments(c.NumKwokDeployments, c.NumKwokReplicas, "kwok")
	// objs = append(objs, kwokDeployments...)