Skip to content

Commit

Permalink
Merge pull request #243 from 14rcole/stoneintg-505
Browse files Browse the repository at this point in the history
fix(stoneintg-505): Use snapshot components when creating SEB
  • Loading branch information
14rcole authored Jul 19, 2023
2 parents 0046c74 + bb48058 commit 96c948e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion controllers/snapshot/snapshot_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (a *Adapter) EnsureSnapshotEnvironmentBindingExist() (reconciler.OperationR
return reconciler.RequeueWithError(err)
}

components, err := a.loader.GetAllApplicationComponents(a.client, a.context, a.application)
components, err := a.loader.GetAllSnapshotComponents(a.client, a.context, a.snapshot)
if err != nil {
return reconciler.RequeueWithError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
Resource: env,
},
{
ContextKey: loader.ApplicationComponentsContextKey,
ContextKey: loader.SnapshotComponentsContextKey,
Resource: []applicationapiv1alpha1.Component{*hasComp},
},
{
Expand Down
22 changes: 22 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ObjectLoader interface {
GetAllEnvironments(c client.Client, ctx context.Context, application *applicationapiv1alpha1.Application) (*[]applicationapiv1alpha1.Environment, error)
GetReleasesWithSnapshot(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*[]releasev1alpha1.Release, error)
GetAllApplicationComponents(c client.Client, ctx context.Context, application *applicationapiv1alpha1.Application) (*[]applicationapiv1alpha1.Component, error)
GetAllSnapshotComponents(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*[]applicationapiv1alpha1.Component, error)
GetApplicationFromSnapshot(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*applicationapiv1alpha1.Application, error)
GetComponentFromSnapshot(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*applicationapiv1alpha1.Component, error)
GetComponentFromPipelineRun(c client.Client, ctx context.Context, pipelineRun *tektonv1beta1.PipelineRun) (*applicationapiv1alpha1.Component, error)
Expand Down Expand Up @@ -119,6 +120,27 @@ func (l *loader) GetAllApplicationComponents(c client.Client, ctx context.Contex
return &applicationComponents.Items, nil
}

// GetAllSnapshotComponents loads from the cluster all Components associated with the given Snapshot.
// If the Snapshot doesn't have any Components or this is not found in the cluster, an error will be returned.
func (l *loader) GetAllSnapshotComponents(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*[]applicationapiv1alpha1.Component, error) {
components := []applicationapiv1alpha1.Component{}

for _, sc := range snapshot.Spec.Components {
component := &applicationapiv1alpha1.Component{}
err := c.Get(ctx, types.NamespacedName{
Namespace: snapshot.Namespace,
Name: sc.Name,
}, component)
if err != nil {
return nil, err
}

components = append(components, *component)
}

return &components, nil
}

// GetApplicationFromSnapshot loads from the cluster the Application referenced in the given Snapshot.
// If the Snapshot doesn't specify an Component or this is not found in the cluster, an error will be returned.
func (l *loader) GetApplicationFromSnapshot(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*applicationapiv1alpha1.Application, error) {
Expand Down
10 changes: 10 additions & 0 deletions loader/loader_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
IntegrationTestScenarioContextKey contextKey = iota
TaskRunContextKey contextKey = iota
ApplicationComponentsContextKey contextKey = iota
SnapshotComponentsContextKey contextKey = iota
EnvironmentContextKey contextKey = iota
ReleaseContextKey contextKey = iota
PipelineRunsContextKey contextKey = iota
Expand Down Expand Up @@ -122,6 +123,15 @@ func (l *mockLoader) GetAllApplicationComponents(c client.Client, ctx context.Co
return &components, err
}

// GetAllSnapshotComponents returns the resource and error passed as values of the context.
func (l *mockLoader) GetAllSnapshotComponents(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*[]applicationapiv1alpha1.Component, error) {
if ctx.Value(SnapshotComponentsContextKey) == nil {
return l.loader.GetAllSnapshotComponents(c, ctx, snapshot)
}
components, err := getMockedResourceAndErrorFromContext(ctx, SnapshotComponentsContextKey, []applicationapiv1alpha1.Component{})
return &components, err
}

// GetApplicationFromSnapshot returns the resource and error passed as values of the context.
func (l *mockLoader) GetApplicationFromSnapshot(c client.Client, ctx context.Context, snapshot *applicationapiv1alpha1.Snapshot) (*applicationapiv1alpha1.Application, error) {
if ctx.Value(ApplicationContextKey) == nil {
Expand Down
15 changes: 15 additions & 0 deletions loader/loader_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ var _ = Describe("Release Adapter", Ordered, func() {
})
})

Context("When calling GetAllSnapshotComponents", func() {
It("returns resource and error from the context", func() {
snapshotComponents := []applicationapiv1alpha1.Component{}
mockContext := GetMockedContext(ctx, []MockData{
{
ContextKey: SnapshotComponentsContextKey,
Resource: snapshotComponents,
},
})
resource, err := loader.GetAllSnapshotComponents(nil, mockContext, nil)
Expect(resource).To(Equal(&snapshotComponents))
Expect(err).To(BeNil())
})
})

Context("When calling GetApplicationFromSnapshot", func() {
It("returns resource and error from the context", func() {
application := &applicationapiv1alpha1.Application{}
Expand Down
44 changes: 44 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,50 @@ var _ = Describe("Loader", Ordered, func() {
Expect(applicationComponents).NotTo(BeNil())
})

It("ensures the Snapshot Components can be found ", func() {
snapshotComponents, err := loader.GetAllSnapshotComponents(k8sClient, ctx, hasSnapshot)
Expect(err).To(BeNil())
Expect(snapshotComponents).NotTo(BeNil())
})

It("ensures GetAllSnapshotComponents does not gather non-snapshot application components ", func() {
// otherComp has the same namespace and application as the snapshot,
// but does not belong to the snapshot itself. When creating SEBs
// for older snapshots we do not want to accidentally add components
// for newer snapshots.
otherComp := &applicationapiv1alpha1.Component{
ObjectMeta: metav1.ObjectMeta{
Name: "other-component",
Namespace: "default",
},
Spec: applicationapiv1alpha1.ComponentSpec{
ComponentName: "other-component",
Application: applicationName,
ContainerImage: "",
Source: applicationapiv1alpha1.ComponentSource{
ComponentSourceUnion: applicationapiv1alpha1.ComponentSourceUnion{
GitSource: &applicationapiv1alpha1.GitSource{
URL: SampleRepoLink,
},
},
},
},
Status: applicationapiv1alpha1.ComponentStatus{
LastBuiltCommit: "",
},
}
Expect(k8sClient.Create(ctx, otherComp)).Should(Succeed())
defer k8sClient.Delete(ctx, otherComp)

snapshotComponents, err := loader.GetAllSnapshotComponents(k8sClient, ctx, hasSnapshot)
Expect(err).To(BeNil())
Expect(snapshotComponents).NotTo(BeNil())
Expect(*snapshotComponents).To(HaveLen(1))
for _, comp := range *snapshotComponents {
Expect(comp.Name).NotTo(Equal("other-component"))
}
})

It("ensures we can get an Application from a Snapshot ", func() {
app, err := loader.GetApplicationFromSnapshot(k8sClient, ctx, hasSnapshot)
Expect(err).To(BeNil())
Expand Down

0 comments on commit 96c948e

Please sign in to comment.