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

client API tasks filters not working with service and desired-state #38283

Closed
caub opened this Issue Nov 27, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@caub
Copy link

caub commented Nov 27, 2018

Description

Steps to reproduce the issue:

  1. docker stack up x -c stack.yml # stack.yml file below, also need docker swarm mode (docker swarm init)
  2. curl --unix-socket /var/run/docker.sock 'http:/v1.39/tasks?filters={"service":{"x_foo":true}}''

stack.yml

version: '3.6'
services:
  foo:
    image: node:slim
    deploy:
      mode: replicated
      replicas: 2
    command: ['node', '-p', 'setInterval(console.log, 10000, "foo")']
  bar:
    image: node:slim
    deploy:
      mode: global
    command: ['node', '-p', 'setInterval(console.log, 10000, "bar")']

Describe the results you received:

empty string

Describe the results you expected:

a list of the 2 running tasks for service foo

Additional information you deem important (e.g. issue happens only occasionally):

docker service ls --format '{{.Name}}\t{{.Replicas}}' gives:
x_bar 1/1
x_foo 2/2

I'm looking to get the number of tasks for the foo service using the API (the goal is to get the service availability like docker service ls command provides in the 'Replicas' command)

related forum question: https://forums.docker.com/t/get-current-service-replicas-via-docker-api/40217

Output of docker version:

Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:49:01 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:16:44 2018
  OS/Arch:          linux/amd64
  Experimental:     false

Output of docker info:

Containers: 3
 Running: 3
 Paused: 0
 Stopped: 0
Images: 358
Server Version: 18.09.0
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: active
 NodeID: mr37h816d1eynacvrcch6o72b
 Is Manager: true
 ClusterID: z1c9ph5sbnf17krd1t5gyq8xi
 Managers: 1
 Nodes: 1
 Default Address Pool: 10.0.0.0/8  
 SubnetSize: 24
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 10
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
  Force Rotate: 0
 Autolock Managers: false
 Root Rotation In Progress: false
 Node Address: 192.168.0.2
 Manager Addresses:
  192.168.0.2:2377
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: fec3683
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.15.0-39-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.56GiB
Name: inspiron
ID: I7ZO:AP4V:XIHI:N6K5:64GD:YP22:DGDS:TI6W:OWWC:5CAJ:JSYL:NJGB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 63
 Goroutines: 186
 System Time: 2018-11-27T22:46:29.771427518+01:00
 EventsListeners: 3
Username: caub
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

WARNING: No swap limit support

Additional environment details (AWS, VirtualBox, physical, etc.):
physical

@thaJeztah

This comment has been minimized.

Copy link
Member

thaJeztah commented Nov 27, 2018

That is because docker service ls calls multiple endpoints to obtain that information;

If you enable debug on the daemon, you can see the API calls that are handled.

For example, the following calls are made to show docker service ls on a swarm that has 2 services running (foo and bar), both having 2 replicas;

docker service ls

DEBU[2018-11-27T22:22:36.796172195Z] Calling GET /_ping                           
DEBU[2018-11-27T22:22:36.796743870Z] Calling GET /v1.39/services                  
DEBU[2018-11-27T22:22:36.798584493Z] Calling GET /v1.39/tasks?filters=%7B%22service%22%3A%7B%22a3ur80tlnd3re2fadsvlu59ai%22%3Atrue%2C%22ogfjnw0tdrge8tw11e5cm4u96%22%3Atrue%7D%7D 
DEBU[2018-11-27T22:22:36.800410650Z] Calling GET /v1.39/nodes  
  • First the CLI calls the /_ping endpoint, to determin what API version the daemon supports, and if swarm is enabled
  • GET /v1.39/services is used to get a list of services
  • GET /v1.39/tasks?filters=%7B%22service%22%3A%7B%22a3ur80tlnd3re2fadsvlu59ai%22%3Atrue%2C%22ogfjnw0tdrge8tw11e5cm4u96%22%3Atrue%7D%7D is called to get the tasks belonging to those services
  • GET /v1.39/nodes (actually not sure what it's used for; will have to check that one)
@thaJeztah

This comment has been minimized.

Copy link
Member

thaJeztah commented Nov 27, 2018

oh, overlooked you were using the tasks endpoint already; could it be that you need to urlencode your filters? And possibly use the service ID's instead of names

@caub

This comment has been minimized.

Copy link

caub commented Nov 28, 2018

All good @thaJeztah thanks very much for your quickness and details

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
yrbsrbhjygbo        x_bar               global              1/1                 node:slim           
t3xtwnx59bjj        x_foo               replicated          2/2                 node:slim
$ curl --unix-socket /var/run/docker.sock http:/v1.39/_ping
OK
$ curl --unix-socket /var/run/docker.sock http:/v1.39/services | jq -c 'map([.ID, .Spec.Name])'
[["t3xtwnx59bjjl6q1hevt8l4dc","x_foo"],["yrbsrbhjygbociac7stygmbcy","x_bar"]]
$ curl --unix-socket /var/run/docker.sock http:/v1.39/tasks -G --data-urlencode 'filters={"service":{"t3xtwnx59bjjl6q1hevt8l4dc":true},"desired-state":{"running":true}}' | jq -c 'map([.ID,.DesiredState])'
[["hrq8zzeq93wjgsuwjcnu28lxy","running"],["yl0g18sugdbsfgqfdggxk0x20","running"]]

@caub caub closed this Nov 28, 2018

@caub

This comment has been minimized.

Copy link

caub commented Nov 28, 2018

/nodes is used for https://github.com/docker/cli/blob/master/cli/command/service/list.go#L105 https://github.com/docker/cli/blob/master/cli/command/service/list.go#L73

I guess it's for counting the number of expected running task when the service is global

@thaJeztah

This comment has been minimized.

Copy link
Member

thaJeztah commented Nov 28, 2018

I guess it's for counting the number of expected running task when the service is global

Ah, yes, that makes sense; forgot about that case 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment