Skip to content

Commit

Permalink
make cpu-cores an alias of cores
Browse files Browse the repository at this point in the history
This change adds the concept of aliases to constraints, and aliases
the existing cpu-cores as an alternate to the now-standard cores.
This change also removes some problematic reflection code that
relied too heavily on internal knowledge of types and implementation
details.
  • Loading branch information
natefinch committed Sep 15, 2016
1 parent 2b61db7 commit 587433a
Show file tree
Hide file tree
Showing 57 changed files with 618 additions and 526 deletions.
4 changes: 2 additions & 2 deletions api/provisioner/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (s *provisionerSuite) TestSetInstanceInfo(c *gc.C) {
c.Assert(err, gc.ErrorMatches, "machine 1 not provisioned")
c.Assert(instanceId, gc.Equals, instance.Id(""))

hwChars := instance.MustParseHardware("cpu-cores=123", "mem=4G")
hwChars := instance.MustParseHardware("cores=123", "mem=4G")

volumes := []params.Volume{{
VolumeTag: "volume-1-0",
Expand Down Expand Up @@ -415,7 +415,7 @@ func (s *provisionerSuite) TestProvisioningInfo(c *gc.C) {
SpaceName: "{{if (lt . 2)}}space1{{else}}space2{{end}}",
})

cons := constraints.MustParse("cpu-cores=12 mem=8G spaces=^space1,space2")
cons := constraints.MustParse("cores=12 mem=8G spaces=^space1,space2")
template := state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Expand Down
10 changes: 5 additions & 5 deletions apiserver/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ func (s *serviceSuite) TestServiceUpdateSetConstraints(c *gc.C) {
application := s.AddTestingService(c, "dummy", s.AddTestingCharm(c, "dummy"))

// Update constraints for the application.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
args := params.ApplicationUpdate{
ApplicationName: "dummy",
Expand All @@ -1379,7 +1379,7 @@ func (s *serviceSuite) TestServiceUpdateAllParams(c *gc.C) {

// Update all the service attributes.
minUnits := 3
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
args := params.ApplicationUpdate{
ApplicationName: "application",
Expand Down Expand Up @@ -2323,7 +2323,7 @@ func (s *serviceSuite) TestClientSetServiceConstraints(c *gc.C) {
application := s.AddTestingService(c, "dummy", s.AddTestingCharm(c, "dummy"))

// Update constraints for the application.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = s.applicationAPI.SetConstraints(params.SetConstraints{ApplicationName: "dummy", Constraints: cons})
c.Assert(err, jc.ErrorIsNil)
Expand All @@ -2337,7 +2337,7 @@ func (s *serviceSuite) TestClientSetServiceConstraints(c *gc.C) {
func (s *serviceSuite) setupSetServiceConstraints(c *gc.C) (*state.Application, constraints.Value) {
application := s.AddTestingService(c, "dummy", s.AddTestingCharm(c, "dummy"))
// Update constraints for the application.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
return application, cons
}
Expand Down Expand Up @@ -2378,7 +2378,7 @@ func (s *serviceSuite) TestClientGetServiceConstraints(c *gc.C) {
application := s.AddTestingService(c, "dummy", s.AddTestingCharm(c, "dummy"))

// Set constraints for the application.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = application.SetConstraints(cons)
c.Assert(err, jc.ErrorIsNil)
Expand Down
8 changes: 4 additions & 4 deletions apiserver/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (s *clientSuite) TestClientWatchAll(c *gc.C) {

func (s *clientSuite) TestClientSetModelConstraints(c *gc.C) {
// Set constraints for the model.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = s.APIState.Client().SetModelConstraints(cons)
c.Assert(err, jc.ErrorIsNil)
Expand All @@ -630,7 +630,7 @@ func (s *clientSuite) TestClientSetModelConstraints(c *gc.C) {

func (s *clientSuite) assertSetModelConstraints(c *gc.C) {
// Set constraints for the model.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = s.APIState.Client().SetModelConstraints(cons)
c.Assert(err, jc.ErrorIsNil)
Expand All @@ -642,7 +642,7 @@ func (s *clientSuite) assertSetModelConstraints(c *gc.C) {

func (s *clientSuite) assertSetModelConstraintsBlocked(c *gc.C, msg string) {
// Set constraints for the model.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = s.APIState.Client().SetModelConstraints(cons)
s.AssertBlocked(c, err, msg)
Expand All @@ -665,7 +665,7 @@ func (s *clientSuite) TestBlockChangesClientSetModelConstraints(c *gc.C) {

func (s *clientSuite) TestClientGetModelConstraints(c *gc.C) {
// Set constraints for the model.
cons, err := constraints.Parse("mem=4096", "cpu-cores=2")
cons, err := constraints.Parse("mem=4096", "cores=2")
c.Assert(err, jc.ErrorIsNil)
err = s.State.SetModelConstraints(cons)
c.Assert(err, jc.ErrorIsNil)
Expand Down
2 changes: 1 addition & 1 deletion apiserver/highavailability/highavailability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func assertKill(c *gc.C, killer KillerForTesting) {

var (
emptyCons = constraints.Value{}
controllerCons = constraints.MustParse("mem=16G cpu-cores=16")
controllerCons = constraints.MustParse("mem=16G cores=16")
defaultSeries = ""
)

Expand Down
2 changes: 1 addition & 1 deletion apiserver/provisioner/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ func (s *withoutControllerSuite) TestDistributionGroupMachineAgentAuth(c *gc.C)

func (s *withoutControllerSuite) TestConstraints(c *gc.C) {
// Add a machine with some constraints.
cons := constraints.MustParse("cpu-cores=123", "mem=8G")
cons := constraints.MustParse("cores=123", "mem=8G")
template := state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Expand Down
8 changes: 4 additions & 4 deletions apiserver/provisioner/provisioninginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *withoutControllerSuite) TestProvisioningInfoWithStorage(c *gc.C) {
_, err := pm.Create("static-pool", "static", map[string]interface{}{"foo": "bar"})
c.Assert(err, jc.ErrorIsNil)

cons := constraints.MustParse("cpu-cores=123 mem=8G")
cons := constraints.MustParse("cores=123 mem=8G")
template := state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *withoutControllerSuite) TestProvisioningInfoWithStorage(c *gc.C) {
func (s *withoutControllerSuite) TestProvisioningInfoWithSingleNegativeAndPositiveSpaceInConstraints(c *gc.C) {
s.addSpacesAndSubnets(c)

cons := constraints.MustParse("cpu-cores=123 mem=8G spaces=^space1,space2")
cons := constraints.MustParse("cores=123 mem=8G spaces=^space1,space2")
template := state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Expand Down Expand Up @@ -234,8 +234,8 @@ func (s *withoutControllerSuite) TestProvisioningInfoWithUnsuitableSpacesConstra
_, err := s.State.AddSpace("empty", "", nil, true)
c.Assert(err, jc.ErrorIsNil)

consEmptySpace := constraints.MustParse("cpu-cores=123 mem=8G spaces=empty")
consMissingSpace := constraints.MustParse("cpu-cores=123 mem=8G spaces=missing")
consEmptySpace := constraints.MustParse("cores=123 mem=8G spaces=empty")
consMissingSpace := constraints.MustParse("cores=123 mem=8G spaces=missing")
templates := []state.MachineTemplate{{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Expand Down
12 changes: 6 additions & 6 deletions cmd/juju/application/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleApplicationConstrants(c *g
applications:
wordpress:
charm: wordpress
constraints: mem=4G cpu-cores=2
constraints: mem=4G cores=2
customized:
charm: precise/dummy-0
num_units: 1
Expand All @@ -561,7 +561,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleApplicationConstrants(c *g
},
"wordpress": {
charm: "cs:xenial/wordpress-42",
constraints: constraints.MustParse("mem=4G cpu-cores=2"),
constraints: constraints.MustParse("mem=4G cores=2"),
},
})
s.assertUnitsCreated(c, map[string]string{
Expand Down Expand Up @@ -598,7 +598,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleApplicationUpgrade(c *gc.C
num_units: 1
options:
blog-title: new title
constraints: spaces=new cpu-cores=8
constraints: spaces=new cores=8
up:
charm: vivid/upgrade-2
num_units: 1
Expand All @@ -610,7 +610,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleApplicationUpgrade(c *gc.C
"wordpress": {
charm: "cs:xenial/wordpress-42",
config: charm.Settings{"blog-title": "new title"},
constraints: constraints.MustParse("spaces=new cpu-cores=8"),
constraints: constraints.MustParse("spaces=new cores=8"),
},
})
s.assertUnitsCreated(c, map[string]string{
Expand Down Expand Up @@ -897,7 +897,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleMachineAttributes(c *gc.C)
machines:
1:
series: xenial
constraints: "cpu-cores=4 mem=4G"
constraints: "cores=4 mem=4G"
annotations:
foo: bar
`)
Expand All @@ -915,7 +915,7 @@ func (s *BundleDeployCharmStoreSuite) TestDeployBundleMachineAttributes(c *gc.C)
c.Assert(m.Series(), gc.Equals, "xenial")
cons, err := m.Constraints()
c.Assert(err, jc.ErrorIsNil)
expectedCons, err := constraints.Parse("cpu-cores=4 mem=4G")
expectedCons, err := constraints.Parse("cores=4 mem=4G")
c.Assert(err, jc.ErrorIsNil)
c.Assert(cons, jc.DeepEquals, expectedCons)
ann, err := s.State.Annotations(m)
Expand Down
2 changes: 1 addition & 1 deletion cmd/juju/application/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ the first unit set them at the model level or pass them as an argument
when deploying.
Examples:
juju set-constraints mysql mem=8G cpu-cores=4
juju set-constraints mysql mem=8G cores=4
juju set-constraints -m mymodel apache2 mem=8G arch=amd64
See also:
Expand Down
13 changes: 10 additions & 3 deletions cmd/juju/application/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
apiparams "github.com/juju/juju/apiserver/params"
"github.com/juju/juju/charmstore"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/config"
Expand Down Expand Up @@ -260,6 +261,7 @@ type DeployCommand struct {

ApplicationName string
Config cmd.FileVar
ConstraintsStr string
Constraints constraints.Value
BindToSpaces string

Expand Down Expand Up @@ -436,7 +438,7 @@ func (c *DeployCommand) SetFlags(f *gnuflag.FlagSet) {
f.IntVar(&c.NumUnits, "n", 1, "Number of application units to deploy for principal charms")
f.StringVar((*string)(&c.Channel), "channel", "", "Channel to use when getting the charm or bundle from the charm store")
f.Var(&c.Config, "config", "Path to yaml-formatted application config")
f.Var(constraints.ConstraintsValue{Target: &c.Constraints}, "constraints", "Set application constraints")
f.StringVar(&c.ConstraintsStr, "constraints", "", "Set application constraints")
f.StringVar(&c.Series, "series", "", "The series on which to deploy")
f.BoolVar(&c.Force, "force", false, "Allow a charm to be deployed to a machine running an unsupported series")
f.Var(storageFlag{&c.Storage, &c.BundleStorage}, "storage", "Charm storage constraints")
Expand Down Expand Up @@ -467,8 +469,8 @@ func (c *DeployCommand) Init(args []string) error {
default:
return cmd.CheckEmpty(args[2:])
}
err := c.parseBind()
if err != nil {

if err := c.parseBind(); err != nil {
return err
}
return c.UnitCommandBase.Init(args)
Expand Down Expand Up @@ -670,6 +672,11 @@ func (c *DeployCommand) parseBind() error {
}

func (c *DeployCommand) Run(ctx *cmd.Context) error {
var err error
c.Constraints, err = common.ParseConstraints(ctx, c.ConstraintsStr)
if err != nil {
return err
}
apiRoot, err := c.NewAPIRoot()
if err != nil {
return errors.Trace(err)
Expand Down
7 changes: 2 additions & 5 deletions cmd/juju/application/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ var initErrorTests = []struct {
}, {
args: []string{"craziness", "burble1", "--to", "#:foo"},
err: `invalid --to parameter "#:foo"`,
}, {
args: []string{"craziness", "burble1", "--constraints", "gibber=plop"},
err: `invalid value "gibber=plop" for flag --constraints: unknown constraint "gibber"`,
}, {
args: []string{"charm", "application", "--force"},
err: `--force is only used with --series`,
Expand Down Expand Up @@ -279,13 +276,13 @@ func (s *DeploySuite) TestConfigError(c *gc.C) {

func (s *DeploySuite) TestConstraints(c *gc.C) {
ch := testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
err := runDeploy(c, ch, "--constraints", "mem=2G cpu-cores=2", "--series", "trusty")
err := runDeploy(c, ch, "--constraints", "mem=2G cores=2", "--series", "trusty")
c.Assert(err, jc.ErrorIsNil)
curl := charm.MustParseURL("local:trusty/dummy-1")
application, _ := s.AssertService(c, "dummy", curl, 1, 0)
cons, err := application.Constraints()
c.Assert(err, jc.ErrorIsNil)
c.Assert(cons, jc.DeepEquals, constraints.MustParse("mem=2G cpu-cores=2"))
c.Assert(cons, jc.DeepEquals, constraints.MustParse("mem=2G cores=2"))
}

func (s *DeploySuite) TestResources(c *gc.C) {
Expand Down
21 changes: 13 additions & 8 deletions cmd/juju/backups/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ func NewRestoreCommand() cmd.Command {
// it is invoked with "juju restore-backup".
type restoreCommand struct {
CommandBase
constraints constraints.Value
filename string
backupId string
bootstrap bool
buildAgent bool
constraints constraints.Value
constraintsStr string
filename string
backupId string
bootstrap bool
buildAgent bool

newAPIClientFunc func() (RestoreAPI, error)
newEnvironFunc func(environs.OpenParams) (environs.Environ, error)
Expand Down Expand Up @@ -105,9 +106,7 @@ func (c *restoreCommand) Info() *cmd.Info {
// SetFlags handles known option flags.
func (c *restoreCommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
f.Var(constraints.ConstraintsValue{Target: &c.constraints},
"constraints", "set model constraints")

f.StringVar(&c.constraintsStr, "constraints", "", "set model constraints")
f.BoolVar(&c.bootstrap, "b", false, "Bootstrap a new state machine")
f.StringVar(&c.filename, "file", "", "Provide a file to be used as the backup.")
f.StringVar(&c.backupId, "id", "", "Provide the name of the backup to be restored")
Expand Down Expand Up @@ -367,6 +366,12 @@ func (c *restoreCommand) newClient() (*backups.Client, error) {

// Run is the entry point for this command.
func (c *restoreCommand) Run(ctx *cmd.Context) error {
var err error
c.constraints, err = common.ParseConstraints(ctx, c.constraintsStr)
if err != nil {
return err
}

if c.Log != nil {
if err := c.Log.Start(ctx); err != nil {
return err
Expand Down
Loading

0 comments on commit 587433a

Please sign in to comment.