Skip to content

Commit

Permalink
revert: revert the changes which are done in deployFromRepository
Browse files Browse the repository at this point in the history
and legacyDeploy methods

feat: Use ResourceAPIClient.Upload to upload the custom resources
for the applications which are deployed with deployFromRepository method
tests: Use grafana-k8s charm to validate the custom resource usage
in the integration tests

Signed-off-by: gatici <[email protected]>
  • Loading branch information
gatici committed Aug 27, 2024
1 parent bdd6fef commit daedc8b
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 250 deletions.
135 changes: 68 additions & 67 deletions internal/juju/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"errors"
"fmt"
"math"
"os"
"reflect"
"sort"
"strconv"
Expand All @@ -36,7 +35,6 @@ import (
apicommoncharm "github.com/juju/juju/api/common/charm"
"github.com/juju/juju/cmd/juju/application/utils"
resourcecmd "github.com/juju/juju/cmd/juju/resource"
"github.com/juju/juju/cmd/modelcmd"
corebase "github.com/juju/juju/core/base"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
Expand Down Expand Up @@ -324,12 +322,6 @@ type DestroyApplicationInput struct {
ModelName string
}

type osFilesystem struct{}

func (osFilesystem) Open(name string) (modelcmd.ReadSeekCloser, error) {
return os.Open(name)
}

func resolveCharmURL(charmName string) (*charm.URL, error) {
path, err := charm.EnsureSchema(charmName, charm.CharmHub)
if err != nil {
Expand All @@ -356,24 +348,15 @@ func (c applicationsClient) CreateApplication(ctx context.Context, input *Create
}

applicationAPIClient := apiapplication.NewClient(conn)
resourceAPIClient, err := apiresources.NewClient(conn)
if err != nil {
return nil, err
}
if applicationAPIClient.BestAPIVersion() >= 19 {
resourceIDs, apiCharmID, err := c.deployFromRepository(applicationAPIClient, transformedInput, conn)
err := c.deployFromRepository(applicationAPIClient, resourceAPIClient, transformedInput)
if err != nil {
return nil, err
}
if len(resourceIDs) != 0 {
toReturn := apiapplication.SetCharmConfig{
ApplicationName: transformedInput.applicationName,
CharmID: apiCharmID,
ResourceIDs: resourceIDs,
}
setCharmConfig := &toReturn

err = applicationAPIClient.SetCharm(model.GenerationMaster, *setCharmConfig)
if err != nil {
return nil, err
}
}
} else {
err = c.legacyDeploy(ctx, conn, applicationAPIClient, transformedInput)
err = jujuerrors.Annotate(err, "legacy deploy method")
Expand All @@ -391,16 +374,14 @@ func (c applicationsClient) CreateApplication(ctx context.Context, input *Create
}, err
}

func (c applicationsClient) deployFromRepository(applicationAPIClient *apiapplication.Client, transformedInput transformedCreateApplicationInput, conn api.Connection) (map[string]string, apiapplication.CharmID, error) {
func (c applicationsClient) deployFromRepository(applicationAPIClient ApplicationAPIClient, resourceAPIClient ResourceAPIClient, transformedInput transformedCreateApplicationInput) error {
settingsForYaml := map[interface{}]interface{}{transformedInput.applicationName: transformedInput.config}
configYaml, err := goyaml.Marshal(settingsForYaml)
resourceIDs := map[string]string{}
apiCharmID := apiapplication.CharmID{}
if err != nil {
return resourceIDs, apiapplication.CharmID{}, jujuerrors.Trace(err)
return jujuerrors.Trace(err)
}
c.Tracef("Calling DeployFromRepository")
deployInfo, pendingResources, errs := applicationAPIClient.DeployFromRepository(apiapplication.DeployFromRepositoryArg{
deployInfo, localPendingResources, errs := applicationAPIClient.DeployFromRepository(apiapplication.DeployFromRepositoryArg{
CharmName: transformedInput.charmName,
ApplicationName: transformedInput.applicationName,
Base: &transformedInput.charmBase,
Expand All @@ -415,47 +396,20 @@ func (c applicationsClient) deployFromRepository(applicationAPIClient *apiapplic
Resources: transformedInput.resources,
Storage: transformedInput.storage,
})
if errs != nil {
return resourceIDs, apiCharmID, errors.Join(errs...)
}

charmsAPIClient := apicharms.NewClient(conn)
modelconfigAPIClient := apimodelconfig.NewClient(conn)
resolvedURL, resolvedOrigin, supportedBases, err := getCharmResolvedUrlAndOrigin(conn, transformedInput)
if err != nil {
return resourceIDs, apiCharmID, err
if len(errs) != 0 {
return errors.Join(errs...)
}
userSuppliedBase := transformedInput.charmBase
baseToUse, err := c.baseToUse(modelconfigAPIClient, userSuppliedBase, resolvedOrigin.Base, supportedBases)
if err != nil {
return resourceIDs, apiCharmID, err
}
if !userSuppliedBase.Empty() && !userSuppliedBase.IsCompatible(baseToUse) {
return resourceIDs, apiCharmID, err
}
resolvedOrigin.Base = baseToUse
series, err := corebase.GetSeriesFromBase(baseToUse)
if err != nil {
return resourceIDs, apiCharmID, err
}
resolvedURL = resolvedURL.WithSeries(series)

// Add charm expects base or series, one of them should exist.
resultOrigin, err := charmsAPIClient.AddCharm(resolvedURL, resolvedOrigin, false)
if err != nil {
return resourceIDs, apiCharmID, err
}
resourceIDs, err = c.getResourceIDs(transformedInput, conn, deployInfo, pendingResources)
if err != nil {
return resourceIDs, apiCharmID, err
}
fileSystem := osFilesystem{}
// Upload the provided local resources to Juju
uploadErr := uploadExistingPendingResources(deployInfo.Name, localPendingResources, fileSystem, resourceAPIClient)

apiCharmID = apiapplication.CharmID{
URL: resolvedURL.String(),
Origin: resultOrigin,
if uploadErr != nil {
return uploadErr
}
return nil

return resourceIDs, apiCharmID, nil
}

// TODO (hml) 23-Feb-2024
Expand All @@ -469,15 +423,62 @@ func (c applicationsClient) legacyDeploy(ctx context.Context, conn api.Connectio
charmsAPIClient := apicharms.NewClient(conn)
modelconfigAPIClient := apimodelconfig.NewClient(conn)

resolvedURL, resolvedOrigin, supportedBases, err := getCharmResolvedUrlAndOrigin(conn, transformedInput)
channel, err := charm.ParseChannel(transformedInput.charmChannel)
if err != nil {
return err
}

charmURL, err := resolveCharmURL(transformedInput.charmName)
if err != nil {
return err
}

if charmURL.Revision != UnspecifiedRevision {
return fmt.Errorf("cannot specify revision in a charm name")
}
if transformedInput.charmRevision != UnspecifiedRevision && channel.Empty() {
return fmt.Errorf("specifying a revision requires a channel for future upgrades")
}

userSuppliedBase := transformedInput.charmBase
platformCons, err := modelconfigAPIClient.GetModelConstraints()
if err != nil {
return err
}
platform := utils.MakePlatform(transformedInput.constraints, userSuppliedBase, platformCons)

urlForOrigin := charmURL
if transformedInput.charmRevision != UnspecifiedRevision {
urlForOrigin = urlForOrigin.WithRevision(transformedInput.charmRevision)
}

// Juju 2.9 cares that the series is in the origin. Juju 3.3 does not.
// We are supporting both now.
if !userSuppliedBase.Empty() {
userSuppliedSeries, err := corebase.GetSeriesFromBase(userSuppliedBase)
if err != nil {
return err
}
urlForOrigin = urlForOrigin.WithSeries(userSuppliedSeries)
}

origin, err := utils.MakeOrigin(charm.Schema(urlForOrigin.Schema), transformedInput.charmRevision, channel, platform)
if err != nil {
return err
}

// Charm or bundle has been supplied as a URL so we resolve and
// deploy using the store but pass in the origin command line
// argument so users can target a specific origin.
resolvedURL, resolvedOrigin, supportedBases, err := resolveCharm(charmsAPIClient, charmURL, origin)
if err != nil {
return err
}
if resolvedOrigin.Type == "bundle" {
return jujuerrors.NotSupportedf("deploying bundles")
}
c.Tracef("resolveCharm returned", map[string]interface{}{"resolvedURL": resolvedURL, "resolvedOrigin": resolvedOrigin, "supportedBases": supportedBases})
userSuppliedBase := transformedInput.charmBase

baseToUse, err := c.baseToUse(modelconfigAPIClient, userSuppliedBase, resolvedOrigin.Base, supportedBases)
if err != nil {
c.Warnf("failed to get a suggested operating system from resolved charm response", map[string]interface{}{"err": err})
Expand Down Expand Up @@ -1459,7 +1460,7 @@ func (c applicationsClient) updateResources(appName string, resources map[string
}

func addPendingResources(appName string, charmResourcesToAdd map[string]charmresources.Meta, resourcesToUse map[string]string,
charmID apiapplication.CharmID, resourcesAPIClient ResourceAPIClient) (map[string]string, error) {
charmID apiapplication.CharmID, resourceAPIClient ResourceAPIClient) (map[string]string, error) {
pendingResourcesforAdd := []charmresources.Resource{}
resourceIDs := map[string]string{}

Expand Down Expand Up @@ -1508,7 +1509,7 @@ func addPendingResources(appName string, charmResourcesToAdd map[string]charmres
if openResErr != nil {
return nil, typedError(openResErr)
}
toRequestUpload, err := resourcesAPIClient.UploadPendingResource(appName, localResource, deployValue, r)
toRequestUpload, err := resourceAPIClient.UploadPendingResource(appName, localResource, deployValue, r)
if err != nil {
return nil, typedError(err)
}
Expand All @@ -1528,7 +1529,7 @@ func addPendingResources(appName string, charmResourcesToAdd map[string]charmres
},
Resources: pendingResourcesforAdd,
}
toRequestAdd, err := resourcesAPIClient.AddPendingResources(resourcesReqforAdd)
toRequestAdd, err := resourceAPIClient.AddPendingResources(resourcesReqforAdd)
if err != nil {
return nil, typedError(err)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/juju/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ApplicationAPIClient interface {
ApplicationsInfo(applications []names.ApplicationTag) ([]params.ApplicationInfoResult, error)
Deploy(args apiapplication.DeployArgs) error
DestroyUnits(in apiapplication.DestroyUnitsParams) ([]params.DestroyUnitResult, error)
DeployFromRepository(arg apiapplication.DeployFromRepositoryArg) (apiapplication.DeployInfo, []apiapplication.PendingResourceUpload, []error)
DestroyApplications(in apiapplication.DestroyApplicationsParams) ([]params.DestroyApplicationResult, error)
Expose(application string, exposedEndpoints map[string]params.ExposedEndpoint) error
Get(branchName, application string) (*params.ApplicationGetResults, error)
Expand All @@ -66,6 +67,7 @@ type ModelConfigAPIClient interface {
type ResourceAPIClient interface {
AddPendingResources(args apiresources.AddPendingResourcesArgs) ([]string, error)
ListResources(applications []string) ([]resources.ApplicationResources, error)
Upload(application, name, filename, pendingID string, reader io.ReadSeeker) error
UploadPendingResource(applicationID string, resource charmresources.Resource, filename string, r io.ReadSeeker) (id string, err error)
}

Expand Down
30 changes: 30 additions & 0 deletions internal/juju/mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit daedc8b

Please sign in to comment.