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

Support for image_id container label #129

Merged
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
5 changes: 5 additions & 0 deletions pkg/api/collectorcontrollerv1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ const (
// ContainerImageLabel is the label for the container image
ContainerImageLabel = "container_image"

// ContainerImageIDLabel is the label for the container image with digest
ContainerImageIDLabel = "container_image_id"

// ExportedNamespaceLabel is the label name of the namespace for a pod
// Defined by the pod namespace
ExportedNamespaceLabel = "exported_namespace"
Expand Down Expand Up @@ -1127,6 +1130,8 @@ type BuiltInLabelsMask struct {
PVType bool `json:"pv_type,omitempty" yaml:"pv_type,omitempty"`

ContainerImage bool `json:"container_image,omitempty" yaml:"container_image,omitempty"`

ContainerImageID bool `json:"container_image_id,omitempty" yaml:"container_image_id,omitempty"`
}

// ExtensionsLabelMask is a mask for user defined metric labels.
Expand Down
19 changes: 19 additions & 0 deletions pkg/collector/builtin_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type builtInLabelsValues struct {
ContainerName string `json:"exported_container,omitempty" yaml:"exported_container,omitempty"`
// ContainerImage is the container image in the container
ContainerImage string `json:"container_image,omitempty" yaml:"container_image,omitempty"`
// ContainerImageID is the container image with digest in the container
ContainerImageID string `json:"container_image_id,omitempty" yaml:"container_image_id,omitempty"`
// PodName is the name of a pod.
PodName string `json:"exported_pod,omitempty" yaml:"exported_pod,omitempty"`
// NamespaceName is the name of a namespace.
Expand Down Expand Up @@ -100,6 +102,9 @@ func getBuiltInLabelNames(mask collectorcontrollerv1alpha1.BuiltInLabelsMask) []
if mask.ContainerImage {
labels = append(labels, collectorcontrollerv1alpha1.ContainerImageLabel)
}
if mask.ContainerImageID {
labels = append(labels, collectorcontrollerv1alpha1.ContainerImageIDLabel)
}
if mask.PodName {
labels = append(labels, collectorcontrollerv1alpha1.ExportedPodLabel)
}
Expand Down Expand Up @@ -178,6 +183,9 @@ func builtInMask(mask collectorcontrollerv1alpha1.BuiltInLabelsMask, m builtInLa
if !mask.ContainerImage {
s.ContainerImage = ""
}
if !mask.ContainerImageID {
s.ContainerImageID = ""
}
if !mask.PodName {
s.PodName = ""
}
Expand Down Expand Up @@ -255,6 +263,9 @@ func getBuiltInLabelValues(mask collectorcontrollerv1alpha1.BuiltInLabelsMask, m
if mask.ContainerImage {
labels = append(labels, m.ContainerImage)
}
if mask.ContainerImageID {
labels = append(labels, m.ContainerImageID)
}
if mask.PodName {
labels = append(labels, m.PodName)
}
Expand Down Expand Up @@ -335,6 +346,7 @@ func getOverrideBuiltInLabelValues(m builtInLabelsValues, overrides map[string]s
s := m
s.ContainerName = f(collectorcontrollerv1alpha1.ExportedContainerLabel, m.ContainerName)
s.ContainerImage = f(collectorcontrollerv1alpha1.ContainerImageLabel, m.ContainerImage)
s.ContainerImageID = f(collectorcontrollerv1alpha1.ContainerImageIDLabel, m.ContainerImageID)
s.PodName = f(collectorcontrollerv1alpha1.ExportedPodLabel, m.PodName)
s.NamespaceName = f(collectorcontrollerv1alpha1.ExportedNamespaceLabel, m.NamespaceName)
s.NodeName = f(collectorcontrollerv1alpha1.ExportedNodeLabel, m.NodeName)
Expand Down Expand Up @@ -371,6 +383,13 @@ func (l builtInLabler) SetLabelsForContainer(labels *builtInLabelsValues, c *cor
labels.ContainerImage = c.Image
}

func (l builtInLabler) SetLabelsForContainerStatus(labels *builtInLabelsValues, c *corev1.ContainerStatus) {
if c == nil {
return
}
labels.ContainerImageID = c.ImageID
}

func (l builtInLabler) SetLabelsForPod(
labels *builtInLabelsValues, p *corev1.Pod, w workload,
node *corev1.Node, namespace *corev1.Namespace) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,17 @@ func getContainerNameToID(pod *corev1.Pod) (map[string]string, string) {
return containerNameToID, podUID
}

func getContainerNameToContainerStatus(statuses []corev1.ContainerStatus) map[string]*corev1.ContainerStatus {
if len(statuses) == 0 {
return nil
}
m := make(map[string]*corev1.ContainerStatus)
for i := range statuses {
m[statuses[i].Name] = &statuses[i]
}
return m
}

func normalizeContainerID(id string) string {
if index := strings.Index(id, "://"); index >= 0 {
id = id[index+3:]
Expand Down Expand Up @@ -782,6 +793,9 @@ func (c *Collector) collectContainers(o *CapacityObjects, ch chan<- prometheus.M
// use the Pod containerStatuses field to map the container name to a containerID.
containerNameToID, podUID := getContainerNameToID(pod)

// get container status by name
containerNameToStatuses := getContainerNameToContainerStatus(pod.Status.ContainerStatuses)

var samplerPodName, samplerPodPhase string
if samplerPod, ok := o.SamplersByNode[pod.Spec.NodeName]; ok {
samplerPodName = samplerPod.Name
Expand All @@ -795,6 +809,7 @@ func (c *Collector) collectContainers(o *CapacityObjects, ch chan<- prometheus.M
container := &pod.Spec.Containers[i]
containerLabels := podLabels
c.Labeler.SetLabelsForContainer(&containerLabels, container)
c.Labeler.SetLabelsForContainerStatus(&containerLabels, containerNameToStatuses[container.Name])

// first try to get the metrics based on the container name and namespace
id := sampler.ContainerKey{ContainerName: container.Name, PodName: pod.Name, NamespaceName: pod.Namespace}
Expand Down
7 changes: 7 additions & 0 deletions pkg/collector/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (l Labeler) SetLabelsForContainer(
// no extension labels for containers
}

// SetLabelsForContainerStatus parses metric labels from a container status
func (l Labeler) SetLabelsForContainerStatus(
labels *LabelsValues, container *corev1.ContainerStatus) {
l.BuiltIn.SetLabelsForContainerStatus(&labels.BuiltIn, container)
// no extension labels for containers
}

func (l Labeler) SetLabelsForPod(
labels *LabelsValues, pod *corev1.Pod, w workload,
node *corev1.Node, namespace *corev1.Namespace) {
Expand Down
Loading