file_linux.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // +build linux
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. // Reads the pod configuration from file or a directory of files.
  15. package config
  16. import (
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. "github.com/fsnotify/fsnotify"
  23. "k8s.io/klog"
  24. "k8s.io/api/core/v1"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. "k8s.io/client-go/util/flowcontrol"
  27. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  28. )
  29. const (
  30. retryPeriod = 1 * time.Second
  31. maxRetryPeriod = 20 * time.Second
  32. )
  33. type retryableError struct {
  34. message string
  35. }
  36. func (e *retryableError) Error() string {
  37. return e.message
  38. }
  39. func (s *sourceFile) startWatch() {
  40. backOff := flowcontrol.NewBackOff(retryPeriod, maxRetryPeriod)
  41. backOffId := "watch"
  42. go wait.Forever(func() {
  43. if backOff.IsInBackOffSinceUpdate(backOffId, time.Now()) {
  44. return
  45. }
  46. if err := s.doWatch(); err != nil {
  47. klog.Errorf("Unable to read config path %q: %v", s.path, err)
  48. if _, retryable := err.(*retryableError); !retryable {
  49. backOff.Next(backOffId, time.Now())
  50. }
  51. }
  52. }, retryPeriod)
  53. }
  54. func (s *sourceFile) doWatch() error {
  55. _, err := os.Stat(s.path)
  56. if err != nil {
  57. if !os.IsNotExist(err) {
  58. return err
  59. }
  60. // Emit an update with an empty PodList to allow FileSource to be marked as seen
  61. s.updates <- kubetypes.PodUpdate{Pods: []*v1.Pod{}, Op: kubetypes.SET, Source: kubetypes.FileSource}
  62. return &retryableError{"path does not exist, ignoring"}
  63. }
  64. w, err := fsnotify.NewWatcher()
  65. if err != nil {
  66. return fmt.Errorf("unable to create inotify: %v", err)
  67. }
  68. defer w.Close()
  69. err = w.Add(s.path)
  70. if err != nil {
  71. return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
  72. }
  73. for {
  74. select {
  75. case event := <-w.Events:
  76. if err = s.produceWatchEvent(&event); err != nil {
  77. return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
  78. }
  79. case err = <-w.Errors:
  80. return fmt.Errorf("error while watching %q: %v", s.path, err)
  81. }
  82. }
  83. }
  84. func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
  85. // Ignore file start with dots
  86. if strings.HasPrefix(filepath.Base(e.Name), ".") {
  87. klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
  88. return nil
  89. }
  90. var eventType podEventType
  91. switch {
  92. case (e.Op & fsnotify.Create) > 0:
  93. eventType = podAdd
  94. case (e.Op & fsnotify.Write) > 0:
  95. eventType = podModify
  96. case (e.Op & fsnotify.Chmod) > 0:
  97. eventType = podModify
  98. case (e.Op & fsnotify.Remove) > 0:
  99. eventType = podDelete
  100. case (e.Op & fsnotify.Rename) > 0:
  101. eventType = podDelete
  102. default:
  103. // Ignore rest events
  104. return nil
  105. }
  106. s.watchEvents <- &watchEvent{e.Name, eventType}
  107. return nil
  108. }
  109. func (s *sourceFile) consumeWatchEvent(e *watchEvent) error {
  110. switch e.eventType {
  111. case podAdd, podModify:
  112. if pod, err := s.extractFromFile(e.fileName); err != nil {
  113. return fmt.Errorf("can't process config file %q: %v", e.fileName, err)
  114. } else {
  115. return s.store.Add(pod)
  116. }
  117. case podDelete:
  118. if objKey, keyExist := s.fileKeyMapping[e.fileName]; keyExist {
  119. pod, podExist, err := s.store.GetByKey(objKey)
  120. if err != nil {
  121. return err
  122. } else if !podExist {
  123. return fmt.Errorf("the pod with key %s doesn't exist in cache", objKey)
  124. } else {
  125. if err = s.store.Delete(pod); err != nil {
  126. return fmt.Errorf("failed to remove deleted pod from cache: %v", err)
  127. } else {
  128. delete(s.fileKeyMapping, e.fileName)
  129. }
  130. }
  131. }
  132. }
  133. return nil
  134. }