func()

in internal/resource/resource_linux.go [178:215]


func (c cgroupv1Client) Apply(constraint Constraint) error {
	if constraint.MaxCPUUsage != 0 && constraint.MaxMemoryUsage != 0 {
		// If we want to set both limits, but one of the controllers do not exist,
		// use fallback option for consistency.
		if c.cpuController == "" && c.memoryController == "" {
			return fmt.Errorf("failed to find both cpu and memory controllers")
		}
	}

	// Write CPU controller configs if MaxCPUUsage is set.
	if constraint.MaxCPUUsage != 0 {
		if c.cpuController == "" {
			return fmt.Errorf("failed to find cpu controller")
		}
		cpuControllerDir := filepath.Join(c.cgroupsDir, c.cpuController, constraint.Name)
		if err := os.MkdirAll(cpuControllerDir, 0755); err != nil {
			return fmt.Errorf("failed to create cgroup cpu controller directory: %w", err)
		}
		if err := writeCPUControllerConfig(cpuControllerDir, constraint, cgroupv1); err != nil {
			return err
		}
	}
	// Write memory controller configs if MaxMemoryUsage is set.
	if constraint.MaxMemoryUsage != 0 {
		if c.memoryController == "" {
			return fmt.Errorf("failed to find memory controller")
		}
		memoryControllerDir := filepath.Join(c.cgroupsDir, c.memoryController, constraint.Name)
		if err := os.MkdirAll(memoryControllerDir, 0755); err != nil {
			return fmt.Errorf("failed to create cgroup memory controller directory: %w", err)
		}
		if err := writeMemoryControllerConfig(memoryControllerDir, constraint, c.memoryLimitFile); err != nil {
			return err
		}
	}

	return nil
}