Skip to content

Commit

Permalink
Move to new charm with channels.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpidcock committed Sep 25, 2020
1 parent e1948a7 commit 036df3a
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 84 deletions.
44 changes: 33 additions & 11 deletions api/common/charms/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/juju/charm/v8"
"github.com/juju/charm/v8/resource"
"github.com/juju/errors"
"github.com/juju/systems"
"github.com/juju/systems/channel"
"github.com/juju/version"

"github.com/juju/juju/api/base"
Expand Down Expand Up @@ -74,6 +76,14 @@ func convertCharmMeta(meta *params.CharmMeta) (*charm.Meta, error) {
if err != nil {
return nil, errors.Trace(err)
}
systems, err := convertCharmSystems(meta.Systems)
if err != nil {
return nil, errors.Trace(err)
}
containers, err := convertCharmContainers(meta.Containers)
if err != nil {
return nil, errors.Trace(err)
}
result := &charm.Meta{
Name: meta.Name,
Summary: meta.Summary,
Expand All @@ -93,10 +103,10 @@ func convertCharmMeta(meta *params.CharmMeta) (*charm.Meta, error) {
Resources: resources,
Terms: meta.Terms,
MinJujuVersion: minVersion,
Systems: convertCharmSystems(meta.Systems),
Systems: systems,
Platforms: convertCharmPlatforms(meta.Platforms),
Architectures: convertCharmArchitectures(meta.Architectures),
Containers: convertCharmContainers(meta.Containers),
Containers: containers,
}
return result, nil
}
Expand Down Expand Up @@ -349,16 +359,24 @@ func (c *charmImpl) Revision() int {
return c.info.Revision
}

func convertCharmSystems(input []params.CharmSystem) []charm.System {
systems := []charm.System(nil)
func convertCharmSystems(input []params.CharmSystem) ([]systems.System, error) {
var err error
res := []systems.System(nil)
for _, v := range input {
systems = append(systems, charm.System{
ch := channel.Channel{}
if v.Channel != "" {
ch, err = channel.Parse(v.Channel)
if err != nil {
return nil, errors.Trace(err)
}
}
res = append(res, systems.System{
OS: v.OS,
Version: v.Version,
Channel: ch,
Resource: v.Resource,
})
}
return systems
return res, nil
}

func convertCharmPlatforms(input []string) []charm.Platform {
Expand All @@ -377,18 +395,22 @@ func convertCharmArchitectures(input []string) []charm.Architecture {
return architectures
}

func convertCharmContainers(input map[string]params.CharmContainer) map[string]charm.Container {
func convertCharmContainers(input map[string]params.CharmContainer) (map[string]charm.Container, error) {
containers := map[string]charm.Container{}
for k, v := range input {
systems, err := convertCharmSystems(v.Systems)
if err != nil {
return nil, errors.Trace(err)
}
containers[k] = charm.Container{
Systems: convertCharmSystems(v.Systems),
Systems: systems,
Mounts: convertCharmMounts(v.Mounts),
}
}
if len(containers) == 0 {
return nil
return nil, nil
}
return containers
return containers, nil
}

func convertCharmMounts(input []params.CharmMount) []charm.Mount {
Expand Down
5 changes: 3 additions & 2 deletions apiserver/common/charms/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/juju/charm/v8/resource"
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/systems"

apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
Expand Down Expand Up @@ -352,12 +353,12 @@ func convertCharmDevices(devices map[string]charm.Device) map[string]params.Char
return results
}

func convertCharmSystems(input []charm.System) []params.CharmSystem {
func convertCharmSystems(input []systems.System) []params.CharmSystem {
systems := []params.CharmSystem(nil)
for _, v := range input {
systems = append(systems, params.CharmSystem{
OS: v.OS,
Version: v.Version,
Channel: v.Channel.String(),
Resource: v.Resource,
})
}
Expand Down
18 changes: 9 additions & 9 deletions apiserver/facades/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7384,13 +7384,13 @@
"CharmSystem": {
"type": "object",
"properties": {
"os": {
"channel": {
"type": "string"
},
"resource": {
"os": {
"type": "string"
},
"version": {
"resource": {
"type": "string"
}
},
Expand Down Expand Up @@ -8865,13 +8865,13 @@
"CharmSystem": {
"type": "object",
"properties": {
"os": {
"channel": {
"type": "string"
},
"resource": {
"os": {
"type": "string"
},
"version": {
"resource": {
"type": "string"
}
},
Expand Down Expand Up @@ -13062,13 +13062,13 @@
"CharmSystem": {
"type": "object",
"properties": {
"os": {
"channel": {
"type": "string"
},
"resource": {
"os": {
"type": "string"
},
"version": {
"resource": {
"type": "string"
}
},
Expand Down
2 changes: 1 addition & 1 deletion apiserver/params/charms.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type CharmDeployment struct {
// CharmSystem mirrors charm.System
type CharmSystem struct {
OS string `json:"os,omitempty"`
Version string `json:"version,omitempty"`
Channel string `json:"channel,omitempty"`
Resource string `json:"resource,omitempty"`
}

Expand Down
18 changes: 7 additions & 11 deletions cloudconfig/podcfg/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"fmt"
"strings"

"github.com/juju/os"

"github.com/juju/errors"
"github.com/juju/os/series"
"github.com/juju/systems"
"github.com/juju/version"

"github.com/juju/juju/controller"
Expand Down Expand Up @@ -102,22 +100,20 @@ func imageRepoToPath(imageRepo string, ver version.Number) string {

// ImageForSystem returns the OCI image path for a generic system.
// NOTE: resource referenced systems are not resolved via ImageForSystem.
func ImageForSystem(imageRepo string, system series.System) (string, error) {
func ImageForSystem(imageRepo string, system systems.System) (string, error) {
if system.Resource != "" {
return "", errors.NotValidf("system can't reference a resource")
}
if system.OS == os.Unknown {
if system.OS == "" {
return "", errors.NotValidf("system must specify os")
}
if system.OS == os.Kubernetes {
return "", errors.NotValidf("kubernetes is not a valid system os")
}
if imageRepo == "" {
imageRepo = JujudOCINamespace
}
image := fmt.Sprintf("%s/%s", imageRepo, system.OS.SystemString())
if system.Version != "" {
image = fmt.Sprintf("%s:%s", image, system.Version)
tag := system.Channel.Track
if system.Channel.Risk != "stable" {
tag = fmt.Sprintf("%s-%s", tag, system.Channel.Risk)
}
image := fmt.Sprintf("%s/%s:%s", imageRepo, system.OS, tag)
return image, nil
}
28 changes: 15 additions & 13 deletions core/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/juju/errors"
"github.com/juju/os"
"github.com/juju/os/series"
"github.com/juju/systems"
)

// ModelType indicates a model type.
Expand Down Expand Up @@ -42,20 +43,21 @@ var caasOS = set.NewStrings(os.Kubernetes.String())

// ValidateSeries ensures the charm series is valid for the model type.
func ValidateSeries(modelType ModelType, charmSeries string, charmV2 bool) error {
system, err := series.ParseSystemFromSeries(charmSeries)
if err != nil {
return errors.Trace(err)
}
if system.Resource != "" {
switch modelType {
case CAAS:
// CAAS models support using a resource as the system.
return nil
case IAAS:
return errors.NotValidf("IAAS models don't support systems referencing a resource")
if charmV2 {
system, err := systems.ParseSystemFromSeries(charmSeries)
if err != nil {
return errors.Trace(err)
}
}
if !charmV2 {
if system.Resource != "" {
switch modelType {
case CAAS:
// CAAS models support using a resource as the system.
return nil
case IAAS:
return errors.NotValidf("IAAS models don't support systems referencing a resource")
}
}
} else {
os, err := series.GetOSFromSeries(charmSeries)
if err != nil {
return errors.Trace(err)
Expand Down
22 changes: 21 additions & 1 deletion core/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,27 @@ func (*ModelSuite) TestValidateSeries(c *gc.C) {
{model.CAAS, "bionic", false},
{model.CAAS, "kubernetes", true},
} {
err := model.ValidateSeries(t.modelType, t.series)
err := model.ValidateSeries(t.modelType, t.series, false)
if t.valid {
c.Check(err, jc.ErrorIsNil)
} else {
c.Check(err, jc.Satisfies, errors.IsNotValid)
}
}
}

func (*ModelSuite) TestValidateSeriesNewCharm(c *gc.C) {
for _, t := range []struct {
modelType model.ModelType
series string
valid bool
}{
{model.IAAS, "bionic", true},
{model.IAAS, "bionic", true},
{model.CAAS, "bionic", true},
{model.CAAS, "bionic", true},
} {
err := model.ValidateSeries(t.modelType, t.series, true)
if t.valid {
c.Check(err, jc.ErrorIsNil)
} else {
Expand Down
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/joyent/gosign v0.0.0-20140524000734-0da0d5f13420
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a
github.com/juju/bundlechanges/v3 v3.0.0-20200909112830-2eb08291b192
github.com/juju/charm/v8 v8.0.0-20200923053153-205fa6e80ebf
github.com/juju/charm/v8 v8.0.0-20200925052646-bb021a575610
github.com/juju/charmrepo/v6 v6.0.0-20200817155725-120bd7a8b1ed
github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c
github.com/juju/cmd v0.0.0-20200108104440-8e43f3faa5c9
Expand All @@ -64,7 +64,7 @@ require (
github.com/juju/mutex v0.0.0-20180619145857-d21b13acf4bf
github.com/juju/names/v4 v4.0.0-20200923012352-008effd8611b
github.com/juju/naturalsort v0.0.0-20180423034842-5b81707e882b
github.com/juju/os v0.0.0-20200923052219-e4c9b9a6564c
github.com/juju/os v0.0.0-20200701063157-8e6dd7a2b438
github.com/juju/packaging v0.0.0-20200421095529-970596d2622a
github.com/juju/persistent-cookiejar v0.0.0-20170428161559-d67418f14c93
github.com/juju/proxy v0.0.0-20180523025733-5f8741c297b4
Expand All @@ -76,6 +76,7 @@ require (
github.com/juju/romulus v0.0.0-20191205211046-fd7cab26ac5f
github.com/juju/rpcreflect v0.0.0-20200416001309-bb46e9ba1476
github.com/juju/schema v1.0.1-0.20190814234152-1f8aaeef0989
github.com/juju/systems v0.0.0-20200925032749-8c613192c759
github.com/juju/terms-client v1.0.2-0.20200331164339-fab45ea044ae
github.com/juju/testing v0.0.0-20200923013621-75df6121fbb0
github.com/juju/txn v0.0.0-20190416045819-5f348e78887d
Expand Down Expand Up @@ -107,9 +108,9 @@ require (
github.com/vmware/govmomi v0.21.1-0.20191008161538-40aebf13ba45
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/net v0.0.0-20200904194848-62affa334b73
golang.org/x/net v0.0.0-20200923182212-328152dc79b1
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200817085935-3ff754bf58a9
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d
golang.org/x/text v0.3.3 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
golang.org/x/tools v0.0.0-20200725200936-102e7d357031
Expand Down Expand Up @@ -185,7 +186,3 @@ replace (
k8s.io/metrics v0.0.0 => k8s.io/metrics v0.18.6
k8s.io/sample-apiserver v0.0.0 => k8s.io/sample-apiserver v0.18.6
)

replace github.com/juju/charm/v8 => github.com/hpidcock-juju/charm/v8 v8.0.0-20200923053153-205fa6e80ebf

replace github.com/juju/os => github.com/hpidcock/os v0.0.0-20200923052219-e4c9b9a6564c
Loading

0 comments on commit 036df3a

Please sign in to comment.