|
| 1 | +package containers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/coroot/coroot-node-agent/common" |
| 15 | + "github.com/coroot/coroot-node-agent/proc" |
| 16 | + "github.com/coroot/logparser" |
| 17 | + "inet.af/netaddr" |
| 18 | + "k8s.io/klog/v2" |
| 19 | +) |
| 20 | + |
| 21 | +const podmanTimeout = 30 * time.Second |
| 22 | + |
| 23 | +var podmanClient *http.Client |
| 24 | + |
| 25 | +func PodmanInit() error { |
| 26 | + sockets := []string{ |
| 27 | + "/run/podman/podman.sock", |
| 28 | + "/var/run/podman/podman.sock", |
| 29 | + } |
| 30 | + var podmanSocket string |
| 31 | + for _, socket := range sockets { |
| 32 | + socketHostPath := proc.HostPath(socket) |
| 33 | + if _, err := os.Stat(socketHostPath); err == nil { |
| 34 | + podmanSocket = socketHostPath |
| 35 | + break |
| 36 | + } |
| 37 | + } |
| 38 | + if podmanSocket == "" { |
| 39 | + return fmt.Errorf("podman socket not found in [%s]", strings.Join(sockets, ",")) |
| 40 | + } |
| 41 | + klog.Infoln("podman socket:", podmanSocket) |
| 42 | + |
| 43 | + podmanClient = &http.Client{ |
| 44 | + Transport: &http.Transport{ |
| 45 | + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 46 | + return net.DialTimeout("unix", podmanSocket, podmanTimeout) |
| 47 | + }, |
| 48 | + DisableCompression: true, |
| 49 | + }, |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +type podmanContainerInfo struct { |
| 55 | + Name string `json:"Name"` |
| 56 | + Image string `json:"ImageName"` |
| 57 | + Config struct { |
| 58 | + Labels map[string]string `json:"Labels"` |
| 59 | + Env []string `json:"Env"` |
| 60 | + } `json:"Config"` |
| 61 | + Mounts []struct { |
| 62 | + Source string `json:"Source"` |
| 63 | + Destination string `json:"Destination"` |
| 64 | + } `json:"Mounts"` |
| 65 | + HostConfig struct { |
| 66 | + LogConfig struct { |
| 67 | + Type string `json:"Type"` |
| 68 | + } `json:"LogConfig"` |
| 69 | + } `json:"HostConfig"` |
| 70 | + NetworkSettings struct { |
| 71 | + Ports map[string][]struct { |
| 72 | + HostIP string `json:"HostIp"` |
| 73 | + HostPort string `json:"HostPort"` |
| 74 | + } `json:"Ports"` |
| 75 | + } `json:"NetworkSettings"` |
| 76 | + LogPath string `json:"LogPath"` |
| 77 | +} |
| 78 | + |
| 79 | +func PodmanInspect(containerID string) (*ContainerMetadata, error) { |
| 80 | + if podmanClient == nil { |
| 81 | + return nil, fmt.Errorf("podman client not initialized") |
| 82 | + } |
| 83 | + resp, err := podmanClient.Get("http://localhost/v4.0.0/libpod/containers/" + containerID + "/json") |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + defer resp.Body.Close() |
| 88 | + |
| 89 | + if resp.StatusCode != http.StatusOK { |
| 90 | + return nil, errors.New(resp.Status) |
| 91 | + } |
| 92 | + |
| 93 | + i := &podmanContainerInfo{} |
| 94 | + if err = json.NewDecoder(resp.Body).Decode(i); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + res := &ContainerMetadata{ |
| 99 | + name: strings.TrimPrefix(i.Name, "/"), |
| 100 | + image: i.Image, |
| 101 | + labels: i.Config.Labels, |
| 102 | + volumes: map[string]string{}, |
| 103 | + hostListens: map[string][]netaddr.IPPort{}, |
| 104 | + networks: map[string]ContainerNetwork{}, |
| 105 | + env: map[string]string{}, |
| 106 | + } |
| 107 | + if res.labels == nil { |
| 108 | + res.labels = map[string]string{} |
| 109 | + } |
| 110 | + |
| 111 | + for _, m := range i.Mounts { |
| 112 | + res.volumes[m.Destination] = common.ParseKubernetesVolumeSource(m.Source) |
| 113 | + } |
| 114 | + |
| 115 | + for _, value := range i.Config.Env { |
| 116 | + idx := strings.Index(value, "=") |
| 117 | + if idx < 0 { |
| 118 | + continue |
| 119 | + } |
| 120 | + res.env[value[:idx]] = value[idx+1:] |
| 121 | + } |
| 122 | + |
| 123 | + if i.NetworkSettings.Ports != nil { |
| 124 | + addrs := map[netaddr.IPPort]struct{}{} |
| 125 | + for port, bindings := range i.NetworkSettings.Ports { |
| 126 | + if !strings.HasSuffix(port, "/tcp") { |
| 127 | + continue |
| 128 | + } |
| 129 | + for _, b := range bindings { |
| 130 | + if ipp, err := netaddr.ParseIPPort(b.HostIP + ":" + b.HostPort); err == nil { |
| 131 | + addrs[ipp] = struct{}{} |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + if len(addrs) > 0 { |
| 136 | + s := make([]netaddr.IPPort, 0, len(addrs)) |
| 137 | + for addr := range addrs { |
| 138 | + if common.PortFilter.ShouldBeSkipped(addr.Port()) { |
| 139 | + continue |
| 140 | + } |
| 141 | + s = append(s, addr) |
| 142 | + } |
| 143 | + res.hostListens["podman"] = s |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + switch i.HostConfig.LogConfig.Type { |
| 148 | + case "json-file", "k8s-file": |
| 149 | + if i.LogPath != "" { |
| 150 | + res.logPath = i.LogPath |
| 151 | + res.logDecoder = logparser.DockerJsonDecoder{} |
| 152 | + } |
| 153 | + default: |
| 154 | + // journald is the Podman default log driver. |
| 155 | + // Store the unit name so runLogParser can subscribe via journald. |
| 156 | + res.podmanJournaldUnit = "libpod-" + containerID + ".scope" |
| 157 | + } |
| 158 | + |
| 159 | + return res, nil |
| 160 | +} |
0 commit comments