Use version package to track all versions

* Server, bindings, and CLI all now pull version information from version
package.
* Current /libpod API version slaved to podman/libpod Version
* Bindings validate against libpod API Minimal version
* Remove pkg/bindings/bindings.go and updated tests

Fixes: #9207

Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
Jhon Honce
2021-03-03 10:47:02 -07:00
parent 87e20560ac
commit f86d641308
17 changed files with 127 additions and 154 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/pkg/api/handlers/utils"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/version"
docker "github.com/docker/docker/api/types"
"github.com/pkg/errors"
)
@ -35,20 +36,20 @@ func VersionHandler(w http.ResponseWriter, r *http.Request) {
Name: "Podman Engine",
Version: versionInfo.Version,
Details: map[string]string{
"APIVersion": utils.APIVersion[utils.LibpodTree][utils.CurrentAPIVersion].String(),
"APIVersion": version.APIVersion[version.Libpod][version.CurrentAPI].String(),
"Arch": goRuntime.GOARCH,
"BuildTime": time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
"Experimental": "true",
"GitCommit": versionInfo.GitCommit,
"GoVersion": versionInfo.GoVersion,
"KernelVersion": infoData.Host.Kernel,
"MinAPIVersion": utils.APIVersion[utils.LibpodTree][utils.MinimalAPIVersion].String(),
"MinAPIVersion": version.APIVersion[version.Libpod][version.MinimalAPI].String(),
"Os": goRuntime.GOOS,
},
}}
apiVersion := utils.APIVersion[utils.CompatTree][utils.CurrentAPIVersion]
minVersion := utils.APIVersion[utils.CompatTree][utils.MinimalAPIVersion]
apiVersion := version.APIVersion[version.Compat][version.CurrentAPI]
minVersion := version.APIVersion[version.Compat][version.MinimalAPI]
utils.WriteResponse(w, http.StatusOK, entities.ComponentVersion{
Version: docker.Version{

View File

@ -10,49 +10,14 @@ import (
"unsafe"
"github.com/blang/semver"
"github.com/containers/podman/v3/version"
"github.com/gorilla/mux"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type (
// VersionTree determines which API endpoint tree for version
VersionTree int
// VersionLevel determines which API level, current or something from the past
VersionLevel int
)
const (
// LibpodTree supports Libpod endpoints
LibpodTree = VersionTree(iota)
// CompatTree supports Libpod endpoints
CompatTree
// CurrentAPIVersion announces what is the current API level
CurrentAPIVersion = VersionLevel(iota)
// MinimalAPIVersion announces what is the oldest API level supported
MinimalAPIVersion
)
var (
// See https://docs.docker.com/engine/api/v1.40/
// libpod compat handlers are expected to honor docker API versions
// APIVersion provides the current and minimal API versions for compat and libpod endpoint trees
// Note: GET|HEAD /_ping is never versioned and provides the API-Version and Libpod-API-Version headers to allow
// clients to shop for the Version they wish to support
APIVersion = map[VersionTree]map[VersionLevel]semver.Version{
LibpodTree: {
CurrentAPIVersion: semver.MustParse("3.0.0"),
MinimalAPIVersion: semver.MustParse("3.0.0"),
},
CompatTree: {
CurrentAPIVersion: semver.MustParse("1.40.0"),
MinimalAPIVersion: semver.MustParse("1.24.0"),
},
}
// ErrVersionNotGiven returned when version not given by client
ErrVersionNotGiven = errors.New("version not given in URL path")
// ErrVersionNotSupported returned when given version is too old
@ -98,14 +63,14 @@ func SupportedVersion(r *http.Request, condition string) (semver.Version, error)
// SupportedVersionWithDefaults validates that the version provided by client valid is supported by server
// minimal API version <= client path version <= maximum API version focused on the endpoint tree from URL
func SupportedVersionWithDefaults(r *http.Request) (semver.Version, error) {
tree := CompatTree
tree := version.Compat
if IsLibpodRequest(r) {
tree = LibpodTree
tree = version.Libpod
}
return SupportedVersion(r,
fmt.Sprintf(">=%s <=%s", APIVersion[tree][MinimalAPIVersion].String(),
APIVersion[tree][CurrentAPIVersion].String()))
fmt.Sprintf(">=%s <=%s", version.APIVersion[tree][version.MinimalAPI].String(),
version.APIVersion[tree][version.CurrentAPI].String()))
}
// WriteResponse encodes the given value as JSON or string and renders it for http client

View File

@ -7,17 +7,18 @@ import (
"net/http/httptest"
"testing"
"github.com/containers/podman/v3/version"
"github.com/gorilla/mux"
)
func TestSupportedVersion(t *testing.T) {
req, err := http.NewRequest("GET",
fmt.Sprintf("/v%s/libpod/testing/versions", APIVersion[LibpodTree][CurrentAPIVersion]),
fmt.Sprintf("/v%s/libpod/testing/versions", version.APIVersion[version.Libpod][version.CurrentAPI]),
nil)
if err != nil {
t.Fatal(err)
}
req = mux.SetURLVars(req, map[string]string{"version": APIVersion[LibpodTree][CurrentAPIVersion].String()})
req = mux.SetURLVars(req, map[string]string{"version": version.APIVersion[version.Libpod][version.CurrentAPI].String()})
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@ -8,6 +8,7 @@ import (
"github.com/containers/podman/v3/pkg/api/handlers/utils"
"github.com/containers/podman/v3/pkg/auth"
"github.com/containers/podman/v3/version"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
@ -55,10 +56,10 @@ func (s *APIServer) APIHandler(h http.HandlerFunc) http.HandlerFunc {
c = context.WithValue(c, "idletracker", s.idleTracker) // nolint
r = r.WithContext(c)
cv := utils.APIVersion[utils.CompatTree][utils.CurrentAPIVersion]
cv := version.APIVersion[version.Compat][version.CurrentAPI]
w.Header().Set("API-Version", fmt.Sprintf("%d.%d", cv.Major, cv.Minor))
lv := utils.APIVersion[utils.LibpodTree][utils.CurrentAPIVersion].String()
lv := version.APIVersion[version.Libpod][version.CurrentAPI].String()
w.Header().Set("Libpod-API-Version", lv)
w.Header().Set("Server", "Libpod/"+lv+" ("+runtime.GOOS+")")
@ -72,5 +73,5 @@ func (s *APIServer) APIHandler(h http.HandlerFunc) http.HandlerFunc {
// VersionedPath prepends the version parsing code
// any handler may override this default when registering URL(s)
func VersionedPath(p string) string {
return "/v{version:[0-9][0-9.]*}" + p
return "/v{version:[0-9][0-9A-Za-z.-]*}" + p
}