Skip to content

Commit dfb626d

Browse files
committed
Use protobuf Timestamp type instead of uint64
This will ensure nanoseconds are taken in account. Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent 3dfa04b commit dfb626d

File tree

16 files changed

+1210
-173
lines changed

16 files changed

+1210
-173
lines changed

api/grpc/server/server.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/docker/containerd/api/grpc/types"
1818
"github.com/docker/containerd/runtime"
1919
"github.com/docker/containerd/supervisor"
20+
"github.com/golang/protobuf/ptypes"
2021
"golang.org/x/net/context"
2122
)
2223

@@ -310,16 +311,24 @@ func (s *apiServer) UpdateProcess(ctx context.Context, r *types.UpdateProcessReq
310311

311312
func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer) error {
312313
t := time.Time{}
313-
if r.Timestamp != 0 {
314-
t = time.Unix(int64(r.Timestamp), 0)
314+
if r.Timestamp != nil {
315+
from, err := ptypes.Timestamp(r.Timestamp)
316+
if err != nil {
317+
return err
318+
}
319+
t = from
315320
}
316321
events := s.sv.Events(t)
317322
defer s.sv.Unsubscribe(events)
318323
for e := range events {
324+
tsp, err := ptypes.TimestampProto(e.Timestamp)
325+
if err != nil {
326+
return err
327+
}
319328
if err := stream.Send(&types.Event{
320329
Id: e.ID,
321330
Type: e.Type,
322-
Timestamp: uint64(e.Timestamp.Unix()),
331+
Timestamp: tsp,
323332
Pid: e.PID,
324333
Status: uint32(e.Status),
325334
}); err != nil {
@@ -330,8 +339,9 @@ func (s *apiServer) Events(r *types.EventsRequest, stream types.API_EventsServer
330339
}
331340

332341
func convertToPb(st *runtime.Stat) *types.StatsResponse {
342+
tsp, _ := ptypes.TimestampProto(st.Timestamp)
333343
pbSt := &types.StatsResponse{
334-
Timestamp: uint64(st.Timestamp.Unix()),
344+
Timestamp: tsp,
335345
CgroupStats: &types.CgroupStats{},
336346
}
337347
systemUsage, _ := getSystemCPUUsage()

api/grpc/types/api.pb.go

Lines changed: 183 additions & 156 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/grpc/types/api.proto

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ syntax = "proto3";
22

33
package types;
44

5+
import "google/protobuf/timestamp.proto";
6+
57
service API {
68
rpc GetServerVersion(GetServerVersionRequest) returns (GetServerVersionResponse) {}
79
rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse) {}
@@ -207,15 +209,17 @@ message UpdateContainerResponse {
207209
}
208210

209211
message EventsRequest {
210-
uint64 timestamp = 1;
212+
// Tag 1 is deprecated (old uint64 timestamp)
213+
google.protobuf.Timestamp timestamp = 2;
211214
}
212215

213216
message Event {
214217
string type = 1;
215218
string id = 2;
216219
uint32 status = 3;
217220
string pid = 4;
218-
uint64 timestamp = 5;
221+
// Tag 5 is deprecated (old uint64 timestamp)
222+
google.protobuf.Timestamp timestamp = 6;
219223
}
220224

221225
message NetworkStats {
@@ -305,7 +309,8 @@ message CgroupStats {
305309
message StatsResponse {
306310
repeated NetworkStats network_stats = 1;
307311
CgroupStats cgroup_stats = 2;
308-
uint64 timestamp = 3;
312+
// Tag 3 is deprecated (old uint64 timestamp)
313+
google.protobuf.Timestamp timestamp = 4;
309314
};
310315

311316
message StatsRequest {

ctr/container.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/containerd/api/grpc/types"
2121
"github.com/docker/containerd/specs"
2222
"github.com/docker/docker/pkg/term"
23+
"github.com/golang/protobuf/ptypes"
2324
netcontext "golang.org/x/net/context"
2425
"google.golang.org/grpc"
2526
"google.golang.org/grpc/grpclog"
@@ -650,7 +651,7 @@ var updateCommand = cli.Command{
650651
}
651652

652653
func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid string, closer func()) {
653-
timestamp := uint64(time.Now().Unix())
654+
timestamp := time.Now()
654655
for {
655656
e, err := events.Recv()
656657
if err != nil {
@@ -659,10 +660,16 @@ func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid strin
659660
os.Exit(128 + int(syscall.SIGHUP))
660661
}
661662
time.Sleep(1 * time.Second)
662-
events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: timestamp})
663+
tsp, err := ptypes.TimestampProto(timestamp)
664+
if err != nil {
665+
closer()
666+
fmt.Fprintf(os.Stderr, "%s", err.Error())
667+
os.Exit(1)
668+
}
669+
events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp})
663670
continue
664671
}
665-
timestamp = e.Timestamp
672+
timestamp, err = ptypes.Timestamp(e.Timestamp)
666673
if e.Id == id && e.Type == "exit" && e.Pid == pid {
667674
closer()
668675
os.Exit(int(e.Status))

ctr/events.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/codegangsta/cli"
1010
"github.com/docker/containerd/api/grpc/types"
11+
"github.com/golang/protobuf/ptypes"
1112
netcontext "golang.org/x/net/context"
1213
)
1314

@@ -22,31 +23,40 @@ var eventsCommand = cli.Command{
2223
},
2324
Action: func(context *cli.Context) {
2425
var (
25-
t int64
26+
t = time.Time{}
2627
c = getClient(context)
2728
)
2829
if ts := context.String("timestamp"); ts != "" {
2930
from, err := time.Parse(time.RFC3339Nano, ts)
3031
if err != nil {
3132
fatal(err.Error(), 1)
3233
}
33-
t = from.Unix()
34+
t = from
35+
}
36+
tsp, err := ptypes.TimestampProto(t)
37+
if err != nil {
38+
fatal(err.Error(), 1)
3439
}
3540
events, err := c.Events(netcontext.Background(), &types.EventsRequest{
36-
Timestamp: uint64(t),
41+
Timestamp: tsp,
3742
})
3843
if err != nil {
3944
fatal(err.Error(), 1)
4045
}
41-
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
46+
w := tabwriter.NewWriter(os.Stdout, 31, 1, 1, ' ', 0)
4247
fmt.Fprint(w, "TIME\tTYPE\tID\tPID\tSTATUS\n")
4348
w.Flush()
4449
for {
4550
e, err := events.Recv()
4651
if err != nil {
4752
fatal(err.Error(), 1)
4853
}
49-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", time.Unix(int64(e.Timestamp), 0).Format(time.RFC3339Nano), e.Type, e.Id, e.Pid, e.Status)
54+
t, err := ptypes.Timestamp(e.Timestamp)
55+
if err != nil {
56+
fmt.Fprintf(os.Stderr, "Unable to convert timestamp")
57+
t = time.Time{}
58+
}
59+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", t.Format(time.RFC3339Nano), e.Type, e.Id, e.Pid, e.Status)
5060
w.Flush()
5161
}
5262
},

integration-test/check_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121
"github.com/docker/containerd/api/grpc/types"
2222
utils "github.com/docker/containerd/testutils"
2323
"github.com/go-check/check"
24+
"github.com/golang/protobuf/ptypes/timestamp"
2425
)
2526

2627
func Test(t *testing.T) {
2728
check.TestingT(t)
2829
}
2930

3031
func init() {
31-
check.Suite(&ContainerdSuite{lastEventTs: uint64(time.Now().Unix())})
32+
check.Suite(&ContainerdSuite{})
3233
}
3334

3435
type ContainerdSuite struct {
@@ -42,7 +43,7 @@ type ContainerdSuite struct {
4243
grpcClient types.APIClient
4344
eventFiltersMutex sync.Mutex
4445
eventFilters map[string]func(event *types.Event)
45-
lastEventTs uint64
46+
lastEventTs *timestamp.Timestamp
4647
}
4748

4849
// getClient returns a connection to the Suite containerd

vendor/src/github.com/golang/protobuf/ptypes/any/any.pb.go

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)