|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "io/fs" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "k8s.io/apimachinery/pkg/util/sets" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + var bundleDir string |
| 21 | + var opConVersion bool |
| 22 | + |
| 23 | + skipRootPaths := sets.NewString( |
| 24 | + "/dev", |
| 25 | + "/etc", |
| 26 | + "/proc", |
| 27 | + "/product_name", |
| 28 | + "/product_uuid", |
| 29 | + "/sys", |
| 30 | + "/bin", |
| 31 | + ) |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "unpack", |
| 34 | + Args: cobra.ExactArgs(0), |
| 35 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 36 | + if opConVersion { |
| 37 | + // TODO |
| 38 | + //fmt.Println(version.String()) |
| 39 | + os.Exit(0) |
| 40 | + } |
| 41 | + var err error |
| 42 | + bundleDir, err = filepath.Abs(bundleDir) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("get absolute path of bundle directory %q: %v", bundleDir, err) |
| 45 | + } |
| 46 | + |
| 47 | + bundleFS := os.DirFS(bundleDir) |
| 48 | + buf := &bytes.Buffer{} |
| 49 | + gzw := gzip.NewWriter(buf) |
| 50 | + tw := tar.NewWriter(gzw) |
| 51 | + if err := fs.WalkDir(bundleFS, ".", func(path string, d fs.DirEntry, err error) error { |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + if d.Type()&os.ModeSymlink != 0 { |
| 57 | + return nil |
| 58 | + } |
| 59 | + if bundleDir == "/" { |
| 60 | + // If bundleDir is the filesystem root, skip some known unrelated directories |
| 61 | + fullPath := filepath.Join(bundleDir, path) |
| 62 | + if skipRootPaths.Has(fullPath) { |
| 63 | + return filepath.SkipDir |
| 64 | + } |
| 65 | + } |
| 66 | + info, err := d.Info() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("get file info for %q: %v", path, err) |
| 69 | + } |
| 70 | + |
| 71 | + h, err := tar.FileInfoHeader(info, "") |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("build tar file info header for %q: %v", path, err) |
| 74 | + } |
| 75 | + h.Uid = 0 |
| 76 | + h.Gid = 0 |
| 77 | + h.Uname = "" |
| 78 | + h.Gname = "" |
| 79 | + h.Name = path |
| 80 | + |
| 81 | + if err := tw.WriteHeader(h); err != nil { |
| 82 | + return fmt.Errorf("write tar header for %q: %v", path, err) |
| 83 | + } |
| 84 | + if d.IsDir() { |
| 85 | + return nil |
| 86 | + } |
| 87 | + f, err := bundleFS.Open(path) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("open file %q: %v", path, err) |
| 90 | + } |
| 91 | + if _, err := io.Copy(tw, f); err != nil { |
| 92 | + return fmt.Errorf("write tar data for %q: %v", path, err) |
| 93 | + } |
| 94 | + return nil |
| 95 | + }); err != nil { |
| 96 | + log.Fatalf("generate tar.gz for bundle dir %q: %v", bundleDir, err) |
| 97 | + } |
| 98 | + if err := tw.Close(); err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | + if err := gzw.Close(); err != nil { |
| 102 | + log.Fatal(err) |
| 103 | + } |
| 104 | + |
| 105 | + bundleMap := map[string]interface{}{ |
| 106 | + "content": buf.Bytes(), |
| 107 | + } |
| 108 | + enc := json.NewEncoder(os.Stdout) |
| 109 | + if err := enc.Encode(bundleMap); err != nil { |
| 110 | + log.Fatalf("encode bundle map as JSON: %v", err) |
| 111 | + } |
| 112 | + return nil |
| 113 | + }, |
| 114 | + } |
| 115 | + cmd.Flags().StringVar(&bundleDir, "bundle-dir", "", "directory in which the bundle can be found") |
| 116 | + cmd.Flags().BoolVar(&opConVersion, "version", false, "displays operator-controller version information") |
| 117 | + |
| 118 | + if err := cmd.Execute(); err != nil { |
| 119 | + log.Fatal(err) |
| 120 | + } |
| 121 | +} |
0 commit comments