Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ping api instead of system info for fingerprinting #186

Merged
merged 3 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

IMPROVEMENTS:

* perf: Use ping api instead of system info for fingerprinting [[GH-186](https://github.com/hashicorp/nomad-driver-podman/pull/186)]
* runtime: Prevent concurrent image pulls of same imageRef [[GH-159](https://github.com/hashicorp/nomad-driver-podman/pull/159)]

## 0.4.0 (July 14, 2022)
Expand Down
28 changes: 28 additions & 0 deletions api/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"context"
"fmt"
"net/http"
)

// Ping podman service
// Return lipod api version
func (c *API) Ping(ctx context.Context) (string, error) {

res, err := c.Get(ctx, "/libpod/_ping")
if err != nil {
return "", err
}

defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("cannot ping Podman api, status code: %d", res.StatusCode)
}
version := res.Header.Get("Libpod-API-Version")
if version == "" {
return "", fmt.Errorf("Unable to get libpod api version from response header")
}
return version, nil
}
64 changes: 40 additions & 24 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,54 @@ func (d *Driver) handleFingerprint(ctx context.Context, ch chan<- *drivers.Finge
}

func (d *Driver) buildFingerprint() *drivers.Fingerprint {
var health drivers.HealthState
var desc string
attrs := map[string]*pstructs.Attribute{}

// be negative and guess that we will not be able to get a podman connection
health = drivers.HealthStateUndetected
desc = "disabled"

// try to connect and get version info
info, err := d.podman.SystemInfo(d.ctx)
if err != nil {
d.logger.Error("Could not get podman info", "error", err)
// Ping podman api
version, err := d.podman.Ping(d.ctx)
if err != nil || version == "" {
// not reachable?
// deactivate driver, forget podman details
d.systemInfo = api.Info{}
d.logger.Error("Could not get podman version", "error", err)
return &drivers.Fingerprint{
Attributes: attrs,
Health: drivers.HealthStateUndetected,
HealthDescription: "disabled",
}
} else {
// yay! we can enable the driver
health = drivers.HealthStateHealthy
desc = "ready"
attrs["driver.podman"] = pstructs.NewBoolAttribute(true)
attrs["driver.podman.version"] = pstructs.NewStringAttribute(info.Version.Version)
attrs["driver.podman.rootless"] = pstructs.NewBoolAttribute(info.Host.Security.Rootless)
attrs["driver.podman.cgroupVersion"] = pstructs.NewStringAttribute(info.Host.CGroupsVersion)
if d.systemInfo.Version.Version == "" {
// keep first received systemInfo in driver struct
// it is used to toggle cgroup v1/v2, rootless/rootful behavior
d.systemInfo = info
d.cgroupV2 = info.Host.CGroupsVersion == "v2"

// it's reachable

// do we already know details about podman or is the version different?
if d.systemInfo.Version.APIVersion != version {
// no? then fetch and cache it
// try to connect and get version info
info, err := d.podman.SystemInfo(d.ctx)
if err != nil {
d.logger.Error("Could not get podman info", "error", err)
return &drivers.Fingerprint{
Attributes: attrs,
Health: drivers.HealthStateUndetected,
HealthDescription: "disabled",
}
} else {
// keep first received systemInfo in driver struct
// it is used to toggle cgroup v1/v2, rootless/rootful behavior
d.systemInfo = info
d.cgroupV2 = info.Host.CGroupsVersion == "v2"
}
}

attrs["driver.podman"] = pstructs.NewBoolAttribute(true)
attrs["driver.podman.version"] = pstructs.NewStringAttribute(version)
attrs["driver.podman.rootless"] = pstructs.NewBoolAttribute(d.systemInfo.Host.Security.Rootless)
attrs["driver.podman.cgroupVersion"] = pstructs.NewStringAttribute(d.systemInfo.Host.CGroupsVersion)
}

return &drivers.Fingerprint{
Attributes: attrs,
Health: health,
HealthDescription: desc,
Health: drivers.HealthStateHealthy,
HealthDescription: "ready",
}
}

Expand Down
7 changes: 7 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func podmanDriverHarness(t *testing.T, cfg map[string]interface{}) *dtestutil.Dr
return harness
}

func TestPodmanDriver_PingPodman(t *testing.T) {
d := podmanDriverHarness(t, nil)
version, err := getPodmanDriver(t, d).podman.Ping(context.Background())
require.NoError(t, err)
require.NotEmpty(t, version)
}

func TestPodmanDriver_Start_NoImage(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
Expand Down