This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 349
reload cni network config if has fs change events #1405
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 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,121 @@ | ||
/* | ||
Copyright The Containerd 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 server | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
|
||
cni "github.com/containerd/go-cni" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// cniNetConfSyncer is used to reload cni network conf triggered by fs change | ||
// events. | ||
type cniNetConfSyncer struct { | ||
// only used for lastSyncStatus | ||
sync.RWMutex | ||
lastSyncStatus error | ||
|
||
watcher *fsnotify.Watcher | ||
confDir string | ||
netPlugin cni.CNI | ||
loadOpts []cni.CNIOpt | ||
} | ||
|
||
// newCNINetConfSyncer creates cni network conf syncer. | ||
func newCNINetConfSyncer(confDir string, netPlugin cni.CNI, loadOpts []cni.CNIOpt) (*cniNetConfSyncer, error) { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create fsnotify watcher") | ||
} | ||
|
||
if err := os.MkdirAll(confDir, 0700); err != nil { | ||
return nil, errors.Wrapf(err, "failed to create cni conf dir=%s for watch", confDir) | ||
} | ||
|
||
if err := watcher.Add(confDir); err != nil { | ||
return nil, errors.Wrapf(err, "failed to watch cni conf dir %s", confDir) | ||
} | ||
|
||
syncer := &cniNetConfSyncer{ | ||
watcher: watcher, | ||
confDir: confDir, | ||
netPlugin: netPlugin, | ||
loadOpts: loadOpts, | ||
} | ||
|
||
if err := syncer.netPlugin.Load(syncer.loadOpts...); err != nil { | ||
logrus.WithError(err).Error("failed to load cni during init, please check CRI plugin status before setting up network for pods") | ||
syncer.updateLastStatus(err) | ||
} | ||
return syncer, nil | ||
} | ||
|
||
// syncLoop monitors any fs change events from cni conf dir and tries to reload | ||
// cni configuration. | ||
func (syncer *cniNetConfSyncer) syncLoop() error { | ||
for { | ||
select { | ||
case event := <-syncer.watcher.Events: | ||
// Only reload config when receiving write/rename/remove | ||
// events | ||
// | ||
// TODO(fuweid): Might only reload target cni config | ||
// files to prevent no-ops. | ||
if event.Op&(fsnotify.Chmod|fsnotify.Create) > 0 { | ||
logrus.Debugf("ignore event from cni conf dir: %s", event) | ||
continue | ||
} | ||
logrus.Debugf("receiving change event from cni conf dir: %s", event) | ||
|
||
lerr := syncer.netPlugin.Load(syncer.loadOpts...) | ||
if lerr != nil { | ||
logrus.WithError(lerr). | ||
Errorf("failed to reload cni configuration after receiving fs change event(%s)", event) | ||
} | ||
syncer.updateLastStatus(lerr) | ||
|
||
case err := <-syncer.watcher.Errors: | ||
if err != nil { | ||
logrus.WithError(err).Error("failed to continue sync cni conf change") | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
// lastStatus retrieves last sync status. | ||
func (syncer *cniNetConfSyncer) lastStatus() error { | ||
syncer.RLock() | ||
defer syncer.RUnlock() | ||
return syncer.lastSyncStatus | ||
} | ||
|
||
// updateLastStatus will be called after every single cni load. | ||
func (syncer *cniNetConfSyncer) updateLastStatus(err error) { | ||
syncer.Lock() | ||
defer syncer.Unlock() | ||
syncer.lastSyncStatus = err | ||
} | ||
|
||
// stop stops watcher in the syncLoop. | ||
func (syncer *cniNetConfSyncer) stop() error { | ||
return syncer.watcher.Close() | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
the previous test failed because the conf dir doesn't exist.