Alerting: Remote Alertmanager to compare current hash with hash from response (#108554)
This commit is contained in:
@@ -300,18 +300,18 @@ func (am *Alertmanager) CompareAndSendConfiguration(ctx context.Context, config
|
||||
return fmt.Errorf("unable to marshal decrypted configuration: %w", err)
|
||||
}
|
||||
|
||||
configHash := md5.Sum(rawPayload)
|
||||
configHash := fmt.Sprintf("%x", md5.Sum(rawPayload))
|
||||
|
||||
// Send the configuration only if we need to.
|
||||
if !am.shouldSendConfig(ctx, configHash) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return am.sendConfiguration(ctx, payload, fmt.Sprintf("%x", configHash), config.CreatedAt, am.isDefaultConfiguration(configHash))
|
||||
return am.sendConfiguration(ctx, payload, configHash, config.CreatedAt, am.isDefaultConfiguration(configHash))
|
||||
}
|
||||
|
||||
func (am *Alertmanager) isDefaultConfiguration(configHash [16]byte) bool {
|
||||
return fmt.Sprintf("%x", configHash) == am.defaultConfigHash
|
||||
func (am *Alertmanager) isDefaultConfiguration(configHash string) bool {
|
||||
return configHash == am.defaultConfigHash
|
||||
}
|
||||
|
||||
// decryptConfiguration creates a copy of the configuration, decrypts it, and returns the decrypted configuration alongside its hash.
|
||||
@@ -749,7 +749,7 @@ func (am *Alertmanager) getFullState(ctx context.Context) (string, error) {
|
||||
|
||||
// shouldSendConfig compares the remote Alertmanager configuration with our local one.
|
||||
// It returns true if the configurations are different.
|
||||
func (am *Alertmanager) shouldSendConfig(ctx context.Context, hash [16]byte) bool {
|
||||
func (am *Alertmanager) shouldSendConfig(ctx context.Context, hash string) bool {
|
||||
rc, err := am.mimirClient.GetGrafanaAlertmanagerConfig(ctx)
|
||||
if err != nil {
|
||||
// Log the error and return true so we try to upload our config anyway.
|
||||
@@ -788,11 +788,5 @@ func (am *Alertmanager) shouldSendConfig(ctx context.Context, hash [16]byte) boo
|
||||
}
|
||||
}
|
||||
|
||||
// Hash and compare Alertmanager configs.
|
||||
rawRemote, err := json.Marshal(rc.GrafanaAlertmanagerConfig)
|
||||
if err != nil {
|
||||
am.log.Error("Unable to marshal the remote Alertmanager configuration for comparison", "err", err)
|
||||
return true
|
||||
}
|
||||
return md5.Sum(rawRemote) != hash
|
||||
return rc.Hash != hash
|
||||
}
|
||||
|
||||
@@ -727,7 +727,7 @@ func Test_isDefaultConfiguration(t *testing.T) {
|
||||
}
|
||||
raw, err := json.Marshal(test.config)
|
||||
require.NoError(tt, err)
|
||||
require.Equal(tt, test.expected, am.isDefaultConfiguration(md5.Sum(raw)))
|
||||
require.Equal(tt, test.expected, am.isDefaultConfiguration(fmt.Sprintf("%x", md5.Sum(raw))))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user