Skip to content

Commit

Permalink
Volumemgr refactoring
Browse files Browse the repository at this point in the history
We have different types of volumes which are handled differently (files,
 zvols and containers).
Let's split them into separate volume handlers and use common interface
for them.

Signed-off-by: Petr Fedchenkov <giggsoff@gmail.com>
  • Loading branch information
giggsoff committed Nov 10, 2022
1 parent 3eea19b commit 9cbacb8
Show file tree
Hide file tree
Showing 28 changed files with 1,096 additions and 921 deletions.
6 changes: 6 additions & 0 deletions pkg/pillar/cas/cas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/lf-edge/edge-containers/pkg/resolver"
"github.com/lf-edge/eve/pkg/pillar/types"
Expand Down Expand Up @@ -147,6 +148,11 @@ var knownCASHandlers = map[string]casDesc{
"containerd": {constructor: newContainerdCAS},
}

// CheckAndCorrectBlobHash checks if the blobHash has hash algo sha256 as prefix. If not then it'll prepend it.
func CheckAndCorrectBlobHash(blobHash string) string {
return fmt.Sprintf("sha256:%s", strings.TrimPrefix(blobHash, "sha256:"))
}

// NewCAS returns new CAS object with a new client of underlying implementor(selectedCAS).
// It's the caller/user's responsibility to close the respective client after use by calling CAS.CloseClient().
func NewCAS(selectedCAS string) (CAS, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/pillar/cmd/domainmgr/domainmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,8 @@ func doActivate(ctx *domainContext, config types.DomainConfig,
}
default:
// assume everything else to be disk formats
_, _, format, _, err := utils.GetVolumeSize(log, ctx.casClient, ds.FileLocation)
if err == nil && format != strings.ToLower(ds.Format.String()) {
format, err := utils.GetVolumeFormat(log, ds.FileLocation)
if err == nil && format != ds.Format {
err = fmt.Errorf("Disk format mismatch, format in config %v and output of qemu-img/zfs get %v\n"+
"Note: Format mismatch may be because of disk corruption also.",
ds.Format, format)
Expand Down
3 changes: 1 addition & 2 deletions pkg/pillar/cmd/volumemgr/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func getManifestsForBareBlob(ctx *volumemgrContext, image, rootHash string, size
}

//Adding config blob to blobStatuses list
configBlobStatus := lookupBlobStatus(ctx,
strings.Replace(desc[0].Digest.String(), "sha256:", "", 1))
configBlobStatus := ctx.LookupBlobStatus(strings.Replace(desc[0].Digest.String(), "sha256:", "", 1))
if configBlobStatus != nil {
blobStatuses = append(blobStatuses, configBlobStatus)
}
Expand Down
18 changes: 6 additions & 12 deletions pkg/pillar/cmd/volumemgr/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,13 @@ func resolveManifestSize(ctx *volumemgrContext, blob types.BlobStatus) int64 {
return size
}

// lookupBlobStatus look for a BlobStatus. Does not attempt to recreate one
// LookupBlobStatus look for a BlobStatus. Does not attempt to recreate one
// from VerifyImageStatus
func lookupBlobStatus(ctx *volumemgrContext, blobSha string) *types.BlobStatus {

func (ctxPtr *volumemgrContext) LookupBlobStatus(blobSha string) *types.BlobStatus {
if blobSha == "" {
return nil
}
pub := ctx.pubBlobStatus
pub := ctxPtr.pubBlobStatus
s, _ := pub.Get(blobSha)
if s == nil {
log.Tracef("lookupBlobStatus(%s) not found", blobSha)
Expand All @@ -407,7 +406,7 @@ func lookupOrCreateBlobStatus(ctx *volumemgrContext, blobSha string) *types.Blob
return nil
}
// Does it already exist?
blob := lookupBlobStatus(ctx, blobSha)
blob := ctx.LookupBlobStatus(blobSha)
if blob != nil {
return blob
}
Expand Down Expand Up @@ -494,7 +493,7 @@ func unpublishBlobStatus(ctx *volumemgrContext, blobs ...*types.BlobStatus) {
}
//If blob is loaded, then remove it from CAS
if blob.State == types.LOADED {
if err := ctx.casClient.RemoveBlob(checkAndCorrectBlobHash(blob.Sha256)); err != nil {
if err := ctx.casClient.RemoveBlob(cas.CheckAndCorrectBlobHash(blob.Sha256)); err != nil {
err := fmt.Errorf("unpublishBlobStatus: Exception while removing loaded blob %s: %s",
blob.Sha256, err.Error())
log.Errorf(err.Error())
Expand Down Expand Up @@ -537,7 +536,7 @@ func populateInitBlobStatus(ctx *volumemgrContext) {
log.Functionf("populateInitBlobStatus: blob %s in CAS could not get mediaType", blobInfo.Digest)
continue
}
if lookupBlobStatus(ctx, blobInfo.Digest) == nil {
if ctx.LookupBlobStatus(blobInfo.Digest) == nil {
log.Functionf("populateInitBlobStatus: Found blob %s in CAS", blobInfo.Digest)
blobStatus := &types.BlobStatus{
Sha256: strings.TrimPrefix(blobInfo.Digest, "sha256:"),
Expand Down Expand Up @@ -608,11 +607,6 @@ func gcImagesFromCAS(ctx *volumemgrContext) {
}
}

//checkAndCorrectBlobHash checks if the blobHash has hash algo sha256 as prefix. If not then it'll prepend it.
func checkAndCorrectBlobHash(blobHash string) string {
return fmt.Sprintf("sha256:%s", strings.TrimPrefix(blobHash, "sha256:"))
}

// lookupImageCAS check if an image reference exists
func lookupImageCAS(reference string, client cas.CAS) bool {
hash, err := client.GetImageHash(reference)
Expand Down
Loading

0 comments on commit 9cbacb8

Please sign in to comment.