Accept relative URLs in addon channels

This commit is contained in:
Justin Santa Barbara 2016-09-26 09:44:30 -04:00
parent d4e659811b
commit f339d272d9
3 changed files with 41 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/kops/channels/pkg/channels"
"k8s.io/kops/util/pkg/tables"
"net/url"
"os"
)
@ -37,9 +38,25 @@ func (c *ApplyChannelCmd) Run(args []string) error {
return err
}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
baseURL, err := url.Parse(cwd + string(os.PathSeparator))
if err != nil {
return fmt.Errorf("error building url for current directory %q: %v", cwd, err)
}
var addons []*channels.Addon
for _, arg := range args {
o, err := channels.LoadAddons(arg)
channel, err := url.Parse(arg)
if err != nil {
return fmt.Errorf("unable to parse argument %q as url", arg)
}
if !channel.IsAbs() {
channel = baseURL.ResolveReference(channel)
}
o, err := channels.LoadAddons(channel)
if err != nil {
return fmt.Errorf("error loading file %q: %v", arg, err)
}

View File

@ -6,11 +6,12 @@ import (
"k8s.io/kops/channels/pkg/api"
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_3"
"k8s.io/kubernetes/pkg/util/validation/field"
"net/url"
)
type Addon struct {
Name string
Channel string
Channel url.URL
Spec *api.AddonSpec
}
@ -21,8 +22,9 @@ type AddonUpdate struct {
}
func (a *Addon) ChannelVersion() *ChannelVersion {
channel := a.Channel.String()
return &ChannelVersion{
Channel: &a.Channel,
Channel: &channel,
Version: a.Spec.Version,
}
}
@ -74,9 +76,16 @@ func (a *Addon) EnsureUpdated(k8sClient *release_1_3.Clientset) (*AddonUpdate, e
}
manifest := *a.Spec.Manifest
glog.Infof("Applying update from %q", manifest)
manifestURL, err := url.Parse(manifest)
if err != nil {
return nil, field.Invalid(field.NewPath("Spec", "Manifest"), manifest, "Not a valid URL")
}
if !manifestURL.IsAbs() {
manifestURL = a.Channel.ResolveReference(manifestURL)
}
glog.Infof("Applying update from %q", manifestURL)
err = Apply(manifest)
err = Apply(manifestURL.String())
if err != nil {
return nil, fmt.Errorf("error applying update from %q: %v", manifest, err)
}

View File

@ -5,16 +5,17 @@ import (
"k8s.io/kops/channels/pkg/api"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kops/util/pkg/vfs"
"net/url"
"strings"
)
type Addons struct {
Channel string
Channel url.URL
APIObject *api.Addons
}
func LoadAddons(location string) (*Addons, error) {
data, err := vfs.Context.ReadFile(location)
func LoadAddons(location *url.URL) (*Addons, error) {
data, err := vfs.Context.ReadFile(location.String())
if err != nil {
return nil, fmt.Errorf("error reading addons from %q: %v", location, err)
}
@ -31,7 +32,7 @@ func LoadAddons(location string) (*Addons, error) {
}
}
return &Addons{Channel: location, APIObject: apiObject}, nil
return &Addons{Channel: *location, APIObject: apiObject}, nil
}
func (a *Addons) GetCurrent() ([]*Addon, error) {
@ -42,7 +43,11 @@ func (a *Addons) GetCurrent() ([]*Addon, error) {
name = *s.Name
}
addon := &Addon{Channel: a.Channel, Spec: s, Name: name}
addon := &Addon{
Channel: a.Channel,
Spec: s,
Name: name,
}
existing := specs[name]
if existing == nil || addon.ChannelVersion().Replaces(existing.ChannelVersion()) {
specs[name] = addon