mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-10 08:56:47 +00:00
**What** - Use the compose-go library to read the service definitions from an external compose file instead of building them in Go - Add default compose file and copy during `faasd install` - Add test for load and parse of compose file - Make testing easier by sorting the env keys - Allow append to instantiate the slices so that we can more easily test for proper parsing (e.g. nil is still nil etc) - Add the arch suffix to the compose file and set this as part of the env when we parse the compose file. This allows faasd to dynamically set the arch suffix used for the basic auth and the gateway images. Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
147 lines
3.5 KiB
Go
Generated
147 lines
3.5 KiB
Go
Generated
/*
|
|
Copyright 2020 The Compose Specification Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package loader
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const endOfSpec = rune(0)
|
|
|
|
// ParseVolume parses a volume spec without any knowledge of the target platform
|
|
func ParseVolume(spec string) (types.ServiceVolumeConfig, error) {
|
|
volume := types.ServiceVolumeConfig{}
|
|
|
|
switch len(spec) {
|
|
case 0:
|
|
return volume, errors.New("invalid empty volume spec")
|
|
case 1, 2:
|
|
volume.Target = spec
|
|
volume.Type = string(types.VolumeTypeVolume)
|
|
return volume, nil
|
|
}
|
|
|
|
buffer := []rune{}
|
|
for _, char := range spec + string(endOfSpec) {
|
|
switch {
|
|
case isWindowsDrive(buffer, char):
|
|
buffer = append(buffer, char)
|
|
case char == ':' || char == endOfSpec:
|
|
if err := populateFieldFromBuffer(char, buffer, &volume); err != nil {
|
|
populateType(&volume)
|
|
return volume, errors.Wrapf(err, "invalid spec: %s", spec)
|
|
}
|
|
buffer = []rune{}
|
|
default:
|
|
buffer = append(buffer, char)
|
|
}
|
|
}
|
|
|
|
populateType(&volume)
|
|
return volume, nil
|
|
}
|
|
|
|
func isWindowsDrive(buffer []rune, char rune) bool {
|
|
return char == ':' && len(buffer) == 1 && unicode.IsLetter(buffer[0])
|
|
}
|
|
|
|
func populateFieldFromBuffer(char rune, buffer []rune, volume *types.ServiceVolumeConfig) error {
|
|
strBuffer := string(buffer)
|
|
switch {
|
|
case len(buffer) == 0:
|
|
return errors.New("empty section between colons")
|
|
// Anonymous volume
|
|
case volume.Source == "" && char == endOfSpec:
|
|
volume.Target = strBuffer
|
|
return nil
|
|
case volume.Source == "":
|
|
volume.Source = strBuffer
|
|
return nil
|
|
case volume.Target == "":
|
|
volume.Target = strBuffer
|
|
return nil
|
|
case char == ':':
|
|
return errors.New("too many colons")
|
|
}
|
|
for _, option := range strings.Split(strBuffer, ",") {
|
|
switch option {
|
|
case "ro":
|
|
volume.ReadOnly = true
|
|
case "rw":
|
|
volume.ReadOnly = false
|
|
case "nocopy":
|
|
volume.Volume = &types.ServiceVolumeVolume{NoCopy: true}
|
|
default:
|
|
if isBindOption(option) {
|
|
volume.Bind = &types.ServiceVolumeBind{Propagation: option}
|
|
}
|
|
// ignore unknown options
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var Propagations = []string{
|
|
types.PropagationRPrivate,
|
|
types.PropagationPrivate,
|
|
types.PropagationRShared,
|
|
types.PropagationShared,
|
|
types.PropagationRSlave,
|
|
types.PropagationSlave,
|
|
}
|
|
|
|
func isBindOption(option string) bool {
|
|
for _, propagation := range Propagations {
|
|
if option == propagation {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func populateType(volume *types.ServiceVolumeConfig) {
|
|
switch {
|
|
// Anonymous volume
|
|
case volume.Source == "":
|
|
volume.Type = string(types.VolumeTypeVolume)
|
|
case isFilePath(volume.Source):
|
|
volume.Type = string(types.VolumeTypeBind)
|
|
default:
|
|
volume.Type = string(types.VolumeTypeVolume)
|
|
}
|
|
}
|
|
|
|
func isFilePath(source string) bool {
|
|
switch source[0] {
|
|
case '.', '/', '~':
|
|
return true
|
|
}
|
|
|
|
// windows named pipes
|
|
if strings.HasPrefix(source, `\\`) {
|
|
return true
|
|
}
|
|
|
|
first, nextIndex := utf8.DecodeRuneInString(source)
|
|
return isWindowsDrive([]rune{first}, rune(source[nextIndex]))
|
|
}
|