瀏覽代碼

api: info: don't use ad-hoc type for compatibility with old api versions

- Add the field as a "deprecated" field in the API type.
- Don't error when failing to parse the options, but produce a warning
  instead, because the client won't be able to fix issues in the daemon
  configuration. This was unlikely to happen, as the daemon probably
  would fail to start with an invalid config, but just in case.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 年之前
父節點
當前提交
94c975e25a
共有 2 個文件被更改,包括 14 次插入15 次删除
  1. 7 15
      api/server/router/system/system_routes.go
  2. 7 0
      api/types/types.go

+ 7 - 15
api/server/router/system/system_routes.go

@@ -67,24 +67,16 @@ func (s *systemRouter) getInfo(ctx context.Context, w http.ResponseWriter, r *ht
 	version := httputils.VersionFromContext(ctx)
 	if versions.LessThan(version, "1.25") {
 		// TODO: handle this conversion in engine-api
-		type oldInfo struct {
-			*types.Info
-			ExecutionDriver string
-		}
-		old := &oldInfo{
-			Info:            info,
-			ExecutionDriver: "<not supported>",
-		}
-		nameOnlySecurityOptions := []string{}
-		kvSecOpts, err := types.DecodeSecurityOptions(old.SecurityOptions)
+		kvSecOpts, err := types.DecodeSecurityOptions(info.SecurityOptions)
 		if err != nil {
-			return err
+			info.Warnings = append(info.Warnings, err.Error())
 		}
-		for _, s := range kvSecOpts {
-			nameOnlySecurityOptions = append(nameOnlySecurityOptions, s.Name)
+		var nameOnly []string
+		for _, so := range kvSecOpts {
+			nameOnly = append(nameOnly, so.Name)
 		}
-		old.SecurityOptions = nameOnlySecurityOptions
-		return httputils.WriteJSON(w, http.StatusOK, old)
+		info.SecurityOptions = nameOnly
+		info.ExecutionDriver = "<not supported>" //nolint:staticcheck // ignore SA1019 (ExecutionDriver is deprecated)
 	}
 	if versions.LessThan(version, "1.39") {
 		if info.KernelVersion == "" {

+ 7 - 0
api/types/types.go

@@ -307,6 +307,9 @@ type Info struct {
 	ProductLicense      string               `json:",omitempty"`
 	DefaultAddressPools []NetworkAddressPool `json:",omitempty"`
 
+	// Legacy API fields for older API versions.
+	legacyFields
+
 	// Warnings contains a slice of warnings that occurred  while collecting
 	// system information. These warnings are intended to be informational
 	// messages for the user, and are not intended to be parsed / used for
@@ -314,6 +317,10 @@ type Info struct {
 	Warnings []string
 }
 
+type legacyFields struct {
+	ExecutionDriver string `json:",omitempty"` // Deprecated: deprecated since API v1.25, but returned for older versions.
+}
+
 // KeyValue holds a key/value pair
 type KeyValue struct {
 	Key, Value string