-
Notifications
You must be signed in to change notification settings - Fork 260
create nnc listener concept and adapt existing poolmonitor and swift … #1323
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
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
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 |
|---|---|---|
|
|
@@ -7,6 +7,9 @@ import ( | |
| "strings" | ||
|
|
||
| "github.com/Azure/azure-container-networking/cns" | ||
| "github.com/Azure/azure-container-networking/cns/logger" | ||
| "github.com/Azure/azure-container-networking/cns/restserver" | ||
| cnstypes "github.com/Azure/azure-container-networking/cns/types" | ||
| "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
@@ -20,6 +23,42 @@ var ( | |
| ErrUnsupportedNCQuantity = errors.New("unsupported number of network containers") | ||
| ) | ||
|
|
||
| type cnsClient interface { | ||
| CreateOrUpdateNetworkContainerInternal(*cns.CreateNetworkContainerRequest) cnstypes.ResponseCode | ||
| } | ||
|
|
||
| var _ nodeNetworkConfigListener = (NodeNetworkConfigListenerFunc)(nil) //nolint:gocritic // clarity | ||
|
|
||
| type NodeNetworkConfigListenerFunc func(*v1alpha.NodeNetworkConfig) error | ||
|
|
||
| func (f NodeNetworkConfigListenerFunc) Update(nnc *v1alpha.NodeNetworkConfig) error { | ||
| return f(nnc) | ||
| } | ||
|
|
||
| // SwiftNodeNetworkConfigListener return a function which satisfies the NodeNetworkConfigListener | ||
| // interface. It accepts a CreateOrUpdateNetworkContainerInternal implementation, and when Update | ||
| // is called, transforms the NNC in to an NC Request and calls the CNS Service implementation with | ||
| // that request. | ||
| func SwiftNodeNetworkConfigListener(cnscli cnsClient) NodeNetworkConfigListenerFunc { | ||
| return func(nnc *v1alpha.NodeNetworkConfig) error { | ||
| // Create NC request and hand it off to CNS | ||
| ncRequest, err := CRDStatusToNCRequest(&nnc.Status) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to convert NNC status to network container request") | ||
| } | ||
| responseCode := cnscli.CreateOrUpdateNetworkContainerInternal(&ncRequest) | ||
| err = restserver.ResponseCodeToError(responseCode) | ||
| if err != nil { | ||
|
Collaborator
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. can combine
Collaborator
Author
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. incorporated this in the next one |
||
| logger.Errorf("[cns-rc] Error creating or updating NC in reconcile: %v", err) | ||
| return errors.Wrap(err, "failed to create or update network container") | ||
| } | ||
|
|
||
| // record assigned IPs metric | ||
| allocatedIPs.Set(float64(len(nnc.Status.NetworkContainers[0].IPAssignments))) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // CRDStatusToNCRequest translates a crd status to createnetworkcontainer request | ||
| func CRDStatusToNCRequest(status *v1alpha.NodeNetworkConfigStatus) (cns.CreateNetworkContainerRequest, error) { | ||
| // if NNC has no NC, return an empty request | ||
|
|
||
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
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.
neat, haven't seen this pattern much but seems pretty clean to me
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.
it is a little on the abstract side, but it is cleaner than needing to attach these funcs to a stub struct as methods when the right closure provides all the context we need. it's the same pattern as
http.Handler/http.HandlerFunc🙂