This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Schrodi
committed
Jun 23, 2021
1 parent
59a9736
commit 9c05826
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ctf | ||
|
||
import ( | ||
"context" | ||
|
||
cdv2 "github.com/gardener/component-spec/bindings-go/apis/v2" | ||
) | ||
|
||
// ListResolver describes a ComponentResolver using a list of Component Descriptors | ||
type ListResolver struct { | ||
List *cdv2.ComponentDescriptorList | ||
blobResolver *AggregatedBlobResolver | ||
} | ||
|
||
// NewListResolver creates a new list resolver. | ||
func NewListResolver(list *cdv2.ComponentDescriptorList, resolvers ...BlobResolver) (*ListResolver, error) { | ||
lr := &ListResolver{ | ||
List: list, | ||
} | ||
if len(resolvers) != 0 { | ||
blobResolver, err := NewAggregatedBlobResolver(resolvers...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
lr.blobResolver = blobResolver | ||
} | ||
return lr, nil | ||
} | ||
|
||
var _ ComponentResolver = &ListResolver{} | ||
|
||
func (l ListResolver) Resolve(_ context.Context, repoCtx cdv2.Repository, name, version string) (*cdv2.ComponentDescriptor, error) { | ||
for _, comp := range l.List.Components { | ||
if !cdv2.TypedObjectEqual(repoCtx, comp.GetEffectiveRepositoryContext()) { | ||
continue | ||
} | ||
if comp.Name == name && comp.Version == version { | ||
return comp.DeepCopy(), nil | ||
} | ||
} | ||
|
||
return nil, NotFoundError | ||
} | ||
|
||
func (l ListResolver) ResolveWithBlobResolver(ctx context.Context, repoCtx cdv2.Repository, name, version string) (*cdv2.ComponentDescriptor, BlobResolver, error) { | ||
cd, err := l.Resolve(ctx, repoCtx, name, version) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if l.blobResolver == nil { | ||
return nil, nil, BlobResolverNotDefinedError | ||
} | ||
return cd, l.blobResolver, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2021 Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctf_test | ||
|
||
import ( | ||
"context" | ||
|
||
cdv2 "github.com/gardener/component-spec/bindings-go/apis/v2" | ||
"github.com/gardener/component-spec/bindings-go/ctf" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ListResolver", func() { | ||
|
||
It("should resolve a component from a list of one component descriptor", func() { | ||
cd := cdv2.ComponentDescriptor{} | ||
cd.Name = "example.com/a" | ||
cd.Version = "0.0.0" | ||
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", "")) | ||
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx) | ||
|
||
lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{ | ||
Components: []cdv2.ComponentDescriptor{cd}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
res, err := lr.Resolve(context.TODO(), &repoCtx, "example.com/a", "0.0.0") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(res.Name).To(Equal("example.com/a")) | ||
}) | ||
|
||
It("should resolve a component from a list of multiple component descriptors", func() { | ||
cd := cdv2.ComponentDescriptor{} | ||
cd.Name = "example.com/a" | ||
cd.Version = "0.0.0" | ||
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", "")) | ||
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx) | ||
|
||
cd2 := cdv2.ComponentDescriptor{} | ||
cd2.Name = "example.com/a" | ||
cd2.Version = "0.0.0" | ||
cd2.Labels = cdv2.Labels{ | ||
{ | ||
Name: "test", | ||
}, | ||
} | ||
repoCtx2, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry2", "")) | ||
cd2.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx2) | ||
|
||
lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{ | ||
Components: []cdv2.ComponentDescriptor{cd, cd2}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
res, err := lr.Resolve(context.TODO(), &repoCtx2, "example.com/a", "0.0.0") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(res.Name).To(Equal("example.com/a")) | ||
Expect(res.Labels).To(ContainElement(cdv2.Label{ | ||
Name: "test", | ||
})) | ||
|
||
res, err = lr.Resolve(context.TODO(), &repoCtx, "example.com/a", "0.0.0") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(res.Name).To(Equal("example.com/a")) | ||
Expect(res.Labels).ToNot(ContainElement(cdv2.Label{ | ||
Name: "test", | ||
})) | ||
}) | ||
|
||
It("should not resolve a component if the repository contexts do not match", func() { | ||
cd := cdv2.ComponentDescriptor{} | ||
cd.Name = "example.com/a" | ||
cd.Version = "0.0.0" | ||
repoCtx, _ := cdv2.NewUnstructured(cdv2.NewOCIRegistryRepository("example.com/registry", "")) | ||
cd.RepositoryContexts = append(cd.RepositoryContexts, &repoCtx) | ||
|
||
lr, err := ctf.NewListResolver(&cdv2.ComponentDescriptorList{ | ||
Components: []cdv2.ComponentDescriptor{cd}, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
_, err = lr.Resolve(context.TODO(), &repoCtx, "example.com/b", "0.0.0") | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err).To(Equal(ctf.NotFoundError)) | ||
}) | ||
|
||
}) |