TransformDevicePlugin transforms k8s-device-plugin daemonset with required config as per ClusterPolicy
(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec, n ClusterPolicyController)
| 1518 | |
| 1519 | // TransformDevicePlugin transforms k8s-device-plugin daemonset with required config as per ClusterPolicy |
| 1520 | func TransformDevicePlugin(obj *appsv1.DaemonSet, config *gpuv1.ClusterPolicySpec, n ClusterPolicyController) error { |
| 1521 | devicePluginContainerName := "nvidia-device-plugin" |
| 1522 | devicePluginMainContainer := findContainerByName(obj.Spec.Template.Spec.Containers, devicePluginContainerName) |
| 1523 | if devicePluginMainContainer == nil { |
| 1524 | return fmt.Errorf("failed to find device plugin container %q", devicePluginContainerName) |
| 1525 | } |
| 1526 | |
| 1527 | // update validation container |
| 1528 | err := transformValidationInitContainer(obj, config) |
| 1529 | if err != nil { |
| 1530 | return err |
| 1531 | } |
| 1532 | |
| 1533 | // update image |
| 1534 | image, err := gpuv1.ImagePath(&config.DevicePlugin) |
| 1535 | if err != nil { |
| 1536 | return err |
| 1537 | } |
| 1538 | devicePluginMainContainer.Image = image |
| 1539 | |
| 1540 | // update image pull policy |
| 1541 | devicePluginMainContainer.ImagePullPolicy = gpuv1.ImagePullPolicy(config.DevicePlugin.ImagePullPolicy) |
| 1542 | |
| 1543 | // set image pull secrets |
| 1544 | if len(config.DevicePlugin.ImagePullSecrets) > 0 { |
| 1545 | addPullSecrets(&obj.Spec.Template.Spec, config.DevicePlugin.ImagePullSecrets) |
| 1546 | } |
| 1547 | |
| 1548 | // set resource limits |
| 1549 | if config.DevicePlugin.Resources != nil { |
| 1550 | // apply resource limits to all containers |
| 1551 | for i := range obj.Spec.Template.Spec.Containers { |
| 1552 | obj.Spec.Template.Spec.Containers[i].Resources.Requests = config.DevicePlugin.Resources.Requests |
| 1553 | obj.Spec.Template.Spec.Containers[i].Resources.Limits = config.DevicePlugin.Resources.Limits |
| 1554 | } |
| 1555 | } |
| 1556 | // set arguments if specified for device-plugin container |
| 1557 | if len(config.DevicePlugin.Args) > 0 { |
| 1558 | devicePluginMainContainer.Args = config.DevicePlugin.Args |
| 1559 | } |
| 1560 | |
| 1561 | // add env to allow injection of /dev/nvidia-fs and /dev/infiniband devices for GDS |
| 1562 | if config.GPUDirectStorage != nil && config.GPUDirectStorage.IsEnabled() { |
| 1563 | setContainerEnv(devicePluginMainContainer, GDSEnabledEnvName, "true") |
| 1564 | setContainerEnv(devicePluginMainContainer, MOFEDEnabledEnvName, "true") |
| 1565 | } |
| 1566 | |
| 1567 | if config.GDRCopy != nil && config.GDRCopy.IsEnabled() { |
| 1568 | setContainerEnv(devicePluginMainContainer, GDRCopyEnabledEnvName, "true") |
| 1569 | } |
| 1570 | |
| 1571 | // apply plugin configuration through ConfigMap if one is provided |
| 1572 | err = handleDevicePluginConfig(obj, config) |
| 1573 | if err != nil { |
| 1574 | return err |
| 1575 | } |
| 1576 | |
| 1577 | setRuntimeClassName(&obj.Spec.Template.Spec, config, n.runtime) |