Skip to content

Commit

Permalink
Merge pull request #10 from ernoaapa/describe-mounts
Browse files Browse the repository at this point in the history
Add missing information to pod describe
  • Loading branch information
ernoaapa authored Dec 31, 2017
2 parents eafb560 + 3101c19 commit 68379ff
Show file tree
Hide file tree
Showing 14 changed files with 1,150 additions and 11 deletions.
85 changes: 85 additions & 0 deletions examples/sshd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
metadata:
name: "sshd"
spec:
hostNetwork: true
containers:
- name: "sshd"
image: "docker.io/linuxkit/sshd:ac5e8364e2e9aa8717a3295c51eb60b8c57373d5"
mounts:
- type: bind
source: /root/.ssh
destination: /root/.ssh
options:
- rw
- rbind
- rprivate
- type: bind
source: /etc/resolv.conf
destination: /etc/resolv.conf
options:
- rw
- rbind
- rprivate
- type: bind
source: /run
destination: /run
options:
- rw
- rbind
- rprivate
- type: bind
source: /tmp
destination: /tmp
options:
- rw
- rbind
- rprivate
- type: bind
source: /etc
destination: /hostroot/etc
options:
- rw
- rbind
- rprivate
- type: bind
source: /usr/bin/ctr
destination: /usr/bin/ctr
options:
- rw
- rbind
- rprivate
- type: bind
source: /usr/bin/runc
destination: /usr/bin/runc
options:
- rw
- rbind
- rprivate
- type: bind
source: /containers
destination: /containers
options:
- rw
- rbind
- rprivate
- type: bind
source: /var/log
destination: /var/log
options:
- rw
- rbind
- rprivate
- type: bind
source: /dev
destination: /dev
options:
- rw
- rbind
- rprivate
- type: bind
source: /sys
destination: /sys
options:
- rw
- rbind
- rprivate
22 changes: 19 additions & 3 deletions pkg/api/mapping/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,25 @@ func MapPodToAPIModel(pod model.Pod) *pods.Pod {
func MapContainersToAPIModel(source []model.Container) (result []*containers.Container) {
for _, container := range source {
result = append(result, &containers.Container{
Name: container.Name,
Image: container.Image,
Pipe: mapPipeToAPIModel(container.Pipe),
Name: container.Name,
Image: container.Image,
WorkingDir: container.WorkingDir,
Args: container.Args,
Env: container.Env,
Mounts: mapMountsToAPIModel(container.Mounts),
Pipe: mapPipeToAPIModel(container.Pipe),
})
}
return result
}

func mapMountsToAPIModel(mounts []model.Mount) (result []*containers.Mount) {
for _, mount := range mounts {
result = append(result, &containers.Mount{
Type: mount.Type,
Source: mount.Source,
Destination: mount.Destination,
Options: mount.Options,
})
}
return result
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/services/pods/v1/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"encoding/json"

utils "github.com/ernoaapa/eliot/pkg/utils/yaml"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)

// UnmarshalYaml reads v1 Pods data in YAML format and unmarshals it to v1 api model
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/services/pods/v1/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ metadata:
name: "foo"
namespace: "my-namespace"
spec:
hostNetwork: true
restartPolicy: always
containers:
- name: "foo-1"
image: "docker.io/library/hello-world:latest"
Expand All @@ -23,6 +25,8 @@ spec:

assert.Equal(t, "foo", pods[0].Metadata.Name, "Should unmarshal name")
assert.Equal(t, 2, len(pods[0].Spec.Containers), "Should have one container spec")
assert.Equal(t, "always", pods[0].Spec.RestartPolicy, "Should have host network")
assert.True(t, pods[0].Spec.HostNetwork, "Should have host network")
}

func TestUnmarshalMultiDocumentYaml(t *testing.T) {
Expand Down Expand Up @@ -55,6 +59,7 @@ func TestUnmarshalListYaml(t *testing.T) {
name: "foo"
namespace: "my-namespace"
spec:
hostNetwork: true
containers:
- name: "foo-1"
image: "docker.io/library/hello-world:latest"
Expand All @@ -73,6 +78,7 @@ func TestUnmarshalListYaml(t *testing.T) {
assert.Equal(t, 2, len(pods), "Should have pod specs")
assert.Equal(t, "foo", pods[0].Metadata.Name, "Should unmarshal name")
assert.Equal(t, 2, len(pods[0].Spec.Containers), "Should have one container spec")
assert.True(t, pods[0].Spec.HostNetwork, "Should have one container spec")
}

func TestUnmarshalListJSON(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/ernoaapa/eliot/pkg/converter"
"github.com/ernoaapa/eliot/pkg/fs"
"github.com/ernoaapa/eliot/pkg/model"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)

// Config is struct for configuration for CLI client
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"

"github.com/ernoaapa/eliot/pkg/fs"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)

// ProjectConfig represents configuration in project directory
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (p *HumanReadablePrinter) PrintPodDetails(pod *pods.Pod, writer io.Writer)
}
return nil
},
"StringsJoin": strings.Join,
})
t, err := t.Parse(humanreadable.PodDetailsTemplate)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion pkg/printers/humanreadable/PodDetailsTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Namespace: {{.Pod.Metadata.Namespace}}
Device: {{.Pod.Status.Hostname}}
State: {{.Status}}
Restart Policy: {{.Pod.Spec.RestartPolicy}}
Host Network: {{.Pod.Spec.HostNetwork}}
Host PID: {{.Pod.Spec.HostPID}}
Containers:{{range .Pod.Spec.Containers}}
{{- $status := GetStatus $pod .Name}}
{{.Name}}:
Expand All @@ -15,7 +17,17 @@ Containers:{{range .Pod.Spec.Containers}}
ContainerID: {{$status.ContainerID}}
State: {{$status.State}}
Restart Count: {{$status.RestartCount}}
{{- end}}
Working Dir: {{.WorkingDir}}
{{- end}}
Args:{{range .Args}}
- {{.}}
{{- end}}
Env:{{range .Env}}
- {{.}}
{{- end}}
Mounts:{{range .Mounts}}
- type={{.Type}},source={{.Source}},destination={{.Destination}},options={{StringsJoin .Options ":"}}
{{- end}}
{{- if .Pipe}}
Pipe:
stdout -> stdin: {{.Pipe.Stdout.Stdin.Name}}
Expand Down
77 changes: 73 additions & 4 deletions pkg/runtime/containerd/mapping/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func InitialisePodModel(container containers.Container, namespace, name, hostnam
Metadata: model.NewMetadata(namespace, name),
Spec: model.PodSpec{
Containers: []model.Container{},
HostNetwork: !haveNamespace(container, specs.NetworkNamespace),
HostPID: !haveNamespace(container, specs.PIDNamespace),
RestartPolicy: getRestartPolicy(container),
},
Status: model.PodStatus{
Expand All @@ -50,10 +52,14 @@ func MapContainersToInternalModel(containers []containers.Container) (result []m
func MapContainerToInternalModel(container containers.Container) model.Container {
labels := ContainerLabels(container.Labels)
return model.Container{
Name: labels.getContainerName(),
Image: container.Image,
Tty: RequireTty(container),
Pipe: mapPipeToInternalModel(container),
Name: labels.getContainerName(),
Image: container.Image,
Tty: RequireTty(container),
Args: processArgs(container),
Env: processEnv(container),
WorkingDir: processWorkingDir(container),
Pipe: mapPipeToInternalModel(container),
Mounts: mapMountsToInternalModel(container),
}
}

Expand Down Expand Up @@ -94,6 +100,54 @@ func mapPipeToInternalModel(container containers.Container) *model.PipeSet {
}
}

func processArgs(container containers.Container) []string {
spec, err := getSpec(container)
if err != nil {
log.Fatalf("Cannot read container spec to resolve process args: %s", err)
return nil
}

return spec.Process.Args
}

func processEnv(container containers.Container) []string {
spec, err := getSpec(container)
if err != nil {
log.Fatalf("Cannot read container spec to resolve process environment variables: %s", err)
return nil
}

return spec.Process.Env
}

func processWorkingDir(container containers.Container) string {
spec, err := getSpec(container)
if err != nil {
log.Fatalf("Cannot read container spec to resolve process current working directory: %s", err)
return ""
}

return spec.Process.Cwd
}

func mapMountsToInternalModel(container containers.Container) (result []model.Mount) {
spec, err := getSpec(container)
if err != nil {
log.Fatalf("Cannot read container spec to resolve container mounts: %s", err)
return result
}

for _, mount := range spec.Mounts {
result = append(result, model.Mount{
Type: mount.Type,
Source: mount.Source,
Destination: mount.Destination,
Options: mount.Options,
})
}
return result
}

// MapContainerStatusToInternalModel maps containerd model to internal container status model
func MapContainerStatusToInternalModel(container containers.Container, status containerd.Status) model.ContainerStatus {
labels := ContainerLabels(container.Labels)
Expand All @@ -118,6 +172,21 @@ func getRestartCount(container containers.Container) int {
return lifecycle.StartCount - 1
}

func haveNamespace(container containers.Container, namespace specs.LinuxNamespaceType) bool {
spec, err := getSpec(container)
if err != nil {
log.Fatalf("Cannot read container spec to resolve namespace %s: %s", namespace, err)
return false
}

for _, ns := range spec.Linux.Namespaces {
if ns.Type == namespace {
return true
}
}
return false
}

func getRestartPolicy(container containers.Container) string {
lifecycle, err := extensions.GetLifecycleExtension(container)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ github.com/containerd/typeurl f6943554a7e7e88b3c14aad190bf05932da84788
github.com/docker/docker v17.05.0-ce
k8s.io/kubernetes v1.9.0-alpha.2
github.com/thejerf/suture v2.0.1
github.com/ghodss/yaml v1.0.0
50 changes: 50 additions & 0 deletions vendor/github.com/ghodss/yaml/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 68379ff

Please sign in to comment.