2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes 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 (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/klog"
|
|
|
|
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/client-go/util/flowcontrol"
|
|
|
|
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
retryPeriod = 1 * time.Second
|
|
|
|
maxRetryPeriod = 20 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type retryableError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *retryableError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceFile) startWatch() {
|
|
|
|
backOff := flowcontrol.NewBackOff(retryPeriod, maxRetryPeriod)
|
2019-09-27 21:51:53 +00:00
|
|
|
backOffID := "watch"
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
go wait.Forever(func() {
|
2019-09-27 21:51:53 +00:00
|
|
|
if backOff.IsInBackOffSinceUpdate(backOffID, time.Now()) {
|
2019-01-12 04:58:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.doWatch(); err != nil {
|
|
|
|
klog.Errorf("Unable to read config path %q: %v", s.path, err)
|
|
|
|
if _, retryable := err.(*retryableError); !retryable {
|
2019-09-27 21:51:53 +00:00
|
|
|
backOff.Next(backOffID, time.Now())
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, retryPeriod)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceFile) doWatch() error {
|
|
|
|
_, err := os.Stat(s.path)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Emit an update with an empty PodList to allow FileSource to be marked as seen
|
|
|
|
s.updates <- kubetypes.PodUpdate{Pods: []*v1.Pod{}, Op: kubetypes.SET, Source: kubetypes.FileSource}
|
|
|
|
return &retryableError{"path does not exist, ignoring"}
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
w, err := fsnotify.NewWatcher()
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create inotify: %v", err)
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
err = w.Add(s.path)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2019-04-07 17:07:55 +00:00
|
|
|
case event := <-w.Events:
|
|
|
|
if err = s.produceWatchEvent(&event); err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
case err = <-w.Errors:
|
2019-01-12 04:58:27 +00:00
|
|
|
return fmt.Errorf("error while watching %q: %v", s.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
|
2019-01-12 04:58:27 +00:00
|
|
|
// Ignore file start with dots
|
|
|
|
if strings.HasPrefix(filepath.Base(e.Name), ".") {
|
|
|
|
klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var eventType podEventType
|
|
|
|
switch {
|
2019-04-07 17:07:55 +00:00
|
|
|
case (e.Op & fsnotify.Create) > 0:
|
2019-01-12 04:58:27 +00:00
|
|
|
eventType = podAdd
|
2019-04-07 17:07:55 +00:00
|
|
|
case (e.Op & fsnotify.Write) > 0:
|
2019-01-12 04:58:27 +00:00
|
|
|
eventType = podModify
|
2019-04-07 17:07:55 +00:00
|
|
|
case (e.Op & fsnotify.Chmod) > 0:
|
2019-01-12 04:58:27 +00:00
|
|
|
eventType = podModify
|
2019-04-07 17:07:55 +00:00
|
|
|
case (e.Op & fsnotify.Remove) > 0:
|
2019-01-12 04:58:27 +00:00
|
|
|
eventType = podDelete
|
2019-04-07 17:07:55 +00:00
|
|
|
case (e.Op & fsnotify.Rename) > 0:
|
2019-01-12 04:58:27 +00:00
|
|
|
eventType = podDelete
|
|
|
|
default:
|
|
|
|
// Ignore rest events
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.watchEvents <- &watchEvent{e.Name, eventType}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceFile) consumeWatchEvent(e *watchEvent) error {
|
|
|
|
switch e.eventType {
|
|
|
|
case podAdd, podModify:
|
2019-09-27 21:51:53 +00:00
|
|
|
pod, err := s.extractFromFile(e.fileName)
|
|
|
|
if err != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return fmt.Errorf("can't process config file %q: %v", e.fileName, err)
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
return s.store.Add(pod)
|
2019-01-12 04:58:27 +00:00
|
|
|
case podDelete:
|
|
|
|
if objKey, keyExist := s.fileKeyMapping[e.fileName]; keyExist {
|
|
|
|
pod, podExist, err := s.store.GetByKey(objKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !podExist {
|
|
|
|
return fmt.Errorf("the pod with key %s doesn't exist in cache", objKey)
|
|
|
|
} else {
|
|
|
|
if err = s.store.Delete(pod); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove deleted pod from cache: %v", err)
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
delete(s.fileKeyMapping, e.fileName)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|