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

chore: better error messages #11738

Merged
merged 2 commits into from
Jan 9, 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
18 changes: 11 additions & 7 deletions controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,14 @@ func (ctrl *ApplicationController) getResourceTree(a *appv1.Application, managed

proj, err := ctrl.getAppProj(a)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get project: %w", err)
}
orphanedNodesMap := make(map[kube.ResourceKey]appv1.ResourceNode)
warnOrphaned := true
if proj.Spec.OrphanedResources != nil {
orphanedNodesMap, err = ctrl.stateCache.GetNamespaceTopLevelResources(a.Spec.Destination.Server, a.Spec.Destination.Namespace)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get namespace top-level resources: %w", err)
}
warnOrphaned = proj.Spec.OrphanedResources.IsWarn()
}
Expand All @@ -436,12 +436,12 @@ func (ctrl *ApplicationController) getResourceTree(a *appv1.Application, managed
var live = &unstructured.Unstructured{}
err := json.Unmarshal([]byte(managedResource.LiveState), &live)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unmarshal live state of managed resources: %w", err)
}
var target = &unstructured.Unstructured{}
err = json.Unmarshal([]byte(managedResource.TargetState), &target)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unmarshal target state of managed resources: %w", err)
}

if live == nil {
Expand All @@ -457,7 +457,11 @@ func (ctrl *ApplicationController) getResourceTree(a *appv1.Application, managed
} else {
err := ctrl.stateCache.IterateHierarchy(a.Spec.Destination.Server, kube.GetResourceKey(live), func(child appv1.ResourceNode, appName string) bool {
permitted, _ := proj.IsResourcePermitted(schema.GroupKind{Group: child.ResourceRef.Group, Kind: child.ResourceRef.Kind}, child.Namespace, a.Spec.Destination, func(project string) ([]*appv1.Cluster, error) {
return ctrl.db.GetProjectClusters(context.TODO(), project)
clusters, err := ctrl.db.GetProjectClusters(context.TODO(), project)
if err != nil {
return nil, fmt.Errorf("failed to get project clusters: %w", err)
}
return clusters, nil
})
if !permitted {
return false
Expand All @@ -466,7 +470,7 @@ func (ctrl *ApplicationController) getResourceTree(a *appv1.Application, managed
return true
})
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to iterate resource hierarchy: %w", err)
}
}
}
Expand Down Expand Up @@ -515,7 +519,7 @@ func (ctrl *ApplicationController) getResourceTree(a *appv1.Application, managed

hosts, err := ctrl.getAppHosts(a, nodes)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get app hosts: %w", err)
}
return &appv1.ApplicationTree{Nodes: nodes, OrphanedNodes: orphanedNodes, Hosts: hosts}, nil
}
Expand Down
10 changes: 5 additions & 5 deletions util/db/cluster.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package db

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"time"

"context"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -233,17 +233,17 @@ func (db *db) GetCluster(_ context.Context, server string) (*appv1.Cluster, erro
func (db *db) GetProjectClusters(ctx context.Context, project string) ([]*appv1.Cluster, error) {
informer, err := db.settingsMgr.GetSecretsInformer()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get secrets informer: %w", err)
}
secrets, err := informer.GetIndexer().ByIndex(settings.ByProjectClusterIndexer, project)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get index by project cluster indexer for project %q: %w", project, err)
}
var res []*appv1.Cluster
for i := range secrets {
cluster, err := secretToCluster(secrets[i].(*apiv1.Secret))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to convert secret to cluster: %w", err)
}
res = append(res, cluster)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func secretToCluster(s *apiv1.Secret) (*appv1.Cluster, error) {
if len(s.Data["config"]) > 0 {
err := json.Unmarshal(s.Data["config"], &config)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unmarshal cluster config: %w", err)
}
}

Expand Down