Skip to content

Commit

Permalink
Mounts EFS volumes to k8s pod/deployment containers
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSeptimus-Klotho committed Aug 11, 2023
1 parent a723052 commit 9657a53
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
14 changes: 10 additions & 4 deletions pkg/provider/aws/knowledgebase/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
k8sSanitizer "github.com/klothoplatform/klotho/pkg/sanitization/kubernetes"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"path"
"sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
"strings"

Expand Down Expand Up @@ -117,8 +118,7 @@ var EksKB = knowledgebase.Build(
}
assumeRolePolicy := resources.GetServiceAccountAssumeRolePolicy(sa.Object.Name, sa.Object.Namespace, oidc)
role.AssumeRolePolicyDoc = assumeRolePolicy
dag.AddDependenciesReflect(role)

dag.AddDependencyWithData(role, oidc, data)
return nil
},
},
Expand Down Expand Up @@ -193,13 +193,19 @@ var EksKB = knowledgebase.Build(
knowledgebase.EdgeBuilder[*kubernetes.Manifest, *resources.EfsFileSystem]{},
knowledgebase.EdgeBuilder[*kubernetes.Pod, *resources.EfsMountTarget]{
Configure: func(pod *kubernetes.Pod, mountTarget *resources.EfsMountTarget, dag *core.ResourceGraph, data knowledgebase.EdgeData) error {
_, err := resources.MountEfsVolume(pod, mountTarget, dag, data.AppName)
if mountTarget.FileSystem == nil {
return fmt.Errorf("%s has no file system", mountTarget.Id())
}
_, err := resources.MountEfsVolume(pod, mountTarget, dag, data.AppName, path.Join("/mnt/efs", mountTarget.FileSystem.Name))
return err
},
},
knowledgebase.EdgeBuilder[*kubernetes.Deployment, *resources.EfsMountTarget]{
Configure: func(deployment *kubernetes.Deployment, mountTarget *resources.EfsMountTarget, dag *core.ResourceGraph, data knowledgebase.EdgeData) error {
_, err := resources.MountEfsVolume(deployment, mountTarget, dag, data.AppName)
if mountTarget.FileSystem == nil {
return fmt.Errorf("%s has no file system", mountTarget.Id())
}
_, err := resources.MountEfsVolume(deployment, mountTarget, dag, data.AppName, path.Join("/mnt/efs", mountTarget.FileSystem.Name))
return err
},
},
Expand Down
93 changes: 86 additions & 7 deletions pkg/provider/aws/resources/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,94 @@ func (cluster *EksCluster) InstallFluentBit(references core.BaseConstructSet, da
return nil
}

func MountEfsVolume(resource core.Resource, mountTarget *EfsMountTarget, dag *core.ResourceGraph, appName string) (*kubernetes.Manifest, error) {
isValidType := false
switch resource.(type) {
func MountEfsVolume(resource core.Resource, mountTarget *EfsMountTarget, dag *core.ResourceGraph, appName string, mountPath string) (*kubernetes.Manifest, error) {
if mountTarget.FileSystem == nil {
return nil, fmt.Errorf("%s has no file system", mountTarget.Id())
}

volumeName := k8sSanitizer.RFC1035LabelSanitizer.Apply(fmt.Sprintf("%s-volume", mountTarget.FileSystem.Name))
volumeMount := corev1.VolumeMount{
Name: volumeName,
MountPath: mountPath,
}
volume := corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "efs-claim",
},
},
}

switch resource := resource.(type) {
// TODO: look into how to mount to a specific container when there are multiple containers in a pod
case *kubernetes.Deployment:
isValidType = true
if resource.Object == nil {
return nil, fmt.Errorf("%s has no Object", resource.Id())
}
if resource.Object.Spec.Template.Spec.Containers == nil {
return nil, fmt.Errorf("efs volume %s cannot be mounted: %s has no containers", mountTarget.FileSystem.Id(), resource.Id())
}
for i, container := range resource.Object.Spec.Template.Spec.Containers {
containerRef := &container
mountAdded := false
for i, existingMount := range containerRef.VolumeMounts {
if volumeMount.Name == existingMount.Name {
containerRef.VolumeMounts[i] = volumeMount
mountAdded = true
break
}
}
if !mountAdded {
containerRef.VolumeMounts = append(containerRef.VolumeMounts, volumeMount)
}
resource.Object.Spec.Template.Spec.Containers[i] = *containerRef
}
volumeAdded := false
for i, existingVolume := range resource.Object.Spec.Template.Spec.Volumes {
if volume.Name == existingVolume.Name {
resource.Object.Spec.Template.Spec.Volumes[i] = volume
volumeAdded = true
break
}
}
if !volumeAdded {
resource.Object.Spec.Template.Spec.Volumes = append(resource.Object.Spec.Template.Spec.Volumes, volume)
}
case *kubernetes.Pod:
isValidType = true
}
if !isValidType {
if resource.Object == nil {
return nil, fmt.Errorf("%s has no Object", resource.Id())
}
if len(resource.Object.Spec.Containers) == 0 {
return nil, fmt.Errorf("efs volume %s cannot be mounted: %s has no containers", mountTarget.FileSystem.Id(), resource.Id())
}
for i, container := range resource.Object.Spec.Containers {
containerRef := &container
mountAdded := false
for i, existingMount := range containerRef.VolumeMounts {
if volumeMount.Name == existingMount.Name {
containerRef.VolumeMounts[i] = volumeMount
mountAdded = true
break
}
}
if !mountAdded {
containerRef.VolumeMounts = append(containerRef.VolumeMounts, volumeMount)
}
resource.Object.Spec.Containers[i] = *containerRef
}
volumeAdded := false
for i, existingVolume := range resource.Object.Spec.Volumes {
if volume.Name == existingVolume.Name {
resource.Object.Spec.Volumes[i] = volume
volumeAdded = true
break
}
}
if !volumeAdded {
resource.Object.Spec.Volumes = append(resource.Object.Spec.Volumes, volume)
}
default:
return nil, fmt.Errorf("resource %s is not a valid EFS volume mount location in an EKS cluster", resource.Id())
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/provider/kubernetes/resources/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package resources

import (
"fmt"
"path"

"github.com/klothoplatform/klotho/pkg/core"
"github.com/klothoplatform/klotho/pkg/sanitization/kubernetes"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down

0 comments on commit 9657a53

Please sign in to comment.