Skip to content

Commit

Permalink
chore: add more logging for failures to get label metadata (argoproj#…
Browse files Browse the repository at this point in the history
…14275)

* chore: add more logging for failures to get label metadata

Signed-off-by: Michael Crenshaw <[email protected]>

* context

Signed-off-by: Michael Crenshaw <[email protected]>

* fix test

Signed-off-by: Michael Crenshaw <[email protected]>

* fix test

Signed-off-by: Michael Crenshaw <[email protected]>

---------

Signed-off-by: Michael Crenshaw <[email protected]>
Signed-off-by: jannfis <[email protected]>
Co-authored-by: jannfis <[email protected]>
  • Loading branch information
2 people authored and tesla59 committed Dec 16, 2023
1 parent 208352a commit 8e5019e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
11 changes: 8 additions & 3 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
}
}
if err != nil {
logCtx := log.WithFields(log.Fields{
"application": q.AppName,
"appNamespace": q.Namespace,
})

// If manifest generation error caching is enabled
if s.initConstants.PauseGenerationAfterFailedGenerationAttempts > 0 {
cache.LogDebugManifestCacheKeyFields("getting manifests cache", "GenerateManifests error", cacheKey, q.ApplicationSource, q.RefSources, q, q.Namespace, q.TrackingMethod, q.AppLabelKey, q.AppName, refSourceCommitSHAs)
Expand All @@ -792,7 +797,7 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
innerRes := &cache.CachedManifestResponse{}
cacheErr := s.cache.GetManifests(cacheKey, appSourceCopy, q.RefSources, q, q.Namespace, q.TrackingMethod, q.AppLabelKey, q.AppName, innerRes, refSourceCommitSHAs)
if cacheErr != nil && cacheErr != cache.ErrCacheMiss {
log.Warnf("manifest cache set error %s: %v", appSourceCopy.String(), cacheErr)
logCtx.Warnf("manifest cache get error %s: %v", appSourceCopy.String(), cacheErr)
ch.errCh <- cacheErr
return
}
Expand All @@ -810,7 +815,7 @@ func (s *Service) runManifestGenAsync(ctx context.Context, repoRoot, commitSHA,
innerRes.MostRecentError = err.Error()
cacheErr = s.cache.SetManifests(cacheKey, appSourceCopy, q.RefSources, q, q.Namespace, q.TrackingMethod, q.AppLabelKey, q.AppName, innerRes, refSourceCommitSHAs)
if cacheErr != nil {
log.Warnf("manifest cache set error %s: %v", appSourceCopy.String(), cacheErr)
logCtx.Warnf("manifest cache set error %s: %v", appSourceCopy.String(), cacheErr)
ch.errCh <- cacheErr
return
}
Expand Down Expand Up @@ -1396,7 +1401,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)
}
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
4 changes: 2 additions & 2 deletions util/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestGetAppInstanceLabelWithInvalidData(t *testing.T) {
assert.Nil(t, err)
_, err = GetAppInstanceLabel(&obj, "valid-label")
assert.Error(t, err)
assert.Equal(t, ".metadata.labels accessor error: contains non-string key in the map: <nil> is of the type <nil>, expected string", err.Error())
assert.Equal(t, "failed to get labels for /v1, Kind=Service /my-service: .metadata.labels accessor error: contains non-string key in the map: <nil> is of the type <nil>, expected string", err.Error())
}

func TestRemoveLabel(t *testing.T) {
Expand All @@ -268,5 +268,5 @@ func TestRemoveLabelWithInvalidData(t *testing.T) {

err = RemoveLabel(&obj, "valid-label")
assert.Error(t, err)
assert.Equal(t, ".metadata.labels accessor error: contains non-string key in the map: <nil> is of the type <nil>, expected string", err.Error())
assert.Equal(t, "failed to get labels for /v1, Kind=Service /my-service: .metadata.labels accessor error: contains non-string key in the map: <nil> is of the type <nil>, expected string", err.Error())
}

0 comments on commit 8e5019e

Please sign in to comment.