func ApplyPriorityLevelConfiguration()

in cmd/kperf/commands/utils/helper.go [71:125]


func ApplyPriorityLevelConfiguration(kubeconfigPath string) error {
	// Load the kubeconfig file
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
	if err != nil {
		return fmt.Errorf("failed to load kubeconfig: %v", err)
	}

	// Create a Kubernetes client
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return fmt.Errorf("failed to create Kubernetes client: %v", err)
	}

	// Define the PriorityLevelConfiguration
	lendablePercent := int32(30)
	plc := &flowcontrolv1.PriorityLevelConfiguration{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "flowcontrol.apiserver.k8s.io/v1",
			Kind:       "PriorityLevelConfiguration",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name: "custom-system",
		},
		Spec: flowcontrolv1.PriorityLevelConfigurationSpec{
			Type: flowcontrolv1.PriorityLevelEnablementLimited,
			Limited: &flowcontrolv1.LimitedPriorityLevelConfiguration{
				LendablePercent: &lendablePercent,
				LimitResponse: flowcontrolv1.LimitResponse{
					Type: flowcontrolv1.LimitResponseTypeQueue,
					Queuing: &flowcontrolv1.QueuingConfiguration{
						Queues:           64,
						HandSize:         6,
						QueueLengthLimit: 50,
					},
				},
			},
		},
	}

	plcCli := clientset.FlowcontrolV1().PriorityLevelConfigurations()

	// Apply the PriorityLevelConfiguration
	_, err = plcCli.Create(context.TODO(), plc, metav1.CreateOptions{})
	if err != nil {
		if apierrors.IsAlreadyExists(err) {
			_, err = plcCli.Update(context.TODO(), plc, metav1.UpdateOptions{})
		}
	}
	if err != nil {
		return fmt.Errorf("failed to apply PriorityLevelConfiguration: %v", err)
	}

	fmt.Printf("Successfully applied PriorityLevelConfiguration: %s\n", plc.Name)
	return nil
}