Skip to content

Commit

Permalink
Various changes to get new charms to work with k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
hpidcock committed Sep 24, 2020
1 parent 8e7bc7d commit c3f71c6
Show file tree
Hide file tree
Showing 35 changed files with 2,821 additions and 349 deletions.
36 changes: 36 additions & 0 deletions api/caasapplicationprovisioner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package caasapplicationprovisioner
import (
"github.com/juju/charm/v8"
"github.com/juju/errors"
"github.com/juju/juju/core/resources"
"github.com/juju/names/v4"
"github.com/juju/version"

Expand Down Expand Up @@ -113,6 +114,8 @@ type ProvisioningInfo struct {
Constraints constraints.Value
Filesystems []storage.KubernetesFilesystemParams
Devices []devices.KubernetesDeviceParams
Series string
ImageRepo string
}

// ProvisioningInfo returns the info needed to provision an operator for an application.
Expand All @@ -139,6 +142,8 @@ func (c *Client) ProvisioningInfo(applicationName string) (ProvisioningInfo, err
CACert: r.CACert,
Tags: r.Tags,
Constraints: r.Constraints,
Series: r.Series,
ImageRepo: r.ImageRepo,
}

for _, fs := range r.Filesystems {
Expand Down Expand Up @@ -279,3 +284,34 @@ func (c *Client) GarbageCollect(
}
return result.OneError()
}

// ApplicationOCIResources returns all the OCI image resources for an application.
func (c *Client) ApplicationOCIResources(appName string) (map[string]resources.DockerImageDetails, error) {
args := params.Entities{Entities: []params.Entity{{
Tag: names.NewApplicationTag(appName).String(),
}}}
var result params.CAASApplicationOCIResourceResults
if err := c.facade.FacadeCall("ApplicationOCIResources", args, &result); err != nil {
return nil, errors.Trace(err)
}
if len(result.Results) != 1 {
return nil, errors.Errorf("expected 1 result, got %d",
len(result.Results))
}
res := result.Results[0]
if res.Error != nil {
return nil, errors.Annotatef(res.Error, "unable to fetch OCI image resources for %s", appName)
}
if res.Result == nil {
return nil, errors.Errorf("missing result")
}
images := make(map[string]resources.DockerImageDetails)
for k, v := range res.Result.Images {
images[k] = resources.DockerImageDetails{
RegistryPath: v.RegistryPath,
Username: v.Username,
Password: v.Password,
}
}
return images, nil
}
57 changes: 57 additions & 0 deletions api/common/charms/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func convertCharmMeta(meta *params.CharmMeta) (*charm.Meta, error) {
Resources: resources,
Terms: meta.Terms,
MinJujuVersion: minVersion,
Systems: convertCharmSystems(meta.Systems),
Platforms: convertCharmPlatforms(meta.Platforms),
Architectures: convertCharmArchitectures(meta.Architectures),
Containers: convertCharmContainers(meta.Containers),
}
return result, nil
}
Expand Down Expand Up @@ -344,3 +348,56 @@ func (c *charmImpl) Actions() *charm.Actions {
func (c *charmImpl) Revision() int {
return c.info.Revision
}

func convertCharmSystems(input []params.CharmSystem) []charm.System {
systems := []charm.System(nil)
for _, v := range input {
systems = append(systems, charm.System{
OS: v.OS,
Version: v.Version,
Resource: v.Resource,
})
}
return systems
}

func convertCharmPlatforms(input []string) []charm.Platform {
platforms := []charm.Platform(nil)
for _, v := range input {
platforms = append(platforms, charm.Platform(v))
}
return platforms
}

func convertCharmArchitectures(input []string) []charm.Architecture {
architectures := []charm.Architecture(nil)
for _, v := range input {
architectures = append(architectures, charm.Architecture(v))
}
return architectures
}

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

func convertCharmMounts(input []params.CharmMount) []charm.Mount {
mounts := []charm.Mount(nil)
for _, v := range input {
mounts = append(mounts, charm.Mount{
Storage: v.Storage,
Location: v.Location,
})
}
return mounts
}
1 change: 1 addition & 0 deletions api/facadeversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var facadeVersions = map[string]int{
"CAASAgent": 1,
"CAASAdmission": 1,
"CAASApplication": 1,
"CAASApplicationProvisioner": 1,
"CAASFirewaller": 1,
"CAASFirewallerEmbedded": 1,
"CAASModelOperator": 1,
Expand Down
2 changes: 2 additions & 0 deletions apiserver/allfacades.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import (
"github.com/juju/juju/apiserver/facades/controller/actionpruner"
"github.com/juju/juju/apiserver/facades/controller/agenttools"
"github.com/juju/juju/apiserver/facades/controller/applicationscaler"
"github.com/juju/juju/apiserver/facades/controller/caasapplicationprovisioner"
"github.com/juju/juju/apiserver/facades/controller/caasfirewaller"
"github.com/juju/juju/apiserver/facades/controller/caasmodeloperator"
"github.com/juju/juju/apiserver/facades/controller/caasoperatorprovisioner"
Expand Down Expand Up @@ -197,6 +198,7 @@ func AllFacades() *facade.Registry {
reg("CAASOperatorUpgrader", 1, caasoperatorupgrader.NewStateCAASOperatorUpgraderAPI)
reg("CAASUnitProvisioner", 1, caasunitprovisioner.NewStateFacade)
reg("CAASApplication", 1, caasapplication.NewStateFacade)
reg("CAASApplicationProvisioner", 1, caasapplicationprovisioner.NewStateCAASApplicationProvisionerAPI)

reg("Controller", 3, controller.NewControllerAPIv3)
reg("Controller", 4, controller.NewControllerAPIv4)
Expand Down
11 changes: 3 additions & 8 deletions apiserver/common/applicationwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
corewatcher "github.com/juju/juju/core/watcher"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
)
Expand Down Expand Up @@ -140,17 +139,13 @@ func (w *applicationWatcher) handle(changes []string) ([]string, error) {
switch w.filter {
case ApplicationFilterCAASLegacy:
meta := ch.Meta()
if meta.Deployment != nil && meta.Deployment.DeploymentMode == charm.ModeEmbedded {
if meta.Format() >= charm.FormatV2 {
// Filter out embedded applications.
continue
}
case ApplicationFilterCAASEmbedded:
meta := ch.Meta()
if meta.Deployment == nil {
// Filter out application that defaulted to legacy deployment mode.
continue
}
if meta.Deployment.DeploymentMode != charm.ModeEmbedded {
if meta.Format() == charm.FormatV1 {
// Filter out non-embedded applications.
continue
}
Expand All @@ -163,7 +158,7 @@ func (w *applicationWatcher) handle(changes []string) ([]string, error) {
}

// Changes is part of corewatcher.StringsWatcher.
func (w *applicationWatcher) Changes() corewatcher.StringsChannel {
func (w *applicationWatcher) Changes() <-chan []string {
return w.out
}

Expand Down
57 changes: 57 additions & 0 deletions apiserver/common/charms/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func convertCharmMeta(meta *charm.Meta) *params.CharmMeta {
Resources: convertCharmResourceMetaMap(meta.Resources),
Terms: meta.Terms,
MinJujuVersion: meta.MinJujuVersion.String(),
Systems: convertCharmSystems(meta.Systems),
Platforms: convertCharmPlatforms(meta.Platforms),
Architectures: convertCharmArchitectures(meta.Architectures),
Containers: convertCharmContainers(meta.Containers),
}
}

Expand Down Expand Up @@ -347,3 +351,56 @@ func convertCharmDevices(devices map[string]charm.Device) map[string]params.Char
}
return results
}

func convertCharmSystems(input []charm.System) []params.CharmSystem {
systems := []params.CharmSystem(nil)
for _, v := range input {
systems = append(systems, params.CharmSystem{
OS: v.OS,
Version: v.Version,
Resource: v.Resource,
})
}
return systems
}

func convertCharmPlatforms(input []charm.Platform) []string {
platforms := []string(nil)
for _, v := range input {
platforms = append(platforms, string(v))
}
return platforms
}

func convertCharmArchitectures(input []charm.Architecture) []string {
architectures := []string(nil)
for _, v := range input {
architectures = append(architectures, string(v))
}
return architectures
}

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

func convertCharmMounts(input []charm.Mount) []params.CharmMount {
mounts := []params.CharmMount(nil)
for _, v := range input {
mounts = append(mounts, params.CharmMount{
Storage: v.Storage,
Location: v.Location,
})
}
return mounts
}
20 changes: 11 additions & 9 deletions apiserver/facades/agent/caasapplication/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"strings"
"time"

"github.com/juju/charm/v8"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v4"
"github.com/juju/retry"
"github.com/juju/utils"
"github.com/juju/utils/v2"

"github.com/juju/juju/agent"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/paths"
"github.com/juju/juju/state"
Expand Down Expand Up @@ -95,23 +95,25 @@ func (f *Facade) UnitIntroduction(args params.CAASUnitIntroductionArgs) (params.
return errResp(errors.NotProvisionedf("application"))
}

ch, _, err := application.Charm()
if err != nil {
return errResp(err)
}
// TODO(new-charms): handle deployment other than statefulset
// ch, _, err := application.Charm()
// if err != nil {
// return errResp(err)
// }
deploymentType := caas.DeploymentStateful

containerID := args.PodName
var unitName *string
switch ch.Meta().Deployment.DeploymentType {
case charm.DeploymentStateful:
switch deploymentType {
case caas.DeploymentStateful:
splitPodName := strings.Split(args.PodName, "-")
ord, err := strconv.Atoi(splitPodName[len(splitPodName)-1])
if err != nil {
return errResp(err)
}
n := fmt.Sprintf("%s/%d", application.Name(), ord)
unitName = &n
case charm.DeploymentStateless, charm.DeploymentDaemon:
case caas.DeploymentStateless, caas.DeploymentDaemon:
// Both handled the same way.
default:
return errResp(errors.NotSupportedf("unknown deployment type"))
Expand Down
4 changes: 2 additions & 2 deletions apiserver/facades/client/charmhub/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func transformStoreURL(clientURL, name string) string {
// transformSeries returns a slice of supported series for that revision.
func transformSeries(channel transport.ChannelMap) []string {
if meta := unmarshalCharmMetadata(channel.Revision.MetadataYAML); meta != nil {
return meta.Series
return meta.ComputedSeries()
}
return nil
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func convertCharm(info transport.InfoResponse) (*params.CharmHubCharm, []string)
if meta := unmarshalCharmMetadata(info.DefaultRelease.Revision.MetadataYAML); meta != nil {
charmHubCharm.Subordinate = meta.Subordinate
charmHubCharm.Relations = transformRelations(meta.Requires, meta.Provides)
series = meta.Series
series = meta.ComputedSeries()
}
if cfg := unmarshalCharmConfig(info.DefaultRelease.Revision.ConfigYAML); cfg != nil {
charmHubCharm.Config = params.ToCharmOptionMap(cfg)
Expand Down
2 changes: 1 addition & 1 deletion apiserver/facades/client/charms/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (c *chRepo) resolveViaChannelMap(curl *charm.URL, origin params.CharmOrigin
if err != nil {
return nil, params.CharmOrigin{}, nil, errors.Annotatef(err, "cannot unmarshal charm metadata")
}
return curl, origin, meta.Series, nil
return curl, origin, meta.ComputedSeries(), nil
}

func unmarshalCharmMetadata(metadataYAML string) (*charm.Meta, error) {
Expand Down
Loading

0 comments on commit c3f71c6

Please sign in to comment.