|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "text/template" |
| 7 | +) |
| 8 | + |
| 9 | +// RuntimeTemplateContext contains the data available to runtime templates in a service spec. |
| 10 | +type RuntimeTemplateContext struct { |
| 11 | + Container RuntimeTemplateContainerContext |
| 12 | +} |
| 13 | + |
| 14 | +// RuntimeTemplateContainerContext contains container data available to runtime templates in a service spec. |
| 15 | +type RuntimeTemplateContainerContext struct { |
| 16 | + Name string |
| 17 | +} |
| 18 | + |
| 19 | +// RenderRuntimeTemplates returns a copy of the service spec with supported runtime template expressions rendered |
| 20 | +// using metadata from ctx. Runtime templates use Go template expressions such as {{.Container.Name}} and are supported |
| 21 | +// in bind volume host paths and volume mount container paths. The original service spec is not modified. |
| 22 | +func (s *ServiceSpec) RenderRuntimeTemplates(ctx RuntimeTemplateContext) (ServiceSpec, error) { |
| 23 | + if ctx.Container.Name == "" { |
| 24 | + return ServiceSpec{}, fmt.Errorf("container name must not be empty") |
| 25 | + } |
| 26 | + |
| 27 | + spec := s.Clone() |
| 28 | + |
| 29 | + for i := range spec.Volumes { |
| 30 | + volume := &spec.Volumes[i] |
| 31 | + if volume.Type != VolumeTypeBind { |
| 32 | + continue |
| 33 | + } |
| 34 | + if err := volume.Validate(); err != nil { |
| 35 | + return ServiceSpec{}, fmt.Errorf("invalid bind volume '%s': %w", volume.Name, err) |
| 36 | + } |
| 37 | + |
| 38 | + hostPath, err := renderRuntimeTemplate(volume.BindOptions.HostPath, ctx) |
| 39 | + if err != nil { |
| 40 | + return ServiceSpec{}, fmt.Errorf("render runtime template in bind volume '%s' host path: %w", |
| 41 | + volume.Name, err) |
| 42 | + } |
| 43 | + volume.BindOptions.HostPath = hostPath |
| 44 | + |
| 45 | + if err = volume.Validate(); err != nil { |
| 46 | + return ServiceSpec{}, fmt.Errorf("invalid rendered bind volume '%s': %w", volume.Name, err) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for i := range spec.Container.VolumeMounts { |
| 51 | + volumeMount := &spec.Container.VolumeMounts[i] |
| 52 | + if err := volumeMount.Validate(); err != nil { |
| 53 | + return ServiceSpec{}, fmt.Errorf("invalid volume mount '%s': %w", volumeMount.VolumeName, err) |
| 54 | + } |
| 55 | + |
| 56 | + containerPath, err := renderRuntimeTemplate(volumeMount.ContainerPath, ctx) |
| 57 | + if err != nil { |
| 58 | + return ServiceSpec{}, fmt.Errorf( |
| 59 | + "render runtime template in volume '%s' container path: %w", volumeMount.VolumeName, err) |
| 60 | + } |
| 61 | + volumeMount.ContainerPath = containerPath |
| 62 | + |
| 63 | + if err = volumeMount.Validate(); err != nil { |
| 64 | + return ServiceSpec{}, fmt.Errorf("invalid rendered volume mount '%s': %w", volumeMount.VolumeName, err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return spec, nil |
| 69 | +} |
| 70 | + |
| 71 | +func renderRuntimeTemplate(value string, ctx RuntimeTemplateContext) (string, error) { |
| 72 | + tmpl, err := template.New("runtime template").Option("missingkey=error").Parse(value) |
| 73 | + if err != nil { |
| 74 | + return "", fmt.Errorf("parse runtime template: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + var rendered bytes.Buffer |
| 78 | + if err = tmpl.Execute(&rendered, ctx); err != nil { |
| 79 | + return "", fmt.Errorf("execute runtime template: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + return rendered.String(), nil |
| 83 | +} |
| 84 | + |
| 85 | +func validateRuntimeTemplates(spec *ServiceSpec) error { |
| 86 | + _, err := spec.RenderRuntimeTemplates(RuntimeTemplateContext{ |
| 87 | + Container: RuntimeTemplateContainerContext{Name: "validation-container-name"}, |
| 88 | + }) |
| 89 | + return err |
| 90 | +} |
0 commit comments