forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
87 lines (71 loc) · 2.22 KB
/
channel.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
package api
import (
"fmt"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
k8sapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"net/url"
)
const DefaultChannelBase = "https://raw.githubusercontent.com/kubernetes/kops/master/channels/"
const DefaultChannel = "stable"
type Channel struct {
unversioned.TypeMeta `json:",inline"`
k8sapi.ObjectMeta `json:"metadata,omitempty"`
Spec ChannelSpec `json:"spec,omitempty"`
}
type ChannelSpec struct {
Images []*ChannelImageSpec `json:"images,omitempty"`
Cluster *ClusterSpec `json:"cluster,omitempty"`
}
type ChannelImageSpec struct {
Labels map[string]string `json:"labels,omitempty"`
ProviderID string `json:"providerID,omitempty"`
Name string `json:"name,omitempty"`
}
// LoadChannel loads a Channel object from the specified VFS location
func LoadChannel(location string) (*Channel, error) {
u, err := url.Parse(location)
if err != nil {
return nil, fmt.Errorf("invalid channel: %q", location)
}
if !u.IsAbs() {
base, err := url.Parse(DefaultChannelBase)
if err != nil {
return nil, fmt.Errorf("invalid base channel location: %q", DefaultChannelBase)
}
u = base.ResolveReference(u)
}
resolved := u.String()
glog.V(2).Infof("Loading channel from %q", resolved)
channel := &Channel{}
channelBytes, err := vfs.Context.ReadFile(resolved)
if err != nil {
return nil, fmt.Errorf("error reading channel %q: %v", resolved, err)
}
err = ParseYaml(channelBytes, channel)
if err != nil {
return nil, fmt.Errorf("error parsing channel %q: %v", resolved, err)
}
glog.V(4).Info("Channel contents: %s", string(channelBytes))
return channel, nil
}
// FindImage returns the image for the cloudprovider, or nil if none found
func (c *Channel) FindImage(provider fi.CloudProviderID) *ChannelImageSpec {
var matches []*ChannelImageSpec
for _, image := range c.Spec.Images {
if image.ProviderID != string(provider) {
continue
}
matches = append(matches, image)
}
if len(matches) == 0 {
glog.V(2).Infof("No matching images in channel for cloudprovider %q", provider)
return nil
}
if len(matches) != 1 {
glog.Warningf("Multiple matching images in channel for cloudprovider %q", provider)
}
return matches[0]
}