Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
fleetctl: check version only if using registry
Browse files Browse the repository at this point in the history
  • Loading branch information
bcwaldon committed Aug 15, 2014
1 parent 07ecfed commit 6aebd0f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 40 deletions.
3 changes: 0 additions & 3 deletions client/api.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package client

import (
"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"

"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
"github.com/coreos/fleet/sign"
Expand All @@ -12,7 +10,6 @@ type API interface {
CreateSignatureSet(*sign.SignatureSet) error
JobSignatureSet(string) (*sign.SignatureSet, error)

LatestVersion() (*semver.Version, error)
Machines() ([]machine.MachineState, error)

Unit(string) (*schema.Unit, error)
Expand Down
6 changes: 0 additions & 6 deletions client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path"

"github.com/coreos/fleet/Godeps/_workspace/src/code.google.com/p/google-api-go-client/googleapi"
"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"

"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
Expand Down Expand Up @@ -125,11 +124,6 @@ func (c *HTTPClient) SetUnitTargetState(name, target string) error {
return c.svc.Units.Set(name, &u).Do()
}

//NOTE(bcwaldon): This is only temporary until a better version negotiation mechanism is in place
func (c *HTTPClient) LatestVersion() (*semver.Version, error) {
return semver.NewVersion("0.0.0")
}

func is404(err error) bool {
googerr, ok := err.(*googleapi.Error)
return ok && googerr.Code == http.StatusNotFound
Expand Down
15 changes: 0 additions & 15 deletions client/registry.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
package client

import (
"net/http"
"time"

"github.com/coreos/fleet/etcd"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/schema"
)

func NewRegistryClient(trans *http.Transport, endpoint, keyPrefix string, requestTimeout time.Duration) (API, error) {
machines := []string{endpoint}
client, err := etcd.NewClient(machines, *trans, requestTimeout)
if err != nil {
return nil, err
}

reg := registry.New(client, keyPrefix)
return &RegistryClient{reg}, nil
}

type RegistryClient struct {
registry.Registry
}
Expand Down
23 changes: 15 additions & 8 deletions fleetctl/fleetctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func getFlags(flagset *flag.FlagSet) (flags []*flag.Flag) {
// latest fleet version found registered in the cluster. If any errors are encountered or fleetctl
// is >= the latest version found, it returns true. If it is < the latest found version, it returns
// false and a scary warning to the user.
func checkVersion() (string, bool) {
func checkVersion(reg registry.Registry) (string, bool) {
fv := version.SemVersion
lv, err := cAPI.LatestVersion()
lv, err := reg.LatestVersion()
if err != nil {
log.Errorf("error attempting to check latest fleet version in Registry: %v", err)
} else if lv != nil && fv.LessThan(*lv) {
Expand Down Expand Up @@ -213,11 +213,6 @@ func main() {
fmt.Fprint(os.Stderr, msg)
os.Exit(1)
}

msg, ok := checkVersion()
if !ok {
fmt.Fprint(os.Stderr, msg)
}
}

os.Exit(cmd.Run(cmd.Flags.Args()))
Expand Down Expand Up @@ -327,7 +322,19 @@ func getRegistryClient() (client.API, error) {
}

timeout := time.Duration(globalFlags.RequestTimeout*1000) * time.Millisecond
return client.NewRegistryClient(&trans, globalFlags.Endpoint, globalFlags.EtcdKeyPrefix, timeout)
machines := []string{globalFlags.Endpoint}
eClient, err := etcd.NewClient(machines, trans, timeout)
if err != nil {
return nil, err
}

reg := registry.New(eClient, globalFlags.EtcdKeyPrefix)

if msg, ok := checkVersion(reg); !ok {
fmt.Fprint(os.Stderr, msg)
}

return &client.RegistryClient{reg}, nil
}

// getChecker creates and returns a HostKeyChecker, or nil if any error is encountered
Expand Down
14 changes: 6 additions & 8 deletions fleetctl/fleetctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,32 @@ package main
import (
"testing"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/version"

"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
)

func newFakeRegistryForCheckVersion(v string) client.API {
func newFakeRegistryForCheckVersion(v string) registry.Registry {
sv, err := semver.NewVersion(v)
if err != nil {
panic(err)
}

reg := registry.NewFakeRegistry()
reg.SetLatestVersion(*sv)

return &client.RegistryClient{reg}
return reg
}

func TestCheckVersion(t *testing.T) {
cAPI = newFakeRegistryForCheckVersion(version.Version)
_, ok := checkVersion()
reg := newFakeRegistryForCheckVersion(version.Version)
_, ok := checkVersion(reg)
if !ok {
t.Errorf("checkVersion failed but should have succeeded")
}
cAPI = newFakeRegistryForCheckVersion("9.0.0")
msg, ok := checkVersion()
reg = newFakeRegistryForCheckVersion("9.0.0")
msg, ok := checkVersion(reg)
if ok || msg == "" {
t.Errorf("checkVersion succeeded but should have failed")
}
Expand Down
3 changes: 3 additions & 0 deletions fleetctl/list_unit_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestListUnitFilesFieldsToStrings(t *testing.T) {
assertEqual(t, "state", string(state), f)
}

// machineStates must be initialized since cAPI is not set
machineStates = map[string]*machine.MachineState{}

u.Machine = "some-id"
ms := listUnitFilesFields["tmachine"](u, true)
assertEqual(t, "machine", "some-id", ms)
Expand Down

0 comments on commit 6aebd0f

Please sign in to comment.