Skip to content
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

Add ECS views #2026

Merged
merged 19 commits into from
Nov 29, 2016
Merged
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
239 changes: 126 additions & 113 deletions app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ import (
)

const (
apiTopologyURL = "/api/topology/"
processesTopologyDescID = "processes"
processesByNameTopologyDescID = "processes-by-name"
containerLabelFiltersGroupID = "container_label_filters_group"
containersTopologyDescID = "containers"
containersByHostnameTopologyDescID = "containers-by-hostname"
containersByImageTopologyDescID = "containers-by-image"
podsTopologyDescID = "pods"
replicaSetsTopologyDescID = "replica-sets"
deploymentsTopologyDescID = "deployments"
servicesTopologyDescID = "services"
hostsTopologyDescID = "hosts"
apiTopologyURL = "/api/topology/"
processesID = "processes"
processesByNameID = "processes-by-name"
containerLabelFiltersGroupID = "container-label-filters-group"
containersID = "containers"
containersByHostnameID = "containers-by-hostname"
containersByImageID = "containers-by-image"
podsID = "pods"
replicaSetsID = "replica-sets"
deploymentsID = "deployments"
servicesID = "services"
hostsID = "hosts"
weaveID = "weave"
ecsTasksID = "ecs-tasks"
ecsServicesID = "ecs-services"
)

var (
topologyRegistry = MakeRegistry()
k8sPseudoFilter = APITopologyOptionGroup{
unmanagedFilter = APITopologyOptionGroup{
ID: "pseudo",
Default: "hide",
Options: []APITopologyOption{
Expand All @@ -42,18 +45,84 @@ var (
}
)

func init() {
AddInitialTopologiesToRegistry(topologyRegistry)
// kubernetesFilters generates the current kubernetes filters based on the
// available k8s topologies.
func kubernetesFilters(namespaces ...string) APITopologyOptionGroup {
options := APITopologyOptionGroup{ID: "namespace", Default: "all"}
for _, namespace := range namespaces {
if namespace == "default" {
options.Default = namespace
}
options.Options = append(options.Options, APITopologyOption{
Value: namespace, Label: namespace, filter: render.IsNamespace(namespace), filterPseudo: false,
})
}
options.Options = append(options.Options, APITopologyOption{Value: "all", Label: "All Namespaces", filter: nil, filterPseudo: false})
return options
}

// updateFilters updates the available filters based on the current report.
// Currently only kubernetes changes.
func updateFilters(rpt report.Report, topologies []APITopologyDesc) []APITopologyDesc {
namespaces := map[string]struct{}{}
for _, t := range []report.Topology{rpt.Pod, rpt.Service, rpt.Deployment, rpt.ReplicaSet} {
for _, n := range t.Nodes {
if state, ok := n.Latest.Lookup(kubernetes.State); ok && state == kubernetes.StateDeleted {
continue
}
if namespace, ok := n.Latest.Lookup(kubernetes.Namespace); ok {
namespaces[namespace] = struct{}{}
}
}
}
var ns []string
for namespace := range namespaces {
ns = append(ns, namespace)
}
sort.Strings(ns)
for i, t := range topologies {
if t.id == podsID || t.id == servicesID || t.id == deploymentsID || t.id == replicaSetsID {
topologies[i] = updateTopologyFilters(t, []APITopologyOptionGroup{
kubernetesFilters(ns...), unmanagedFilter,
})
}
}
return topologies
}

// updateTopologyFilters recursively sets the options on a topology description
func updateTopologyFilters(t APITopologyDesc, options []APITopologyOptionGroup) APITopologyDesc {
t.Options = options
for i, sub := range t.SubTopologies {
t.SubTopologies[i] = updateTopologyFilters(sub, options)
}
return t
}

// MakeAPITopologyOption provides an external interface to the package for creating an APITopologyOption.
func MakeAPITopologyOption(value string, label string, filterFunc render.FilterFunc, pseudo bool) APITopologyOption {
return APITopologyOption{Value: value, Label: label, filter: filterFunc, filterPseudo: pseudo}
}

// AddInitialTopologiesToRegistry does the initial setup for a Registry.
// This is needed for testing.
func AddInitialTopologiesToRegistry(registry *Registry) {
// Registry is a threadsafe store of the available topologies
type Registry struct {
sync.RWMutex
items map[string]APITopologyDesc
}

// MakeRegistry returns a new Registry
func MakeRegistry() *Registry {
registry := &Registry{
items: map[string]APITopologyDesc{},
}
containerFilters := []APITopologyOptionGroup{
{
ID: containerLabelFiltersGroupID,
Default: "application",
Options: []APITopologyOption{{Value: "all", Label: "All", filter: nil, filterPseudo: false}, {Value: "system", Label: "System Containers", filter: render.IsSystem, filterPseudo: false}, {Value: "notsystem", Label: "Application Containers", filter: render.IsApplication, filterPseudo: false}},
Options: []APITopologyOption{
{Value: "all", Label: "All", filter: nil, filterPseudo: false},
{Value: "system", Label: "System Containers", filter: render.IsSystem, filterPseudo: false},
{Value: "notsystem", Label: "Application Containers", filter: render.IsApplication, filterPseudo: false}},
},
{
ID: "stopped",
Expand Down Expand Up @@ -90,157 +159,101 @@ func AddInitialTopologiesToRegistry(registry *Registry) {
// be the verb to get to that state
registry.Add(
APITopologyDesc{
id: processesTopologyDescID,
id: processesID,

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

renderer: render.FilterUnconnected(render.ProcessWithContainerNameRenderer),
Name: "Processes",
Rank: 1,
Options: unconnectedFilter,
HideIfEmpty: true,
},
APITopologyDesc{
id: processesByNameTopologyDescID,
parent: "processes",
id: processesByNameID,
parent: processesID,
renderer: render.FilterUnconnected(render.ProcessNameRenderer),
Name: "by name",
Options: unconnectedFilter,
HideIfEmpty: true,
},
APITopologyDesc{
id: containersTopologyDescID,
id: containersID,
renderer: render.ContainerWithImageNameRenderer,
Name: "Containers",
Rank: 2,
Options: containerFilters,
},
APITopologyDesc{
id: containersByHostnameTopologyDescID,
parent: "containers",
id: containersByHostnameID,
parent: containersID,
renderer: render.ContainerHostnameRenderer,
Name: "by DNS name",
Options: containerFilters,
},
APITopologyDesc{
id: containersByImageTopologyDescID,
parent: "containers",
id: containersByImageID,
parent: containersID,
renderer: render.ContainerImageRenderer,
Name: "by image",
Options: containerFilters,
},
APITopologyDesc{
id: podsTopologyDescID,
id: podsID,
renderer: render.PodRenderer,
Name: "Pods",
Rank: 3,
HideIfEmpty: true,
},
APITopologyDesc{
id: replicaSetsTopologyDescID,
parent: "pods",
id: replicaSetsID,
parent: podsID,
renderer: render.ReplicaSetRenderer,
Name: "replica sets",
HideIfEmpty: true,
},
APITopologyDesc{
id: deploymentsTopologyDescID,
parent: "pods",
id: deploymentsID,
parent: podsID,
renderer: render.DeploymentRenderer,
Name: "deployments",
HideIfEmpty: true,
},
APITopologyDesc{
id: servicesTopologyDescID,
parent: "pods",
id: servicesID,
parent: podsID,
renderer: render.PodServiceRenderer,
Name: "services",
HideIfEmpty: true,
},
APITopologyDesc{
id: hostsTopologyDescID,
id: ecsTasksID,
renderer: render.ECSTaskRenderer,
Name: "Tasks",
Rank: 3,
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
id: ecsServicesID,
parent: ecsTasksID,
renderer: render.ECSServiceRenderer,
Name: "services",
Options: []APITopologyOptionGroup{unmanagedFilter},
HideIfEmpty: true,
},
APITopologyDesc{
id: hostsID,
renderer: render.HostRenderer,
Name: "Hosts",
Rank: 4,
},
APITopologyDesc{
id: "weave",
parent: "hosts",
renderer: render.WeaveRenderer,
Name: "weave net",
HideIfEmpty: true,
id: weaveID,
parent: hostsID,
renderer: render.WeaveRenderer,
Name: "Weave Net",
},
)
}

// kubernetesFilters generates the current kubernetes filters based on the
// available k8s topologies.
func kubernetesFilters(namespaces ...string) APITopologyOptionGroup {
options := APITopologyOptionGroup{ID: "namespace", Default: "all"}
for _, namespace := range namespaces {
if namespace == "default" {
options.Default = namespace
}
options.Options = append(options.Options, APITopologyOption{
Value: namespace, Label: namespace, filter: render.IsNamespace(namespace), filterPseudo: false,
})
}
options.Options = append(options.Options, APITopologyOption{Value: "all", Label: "All Namespaces", filter: nil, filterPseudo: false})
return options
}

// updateFilters updates the available filters based on the current report.
// Currently only kubernetes changes.
func updateFilters(rpt report.Report, topologies []APITopologyDesc) []APITopologyDesc {
namespaces := map[string]struct{}{}
for _, t := range []report.Topology{rpt.Pod, rpt.Service, rpt.Deployment, rpt.ReplicaSet} {
for _, n := range t.Nodes {
if state, ok := n.Latest.Lookup(kubernetes.State); ok && state == kubernetes.StateDeleted {
continue
}
if namespace, ok := n.Latest.Lookup(kubernetes.Namespace); ok {
namespaces[namespace] = struct{}{}
}
}
}
var ns []string
for namespace := range namespaces {
ns = append(ns, namespace)
}
sort.Strings(ns)
for i, t := range topologies {
if t.id == podsTopologyDescID || t.id == servicesTopologyDescID || t.id == deploymentsTopologyDescID || t.id == replicaSetsTopologyDescID {
topologies[i] = updateTopologyFilters(t, []APITopologyOptionGroup{
kubernetesFilters(ns...), k8sPseudoFilter,
})
}
}
return topologies
}

// updateTopologyFilters recursively sets the options on a topology description
func updateTopologyFilters(t APITopologyDesc, options []APITopologyOptionGroup) APITopologyDesc {
t.Options = options
for i, sub := range t.SubTopologies {
t.SubTopologies[i] = updateTopologyFilters(sub, options)
}
return t
}

// MakeAPITopologyOption provides an external interface to the package for creating an APITopologyOption.
func MakeAPITopologyOption(value string, label string, filterFunc render.FilterFunc, pseudo bool) APITopologyOption {
return APITopologyOption{Value: value, Label: label, filter: filterFunc, filterPseudo: pseudo}
}

// Registry is a threadsafe store of the available topologies
type Registry struct {
sync.RWMutex
items map[string]APITopologyDesc
}

// MakeRegistry returns a new Registry
func MakeRegistry() *Registry {
newRegistry := &Registry{
items: map[string]APITopologyDesc{},
}
return newRegistry
return registry
}

// APITopologyDesc is returned in a list by the /api/topology handler.
Expand Down Expand Up @@ -297,7 +310,7 @@ func AddContainerFilters(newFilters ...APITopologyOption) {
func (r *Registry) AddContainerFilters(newFilters ...APITopologyOption) {
r.Lock()
defer r.Unlock()
for _, key := range []string{containersTopologyDescID, containersByHostnameTopologyDescID, containersByImageTopologyDescID} {
for _, key := range []string{containersID, containersByHostnameID, containersByImageID} {
for i := range r.items[key].Options {
if r.items[key].Options[i].ID == containerLabelFiltersGroupID {
r.items[key].Options[i].Options = append(r.items[key].Options[i].Options, newFilters...)
Expand Down
Loading