Skip to content

Commit

Permalink
Handle bad metadata secret values gracefully (#224)
Browse files Browse the repository at this point in the history
* Handle bad metadata secret values gracefully

* Fix chainguard build
  • Loading branch information
divolgin authored Dec 9, 2024
1 parent eb04766 commit bf14926
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion deploy/apko.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contents:
- https://packages.wolfi.dev/os/wolfi-signing.rsa.pub
- ./melange.rsa.pub
packages:
- replicated-head # This is expected to be built locally by `melange`.
- replicated # This is expected to be built locally by `melange`.
- bash
- busybox
- curl
Expand Down
2 changes: 1 addition & 1 deletion deploy/melange.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package:
name: replicated-head
name: replicated
version: ${GIT_TAG}
epoch: 0
description: replicated package
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/latest_custom_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func SyncCustomAppMetrics(ctx context.Context, clientset kubernetes.Interface, n
existing := map[string]interface{}{}

err := get(ctx, clientset, namespace, customMetricsSecretKey, &existing)
if err != nil && errors.Cause(err) != ErrReplicatedMetadataSecretNotFound {
if err != nil && errors.Cause(err) != ErrReplicatedMetadataNotFound {
return nil, errors.Wrapf(err, "failed to get custom metrics data")
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated-sdk/pkg/logger"
"github.com/replicatedhq/replicated-sdk/pkg/util"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -90,8 +91,7 @@ func save(ctx context.Context, clientset kubernetes.Interface, namespace string,
}

var (
ErrReplicatedMetadataSecretEmpty = errors.New("replicated metadata secret is empty")
ErrReplicatedMetadataSecretNotFound = errors.New("replicated metadata secret not found")
ErrReplicatedMetadataNotFound = errors.New("replicated metadata not found")
)

func get(ctx context.Context, clientset kubernetes.Interface, namespace string, key replicatedMetadataSecretKey, v interface{}) error {
Expand All @@ -101,20 +101,21 @@ func get(ctx context.Context, clientset kubernetes.Interface, namespace string,
}

if kuberneteserrors.IsNotFound(err) {
return ErrReplicatedMetadataSecretNotFound
return ErrReplicatedMetadataNotFound
}

if len(secret.Data) == 0 {
return ErrReplicatedMetadataSecretEmpty
return ErrReplicatedMetadataNotFound
}

dataBytes, ok := secret.Data[string(key)]
if !ok || len(dataBytes) == 0 {
return errors.Wrapf(ErrReplicatedMetadataSecretEmpty, "key (%s) not found in secret", key)
return errors.Wrapf(ErrReplicatedMetadataNotFound, "key (%s) not found in secret", key)
}

if err := json.Unmarshal(dataBytes, v); err != nil {
return errors.Wrapf(err, "failed to unmarshal secret data for key %s", key)
logger.Infof("failed to unmarshal secret data for key %s: %v", key, err)
return ErrReplicatedMetadataNotFound
}

return nil
Expand Down

0 comments on commit bf14926

Please sign in to comment.