-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
component version check as lib function (#675)
## Description Move basic component check functionality from check command into library. Package `.../ocm/check` now provides basic chackes bases on some options uses to specify which checks should be performed. ## What type of PR is this? (check all applicable) - [x] 🍕 Feature - [ ] 🐛 Bug Fix - [ ] 📝 Documentation Update - [ ] 🎨 Style - [ ] 🧑💻 Code Refactor - [ ] 🔥 Performance Improvements - [x] ✅ Test - [ ] 🤖 Build - [ ] 🔁 CI - [ ] 📦 Chore (Release) - [ ] ⏩ Revert ## Related Tickets & Documents <!-- Please use this format link issue numbers: Fixes #123 https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword --> - Related Issue # (issue) - Closes # (issue) - Fixes # (issue) > Remove if not applicable ## Screenshots <!-- Visual changes require screenshots --> ## Added tests? - [ ] 👍 yes - [ ] 🙅 no, because they aren't needed - [ ] 🙋 no, because I need help - [ ] Separate ticket for tests # (issue/pr) Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration ## Added to documentation? - [ ] 📜 README.md - [ ] 🙅 no documentation needed ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
- Loading branch information
1 parent
2c12eb2
commit 09b2145
Showing
9 changed files
with
462 additions
and
124 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
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
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,151 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package check | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/open-component-model/ocm/pkg/common" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
"github.com/open-component-model/ocm/pkg/optionutils" | ||
) | ||
|
||
type Result struct { | ||
Missing Missing `json:"missing,omitempty"` | ||
Resources []metav1.Identity `json:"resources,omitempty"` | ||
Sources []metav1.Identity `json:"sources,omitempty"` | ||
} | ||
|
||
func newResult() *Result { | ||
return &Result{Missing: Missing{}} | ||
} | ||
|
||
func (r *Result) IsEmpty() bool { | ||
if r == nil { | ||
return true | ||
} | ||
return len(r.Missing) == 0 && len(r.Resources) == 0 && len(r.Sources) == 0 | ||
} | ||
|
||
type Missing map[common.NameVersion]common.History | ||
|
||
func (n Missing) MarshalJSON() ([]byte, error) { | ||
m := map[string]common.History{} | ||
for k, v := range n { | ||
m[k.String()] = v | ||
} | ||
return json.Marshal(m) | ||
} | ||
|
||
type Cache = map[common.NameVersion]*Result | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
// Check provides a check object for checking component versions | ||
// to completely available in an ocm repository. | ||
// By default, it only checks the component reference closure | ||
// to be in the same repository. | ||
// Optionally, it is possible to check for inlined | ||
// resources and sources, also. | ||
func Check(opts ...Option) *Options { | ||
return optionutils.EvalOptions(opts...) | ||
} | ||
|
||
func (a *Options) For(cv ocm.ComponentVersionAccess) (*Result, error) { | ||
cache := Cache{} | ||
return a.handle(cache, cv, common.History{common.VersionedElementKey(cv)}) | ||
} | ||
|
||
func (a *Options) ForId(repo ocm.Repository, id common.NameVersion) (*Result, error) { | ||
cv, err := repo.LookupComponentVersion(id.GetName(), id.GetVersion()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer cv.Close() | ||
return a.For(cv) | ||
} | ||
|
||
func (a *Options) check(cache Cache, repo ocm.Repository, id common.NameVersion, h common.History) (*Result, error) { | ||
if r, ok := cache[id]; ok { | ||
return r, nil | ||
} | ||
|
||
err := h.Add(ocm.KIND_COMPONENTVERSION, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cv, err := repo.LookupComponentVersion(id.GetName(), id.GetVersion()) | ||
if err != nil { | ||
if !errors.IsErrNotFound(err) { | ||
return nil, err | ||
} | ||
err = nil | ||
} | ||
|
||
var r *Result | ||
if cv == nil { | ||
r = &Result{Missing: Missing{id: h}} | ||
} else { | ||
defer cv.Close() | ||
r, err = a.handle(cache, cv, h) | ||
} | ||
cache[id] = r | ||
return r, err | ||
} | ||
|
||
func (a *Options) handle(cache Cache, cv ocm.ComponentVersionAccess, h common.History) (*Result, error) { | ||
result := newResult() | ||
|
||
for _, r := range cv.GetDescriptor().References { | ||
id := common.NewNameVersion(r.ComponentName, r.Version) | ||
n, err := a.check(cache, cv.Repository(), id, h) | ||
if err != nil { | ||
return result, err | ||
} | ||
if n != nil && len(n.Missing) > 0 { | ||
for k, v := range n.Missing { | ||
result.Missing[k] = v | ||
} | ||
} | ||
} | ||
|
||
var err error | ||
|
||
list := errors.ErrorList{} | ||
if optionutils.AsBool(a.CheckLocalResources) { | ||
result.Resources, err = a.checkArtifacts(cv.GetContext(), cv.GetDescriptor().Resources) | ||
list.Add(err) | ||
} | ||
if optionutils.AsBool(a.CheckLocalSources) { | ||
result.Sources, err = a.checkArtifacts(cv.GetContext(), cv.GetDescriptor().Sources) | ||
list.Add(err) | ||
} | ||
if result.IsEmpty() { | ||
result = nil | ||
} | ||
return result, list.Result() | ||
} | ||
|
||
func (a *Options) checkArtifacts(ctx ocm.Context, accessor compdesc.ElementAccessor) ([]metav1.Identity, error) { | ||
var result []metav1.Identity | ||
|
||
list := errors.ErrorList{} | ||
for i := 0; i < accessor.Len(); i++ { | ||
e := accessor.Get(i).(compdesc.ElementArtifactAccessor) | ||
|
||
m, err := ctx.AccessSpecForSpec(e.GetAccess()) | ||
if err == nil { | ||
if !m.IsLocal(ctx) { | ||
result = append(result, e.GetMeta().GetIdentity(accessor)) | ||
} | ||
} else { | ||
list.Add(err) | ||
} | ||
} | ||
return result, list.Result() | ||
} |
Oops, something went wrong.