createConfigMapVolumeMounts creates a VolumeMount for each key in the ConfigMap. Use subPath to ensure original contents at destinationDir are not overwritten.
(n ClusterPolicyController, configMapName string, destinationDir string)
| 3459 | // in the ConfigMap. Use subPath to ensure original contents |
| 3460 | // at destinationDir are not overwritten. |
| 3461 | func createConfigMapVolumeMounts(n ClusterPolicyController, configMapName string, destinationDir string) ([]corev1.VolumeMount, []corev1.KeyToPath, error) { |
| 3462 | ctx := n.ctx |
| 3463 | // get the ConfigMap |
| 3464 | cm := &corev1.ConfigMap{} |
| 3465 | opts := client.ObjectKey{Namespace: n.operatorNamespace, Name: configMapName} |
| 3466 | err := n.client.Get(ctx, opts, cm) |
| 3467 | if err != nil { |
| 3468 | return nil, nil, fmt.Errorf("ERROR: could not get ConfigMap %s from client: %v", configMapName, err) |
| 3469 | } |
| 3470 | |
| 3471 | // create one volume mount per file in the ConfigMap and use subPath |
| 3472 | var filenames []string |
| 3473 | for filename := range cm.Data { |
| 3474 | filenames = append(filenames, filename) |
| 3475 | } |
| 3476 | // sort so volume mounts are added to spec in deterministic order |
| 3477 | sort.Strings(filenames) |
| 3478 | var itemsToInclude []corev1.KeyToPath |
| 3479 | var volumeMounts []corev1.VolumeMount |
| 3480 | for _, filename := range filenames { |
| 3481 | volumeMounts = append(volumeMounts, |
| 3482 | corev1.VolumeMount{Name: configMapName, ReadOnly: true, MountPath: filepath.Join(destinationDir, filename), SubPath: filename}) |
| 3483 | itemsToInclude = append(itemsToInclude, corev1.KeyToPath{ |
| 3484 | Key: filename, |
| 3485 | Path: filename, |
| 3486 | }) |
| 3487 | } |
| 3488 | return volumeMounts, itemsToInclude, nil |
| 3489 | } |
| 3490 | |
| 3491 | func createConfigMapVolume(configMapName string, itemsToInclude []corev1.KeyToPath) corev1.Volume { |
| 3492 | volumeSource := corev1.VolumeSource{ |
no test coverage detected