squakez commented on code in PR #7013:
URL: https://github.com/apache/camel-k/pull/7013#discussion_r4810020532


##########
pkg/trait/mount.go:
##########
@@ -311,6 +317,170 @@ func (t *mountTrait) configureCamelVolumesAndMounts(e 
*Environment, vols *[]core
        }
 }
 
+// mountResources mounts resources individually by default. When projected 
volumes are enabled,
+// resources targeting the same directory are combined into a single projected 
volume.
+func (t *mountTrait) mountResources(
+       e *Environment,
+       vols *[]corev1.Volume,
+       mnts *[]corev1.VolumeMount,
+       icnts *[]corev1.Container,
+       resources []*utilResource.Config,
+) error {
+       if !t.ProjectedVolumes {
+               for _, conf := range resources {
+                       t.mountResource(vols, mnts, icnts, conf)
+               }
+
+               return nil
+       }
+
+       groups := make(map[string][]*utilResource.Config)
+       var groupOrder []string
+       var standalone []*utilResource.Config
+       for _, conf := range resources {
+               if conf.DestinationPath() == "" {
+                       standalone = append(standalone, conf)
+                       continue
+               }
+
+               mountPath := conf.DestinationPath()
+               if conf.Key() != "" {
+                       mountPath = filepath.Dir(mountPath)
+               }
+               mountPath = filepath.Clean(mountPath)
+               if _, exists := groups[mountPath]; !exists {
+                       groupOrder = append(groupOrder, mountPath)
+               }
+               groups[mountPath] = append(groups[mountPath], conf)
+       }
+
+       for _, conf := range standalone {
+               t.mountResource(vols, mnts, icnts, conf)
+       }
+       for _, mountPath := range groupOrder {
+               group := groups[mountPath]
+               if len(group) == 1 {
+                       t.mountResource(vols, mnts, icnts, group[0])
+                       continue
+               }
+               if err := t.mountProjectedResources(e, vols, mnts, icnts, 
mountPath, group); err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
+// mountProjectedResources combines ConfigMap and Secret resources into one 
projected volume.
+// Duplicate destination paths are rejected rather than relying on Kubernetes 
projection precedence.
+func (t *mountTrait) mountProjectedResources(
+       e *Environment,
+       vols *[]corev1.Volume,
+       mnts *[]corev1.VolumeMount,
+       icnts *[]corev1.Container,
+       mountPath string,
+       resources []*utilResource.Config,
+) error {
+       projections := make([]corev1.VolumeProjection, 0, len(resources))
+       seenPaths := make(map[string]string)
+       var seenPathOrder []string
+
+       for _, conf := range resources {
+               var items []corev1.KeyToPath
+               var projectedPaths []string
+               if conf.Key() != "" {
+                       fileName := filepath.Base(conf.DestinationPath())
+                       items = []corev1.KeyToPath{{Key: conf.Key(), Path: 
fileName}}
+                       projectedPaths = []string{fileName}
+               } else {
+                       // Leave Items nil so newly added keys continue to be 
projected without
+                       // requiring the Integration pod to be recreated.
+                       keys, err := t.projectedResourceKeys(e, conf)
+                       if err != nil {
+                               return err
+                       }
+                       projectedPaths = keys
+               }
+
+               for _, projectedPath := range projectedPaths {
+                       for _, existingPath := range seenPathOrder {
+                               if projectedPath == existingPath || 
strings.HasPrefix(projectedPath, existingPath+"/") || 
strings.HasPrefix(existingPath, projectedPath+"/") {
+                                       return fmt.Errorf("projected volume 
mount path %q conflicts with %q requested by %s (also requested by %s)", 
projectedPath, existingPath, seenPaths[existingPath], conf.String())
+                               }
+                       }
+                       seenPaths[projectedPath] = conf.String()
+                       seenPathOrder = append(seenPathOrder, projectedPath)
+               }
+
+               switch conf.StorageType() {
+               case utilResource.StorageTypeConfigmap:
+                       projections = append(projections, 
corev1.VolumeProjection{
+                               ConfigMap: &corev1.ConfigMapProjection{
+                                       LocalObjectReference: 
corev1.LocalObjectReference{Name: conf.Name()},
+                                       Items:                items,
+                               },
+                       })
+               case utilResource.StorageTypeSecret:
+                       projections = append(projections, 
corev1.VolumeProjection{
+                               Secret: &corev1.SecretProjection{
+                                       LocalObjectReference: 
corev1.LocalObjectReference{Name: conf.Name()},
+                                       Items:                items,
+                               },
+                       })
+               default:
+                       return fmt.Errorf("unsupported projected resource 
storage type %s", conf.StorageType())
+               }
+       }
+
+       volumeName, _ := 
sanitizeVolumeName("projected-"+filepath.Base(mountPath), vols)
+       volume := corev1.Volume{
+               Name: volumeName,
+               VolumeSource: corev1.VolumeSource{
+                       Projected: &corev1.ProjectedVolumeSource{Sources: 
projections},
+               },
+       }
+       volumeMount := getMount(volumeName, mountPath, "", true)
+       *vols = append(*vols, volume)
+       *mnts = append(*mnts, *volumeMount)
+       for i := range *icnts {
+               (*icnts)[i].VolumeMounts = append((*icnts)[i].VolumeMounts, 
*volumeMount)
+       }
+
+       return nil
+}
+
+// projectedResourceKeys returns all keys exposed by an unfiltered ConfigMap 
or Secret.
+// Reading the keys lets us detect collisions before producing an ambiguous 
projection.
+func (t *mountTrait) projectedResourceKeys(e *Environment, conf 
*utilResource.Config) ([]string, error) {
+       keys := make([]string, 0)
+       switch conf.StorageType() {
+       case utilResource.StorageTypeConfigmap:
+               configMap := &corev1.ConfigMap{}
+               if err := e.Client.Get(e.Ctx, types.NamespacedName{Namespace: 
e.Integration.Namespace, Name: conf.Name()}, configMap); err != nil {
+                       return nil, fmt.Errorf("could not read ConfigMap %s for 
projected mount: %w", conf.Name(), err)
+               }
+               for key := range configMap.Data {
+                       keys = append(keys, key)
+               }
+               for key := range configMap.BinaryData {
+                       keys = append(keys, key)
+               }
+       case utilResource.StorageTypeSecret:
+               secret := &corev1.Secret{}
+               if err := e.Client.Get(e.Ctx, types.NamespacedName{Namespace: 
e.Integration.Namespace, Name: conf.Name()}, secret); err != nil {

Review Comment:
   Right now we have configmap and secret credentials. However we may remove 
get privilege for secrets in the future, so we should avoid getting those info.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to