-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support reload config automatically #1464
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
920e44e
Support reload config automatically
endesapt 0d6c855
add license
endesapt 59f5137
added documentation in README.md
endesapt 854c6b4
refatcor: move checksum to SafeConfig
endesapt f87f233
chore(changelog): add notes for auto reload feature
endesapt 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| // Copyright 2025 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package config | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func GenerateChecksum(yamlFilePath string) (string, error) { | ||
| hash := sha256.New() | ||
| yamlContent, err := os.ReadFile(yamlFilePath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error reading YAML file: %w", err) | ||
| } | ||
| _, err = hash.Write(yamlContent) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error writing YAML file to hash: %w", err) | ||
| } | ||
|
|
||
| return hex.EncodeToString(hash.Sum(nil)), nil | ||
| } |
This file contains hidden or 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,109 @@ | ||
| // Copyright 2025 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGenerateChecksum(t *testing.T) { | ||
| // Create a temporary file and establish the "original" state. | ||
| // All subsequent tests will measure against this state. | ||
| originalContent := []byte("modules:\n http_2xx:\n prober: http\n") | ||
|
|
||
| tempDir := t.TempDir() | ||
| filePath := filepath.Join(tempDir, "config.yaml") | ||
|
|
||
| if err := os.WriteFile(filePath, originalContent, 0644); err != nil { | ||
| t.Fatalf("Could not write initial file: %v", err) | ||
| } | ||
|
|
||
| originalChecksum, err := GenerateChecksum(filePath) | ||
| if err != nil { | ||
| t.Fatalf("Could not generate initial checksum: %v", err) | ||
| } | ||
| if originalChecksum == "" { | ||
| t.Fatal(" Initial checksum should not be empty") | ||
| } | ||
|
|
||
| t.Run("when content is appended", func(t *testing.T) { | ||
| testModification(t, filePath, originalContent, originalChecksum, []byte("modules:\n http_2xx:\n prober: http\n new_module:\n")) | ||
| }) | ||
|
|
||
| t.Run("when content is completely replaced", func(t *testing.T) { | ||
| testModification(t, filePath, originalContent, originalChecksum, []byte("completely: different\n")) | ||
| }) | ||
|
|
||
| t.Run("when content is cleared (file becomes empty)", func(t *testing.T) { | ||
| testModification(t, filePath, originalContent, originalChecksum, []byte("")) | ||
| }) | ||
|
|
||
| // These tests do not fit the modify-restore pattern and are handled independently. | ||
| t.Run("should return an error for a non-existent file", func(t *testing.T) { | ||
| nonExistentPath := filepath.Join(tempDir, "this-file-does-not-exist.yaml") | ||
|
|
||
| checksum, err := GenerateChecksum(nonExistentPath) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("Expected an error for a non-existent file, but got nil") | ||
| } | ||
| if checksum != "" { | ||
| t.Errorf("Checksum should be empty on error, but got: %s", checksum) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("should return an error when path is a directory", func(t *testing.T) { | ||
| checksum, err := GenerateChecksum(tempDir) | ||
|
|
||
| if err == nil { | ||
| t.Fatal("Expected an error when the path is a directory, but got nil") | ||
| } | ||
| if checksum != "" { | ||
| t.Errorf("Checksum should be empty on error, but got: %s", checksum) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // testModification is a helper function that encapsulates the user's requested test logic: | ||
| // 1. Write new content. | ||
| // 2. Check that the checksum is different. | ||
| // 3. Write the original content back. | ||
| // 4. Check that the checksum is the same as the original. | ||
| func testModification(t *testing.T, filePath string, originalContent []byte, originalChecksum string, newContent []byte) { | ||
| t.Helper() | ||
|
|
||
| if err := os.WriteFile(filePath, newContent, 0644); err != nil { | ||
| t.Fatalf("Failed to write new content to file: %v", err) | ||
| } | ||
|
|
||
| modifiedChecksum, err := GenerateChecksum(filePath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to generate checksum for modified file: %v", err) | ||
| } | ||
| if modifiedChecksum == originalChecksum { | ||
| t.Error("Checksum did not change after modifying file content") | ||
| } | ||
| if err := os.WriteFile(filePath, originalContent, 0644); err != nil { | ||
| t.Fatalf("Failed to restore original content to file: %v", err) | ||
| } | ||
|
|
||
| restoredChecksum, err := GenerateChecksum(filePath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to generate checksum for restored file: %v", err) | ||
| } | ||
| if restoredChecksum != originalChecksum { | ||
| t.Errorf("Checksum should be restored to original value, but it was not.\nOriginal: %s\nRestored: %s", originalChecksum, restoredChecksum) | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.