Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #256

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -134,7 +135,7 @@ func init() {

func runApplyCmd(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("name and module are required")
return errors.New("name and module are required")
}

applyArgs.name = args[0]
Expand Down
15 changes: 11 additions & 4 deletions cmd/timoni/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -91,7 +92,7 @@ func init() {

func runBuildCmd(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("name and module are required")
return errors.New("name and module are required")
}

buildArgs.name = args[0]
Expand Down Expand Up @@ -190,6 +191,7 @@ func runBuildCmd(cmd *cobra.Command, args []string) error {
sb.WriteString("---\n")
}
_, err = cmd.OutOrStdout().Write([]byte(sb.String()))
return err
case "json":
list := struct {
ApiVersion string `json:"apiVersion,omitempty"`
Expand All @@ -206,15 +208,14 @@ func runBuildCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("converting objects failed: %w", err)
}
_, err = cmd.OutOrStdout().Write(b)
return err
default:
return fmt.Errorf("unknown --output=%s, can be yaml or json", buildArgs.output)
}

return err
}

func convertToCue(cmd *cobra.Command, paths []string) ([][]byte, error) {
valuesCue := make([][]byte, len(paths), len(paths))
valuesCue := make([][]byte, len(paths))
for i, path := range paths {
var (
bs []byte
Expand Down Expand Up @@ -245,8 +246,14 @@ func convertToCue(cmd *cobra.Command, paths []string) ([][]byte, error) {
continue
case ".json":
node, err = cuejson.Extract(path, bs)
if err != nil {
return nil, fmt.Errorf("could not extract JSON from %s: %w", path, err)
}
case ".yaml", ".yml":
node, err = cueyaml.Extract(path, bs)
if err != nil {
return nil, fmt.Errorf("could not extract YAML from %s: %w", path, err)
}
default:
return nil, fmt.Errorf("unknown values file format for %s", path)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"maps"
Expand Down Expand Up @@ -98,7 +99,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
start := time.Now()
files := bundleApplyArgs.files
if len(files) == 0 {
return fmt.Errorf("no bundle provided with -f")
return errors.New("no bundle provided with -f")
}
var stdinFile string
for i, file := range files {
Expand Down Expand Up @@ -140,7 +141,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {

clusters := rt.SelectClusters(bundleArgs.runtimeCluster, bundleArgs.runtimeClusterGroup)
if len(clusters) == 0 {
return fmt.Errorf("no cluster found")
return errors.New("no cluster found")
}

ctxPull, cancel := context.WithTimeout(ctx, rootArgs.timeout)
Expand Down Expand Up @@ -495,7 +496,7 @@ func bundleInstancesOwnershipConflicts(bundleInstances []*engine.BundleInstance)
func saveReaderToFile(reader io.Reader) (string, error) {
f, err := os.CreateTemp("", "*.cue")
if err != nil {
return "", fmt.Errorf("unable to create temp dir for stdin")
return "", errors.New("unable to create temp dir for stdin")
}

defer f.Close()
Expand Down
7 changes: 4 additions & 3 deletions cmd/timoni/bundle_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"maps"
"os"
Expand Down Expand Up @@ -73,7 +74,7 @@ func init() {
func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {
files := bundleBuildArgs.files
if len(files) == 0 {
return fmt.Errorf("no bundle provided with -f")
return errors.New("no bundle provided with -f")
}
var stdinFile string
for i, file := range files {
Expand Down Expand Up @@ -116,10 +117,10 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {

clusters := rt.SelectClusters(bundleArgs.runtimeCluster, bundleArgs.runtimeClusterGroup)
if len(clusters) > 1 {
return fmt.Errorf("you must select a cluster with --runtime-cluster")
return errors.New("you must select a cluster with --runtime-cluster")
}
if len(clusters) == 0 {
return fmt.Errorf("no cluster found")
return errors.New("no cluster found")
}

cluster := clusters[0]
Expand Down
5 changes: 3 additions & 2 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -77,7 +78,7 @@ func init() {

func runBundleDelCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 && bundleDelArgs.filename == "" && bundleDelArgs.name == "" {
return fmt.Errorf("bundle name is required")
return errors.New("bundle name is required")
}

switch {
Expand All @@ -99,7 +100,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {

clusters := rt.SelectClusters(bundleArgs.runtimeCluster, bundleArgs.runtimeClusterGroup)
if len(clusters) == 0 {
return fmt.Errorf("no cluster found")
return errors.New("no cluster found")
}

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
Expand Down
3 changes: 2 additions & 1 deletion cmd/timoni/inspect_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,7 +56,7 @@ func init() {

func runInspectModuleCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("instance name is required")
return errors.New("instance name is required")
}
inspectModuleArgs.name = args[0]

Expand Down
5 changes: 3 additions & 2 deletions cmd/timoni/mod_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -60,7 +61,7 @@ const (

func runInitModCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("module name is required")
return errors.New("module name is required")
}
initModArgs.name = args[0]

Expand Down Expand Up @@ -170,7 +171,7 @@ func initModuleFromTemplate(mName, mTmpl, src string, dst string) (err error) {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
return errors.New("source is not a directory")
}

_, err = os.Stat(dst)
Expand Down
3 changes: 2 additions & 1 deletion cmd/timoni/mod_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -104,7 +105,7 @@ func init() {

func pullCmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("module URL is required")
return errors.New("module URL is required")
}

version := pullModArgs.version.String()
Expand Down
5 changes: 3 additions & 2 deletions cmd/timoni/runtime_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -61,7 +62,7 @@ func init() {
func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error {
files := runtimeBuildArgs.files
if len(files) == 0 {
return fmt.Errorf("no runtime provided with -f")
return errors.New("no runtime provided with -f")
}
var stdinFile string
for i, file := range files {
Expand All @@ -88,7 +89,7 @@ func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error {

clusters := rt.SelectClusters(runtimeBuildArgs.clusterSelector, runtimeBuildArgs.clusterGroupSelector)
if len(clusters) == 0 {
return fmt.Errorf("no cluster found")
return errors.New("no cluster found")
}

for _, cluster := range clusters {
Expand Down
9 changes: 5 additions & 4 deletions docs/bundle-multi-cluster.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Multi-cluster Deployments

Timoni offers a declarative way of managing the app delivery across environments.
Timoni offers a declarative way of managing the delivery of applications across environments.
The Timoni [Runtime](bundle-runtime.md) allows defining groups of clusters where apps are being deployed.
The Timoni [Bundle](bundle.md) supports customising the app configuration based on the target
The Timoni [Bundle](bundle.md) supports customising the apps configuration based on the target
environment (group of clusters) and even for a specific cluster in a group.

```mermaid
Expand All @@ -19,8 +19,9 @@ G --> I[4. Region-B]
```

When applying a Bundle to multiple clusters, Timoni iterates over the clusters
in the order defined in the Runtime definition. It connects to each cluster,
deploys the app changes, runs health checks and e2e tests before moving to the next cluster.
in the order defined in the Runtime definition.
It connects to each cluster, deploys the app changes, runs health checks,
and end-to-end tests before moving to the next cluster.

## Multi-clusters definitions

Expand Down
5 changes: 3 additions & 2 deletions internal/engine/importer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"errors"
"fmt"
"path"
"strings"
Expand Down Expand Up @@ -140,11 +141,11 @@ func convertCRD(crd cue.Value) (*IntermediateCRD, error) {

vlist := crd.LookupPath(cue.ParsePath("spec.versions"))
if !vlist.Exists() {
return nil, fmt.Errorf("crd versions list is absent")
return nil, errors.New("crd versions list is absent")
}
iter, err := vlist.List()
if err != nil {
return nil, fmt.Errorf("crd versions field is not a list")
return nil, errors.New("crd versions field is not a list")
}

ctx := crd.Context()
Expand Down
7 changes: 4 additions & 3 deletions internal/engine/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package engine

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -176,7 +177,7 @@ func (b *ModuleBuilder) Build(tags ...string) (cue.Value, error) {

modInstances := load.Instances([]string{}, cfg)
if len(modInstances) == 0 {
return value, fmt.Errorf("no instances found")
return value, errors.New("no instances found")
}

modInstance := modInstances[0]
Expand Down Expand Up @@ -263,8 +264,8 @@ func (b *ModuleBuilder) GetModuleName() (string, error) {
}

mod, err := expr.String()
if expr.Err() != nil {
return "", fmt.Errorf("lookup module name failed: %w", expr.Err())
if err != nil {
return "", fmt.Errorf("lookup module name failed: %w", err)
}

return mod, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func ExtractStringFromFile(ctx *cue.Context, filePath, exprPath string) (string,
}

result, err := expr.String()
if expr.Err() != nil {
return "", fmt.Errorf("reading string failed: %w", expr.Err())
if err != nil {
return "", fmt.Errorf("reading string failed: %w", err)
}

return result, nil
Expand Down
6 changes: 1 addition & 5 deletions internal/flags/credentials.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package flags

import (
"fmt"
)

type Credentials string

func (f *Credentials) String() string {
Expand All @@ -20,5 +16,5 @@ func (f *Credentials) Type() string {
}

func (f *Credentials) Description() string {
return fmt.Sprintf("The credentials for the container registry in the format '<username>[:<password>]'.")
return "The credentials for the container registry in the format '<username>[:<password>]'."
}
4 changes: 1 addition & 3 deletions internal/flags/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package flags

import (
"fmt"

"github.com/Masterminds/semver/v3"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
Expand Down Expand Up @@ -33,5 +31,5 @@ func (f *Version) Shorthand() string {
}

func (f *Version) Description() string {
return fmt.Sprintf("The version of the module e.g. '1.0.0' or '1.0.0-rc.1'.")
return "The version of the module e.g. '1.0.0' or '1.0.0-rc.1'."
}
4 changes: 2 additions & 2 deletions internal/oci/pull_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func PullModule(ociURL, dstPath, cacheDir string, opts []crane.Option) (*apiv1.M
}

version := ""
if rev, ok := manifest.Annotations[apiv1.RevisionAnnotation]; ok == true {
if rev, ok := manifest.Annotations[apiv1.RevisionAnnotation]; ok {
// For backwards compatibility with Timoni v0.13
version = rev
}
if ver, ok := manifest.Annotations[apiv1.VersionAnnotation]; ok == true {
if ver, ok := manifest.Annotations[apiv1.VersionAnnotation]; ok {
version = ver
}

Expand Down