72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
|
|
// Command flock-installer is the DaemonSet init container. It copies
|
||
|
|
// /usr/local/bin/flock to /opt/cni/bin/flock on the host, and writes the
|
||
|
|
// CNI conflist to /etc/cni/net.d/01-flock.conflist. The init container
|
||
|
|
// then exits; kubelet picks up the conflist on its next CNI scan.
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"flag"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"log"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
)
|
||
|
|
|
||
|
|
const conflistTemplate = `{
|
||
|
|
"cniVersion": "1.0.0",
|
||
|
|
"name": "flock",
|
||
|
|
"plugins": [
|
||
|
|
{
|
||
|
|
"type": "flock"
|
||
|
|
}
|
||
|
|
]
|
||
|
|
}
|
||
|
|
`
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
var (
|
||
|
|
src = flag.String("src", "/usr/local/bin/flock", "source binary path")
|
||
|
|
binDst = flag.String("bin", "/host/opt/cni/bin/flock", "destination CNI binary path")
|
||
|
|
confDst = flag.String("conflist", "/host/etc/cni/net.d/01-flock.conflist", "destination conflist path")
|
||
|
|
)
|
||
|
|
flag.Parse()
|
||
|
|
|
||
|
|
if err := copyFile(*src, *binDst, 0o755); err != nil {
|
||
|
|
log.Fatalf("install binary: %v", err)
|
||
|
|
}
|
||
|
|
if err := os.MkdirAll(filepath.Dir(*confDst), 0o755); err != nil {
|
||
|
|
log.Fatalf("conflist mkdir: %v", err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(*confDst, []byte(conflistTemplate), 0o644); err != nil {
|
||
|
|
log.Fatalf("write conflist: %v", err)
|
||
|
|
}
|
||
|
|
fmt.Printf("flock-installer: installed %s and %s\n", *binDst, *confDst)
|
||
|
|
}
|
||
|
|
|
||
|
|
func copyFile(src, dst string, mode os.FileMode) error {
|
||
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
in, err := os.Open(src)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
defer in.Close()
|
||
|
|
tmp := dst + ".tmp"
|
||
|
|
out, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if _, err := io.Copy(out, in); err != nil {
|
||
|
|
out.Close()
|
||
|
|
os.Remove(tmp)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err := out.Close(); err != nil {
|
||
|
|
os.Remove(tmp)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return os.Rename(tmp, dst)
|
||
|
|
}
|