github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/plan/task/core/mount.go (about) 1 package core 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "strings" 7 8 "dagger.io/dagger" 9 ) 10 11 func init() { 12 DefaultFactory.Register(&Mount{}) 13 } 14 15 type Mounter interface { 16 MountType() string 17 MountTo(client *dagger.Client, container *dagger.Container) *dagger.Container 18 } 19 20 type Mount struct { 21 Mounter `json:"-"` 22 } 23 24 func (m *Mount) UnmarshalJSON(data []byte) error { 25 mt := &struct { 26 Type string `json:"type"` 27 }{} 28 29 if err := json.Unmarshal(data, mt); err != nil { 30 return err 31 } 32 33 for _, v := range m.OneOf() { 34 if i, ok := v.(Mounter); ok { 35 if i.MountType() == mt.Type { 36 i = reflect.New(reflect.TypeOf(i).Elem()).Interface().(Mounter) 37 38 if err := json.Unmarshal(data, i); err != nil { 39 return err 40 } 41 42 m.Mounter = i 43 return nil 44 } 45 } 46 47 } 48 49 return nil 50 } 51 52 func (m Mount) MarshalJSON() ([]byte, error) { 53 return json.Marshal(m.Mounter) 54 } 55 56 func (Mount) OneOf() []any { 57 return []any{ 58 &MountCacheDir{}, 59 &MountTemp{}, 60 &MountFs{}, 61 &MountSecret{}, 62 &MountFile{}, 63 } 64 } 65 66 var _ Mounter = &MountCacheDir{} 67 var _ Mounter = &MountTemp{} 68 var _ Mounter = &MountFs{} 69 var _ Mounter = &MountSecret{} 70 var _ Mounter = &MountFile{} 71 72 type MountCacheDir struct { 73 Type string `json:"type" enum:"cache"` 74 Dest string `json:"dest"` 75 Contents CacheDir `json:"contents"` 76 } 77 78 type CacheDir struct { 79 ID string `json:"id"` 80 Concurrency string `json:"concurrency" default:"shared" wagon:"deprecated"` 81 } 82 83 func (MountCacheDir) MountType() string { 84 return "cache" 85 } 86 87 func (m MountCacheDir) MountTo(client *dagger.Client, c *dagger.Container) *dagger.Container { 88 return c.WithMountedCache(m.Dest, client.CacheVolume(m.Contents.ID), dagger.ContainerWithMountedCacheOpts{}) 89 } 90 91 type MountTemp struct { 92 Type string `json:"type" enum:"tmp"` 93 Dest string `json:"dest"` 94 Contents TempDir `json:"contents"` 95 } 96 97 type TempDir struct { 98 Size int64 `json:"size" default:"0" wagon:"deprecated"` 99 } 100 101 func (t MountTemp) MountTo(client *dagger.Client, container *dagger.Container) *dagger.Container { 102 return container.WithMountedTemp(t.Dest) 103 } 104 105 func (MountTemp) MountType() string { 106 return "tmp" 107 } 108 109 type MountFs struct { 110 Type string `json:"type" enum:"fs"` 111 Contents FS `json:"contents"` 112 113 Dest string `json:"dest"` 114 Source *string `json:"source,omitempty"` 115 116 ReadOnly bool `json:"ro,omitempty" wagon:"deprecated"` 117 } 118 119 func (f MountFs) MountTo(c *dagger.Client, container *dagger.Container) *dagger.Container { 120 dir := f.Contents.LoadDirectory(c) 121 122 if source := f.Source; source != nil { 123 src := *source 124 // Same as k8s, when 125 // 126 // mountPath: /etc/config/xxxfile 127 // subPath: xxxfile 128 // 129 // will mount as file 130 if strings.HasSuffix(f.Dest, src) { 131 return container.WithMountedFile(f.Dest, dir.File(src)) 132 } 133 134 // otherwise will mount with sub dir 135 dir = dir.Directory(src) 136 } 137 138 return container.WithMountedDirectory(f.Dest, dir) 139 } 140 141 func (MountFs) MountType() string { 142 return "fs" 143 } 144 145 type MountSecret struct { 146 Type string `json:"type" enum:"secret"` 147 Dest string `json:"dest"` 148 Contents Secret `json:"contents"` 149 150 Uid int `json:"uid" default:"0" wagon:"deprecated"` 151 Gid int `json:"gid" default:"0" wagon:"deprecated"` 152 Mask int `json:"mask" default:"0o644" wagon:"deprecated"` 153 } 154 155 func (m MountSecret) MountTo(client *dagger.Client, container *dagger.Container) *dagger.Container { 156 return container.WithMountedSecret(m.Dest, client.LoadSecretFromID(m.Contents.SecretID())) 157 } 158 159 func (MountSecret) MountType() string { 160 return "secret" 161 } 162 163 type MountFile struct { 164 Type string `json:"type" enum:"file"` 165 Dest string `json:"dest"` 166 Contents string `json:"contents"` 167 Permissions int `json:"mask" default:"0o644"` 168 } 169 170 func (m MountFile) MountTo(client *dagger.Client, container *dagger.Container) *dagger.Container { 171 f := client.Container(). 172 WithNewFile("/tmp", dagger.ContainerWithNewFileOpts{ 173 Contents: m.Contents, 174 Permissions: m.Permissions, 175 }). 176 File("/tmp") 177 178 return container.WithMountedFile(m.Dest, f) 179 } 180 181 func (MountFile) MountType() string { 182 return "file" 183 }