Skip to content

Instantly share code, notes, and snippets.

@wklken
Created June 5, 2018 14:31
Show Gist options
  • Save wklken/145c8d70389c3f11381a1771623a3ba8 to your computer and use it in GitHub Desktop.
Save wklken/145c8d70389c3f11381a1771623a3ba8 to your computer and use it in GitHub Desktop.
" mkdir -p $HOME/.vim/swapfiles && mkdir -p $HOME/.vim/backupfiles
set directory=$HOME/.vim/swapfiles/
set backupdir=$HOME/.vim/backupfiles/

this is part of kubelet static pod file watch logic

just want to know why the manifests has been changed but the kubelet did not retart the container

./watchdir $PWD/manifests

# vim with swap and backup files

Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swx, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swx, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=512) and type=%!s(uint32=512) podDelete 1
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=64) and type=%!s(uint32=64) podDelete 2
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml~ mask=%!s(uint32=128) and type=%!s(uint32=128) podAdd 2
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=2) and type=%!s(uint32=2) podModify 1
eventType=%!s(main.podEventType=1)
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml~ mask=%!s(uint32=512) and type=%!s(uint32=512) podDelete 1
eventType=%!s(main.podEventType=2)
Ignored pod manifest: /etc/kubernetes/manifests/.test.yaml.swp, because it starts with dots

# vim with not swap and backup files


"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=512) and type=%!s(uint32=512) podDelete 1
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=64) and type=%!s(uint32=64) podDelete 2
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml~ mask=%!s(uint32=128) and type=%!s(uint32=128) podAdd 2
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=2) and type=%!s(uint32=2) podModify 1
eventType=%!s(main.podEventType=1)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml~ mask=%!s(uint32=512) and type=%!s(uint32=512) podDelete 1
eventType=%!s(main.podEventType=2)

# vim with not swap and no backup files


"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/4913 mask=%!s(uint32=512) and type=%!s(uint32=512) podDelete 1
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=64) and type=%!s(uint32=64) podDelete 2
eventType=%!s(main.podEventType=2)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=256) and type=%!s(uint32=256) podAdd 1
eventType=%!s(main.podEventType=0)
"/etc/kubernetes/manifests" /etc/kubernetes/manifests/test.yaml mask=%!s(uint32=2) and type=%!s(uint32=2) podModify 1
eventType=%!s(main.podEventType=1)
build:
GOOS=linux GOARCH=amd64 go build -o watchdir watchdir.go
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"golang.org/x/exp/inotify"
)
type podEventType int
const (
podAdd podEventType = iota
podModify
podDelete
)
func watch(path string) error {
w, err := inotify.NewWatcher()
if err != nil {
return fmt.Errorf("unable to create inotify: %v", err)
}
defer w.Close()
err = w.AddWatch(path, inotify.IN_DELETE_SELF|inotify.IN_CREATE|inotify.IN_MOVED_TO|inotify.IN_MODIFY|inotify.IN_MOVED_FROM|inotify.IN_DELETE)
if err != nil {
return fmt.Errorf("unable to create inotify for path %q: %v", path, err)
}
for {
select {
case event := <-w.Event:
err = processEvent(path, event)
if err != nil {
return fmt.Errorf("error while processing event (%+v): %v", event, err)
}
case err = <-w.Error:
return fmt.Errorf("error while watching %q: %v", path, err)
}
}
}
func processEvent(path string, e *inotify.Event) error {
// Ignore file start with dots
if strings.HasPrefix(filepath.Base(e.Name), ".") {
fmt.Printf("Ignored pod manifest: %s, because it starts with dots\n", e.Name)
return nil
}
var eventType podEventType
switch {
case (e.Mask & inotify.IN_ISDIR) > 0:
fmt.Printf("Not recursing into manifest path %q %s\n", path, e.Name)
return nil
case (e.Mask & inotify.IN_CREATE) > 0:
eventType = podAdd
fmt.Printf("%q %s mask=%s and type=%s podAdd 1\n", path, e.Name, e.Mask, inotify.IN_CREATE)
case (e.Mask & inotify.IN_MOVED_TO) > 0:
eventType = podAdd
fmt.Printf("%q %s mask=%s and type=%s podAdd 2\n", path, e.Name, e.Mask, inotify.IN_MOVED_TO)
case (e.Mask & inotify.IN_MODIFY) > 0:
eventType = podModify
fmt.Printf("%q %s mask=%s and type=%s podModify 1\n", path, e.Name, e.Mask, inotify.IN_MODIFY)
case (e.Mask & inotify.IN_DELETE) > 0:
eventType = podDelete
fmt.Printf("%q %s mask=%s and type=%s podDelete 1\n", path, e.Name, e.Mask, inotify.IN_DELETE)
case (e.Mask & inotify.IN_MOVED_FROM) > 0:
eventType = podDelete
fmt.Printf("%q %s mask=%s and type=%s podDelete 2\n", path, e.Name, e.Mask, inotify.IN_MOVED_FROM)
case (e.Mask & inotify.IN_DELETE_SELF) > 0:
return fmt.Errorf("the watched path is deleted")
default:
// Ignore rest events
return fmt.Errorf("ignore, return nil")
return nil
}
fmt.Printf("eventType=%s\n", eventType)
// switch eventType {
// case podAdd, podModify:
// if pod, err := s.extractFromFile(e.Name); err != nil {
// fmt.Printf("Can't process manifest file %q: %v", e.Name, err)
// } else {
// return s.store.Add(pod)
// }
// case podDelete:
// if objKey, keyExist := s.fileKeyMapping[e.Name]; 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 {
// return s.store.Delete(pod)
// }
// }
// }
return nil
}
func main() {
path := os.Args[1]
watch(path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment