-
Notifications
You must be signed in to change notification settings - Fork 260
write empty json object to empty cni statefile if exists #933
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package cnireconciler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "os" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| ) | ||
|
|
||
| // WriteObjectToCNIStatefile checks for a file at the CNI statefile path, | ||
| // and checks if it is empty. If it is empty, writes an empty JSON object to | ||
| // it so older CNI can execute. Does nothing and returns no error if the | ||
| // file does not exist. | ||
| // | ||
| // This is a hack to get older CNI to run when CNS has mounted the statefile | ||
| // path, but the statefile wasn't written by CNI yet. Kubelet will stub an | ||
| // empty file on the host filesystem, crashing older CNI because it doesn't know | ||
| // how to handle empty statefiles. | ||
| func WriteObjectToCNIStatefile() error { | ||
| filename := "/var/run/azure-vnet.json" | ||
| return writeObjectToFile(filename) | ||
| } | ||
|
|
||
| func writeObjectToFile(filename string) error { | ||
| fi, err := os.Stat(filename) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a log line? |
||
| } | ||
| return err | ||
| } | ||
|
|
||
| if fi.Size() != 0 { | ||
| return nil | ||
| } | ||
|
|
||
| logger.Printf("Writing {} to CNI statefile") | ||
| b, _ := json.Marshal(map[string]string{}) | ||
rbtr marked this conversation as resolved.
Show resolved
Hide resolved
rbtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return os.WriteFile(filename, b, os.FileMode(0666)) | ||
| } | ||
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,41 @@ | ||
| package cnireconciler | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestWriteObjectToFile(t *testing.T) { | ||
| name := "testdata/test" | ||
| err := os.MkdirAll(path.Dir(name), 0666) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = os.Stat(name) | ||
| require.ErrorIs(t, err, os.ErrNotExist) | ||
|
|
||
| // create empty file | ||
| _, err = os.Create(name) | ||
| require.NoError(t, err) | ||
| defer os.Remove(name) | ||
|
|
||
| // check it's empty | ||
| fi, _ := os.Stat(name) | ||
| assert.Equal(t, fi.Size(), int64(0)) | ||
|
|
||
| // populate | ||
| require.NoError(t, writeObjectToFile(name)) | ||
|
|
||
| // read | ||
| b, err := os.ReadFile(name) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, string(b), "{}") | ||
| } | ||
|
|
||
| func init() { | ||
| logger.InitLogger("testlogs", 0, 0, "./") | ||
| } |
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.
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.
Hi @rbtr 👋 what's the best way I can make this different for OS windows?
I was thinking about making a
statefile_windows.gowith this same code but different filename, that seems to copy alot however.Any ideas? I just want to defer to you since you know alot more about go than I do, I can only think about an if statement if the OS is windows, just wondering if that's enough
Uh oh!
There was an error while loading. Please reload this page.
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.
This was a hack for older CNIs, pre 1.4.7 or so; if you're adding support to Windows now then this might not even be necessary.
If it is necessary, is the only thing that's different on Windows the filepath? That could be injected from an early OS check all the way down in to this function. OR, probably more correctly, this shouldn't be a hardcoded const and should come from env or config (with a default for each OS).