-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request juju#18022 from barrettj12/assumes-msg
juju#18022 Error messages for supported features were intended to be user-friendly, but they are still too abstract to easily understand. They refer to the concept of "features", which is an internal Juju construct and as such, should not be mentioned to users. This PR attempts to improve the error messaging to make it more straightforward. ### Before ```console $ juju deploy nvidia-gpu-operator --channel 1.29/stable ERROR Charm feature requirements cannot be met: - charm requires feature "k8s-api" but model does not support it Feature descriptions: - "k8s-api": the Kubernetes API lets charms query and manipulate the state of API objects in a Kubernetes cluster For additional information please see: https://juju.is/docs/olm/supported-features ERROR failed to deploy charm "nvidia-gpu-operator" ``` ### After ```console $ juju deploy assumes-juju ERROR Charm cannot be deployed because: - charm requires Juju version >= 4.0.0, model has version 3.5.4 ``` ```console $ juju deploy nvidia-gpu-operator --channel 1.29/stable ERROR Charm cannot be deployed because: - charm must be deployed on a Kubernetes cloud ``` ```console $ juju deploy assumes-k8s-new ERROR Charm cannot be deployed because: - charm requires Kubernetes version >= 1.35.0, model is running on version 1.30.0 ``` ## Checklist <!-- If an item is not applicable, use `~strikethrough~`. --> - [x] Code style: imports ordered, good names, simple structure, etc - [x] Comments saying why design decisions were made - [x] Go unit tests, with comments saying what you're testing - ~[ ] [Integration tests](https://github.com/juju/juju/tree/main/tests), with comments saying what you're testing~ - ~[ ] [doc.go](https://discourse.charmhub.io/t/readme-in-packages/451) added or updated in changed packages~ ## QA steps Create a new charm: ``` mkdir assumes-juju cd assumes-juju charmcraft init ``` Remove the `containers` and `resources` sections. At the bottom of `charmcraft.yaml`, add the lines: ```yaml assumes: - juju >= 4.0 ``` Build the charm: ``` charmcraft pack ``` Bootstrap Juju (any cloud) and try to deploy the charm. Check the error message. Also test the following scenarios: - deploy a charm that assumes `k8s-api` on a machine cloud (you can use any k8s charm e.g. `nvidia-gpu-operator` for this, no need to build your own) - deploy a charm that assumes `k8s-api >= 1.35` on a k8s cloud ## Links <!-- Link to all relevant specification, documentation, bug, issue or JIRA card. --> **Launchpad bug:** https://bugs.launchpad.net/juju/+bug/2069986 **Jira card:** [JUJU-6584](https://warthogs.atlassian.net/browse/JUJU-6584) [JUJU-6584]: https://warthogs.atlassian.net/browse/JUJU-6584?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
Showing
10 changed files
with
156 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package assumes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/version/v2" | ||
gc "gopkg.in/check.v1" | ||
) | ||
|
||
type errorSuite struct{} | ||
|
||
var _ = gc.Suite(&errorSuite{}) | ||
|
||
var errorTests = []struct { | ||
description string | ||
featureSet FeatureSet | ||
assumes string | ||
expectedErr string | ||
}{{ | ||
description: "Unsupported Juju version", | ||
featureSet: FeatureSet{features: map[string]Feature{ | ||
"juju": JujuFeature(version.MustParse("2.9.42")), | ||
}}, | ||
assumes: "assumes:\n- juju >= 3.1", | ||
expectedErr: "(?s).*charm requires Juju version >= 3.1.0.*", | ||
}, { | ||
description: "Deploying k8s charm on machine cloud", | ||
featureSet: FeatureSet{}, | ||
assumes: "assumes:\n- k8s-api", | ||
expectedErr: "(?s).*charm must be deployed on a Kubernetes cloud.*", | ||
}, { | ||
description: "k8s version too low", | ||
featureSet: FeatureSet{features: map[string]Feature{ | ||
"k8s-api": K8sAPIFeature(version.MustParse("1.25.0")), | ||
}}, | ||
assumes: "assumes:\n- k8s-api >= 1.30", | ||
expectedErr: "(?s).*charm requires Kubernetes version >= 1.30.*", | ||
}} | ||
|
||
func (*errorSuite) TestErrorMessages(c *gc.C) { | ||
for _, test := range errorTests { | ||
fmt.Println(test.description) | ||
assumesTree := mustParseAssumesExpr(c, test.assumes) | ||
err := test.featureSet.Satisfies(assumesTree) | ||
c.Check(err, gc.ErrorMatches, test.expectedErr) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package assumes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/version/v2" | ||
) | ||
|
||
var ( | ||
// A set of user-friendly descriptions for potentially supported | ||
// features that are known to the controller. This allows us to | ||
// generate better error messages when an "assumes" expression requests a | ||
// feature that is not included in the feature set supported by the | ||
// current model. | ||
UserFriendlyFeatureDescriptions = map[string]string{ | ||
"juju": "the version of Juju used by the model", | ||
"k8s-api": "the Kubernetes API lets charms query and manipulate the state of API objects in a Kubernetes cluster", | ||
} | ||
) | ||
|
||
// featureMissingErrs is a list of user-friendly error messages to return when | ||
// a given feature is expected by a charm, but not present in the model. | ||
var featureMissingErrs = map[string]string{ | ||
"juju": "charm requires Juju", // this should never happen | ||
"k8s-api": "charm must be deployed on a Kubernetes cloud", | ||
} | ||
|
||
// featureMissingErr returns a user-friendly error message to return when a | ||
// given feature is expected by a charm, but not present in the model. | ||
func featureMissingErr(featureName string) string { | ||
if errStr, ok := featureMissingErrs[featureName]; ok { | ||
return errStr | ||
} | ||
// We don't have a specific error message defined, so return a default. | ||
return fmt.Sprintf("charm requires feature %q but model does not support it", featureName) | ||
} | ||
|
||
// featureVersionMismatchErrs is a list of functions which generate | ||
// user-friendly error messages when a given feature is present in the model, | ||
// but the version is lower than required by the charm. | ||
var featureVersionMismatchErrs = map[string]func(constraint, requiredVersion, actualVersion string) string{ | ||
"juju": func(c, rv, av string) string { | ||
return fmt.Sprintf("charm requires Juju version %s %s, model has version %s", c, rv, av) | ||
}, | ||
"k8s-api": func(c, rv, av string) string { | ||
return fmt.Sprintf("charm requires Kubernetes version %s %s, model is running on version %s", c, rv, av) | ||
}, | ||
} | ||
|
||
// featureVersionMismatchErr returns a user-friendly error message to return when | ||
// a given feature is present in the model, but the version is lower than | ||
// required by the charm. | ||
func featureVersionMismatchErr(featureName, constraint, requiredVersion, actualVersion string) string { | ||
if f, ok := featureVersionMismatchErrs[featureName]; ok { | ||
return f(constraint, requiredVersion, actualVersion) | ||
} | ||
// We don't have a specific error message defined, so return a default. | ||
return fmt.Sprintf("charm requires feature %q (version %s %s) but model currently supports version %s", | ||
featureName, constraint, requiredVersion, actualVersion) | ||
} | ||
|
||
// JujuFeature returns a new Feature representing the Juju API for the given | ||
// version. | ||
func JujuFeature(ver version.Number) Feature { | ||
return Feature{ | ||
Name: "juju", | ||
Description: UserFriendlyFeatureDescriptions["juju"], | ||
Version: &ver, | ||
} | ||
} | ||
|
||
// K8sAPIFeature returns a new Feature representing the Kubernetes API for the | ||
// given version. | ||
func K8sAPIFeature(ver version.Number) Feature { | ||
return Feature{ | ||
Name: "k8s-api", | ||
Description: UserFriendlyFeatureDescriptions["k8s-api"], | ||
Version: &ver, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters