Prechádzať zdrojové kódy

Move timeutils functions to the only places where they are used.

- Move time json marshaling to the jsonlog package: this is a docker
  internal hack that we should not promote as a library.
- Move Timestamp encoding/decoding functions to the API types: This is
  only used there. It could be a standalone library but I don't this
it's worth having a separated repo for this. It could introduce more
complexity than it solves.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 rokov pred
rodič
commit
27220ecc6b

+ 3 - 3
api/client/lib/events.go

@@ -6,8 +6,8 @@ import (
 	"time"
 
 	"github.com/docker/docker/api/types"
+	timetypes "github.com/docker/docker/api/types/time"
 	"github.com/docker/docker/pkg/parsers/filters"
-	"github.com/docker/docker/pkg/timeutils"
 )
 
 // Events returns a stream of events in the daemon in a ReadCloser.
@@ -17,14 +17,14 @@ func (cli *Client) Events(options types.EventsOptions) (io.ReadCloser, error) {
 	ref := time.Now()
 
 	if options.Since != "" {
-		ts, err := timeutils.GetTimestamp(options.Since, ref)
+		ts, err := timetypes.GetTimestamp(options.Since, ref)
 		if err != nil {
 			return nil, err
 		}
 		query.Set("since", ts)
 	}
 	if options.Until != "" {
-		ts, err := timeutils.GetTimestamp(options.Until, ref)
+		ts, err := timetypes.GetTimestamp(options.Until, ref)
 		if err != nil {
 			return nil, err
 		}

+ 2 - 2
api/client/lib/logs.go

@@ -6,7 +6,7 @@ import (
 	"time"
 
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/pkg/timeutils"
+	timetypes "github.com/docker/docker/api/types/time"
 )
 
 // ContainerLogs returns the logs generated by a container in an io.ReadCloser.
@@ -22,7 +22,7 @@ func (cli *Client) ContainerLogs(options types.ContainerLogsOptions) (io.ReadClo
 	}
 
 	if options.Since != "" {
-		ts, err := timeutils.GetTimestamp(options.Since, time.Now())
+		ts, err := timetypes.GetTimestamp(options.Since, time.Now())
 		if err != nil {
 			return nil, err
 		}

+ 2 - 2
api/server/router/container/container_routes.go

@@ -13,11 +13,11 @@ import (
 	"github.com/docker/distribution/registry/api/errcode"
 	"github.com/docker/docker/api/server/httputils"
 	"github.com/docker/docker/api/types"
+	timetypes "github.com/docker/docker/api/types/time"
 	"github.com/docker/docker/daemon"
 	derr "github.com/docker/docker/errors"
 	"github.com/docker/docker/pkg/ioutils"
 	"github.com/docker/docker/pkg/signal"
-	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/runconfig"
 	"github.com/docker/docker/utils"
 	"golang.org/x/net/context"
@@ -101,7 +101,7 @@ func (s *containerRouter) getContainersLogs(ctx context.Context, w http.Response
 
 	var since time.Time
 	if r.Form.Get("since") != "" {
-		s, n, err := timeutils.ParseTimestamps(r.Form.Get("since"), 0)
+		s, n, err := timetypes.ParseTimestamps(r.Form.Get("since"), 0)
 		if err != nil {
 			return err
 		}

+ 3 - 3
api/server/router/system/system_routes.go

@@ -9,10 +9,10 @@ import (
 	"github.com/docker/docker/api"
 	"github.com/docker/docker/api/server/httputils"
 	"github.com/docker/docker/api/types"
+	timetypes "github.com/docker/docker/api/types/time"
 	"github.com/docker/docker/pkg/ioutils"
 	"github.com/docker/docker/pkg/jsonmessage"
 	"github.com/docker/docker/pkg/parsers/filters"
-	"github.com/docker/docker/pkg/timeutils"
 	"golang.org/x/net/context"
 )
 
@@ -46,11 +46,11 @@ func (s *systemRouter) getEvents(ctx context.Context, w http.ResponseWriter, r *
 	if err := httputils.ParseForm(r); err != nil {
 		return err
 	}
-	since, sinceNano, err := timeutils.ParseTimestamps(r.Form.Get("since"), -1)
+	since, sinceNano, err := timetypes.ParseTimestamps(r.Form.Get("since"), -1)
 	if err != nil {
 		return err
 	}
-	until, untilNano, err := timeutils.ParseTimestamps(r.Form.Get("until"), -1)
+	until, untilNano, err := timetypes.ParseTimestamps(r.Form.Get("until"), -1)
 	if err != nil {
 		return err
 	}

+ 1 - 1
pkg/timeutils/utils.go → api/types/time/timestamp.go

@@ -1,4 +1,4 @@
-package timeutils
+package time
 
 import (
 	"fmt"

+ 1 - 1
pkg/timeutils/utils_test.go → api/types/time/timestamp_test.go

@@ -1,4 +1,4 @@
-package timeutils
+package time
 
 import (
 	"fmt"

+ 1 - 3
daemon/logger/jsonfilelog/jsonfilelog.go

@@ -14,7 +14,6 @@ import (
 	"github.com/docker/docker/daemon/logger"
 	"github.com/docker/docker/daemon/logger/loggerutils"
 	"github.com/docker/docker/pkg/jsonlog"
-	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/pkg/units"
 )
 
@@ -87,8 +86,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
 
 // Log converts logger.Message to jsonlog.JSONLog and serializes it to file.
 func (l *JSONFileLogger) Log(msg *logger.Message) error {
-
-	timestamp, err := timeutils.FastMarshalJSON(msg.Timestamp)
+	timestamp, err := jsonlog.FastTimeMarshalJSON(msg.Timestamp)
 	if err != nil {
 		return err
 	}

+ 2 - 2
daemon/logger/logger.go

@@ -11,7 +11,7 @@ import (
 	"errors"
 	"time"
 
-	"github.com/docker/docker/pkg/timeutils"
+	"github.com/docker/docker/pkg/jsonlog"
 )
 
 // ErrReadLogsNotSupported is returned when the logger does not support reading logs.
@@ -19,7 +19,7 @@ var ErrReadLogsNotSupported = errors.New("configured logging reader does not sup
 
 const (
 	// TimeFormat is the time format used for timestamps sent to log readers.
-	TimeFormat           = timeutils.RFC3339NanoFixed
+	TimeFormat           = jsonlog.RFC3339NanoFixed
 	logWatcherBufferSize = 4096
 )
 

+ 2 - 2
docker/daemon.go

@@ -20,11 +20,11 @@ import (
 	"github.com/docker/docker/daemon/logger"
 	"github.com/docker/docker/dockerversion"
 	"github.com/docker/docker/opts"
+	"github.com/docker/docker/pkg/jsonlog"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/pidfile"
 	"github.com/docker/docker/pkg/signal"
 	"github.com/docker/docker/pkg/system"
-	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/pkg/tlsconfig"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/utils"
@@ -150,7 +150,7 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
 		logrus.Warn("Running experimental build")
 	}
 
-	logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: timeutils.RFC3339NanoFixed})
+	logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: jsonlog.RFC3339NanoFixed})
 
 	if err := setDefaultUmask(); err != nil {
 		logrus.Fatalf("Failed to set umask: %v", err)

+ 2 - 2
integration-cli/docker_cli_logs_test.go

@@ -11,7 +11,7 @@ import (
 	"time"
 
 	"github.com/docker/docker/pkg/integration/checker"
-	"github.com/docker/docker/pkg/timeutils"
+	"github.com/docker/docker/pkg/jsonlog"
 	"github.com/go-check/check"
 )
 
@@ -75,7 +75,7 @@ func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
 
 	for _, l := range lines {
 		if l != "" {
-			_, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
+			_, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l))
 			c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
 			// ensure we have padded 0's
 			c.Assert(l[29], checker.Equals, uint8('Z'))

+ 2 - 6
pkg/jsonlog/jsonlog_marshalling.go

@@ -13,8 +13,6 @@
 //        "bytes"
 //-
 //        "unicode/utf8"
-//+
-//+       "github.com/docker/docker/pkg/timeutils"
 // )
 //
 // func (mj *JSONLog) MarshalJSON() ([]byte, error) {
@@ -43,7 +41,7 @@
 //        }
 //        buf.WriteString(`"time":`)
 //-       obj, err = mj.Created.MarshalJSON()
-//+       timestamp, err = timeutils.FastMarshalJSON(mj.Created)
+//+       timestamp, err = FastTimeMarshalJSON(mj.Created)
 //        if err != nil {
 //                return err
 //        }
@@ -69,8 +67,6 @@ package jsonlog
 import (
 	"bytes"
 	"unicode/utf8"
-
-	"github.com/docker/docker/pkg/timeutils"
 )
 
 // MarshalJSON marshals the JSONLog.
@@ -111,7 +107,7 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
 		buf.WriteString(`,`)
 	}
 	buf.WriteString(`"time":`)
-	timestamp, err = timeutils.FastMarshalJSON(mj.Created)
+	timestamp, err = FastTimeMarshalJSON(mj.Created)
 	if err != nil {
 		return err
 	}

+ 4 - 4
pkg/timeutils/json.go → pkg/jsonlog/time_marshalling.go

@@ -1,5 +1,5 @@
-// Package timeutils provides helper functions to parse and print time (time.Time).
-package timeutils
+// Package jsonlog provides helper functions to parse and print time (time.Time) as JSON.
+package jsonlog
 
 import (
 	"errors"
@@ -15,9 +15,9 @@ const (
 	JSONFormat = `"` + time.RFC3339Nano + `"`
 )
 
-// FastMarshalJSON avoids one of the extra allocations that
+// FastTimeMarshalJSON avoids one of the extra allocations that
 // time.MarshalJSON is making.
-func FastMarshalJSON(t time.Time) (string, error) {
+func FastTimeMarshalJSON(t time.Time) (string, error) {
 	if y := t.Year(); y < 0 || y >= 10000 {
 		// RFC 3339 is clear that years are 4 digits exactly.
 		// See golang.org/issue/4556#c15 for more discussion.

+ 9 - 9
pkg/timeutils/json_test.go → pkg/jsonlog/time_marshalling_test.go

@@ -1,4 +1,4 @@
-package timeutils
+package jsonlog
 
 import (
 	"testing"
@@ -6,23 +6,23 @@ import (
 )
 
 // Testing to ensure 'year' fields is between 0 and 9999
-func TestFastMarshalJSONWithInvalidDate(t *testing.T) {
+func TestFastTimeMarshalJSONWithInvalidDate(t *testing.T) {
 	aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
-	json, err := FastMarshalJSON(aTime)
+	json, err := FastTimeMarshalJSON(aTime)
 	if err == nil {
-		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
+		t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
 	}
 	anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
-	json, err = FastMarshalJSON(anotherTime)
+	json, err = FastTimeMarshalJSON(anotherTime)
 	if err == nil {
-		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
+		t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
 	}
 
 }
 
-func TestFastMarshalJSON(t *testing.T) {
+func TestFastTimeMarshalJSON(t *testing.T) {
 	aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
-	json, err := FastMarshalJSON(aTime)
+	json, err := FastTimeMarshalJSON(aTime)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -36,7 +36,7 @@ func TestFastMarshalJSON(t *testing.T) {
 		t.Fatal(err)
 	}
 	aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
-	json, err = FastMarshalJSON(aTime)
+	json, err = FastTimeMarshalJSON(aTime)
 	if err != nil {
 		t.Fatal(err)
 	}

+ 3 - 3
pkg/jsonmessage/jsonmessage.go

@@ -7,8 +7,8 @@ import (
 	"strings"
 	"time"
 
+	"github.com/docker/docker/pkg/jsonlog"
 	"github.com/docker/docker/pkg/term"
-	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/pkg/units"
 )
 
@@ -123,9 +123,9 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
 		return nil
 	}
 	if jm.TimeNano != 0 {
-		fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(timeutils.RFC3339NanoFixed))
+		fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(jsonlog.RFC3339NanoFixed))
 	} else if jm.Time != 0 {
-		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(timeutils.RFC3339NanoFixed))
+		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(jsonlog.RFC3339NanoFixed))
 	}
 	if jm.ID != "" {
 		fmt.Fprintf(out, "%s: ", jm.ID)

+ 7 - 7
pkg/jsonmessage/jsonmessage_test.go

@@ -7,8 +7,8 @@ import (
 	"testing"
 	"time"
 
+	"github.com/docker/docker/pkg/jsonlog"
 	"github.com/docker/docker/pkg/term"
-	"github.com/docker/docker/pkg/timeutils"
 )
 
 func TestError(t *testing.T) {
@@ -71,8 +71,8 @@ func TestJSONMessageDisplay(t *testing.T) {
 			From:   "From",
 			Status: "status",
 		}: {
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(timeutils.RFC3339NanoFixed)),
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(timeutils.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
 		},
 		// General, with nano precision time
 		JSONMessage{
@@ -81,8 +81,8 @@ func TestJSONMessageDisplay(t *testing.T) {
 			From:     "From",
 			Status:   "status",
 		}: {
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
 		},
 		// General, with both times Nano is preferred
 		JSONMessage{
@@ -92,8 +92,8 @@ func TestJSONMessageDisplay(t *testing.T) {
 			From:     "From",
 			Status:   "status",
 		}: {
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
-			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(timeutils.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
+			fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
 		},
 		// Stream over status
 		JSONMessage{