-
Notifications
You must be signed in to change notification settings - Fork 37
/
config.go
127 lines (104 loc) · 2.59 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package config
import (
"fmt"
"github.com/ghodss/yaml"
ctlres "github.com/k14s/kbld/pkg/kbld/resources"
)
const (
configAPIVersion = "kbld.k14s.io/v1alpha1"
sourcesKind = "Sources" // specify list of sources for building images
imageOverridesKind = "ImageOverrides" // specify alternative image urls
imageDestinationsKind = "ImageDestinations" // specify image push destinations
)
var (
configKinds = []string{sourcesKind, imageOverridesKind, imageDestinationsKind}
)
type Config struct {
APIVersion string `json:"apiVersion"`
Kind string
Sources []Source
Overrides []ImageOverride
Destinations []ImageDestination
}
type Source struct {
Image string
Path string
Docker *SourceDockerOpts
Pack *SourcePackOpts
}
type ImageOverride struct {
Image string
NewImage string `json:"newImage"`
Preresolved bool
}
type ImageDestination struct {
Image string
NewImage string `json:"newImage"`
}
func NewConfigFromResource(res ctlres.Resource) (Config, error) {
bs, err := res.AsYAMLBytes()
if err != nil {
return Config{}, err
}
var config Config
err = yaml.Unmarshal(bs, &config)
if err != nil {
return Config{}, fmt.Errorf("Unmarshaling %s: %s", res.Description(), err)
}
err = config.Validate()
if err != nil {
return Config{}, fmt.Errorf("Validating %s: %s", res.Description(), err)
}
for i, imageDst := range config.Destinations {
if len(imageDst.NewImage) == 0 {
imageDst.NewImage = imageDst.Image
}
config.Destinations[i] = imageDst
}
return config, nil
}
func (d Config) Validate() error {
for i, src := range d.Sources {
err := src.Validate()
if err != nil {
return fmt.Errorf("Validating Sources[%d]: %s", i, err)
}
}
for i, override := range d.Overrides {
err := override.Validate()
if err != nil {
return fmt.Errorf("Validating Overrides[%d]: %s", i, err)
}
}
for i, dst := range d.Destinations {
err := dst.Validate()
if err != nil {
return fmt.Errorf("Validating Destinations[%d]: %s", i, err)
}
}
return nil
}
func (d Source) Validate() error {
if len(d.Image) == 0 {
return fmt.Errorf("Expected Image to be non-empty")
}
if len(d.Path) == 0 {
return fmt.Errorf("Expected Path to be non-empty")
}
return nil
}
func (d ImageOverride) Validate() error {
if len(d.Image) == 0 {
return fmt.Errorf("Expected Image to be non-empty")
}
if len(d.NewImage) == 0 {
return fmt.Errorf("Expected NewImage to be non-empty")
}
return nil
}
func (d ImageDestination) Validate() error {
if len(d.Image) == 0 {
return fmt.Errorf("Expected Image to be non-empty")
}
return nil
}