Skip to content

Commit b17b70b

Browse files
authored
Merge pull request #1270 from hongweiliu17/STONEINTG-1317
fix(STONEINTG-1317): annotate snapshot for the missed component
2 parents 0be0711 + 37174ca commit b17b70b

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

gitops/snapshot.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ func HasSnapshotRerunLabelChanged(objectOld, objectNew client.Object) bool {
833833
func PrepareSnapshot(ctx context.Context, adapterClient client.Client, application *applicationapiv1alpha1.Application, applicationComponents *[]applicationapiv1alpha1.Component, component *applicationapiv1alpha1.Component, newContainerImage string, newComponentSource *applicationapiv1alpha1.ComponentSource) (*applicationapiv1alpha1.Snapshot, error) {
834834
log := log.FromContext(ctx)
835835
var snapshotComponents []applicationapiv1alpha1.SnapshotComponent
836+
var invalidComponents []string
836837
for _, applicationComponent := range *applicationComponents {
837838
applicationComponent := applicationComponent // G601
838839
containerImage := applicationComponent.Status.LastPromotedImage
@@ -856,6 +857,7 @@ func PrepareSnapshot(ctx context.Context, adapterClient client.Client, applicati
856857
componentSource, err = GetComponentSourceFromComponent(&applicationComponent)
857858
if err != nil {
858859
log.Error(err, "component cannot be added to snapshot for application due to missing git source", "component.Name", applicationComponent.Name)
860+
invalidComponents = append(invalidComponents, applicationComponent.Name)
859861
continue
860862
}
861863
}
@@ -866,13 +868,15 @@ func PrepareSnapshot(ctx context.Context, adapterClient client.Client, applicati
866868
// including a component that is incomplete.
867869
if containerImage == "" {
868870
log.Info("component cannot be added to snapshot for application due to missing containerImage", "component.Name", applicationComponent.Name)
871+
invalidComponents = append(invalidComponents, applicationComponent.Name)
869872
continue
870873
} else {
871874
// if the containerImage doesn't have a valid digest, the component
872875
// will not be added to snapshot
873876
err = ValidateImageDigest(containerImage)
874877
if err != nil {
875878
log.Error(err, "component cannot be added to snapshot for application due to invalid digest in containerImage", "component.Name", applicationComponent.Name)
879+
invalidComponents = append(invalidComponents, applicationComponent.Name)
876880
continue
877881
}
878882
snapshotComponents = append(snapshotComponents, applicationapiv1alpha1.SnapshotComponent{
@@ -884,7 +888,7 @@ func PrepareSnapshot(ctx context.Context, adapterClient client.Client, applicati
884888
}
885889

886890
if len(snapshotComponents) == 0 {
887-
return nil, helpers.NewMissingValidComponentError(component.Name)
891+
return nil, helpers.NewMissingValidComponentError(strings.Join(invalidComponents, ", "))
888892
}
889893
snapshot := NewSnapshot(application, &snapshotComponents)
890894

@@ -895,6 +899,12 @@ func PrepareSnapshot(ctx context.Context, adapterClient client.Client, applicati
895899
}
896900
}
897901

902+
if len(invalidComponents) > 0 {
903+
if err := metadata.SetAnnotation(snapshot, helpers.CreateSnapshotAnnotationName, fmt.Sprintf("Component(s) '%s' is(are) not included in snapshot due to missing valid containerImage or git source", strings.Join(invalidComponents, ", "))); err != nil {
904+
return nil, fmt.Errorf("failed to set annotation %s: %w", SnapshotGitSourceRepoURLAnnotation, err)
905+
}
906+
}
907+
898908
err := ctrl.SetControllerReference(application, snapshot, adapterClient.Scheme())
899909
if err != nil {
900910
return nil, err

gitops/snapshot_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
102102
Namespace: namespace,
103103
},
104104
Spec: applicationapiv1alpha1.ComponentSpec{
105-
ComponentName: "bad-component",
106-
Application: applicationName,
105+
ComponentName: "bad-component",
106+
Application: applicationName,
107+
ContainerImage: sampleImage,
107108
},
108109
}
109110
Expect(k8sClient.Create(ctx, badComp)).Should(Succeed())
@@ -678,7 +679,7 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
678679
},
679680
},
680681
}
681-
allApplicationComponents := &[]applicationapiv1alpha1.Component{*hasComp, *hasComp2}
682+
allApplicationComponents := &[]applicationapiv1alpha1.Component{*hasComp, *hasComp2, *badComp}
682683
snapshot, err := gitops.PrepareSnapshot(ctx, k8sClient, hasApp, allApplicationComponents, hasComp, validImagePullSpec, componentSource)
683684
Expect(snapshot).NotTo(BeNil())
684685
Expect(err).NotTo(HaveOccurred())
@@ -688,6 +689,7 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
688689
snapshotComponent := snapshotComponent
689690
Expect(snapshotComponent.ContainerImage).NotTo(Equal(invalidImagePullSpec))
690691
}
692+
Expect(snapshot.Annotations["test.appstudio.openshift.io/create-snapshot-status"]).To(Equal("Component(s) 'second-component, bad-component' is(are) not included in snapshot due to missing valid containerImage or git source"))
691693
})
692694

693695
It("Return false when the image url contains invalid digest", func() {

helpers/errorhandlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ func IsInvalidImageDigestError(err error) bool {
8080
return getReason(err) == ReasonInvalidImageDigestError
8181
}
8282

83-
func NewMissingValidComponentError(componentName string) error {
83+
func NewMissingValidComponentError(componentNames string) error {
8484
return &IntegrationError{
8585
Reason: ReasonMissingValidComponentError,
86-
Message: fmt.Sprintf("The only one component %s is invalid, valid .Status.LastPromotedImage is missing", componentName),
86+
Message: fmt.Sprintf("The component(s) '%s' is(are) invalid due to missing valid container image or git source", componentNames),
8787
}
8888
}
8989

helpers/errorhandlers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var _ = Describe("Helpers for error handlers", Ordered, func() {
9898
It("Can define MissingValidComponentError", func() {
9999
err := helpers.NewMissingValidComponentError("componentName")
100100
Expect(helpers.IsMissingValidComponentError(err)).To(BeTrue())
101-
Expect(err.Error()).To(Equal("The only one component componentName is invalid, valid .Status.LastPromotedImage is missing"))
101+
Expect(err.Error()).To(Equal("The component(s) 'componentName' is(are) invalid due to missing valid container image or git source"))
102102
})
103103

104104
It("Can handle non integration error", func() {

internal/controller/buildpipeline/buildpipeline_adapter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
519519
snapshot, err := gitops.PrepareSnapshot(adapter.context, adapter.client, hasApp, applicationComponents, hasComp, imagePullSpec, componentSource)
520520
Expect(snapshot).NotTo(BeNil())
521521
Expect(err).ToNot(HaveOccurred())
522-
Expect(snapshot).NotTo(BeNil())
523522
Expect(snapshot.Spec.Components).To(HaveLen(1), "One component should have been added to snapshot. Other component should have been omited due to empty ContainerImage field or missing valid digest")
524523
Expect(snapshot.Spec.Components[0].Name).To(Equal(hasComp.Name), "The built component should have been added to the snapshot")
524+
Expect(snapshot.Annotations[helpers.CreateSnapshotAnnotationName]).To(Equal("Component(s) 'another-component-sample' is(are) not included in snapshot due to missing valid containerImage or git source"))
525525
})
526526

527527
It("ensures that snapshot has label pointing to build pipelinerun", func() {

0 commit comments

Comments
 (0)