Skip to content

Add server info #128

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,20 @@ Within the templates, the object emitted by docker-gen will be a structure consi

```go
type RuntimeContainer struct {
ID string
Addresses []Address
Gateway string
Name string
Hostname string
Image DockerImage
Env map[string]string
Volumes map[string]Volume
Node SwarmNode
Labels map[string]string
IP string
ID string
Addresses []Address
Gateway string
Name string
Hostname string
Image DockerImage
Env map[string]string
Volumes map[string]Volume
Node SwarmNode
Labels map[string]string
Server ServerInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name this Docker instead? Server is kind of nebulous.

IP string
IP6LinkLocal string
IP6Global string
}

type Address struct {
Expand Down Expand Up @@ -173,6 +176,21 @@ type SwarmNode struct {
Name string
Address Address
}

type ServerInfo struct {
Name string
NumContainers int
NumImages int
DockerInfo DockerInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ServerInfo is renamed to Docker, field could renamed to just Info. So the the template looks like Docker.Info.

}

type DockerInfo struct {
Version string
ApiVersion string
GoVersion string
OperatingSystem string
Architecture string
}
```

For example, this is a JSON version of an emitted RuntimeContainer struct:
Expand Down
16 changes: 16 additions & 0 deletions docker-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type RuntimeContainer struct {
Volumes map[string]Volume
Node SwarmNode
Labels map[string]string
Server ServerInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should go on the Context and not on each RuntimeContainer. That way you could access it directly without a reference to a container.

See: https://github.com/jwilder/docker-gen/blob/master/docker-gen.go#L131

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a great idea. Can you get to the Context from inside a template, though? It looks like it only sends an array of containers to the generateFile function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like putting it on Context would be quite a bit of effort and wouldn't work that well.

Assuming there would be a func (c *Context) Docker() *Docker function, that function would have to call getEndpoint() and NewDockerClient() to create a Docker client each time it's called since it has no access to an existing docker.Client. I also can't see how docker-gen would be able to avoid hitting the Docker server every time the function is called.

I suppose you could stash a pointer to the docker.Client inside of each RuntimeContainer, but that seems like a terrible hack.

I could see making it work if Context were turned into a struct, but then it would not be possible to call range on it and iterate over a list of RuntimeContainer instances without changing every existing docker-gen template.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a poke to keep this change alive. I have been using it in production here for the past few days without any changes and it's been working well. I do agree that copying the same info over to every RuntimeContainer object is probably not efficient, but as far as being practical, it's been good.

IP string
IP6LinkLocal string
IP6Global string
Expand All @@ -89,6 +90,21 @@ type SwarmNode struct {
Address Address
}

type ServerInfo struct {
Name string
NumContainers int
NumImages int
DockerInfo DockerInfo
}

type DockerInfo struct {
Version string
ApiVersion string
GoVersion string
OperatingSystem string
Architecture string
}

func (strings *stringslice) String() string {
return "[]"
}
Expand Down
25 changes: 25 additions & 0 deletions docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func splitDockerImage(img string) (string, string, string) {

func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {

apiInfo, err := client.Info()
if err != nil {
log.Printf("error retrieving docker server info: %s\n", err)
}

apiVersion, err := client.Version()
if err != nil {
log.Printf("error retrieving docker server version info: %s\n", err)
}

apiContainers, err := client.ListContainers(docker.ListContainersOptions{
All: false,
Size: false,
Expand Down Expand Up @@ -128,6 +138,7 @@ func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
Volumes: make(map[string]Volume),
Node: SwarmNode{},
Labels: make(map[string]string),
Server: ServerInfo{},
IP: container.NetworkSettings.IPAddress,
IP6LinkLocal: container.NetworkSettings.LinkLocalIPv6Address,
IP6Global: container.NetworkSettings.GlobalIPv6Address,
Expand Down Expand Up @@ -155,6 +166,20 @@ func getContainers(client *docker.Client) ([]*RuntimeContainer, error) {
ReadWrite: container.VolumesRW[k],
}
}
if apiInfo != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be done once before getting the containers. All the containers can just have point to the same reference since it's all the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll fix that once we sort out the Context thing.

runtimeContainer.Server.Name = apiInfo.Get("Name")
runtimeContainer.Server.NumContainers = apiInfo.GetInt("Containers")
runtimeContainer.Server.NumImages = apiInfo.GetInt("Images")
runtimeContainer.Server.DockerInfo = DockerInfo{}

if apiVersion != nil {
runtimeContainer.Server.DockerInfo.Version = apiVersion.Get("Version")
runtimeContainer.Server.DockerInfo.ApiVersion = apiVersion.Get("ApiVersion")
runtimeContainer.Server.DockerInfo.GoVersion = apiVersion.Get("GoVersion")
runtimeContainer.Server.DockerInfo.OperatingSystem = apiVersion.Get("Os")
runtimeContainer.Server.DockerInfo.Architecture = apiVersion.Get("Arch")
}
}
if container.Node != nil {
runtimeContainer.Node.ID = container.Node.ID
runtimeContainer.Node.Name = container.Node.Name
Expand Down