@@ -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