-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat[sc-100944]: Support upserting and deleting custom metrics #191
Merged
FourSigma
merged 10 commits into
main
from
siva/sc-100944/support-upserting-and-deleting-custom-metrics
Jun 20, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cbd5d3a
sync tag function
0ec8b8a
updated sync logic and change secret var name
0011a29
minor refactor
af1bf2a
modify handlers
1d65986
working DELETE
bf808b9
add merge test
243194e
fix test
286c93a
dep inject client set for custom metrics handler
dcb0a24
update uid
86a4268
marc's suggestion
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package meta | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/replicated-sdk/pkg/meta/types" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const ( | ||
instanceTagSecretKey replicatedMetadataSecretKey = "instance-tag-data" | ||
) | ||
|
||
func SaveInstanceTag(ctx context.Context, clientset kubernetes.Interface, namespace string, tdata types.InstanceTagData) error { | ||
return save(ctx, clientset, namespace, instanceTagSecretKey, tdata) | ||
} | ||
|
||
func GetInstanceTag(ctx context.Context, clientset kubernetes.Interface, namespace string) (*types.InstanceTagData, error) { | ||
t := types.InstanceTagData{} | ||
|
||
if err := get(ctx, clientset, namespace, instanceTagSecretKey, &t); err != nil { | ||
return nil, errors.Wrapf(err, "failed to get instance tag data") | ||
} | ||
|
||
return &t, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package meta | ||
|
||
import ( | ||
"context" | ||
"maps" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const ( | ||
customMetricsSecretKey replicatedMetadataSecretKey = "latest-custom-metrics" | ||
) | ||
|
||
func SyncCustomAppMetrics(ctx context.Context, clientset kubernetes.Interface, namespace string, inboundMetrics map[string]interface{}, overwrite bool) (map[string]interface{}, error) { | ||
existing := map[string]interface{}{} | ||
|
||
err := get(ctx, clientset, namespace, customMetricsSecretKey, &existing) | ||
if err != nil && errors.Cause(err) != ErrReplicatedMetadataSecretNotFound { | ||
return nil, errors.Wrapf(err, "failed to get custom metrics data") | ||
} | ||
|
||
modified := mergeCustomAppMetrics(existing, inboundMetrics, overwrite) | ||
|
||
if err := save(ctx, clientset, namespace, customMetricsSecretKey, modified); err != nil { | ||
return nil, errors.Wrap(err, "failed to save custom metrics") | ||
} | ||
|
||
return modified, nil | ||
} | ||
|
||
func mergeCustomAppMetrics(existingMetrics map[string]interface{}, inboundMetrics map[string]interface{}, overwrite bool) map[string]interface{} { | ||
if existingMetrics == nil { | ||
existingMetrics = map[string]interface{}{} | ||
} | ||
|
||
if inboundMetrics == nil { | ||
inboundMetrics = map[string]interface{}{} | ||
} | ||
|
||
if overwrite { | ||
return inboundMetrics | ||
} | ||
|
||
if len(inboundMetrics) == 0 || maps.Equal(existingMetrics, inboundMetrics) { | ||
return existingMetrics | ||
} | ||
|
||
for k, v := range inboundMetrics { | ||
if v == nil { | ||
delete(existingMetrics, k) | ||
continue | ||
} | ||
|
||
existingMetrics[k] = v | ||
} | ||
|
||
return existingMetrics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package meta | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_mergeCustomAppMetrics(tst *testing.T) { | ||
tests := []struct { | ||
name string | ||
existingMetrics map[string]interface{} | ||
inboundMetrics map[string]interface{} | ||
overwrite bool | ||
assertFn func(*testing.T, map[string]interface{}) | ||
}{ | ||
{ | ||
name: "should return empty if both are empty", | ||
existingMetrics: map[string]interface{}{}, | ||
inboundMetrics: map[string]interface{}{}, | ||
overwrite: false, | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
assert.NotNil(t, actual) | ||
assert.Empty(t, actual) | ||
}, | ||
}, | ||
{ | ||
name: "should tolerate nil value on existingMetrics", | ||
existingMetrics: nil, | ||
inboundMetrics: map[string]interface{}{"numProjects": 10}, | ||
overwrite: false, | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
expected := map[string]interface{}{"numProjects": 10} | ||
assert.Equal(t, expected, actual) | ||
}, | ||
}, | ||
{ | ||
name: "should tolerate nil value on inboundMetrics", | ||
existingMetrics: map[string]interface{}{"numProjects": 10}, | ||
inboundMetrics: nil, | ||
overwrite: false, | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
expected := map[string]interface{}{"numProjects": 10} | ||
assert.Equal(t, expected, actual) | ||
}, | ||
}, | ||
{ | ||
name: "should return inboundMetrics when overwrite is true", | ||
existingMetrics: map[string]interface{}{"numProjects": 10, "token": "1234"}, | ||
inboundMetrics: map[string]interface{}{"newProjects": 100, "newToken": 10}, | ||
overwrite: true, // overwrites existing metric data with inbound metrics data | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
expected := map[string]interface{}{"newProjects": 100, "newToken": 10} | ||
assert.Equal(t, expected, actual) | ||
}, | ||
}, | ||
{ | ||
name: "should return merged data when overwrite is false", | ||
existingMetrics: map[string]interface{}{"numProjects": 10, "token": "1234"}, | ||
inboundMetrics: map[string]interface{}{"numProjects": 66666, "numPeople": 100}, | ||
overwrite: false, | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
expected := map[string]interface{}{"numPeople": 100, "numProjects": 66666, "token": "1234"} | ||
assert.Equal(t, expected, actual) | ||
}, | ||
}, | ||
{ | ||
name: "should delete existing metric key when the corresponding inboundMetrics value is nil", | ||
existingMetrics: map[string]interface{}{"numProjects": 10, "token": "1234"}, | ||
inboundMetrics: map[string]interface{}{"numProjects": nil}, // delete numProjects | ||
overwrite: false, | ||
assertFn: func(t *testing.T, actual map[string]interface{}) { | ||
expected := map[string]interface{}{"token": "1234"} | ||
assert.Equal(t, expected, actual) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
m := mergeCustomAppMetrics(tt.existingMetrics, tt.inboundMetrics, tt.overwrite) | ||
tt.assertFn(tst, m) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on having this in the
k8sutil
package and theif testClientset != nil
logic can be part of theGetClientset()
function? That way it could be used without the need to if/else each time we want to potentially use the test client in a handler (or elsewhere).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is great suggestion Craig. Really neat.
CC: @marccampbell
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only concern is that we need to return a
kubernetes.Interface
forGetClientSet()
instead of the pointer type*kubernetes.Clientset
. Are we okay with returning interfaces instead of concrete types? The Go pattern has usually been accept interfaces and return structs. @cbodonnellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me. I'm good with the PR as it is 👍