Skip to content

Commit 432ab44

Browse files
committed
Fix for ImageId and Repository empty issue
1 parent b04024a commit 432ab44

File tree

2 files changed

+135
-77
lines changed

2 files changed

+135
-77
lines changed

source/plugins/go/input/lib/kubernetescontainerinventory.go

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ func GetContainerInventoryRecords(podItem map[string]interface{}, batchTime stri
7979
if imageIDValue, ok := containerStatusMap["imageID"].(string); ok && imageIDValue != "" {
8080
if atLocation := strings.Index(imageIDValue, "@"); atLocation != -1 {
8181
containerInventoryRecord["ImageId"] = imageIDValue[atLocation+1:]
82+
} else {
83+
containerInventoryRecord["ImageId"] = imageIDValue
8284
}
8385
}
8486

87+
if imageValue, ok := containerStatusMap["image"].(string); ok && imageValue != "" {
88+
addImageInfoToContainerInventoryRecord(containerInventoryRecord, imageValue)
89+
}
90+
8591
containerInventoryRecord["ExitCode"] = 0
8692
isContainerTerminated := false
8793
isContainerWaiting := false
@@ -116,36 +122,7 @@ func GetContainerInventoryRecords(podItem map[string]interface{}, batchTime stri
116122

117123
if containerInfoMap, ok := containersInfoMap[containerName]; ok {
118124
if imageValue, ok := containerInfoMap["image"]; ok && imageValue != "" {
119-
atLocation := strings.Index(imageValue, "@")
120-
isDigestSpecified := false
121-
if atLocation != -1 {
122-
imageValue = imageValue[:atLocation]
123-
if containerInventoryRecord["ImageId"] == nil || containerInventoryRecord["ImageId"] == "" {
124-
containerInventoryRecord["ImageId"] = imageValue[atLocation+1:]
125-
}
126-
isDigestSpecified = true
127-
}
128-
slashLocation := strings.Index(imageValue, "/")
129-
colonLocation := strings.Index(imageValue, ":")
130-
if colonLocation != -1 {
131-
if slashLocation == -1 {
132-
containerInventoryRecord["Image"] = imageValue[:colonLocation]
133-
} else {
134-
containerInventoryRecord["Repository"] = imageValue[:slashLocation]
135-
containerInventoryRecord["Image"] = imageValue[slashLocation+1 : colonLocation]
136-
}
137-
containerInventoryRecord["ImageTag"] = imageValue[colonLocation+1:]
138-
} else {
139-
if slashLocation == -1 {
140-
containerInventoryRecord["Image"] = imageValue
141-
} else {
142-
containerInventoryRecord["Repository"] = imageValue[:slashLocation]
143-
containerInventoryRecord["Image"] = imageValue[slashLocation+1:]
144-
}
145-
if !isDigestSpecified {
146-
containerInventoryRecord["ImageTag"] = "latest"
147-
}
148-
}
125+
addImageInfoToContainerInventoryRecord(containerInventoryRecord, imageValue)
149126
}
150127

151128
podName := containerInfoMap["PodName"]
@@ -182,6 +159,58 @@ func GetContainerInventoryRecords(podItem map[string]interface{}, batchTime stri
182159
return containerInventoryRecords
183160
}
184161

162+
func addImageInfoToContainerInventoryRecord(containerInventoryRecord map[string]interface{}, imageValue string) {
163+
// image can be in any one of below formats:
164+
// repository/image[:imagetag | @digest], repository/image:imagetag@digest, repo/image, image:imagetag, image@digest, image
165+
if imageValue == "" {
166+
return
167+
}
168+
atLocation := strings.Index(imageValue, "@")
169+
isDigestSpecified := false
170+
if atLocation != -1 {
171+
if containerInventoryRecord["ImageId"] == nil || containerInventoryRecord["ImageId"] == "" {
172+
containerInventoryRecord["ImageId"] = imageValue[atLocation+1:]
173+
}
174+
imageValue = imageValue[:atLocation]
175+
isDigestSpecified = true
176+
}
177+
slashLocation := strings.Index(imageValue, "/")
178+
colonLocation := strings.Index(imageValue, ":")
179+
if colonLocation != -1 {
180+
if slashLocation == -1 {
181+
if containerInventoryRecord["Image"] == nil || containerInventoryRecord["Image"] == "" {
182+
containerInventoryRecord["Image"] = imageValue[:colonLocation]
183+
}
184+
} else {
185+
if containerInventoryRecord["Repository"] == nil || containerInventoryRecord["Repository"] == "" {
186+
containerInventoryRecord["Repository"] = imageValue[:slashLocation]
187+
}
188+
if containerInventoryRecord["Image"] == nil || containerInventoryRecord["Image"] == "" {
189+
containerInventoryRecord["Image"] = imageValue[slashLocation+1 : colonLocation]
190+
}
191+
}
192+
if containerInventoryRecord["ImageTag"] == nil || containerInventoryRecord["ImageTag"] == "" {
193+
containerInventoryRecord["ImageTag"] = imageValue[colonLocation+1:]
194+
}
195+
} else {
196+
if slashLocation == -1 {
197+
if containerInventoryRecord["Image"] == nil || containerInventoryRecord["Image"] == "" {
198+
containerInventoryRecord["Image"] = imageValue
199+
}
200+
} else {
201+
if containerInventoryRecord["Repository"] == nil || containerInventoryRecord["Repository"] == "" {
202+
containerInventoryRecord["Repository"] = imageValue[:slashLocation]
203+
}
204+
if containerInventoryRecord["Image"] == nil || containerInventoryRecord["Image"] == "" {
205+
containerInventoryRecord["Image"] = imageValue[slashLocation+1:]
206+
}
207+
}
208+
if !isDigestSpecified && (containerInventoryRecord["ImageTag"] == nil || containerInventoryRecord["ImageTag"] == "") {
209+
containerInventoryRecord["ImageTag"] = "latest"
210+
}
211+
}
212+
}
213+
185214
func getContainersInfoMap(podItem map[string]interface{}, isWindows bool) map[string]map[string]string {
186215
containersInfoMap := make(map[string]map[string]string)
187216

@@ -562,7 +591,7 @@ func GetContainerInventory(namespaceFilteringMode string, namespaces []string, b
562591
return GetContainerInventoryHelper(podList, namespaceFilteringMode, namespaces, batchTime)
563592
}
564593

565-
func GetContainerInventoryHelper(podList map[string]interface{}, namespaceFilteringMode string, namespaces []string, batchTime string) ([]string, []map[string]interface{}){
594+
func GetContainerInventoryHelper(podList map[string]interface{}, namespaceFilteringMode string, namespaces []string, batchTime string) ([]string, []map[string]interface{}) {
566595
containerIds := []string{}
567596
containerInventory := []map[string]interface{}{}
568597
clusterCollectEnvironmentVar := os.Getenv("AZMON_CLUSTER_COLLECT_ENV_VAR")

source/plugins/ruby/kubernetes_container_inventory.rb

Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ def getContainerInventoryRecords(podItem, batchTime, clusterCollectEnvironmentVa
4343
# for containers that have image issues (like invalid image/tag etc..) this will be empty. do not make it all 0
4444
containerInventoryRecord["InstanceID"] = containerId
4545
end
46-
# imagedId is of the format - repo@sha256:imageid
46+
# imagedId is either of the the format - repo@sha256:imageid or sha256:imageid
4747
imageIdValue = containerStatus["imageID"]
4848
if !imageIdValue.nil? && !imageIdValue.empty?
4949
atLocation = imageIdValue.index("@")
5050
if !atLocation.nil?
5151
containerInventoryRecord["ImageId"] = imageIdValue[(atLocation + 1)..-1]
52+
else
53+
containerInventoryRecord["ImageId"] = imageIdValue
5254
end
5355
end
56+
57+
addImageInfoToContainerInventoryRecord(containerInventoryRecord, containerStatus["image"])
58+
5459
containerInventoryRecord["ExitCode"] = 0
5560
isContainerTerminated = false
5661
isContainerWaiting = false
@@ -84,50 +89,8 @@ def getContainerInventoryRecords(podItem, batchTime, clusterCollectEnvironmentVa
8489
end
8590

8691
containerInfoMap = containersInfoMap[containerName]
87-
# image can be in any one of below format in spec
88-
# repository/image[:imagetag | @digest], repository/image:imagetag@digest, repo/image, image:imagetag, image@digest, image
89-
imageValue = containerInfoMap["image"]
90-
if !imageValue.nil? && !imageValue.empty?
91-
# Find delimiters in image format
92-
atLocation = imageValue.index("@")
93-
isDigestSpecified = false
94-
if !atLocation.nil?
95-
# repository/image@digest or repository/image:imagetag@digest, image@digest
96-
imageValue = imageValue[0..(atLocation - 1)]
97-
# Use Digest from the spec's image in case when the status doesnt get populated i.e. container in pending or image pull back etc.
98-
if containerInventoryRecord["ImageId"].nil? || containerInventoryRecord["ImageId"].empty?
99-
containerInventoryRecord["ImageId"] = imageValue[(atLocation + 1)..-1]
100-
end
101-
isDigestSpecified = true
102-
end
103-
slashLocation = imageValue.index("/")
104-
colonLocation = imageValue.index(":")
105-
if !colonLocation.nil?
106-
if slashLocation.nil?
107-
# image:imagetag
108-
containerInventoryRecord["Image"] = imageValue[0..(colonLocation - 1)]
109-
else
110-
# repository/image:imagetag
111-
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
112-
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..(colonLocation - 1)]
113-
end
114-
containerInventoryRecord["ImageTag"] = imageValue[(colonLocation + 1)..-1]
115-
else
116-
if slashLocation.nil?
117-
# image
118-
containerInventoryRecord["Image"] = imageValue
119-
else
120-
# repo/image
121-
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
122-
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..-1]
123-
end
124-
# if no tag specified, k8s assumes latest as imagetag and this is same behavior from docker API and from status.
125-
# Ref - https://kubernetes.io/docs/concepts/containers/images/#image-names
126-
if isDigestSpecified == false
127-
containerInventoryRecord["ImageTag"] = "latest"
128-
end
129-
end
130-
end
92+
# Populate the fields related to the image if not already populated
93+
addImageInfoToContainerInventoryRecord(containerInventoryRecord, containerInfoMap["image"])
13194

13295
podName = containerInfoMap["PodName"]
13396
namespace = containerInfoMap["Namespace"]
@@ -169,6 +132,72 @@ def getContainerInventoryRecords(podItem, batchTime, clusterCollectEnvironmentVa
169132
return containerInventoryRecords
170133
end
171134

135+
def addImageInfoToContainerInventoryRecord(containerInventoryRecord, imageValue)
136+
begin
137+
if imageValue.nil? || imageValue.empty?
138+
return
139+
# image can be in any one of below formats:
140+
# repository/image[:imagetag | @digest], repository/image:imagetag@digest, repo/image, image:imagetag, image@digest, image
141+
# Find delimiters in image format
142+
atLocation = imageValue.index("@")
143+
isDigestSpecified = false
144+
if !atLocation.nil?
145+
# Use Digest from the spec's image in case when the status doesnt get populated i.e. container in pending or image pull back etc.
146+
if containerInventoryRecord["ImageId"].nil? || containerInventoryRecord["ImageId"].empty?
147+
containerInventoryRecord["ImageId"] = imageValue[(atLocation + 1)..-1]
148+
end
149+
# repository/image@digest or repository/image:imagetag@digest, image@digest
150+
imageValue = imageValue[0..(atLocation - 1)]
151+
isDigestSpecified = true
152+
end
153+
slashLocation = imageValue.index("/")
154+
colonLocation = imageValue.index(":")
155+
if !colonLocation.nil?
156+
if slashLocation.nil?
157+
# image:imagetag
158+
if containerInventoryRecord["Image"].nil? || containerInventoryRecord["Image"].empty?
159+
containerInventoryRecord["Image"] = imageValue[0..(colonLocation - 1)]
160+
end
161+
else
162+
# repository/image:imagetag
163+
if containerInventoryRecord["Repository"].nil? || containerInventoryRecord["Repository"].empty?
164+
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
165+
end
166+
if containerInventoryRecord["Image"].nil? || containerInventoryRecord["Image"].empty?
167+
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..(colonLocation - 1)]
168+
end
169+
end
170+
if containerInventoryRecord["ImageTag"].nil? || containerInventoryRecord["ImageTag"].empty?
171+
containerInventoryRecord["ImageTag"] = imageValue[(colonLocation + 1)..-1]
172+
end
173+
else
174+
if slashLocation.nil?
175+
# image
176+
if containerInventoryRecord["Image"].nil? || containerInventoryRecord["Image"].empty?
177+
containerInventoryRecord["Image"] = imageValue
178+
end
179+
else
180+
# repo/image
181+
if containerInventoryRecord["Repository"].nil? || containerInventoryRecord["Repository"].empty?
182+
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
183+
end
184+
if containerInventoryRecord["Image"].nil? || containerInventoryRecord["Image"].empty?
185+
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..-1]
186+
end
187+
end
188+
# if no tag specified, k8s assumes latest as imagetag and this is same behavior from docker API and from status.
189+
# Ref - https://kubernetes.io/docs/concepts/containers/images/#image-names
190+
if isDigestSpecified == false && (containerInventoryRecord["ImageTag"].nil? || containerInventoryRecord["ImageTag"].empty?)
191+
containerInventoryRecord["ImageTag"] = "latest"
192+
end
193+
end
194+
rescue => error
195+
$log.warn("KubernetesContainerInventory::addImageInfoToContainerInventoryRecord : Add Image Info to Container Inventory Records failed: #{error}")
196+
$log.debug_backtrace(error.backtrace)
197+
ApplicationInsightsUtility.sendExceptionTelemetry(error)
198+
end
199+
end
200+
172201
def getContainersInfoMap(podItem, isWindows)
173202
containersInfoMap = {}
174203
begin
@@ -405,4 +434,4 @@ def isSandboxingPod(podItem)
405434
return isKataRuntimePod
406435
end
407436
end
408-
end
437+
end

0 commit comments

Comments
 (0)