Skip to content

Commit ccfdd1b

Browse files
committed
Address Copilot comment
1 parent e0e81f9 commit ccfdd1b

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Microsoft.ComponentDetection.Detectors/kubernetes/KubernetesComponentDetector.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,19 @@ private static bool IsKubernetesManifest(YamlMappingNode rootMapping)
210210

211211
private void ExtractImageReferences(YamlMappingNode rootMapping, ISingleFileComponentRecorder recorder, string fileLocation)
212212
{
213-
// For Pod, the spec is at the top level
214-
// For Deployment/StatefulSet/etc, the pod spec is at spec.template.spec
213+
// PodTemplate has pod spec at root.template.spec.
214+
var rootTemplate = GetMappingChild(rootMapping, "template");
215+
if (rootTemplate != null)
216+
{
217+
var rootTemplateSpec = GetMappingChild(rootTemplate, "spec");
218+
if (rootTemplateSpec != null)
219+
{
220+
this.ExtractContainerImages(rootTemplateSpec, recorder, fileLocation);
221+
}
222+
}
223+
224+
// For Pod, the spec is at the top level.
225+
// For Deployment/StatefulSet/etc, the pod spec is at spec.template.spec.
215226
var spec = GetMappingChild(rootMapping, "spec");
216227
if (spec == null)
217228
{

test/Microsoft.ComponentDetection.Detectors.Tests/KubernetesComponentDetectorTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ public async Task TestK8s_PodWithContainerImageAsync()
4343
dockerRef.Tag.Should().Be("1.21");
4444
}
4545

46+
[TestMethod]
47+
public async Task TestK8s_PodTemplateAsync()
48+
{
49+
var manifest = @"
50+
apiVersion: v1
51+
kind: PodTemplate
52+
metadata:
53+
name: my-template
54+
template:
55+
metadata:
56+
labels:
57+
app: my-app
58+
spec:
59+
containers:
60+
- name: app
61+
image: nginx:1.21
62+
";
63+
64+
var (scanResult, componentRecorder) = await this.DetectorTestUtility
65+
.WithFile("podtemplate.yaml", manifest)
66+
.ExecuteDetectorAsync();
67+
68+
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
69+
var components = componentRecorder.GetDetectedComponents();
70+
components.Should().ContainSingle();
71+
72+
var dockerRef = components.First().Component as DockerReferenceComponent;
73+
dockerRef.Should().NotBeNull();
74+
dockerRef.Repository.Should().Be("library/nginx");
75+
dockerRef.Tag.Should().Be("1.21");
76+
}
77+
4678
[TestMethod]
4779
public async Task TestK8s_DeploymentWithMultipleContainersAsync()
4880
{

0 commit comments

Comments
 (0)