Repository: k3s-io/k3s
File: pkg/cloudprovider/servicelb.go (Go)

Code before:
	return k.LBDefaultPriorityClassName
}

// getTolerations retrieves the tolerations from a service's annotations. 
// It parses the tolerations from a JSON or YAML string stored in the annotations. 
func (k *k3s) getTolerations(svc *core.Service) ([]core.Toleration, error) {
	tolerationsStr, ok := svc.Annotations[tolerationsAnnotation]
	if !ok {
		return []core.Toleration{}, nil
	}

	var tolerations []core.Toleration
	if err := json.Unmarshal([]byte(tolerationsStr), &tolerations); err != nil {
		if err := yaml.Unmarshal([]byte(tolerationsStr), &tolerations); err != nil {
			return nil, fmt.Errorf("failed to parse tolerations from annotation %s: %v", tolerationsAnnotation, err)
		}
	}

	for i := range tolerations {
		if err := validateToleration(&tolerations[i]); err != nil {
			return nil, fmt.Errorf("validation failed for toleration %d: %v", i, err)
		}
	}

	return tolerations, nil
}

// validateToleration ensures a toleration has valid fields according to its operator.
func validateToleration(toleration *core.Toleration) error {
	if toleration.Operator == "" {
		toleration.Operator = core.TolerationOpEqual
	}

	if toleration.Key == "" && toleration.Operator != core.TolerationOpExists {
		return fmt.Errorf("toleration with empty key must have operator 'Exists'")
	}

	if toleration.Operator == core.TolerationOpExists && toleration.Value != "" {
		return fmt.Errorf("toleration with operator 'Exists' must have an empty value")
	}

	return nil
}

// generateName generates a distinct name for the DaemonSet based on the service name and UID
func generateName(svc *core.Service) string {
	name := svc.Name
	// ensure that the service name plus prefix and uuid aren't overly long, but
	// don't cut the service name at a trailing hyphen.
	if len(name) > 48 {
		trimlen := 48

Review comment (refactor):
Is there a core Kubernetes helper we could use for this, instead of maintaining our own validation code?

Code after:

// getTolerations retrieves the tolerations from a service's annotations. 
// It parses the tolerations from a JSON or YAML string stored in the annotations. 
func (k *k3s) getTolerations(svc *core.Service) ([]core.Toleration, error) {
	tolerationsStr, ok := svc.Annotations[tolerationsAnnotation]
	if !ok {
		return []core.Toleration{}, nil
	}

	var tolerations []core.Toleration
	if err := json.Unmarshal([]byte(tolerationsStr), &tolerations); err != nil {
		if err := yaml.Unmarshal([]byte(tolerationsStr), &tolerations); err != nil {
			return nil, fmt.Errorf("failed to parse tolerations from annotation %s: %v", tolerationsAnnotation, err)
		}
	}

	for i := range tolerations {
		if err := validateToleration(&tolerations[i]); err != nil {
			return nil, fmt.Errorf("validation failed for toleration %d: %v", i, err)
		}
	}

	return tolerations, nil
}

// validateToleration ensures a toleration has valid fields according to its operator.
func validateToleration(toleration *core.Toleration) error {
	if toleration.Operator == "" {
		toleration.Operator = core.TolerationOpEqual
	}

	if toleration.Key == "" && toleration.Operator != core.TolerationOpExists {
		return fmt.Errorf("toleration with empty key must have operator 'Exists'")
	}

	if toleration.Operator == core.TolerationOpExists && toleration.Value != "" {
		return fmt.Errorf("toleration with operator 'Exists' must have an empty value")
	}

	return nil
}

// generateName generates a distinct name for the DaemonSet based on the service name and UID
func generateName(svc *core.Service) string {
	name := svc.Name
	// ensure that the service name plus prefix and uuid aren't overly long, but
	// don't cut the service name at a trailing hyphen.
	if len(name) > 48 {
		trimlen := 48
		for name[trimlen-1] == '-' {
			trimlen--