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: add more logging for failures to get label metadata #14275

Merged
merged 5 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ func GenerateManifests(ctx context.Context, appPath, repoRoot, revision string,
if q.AppLabelKey != "" && q.AppName != "" && !kube.IsCRD(target) {
err = resourceTracking.SetAppInstance(target, q.AppLabelKey, q.AppName, q.Namespace, v1alpha1.TrackingMethod(q.TrackingMethod))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to set app instance tracking info on manifest: %w", err)
}
}
manifestStr, err := json.Marshal(target.Object)
Expand Down
33 changes: 23 additions & 10 deletions util/argo/resource_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/kube"
argokube "github.com/argoproj/argo-cd/v2/util/kube"
"github.com/argoproj/argo-cd/v2/util/settings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

const (
Expand All @@ -31,7 +32,7 @@ type ResourceTracking interface {
Normalize(config, live *unstructured.Unstructured, labelKey, trackingMethod string) error
}

//AppInstanceValue store information about resource tracking info
// AppInstanceValue store information about resource tracking info
type AppInstanceValue struct {
ApplicationName string
Group string
Expand Down Expand Up @@ -140,7 +141,11 @@ func (rt *resourceTracking) SetAppInstance(un *unstructured.Unstructured, key, v
}
switch trackingMethod {
case TrackingMethodLabel:
return argokube.SetAppInstanceLabel(un, key, val)
err := argokube.SetAppInstanceLabel(un, key, val)
if err != nil {
return fmt.Errorf("failed to set app instance label: %w", err)
}
return nil
case TrackingMethodAnnotation:
return setAppInstanceAnnotation()
case TrackingMethodAnnotationAndLabel:
Expand All @@ -151,18 +156,26 @@ func (rt *resourceTracking) SetAppInstance(un *unstructured.Unstructured, key, v
if len(val) > LabelMaxLength {
val = val[:LabelMaxLength]
}
return argokube.SetAppInstanceLabel(un, key, val)
err = argokube.SetAppInstanceLabel(un, key, val)
if err != nil {
return fmt.Errorf("failed to set app instance label: %w", err)
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
default:
return argokube.SetAppInstanceLabel(un, key, val)
err := argokube.SetAppInstanceLabel(un, key, val)
if err != nil {
return fmt.Errorf("failed to set app instance label: %w", err)
}
return nil
}
}

//BuildAppInstanceValue build resource tracking id in format <application-name>;<group>/<kind>/<namespace>/<name>
// BuildAppInstanceValue build resource tracking id in format <application-name>;<group>/<kind>/<namespace>/<name>
func (rt *resourceTracking) BuildAppInstanceValue(value AppInstanceValue) string {
return fmt.Sprintf("%s:%s/%s:%s/%s", value.ApplicationName, value.Group, value.Kind, value.Namespace, value.Name)
}

//ParseAppInstanceValue parse resource tracking id from format <application-name>:<group>/<kind>:<namespace>/<name> to struct
// ParseAppInstanceValue parse resource tracking id from format <application-name>:<group>/<kind>:<namespace>/<name> to struct
func (rt *resourceTracking) ParseAppInstanceValue(value string) (*AppInstanceValue, error) {
var appInstanceValue AppInstanceValue
parts := strings.Split(value, ":")
Expand Down Expand Up @@ -198,7 +211,7 @@ func (rt *resourceTracking) Normalize(config, live *unstructured.Unstructured, l

label, err := kube.GetAppInstanceLabel(live, labelKey)
if err != nil {
return err
return fmt.Errorf("failed to get app instance label: %w", err)
}
if label == "" {
return nil
Expand All @@ -215,12 +228,12 @@ func (rt *resourceTracking) Normalize(config, live *unstructured.Unstructured, l

label, err = argokube.GetAppInstanceLabel(config, labelKey)
if err != nil {
return err
return fmt.Errorf("failed to get app instance label: %w", err)
}
if label == "" {
err = argokube.RemoveLabel(live, labelKey)
if err != nil {
return err
return fmt.Errorf("failed to remove app instance label: %w", err)
}
}

Expand Down
7 changes: 4 additions & 3 deletions util/kube/kube.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kube

import (
"fmt"
"regexp"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
Expand All @@ -23,7 +24,7 @@ func SetAppInstanceLabel(target *unstructured.Unstructured, key, val string) err
// Do not use target.GetLabels(), https://github.com/argoproj/argo-cd/issues/13730
labels, _, err := unstructured.NestedStringMap(target.Object, "metadata", "labels")
if err != nil {
return err
return fmt.Errorf("failed to get labels from target object %s %s/%s: %w", target.GroupVersionKind().String(), target.GetNamespace(), target.GetName(), err)
}
if labels == nil {
labels = make(map[string]string)
Expand Down Expand Up @@ -131,7 +132,7 @@ func GetAppInstanceLabel(un *unstructured.Unstructured, key string) (string, err
// Do not use target.GetLabels(), https://github.com/argoproj/argo-cd/issues/13730
labels, _, err := unstructured.NestedStringMap(un.Object, "metadata", "labels")
if err != nil {
return "", err
return "", fmt.Errorf("failed to get labels for %s %s/%s: %w", un.GroupVersionKind().String(), un.GetNamespace(), un.GetName(), err)
}
if labels != nil {
return labels[key], nil
Expand All @@ -144,7 +145,7 @@ func RemoveLabel(un *unstructured.Unstructured, key string) error {
// Do not use target.GetLabels(), https://github.com/argoproj/argo-cd/issues/13730
labels, _, err := unstructured.NestedStringMap(un.Object, "metadata", "labels")
if err != nil {
return err
return fmt.Errorf("failed to get labels for %s %s/%s: %w", un.GroupVersionKind().String(), un.GetNamespace(), un.GetName(), err)
}
if labels == nil {
return nil
Expand Down