Skip to content

Commit

Permalink
Merge 33ec102 into 6c60284
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanford committed Jun 13, 2023
2 parents 6c60284 + 33ec102 commit 3c775b8
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions pkg/cache/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"path/filepath"
"strings"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/model"
"github.com/operator-framework/operator-registry/pkg/api"
"github.com/operator-framework/operator-registry/pkg/registry"
"k8s.io/apimachinery/pkg/util/sets"
)

var _ Cache = &JSON{}
Expand Down Expand Up @@ -135,6 +137,17 @@ func NewJSON(baseDir string) *JSON {
return &JSON{baseDir: baseDir}
}

func LoadJSONFromModel(baseDir string, m model.Model) (*JSON, error) {
c := NewJSON(baseDir)
if err := c.buildFromModel(m); err != nil {
return nil, err

Check warning on line 143 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L141-L143

Added lines #L141 - L143 were not covered by tests
}
if err := c.Load(); err != nil {
return nil, err

Check warning on line 146 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L145-L146

Added lines #L145 - L146 were not covered by tests
}
return c, nil

Check warning on line 148 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L148

Added line #L148 was not covered by tests
}

const (
jsonDigestFile = "digest"
jsonDir = "cache"
Expand Down Expand Up @@ -179,6 +192,30 @@ func (q *JSON) computeDigest(fbcFsys fs.FS) (string, error) {
}

func (q *JSON) Build(ctx context.Context, fbcFsys fs.FS) error {
fbc, err := declcfg.LoadFS(ctx, fbcFsys)
if err != nil {
return err

Check warning on line 197 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L197

Added line #L197 was not covered by tests
}
fbcModel, err := declcfg.ConvertToModel(*fbc)
if err != nil {
return err

Check warning on line 201 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L201

Added line #L201 was not covered by tests
}

if err := q.buildFromModel(fbcModel); err != nil {
return err

Check warning on line 205 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L205

Added line #L205 was not covered by tests
}

digest, err := q.computeDigest(fbcFsys)
if err != nil {
return err

Check warning on line 210 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L210

Added line #L210 was not covered by tests
}
if err := os.WriteFile(filepath.Join(q.baseDir, jsonDigestFile), []byte(digest), jsonCacheModeFile); err != nil {
return err

Check warning on line 213 in pkg/cache/json.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/json.go#L213

Added line #L213 was not covered by tests
}
return nil
}

func (q *JSON) buildFromModel(fbcModel model.Model) error {
// ensure that generated cache is available to all future users
oldUmask := umask(000)
defer umask(oldUmask)
Expand All @@ -190,15 +227,6 @@ func (q *JSON) Build(ctx context.Context, fbcFsys fs.FS) error {
return fmt.Errorf("ensure clean base directory: %v", err)
}

fbc, err := declcfg.LoadFS(ctx, fbcFsys)
if err != nil {
return err
}
fbcModel, err := declcfg.ConvertToModel(*fbc)
if err != nil {
return err
}

pkgs, err := packagesFromModel(fbcModel)
if err != nil {
return err
Expand Down Expand Up @@ -232,13 +260,6 @@ func (q *JSON) Build(ctx context.Context, fbcFsys fs.FS) error {
}
}
}
digest, err := q.computeDigest(fbcFsys)
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(q.baseDir, jsonDigestFile), []byte(digest), jsonCacheModeFile); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 3c775b8

Please sign in to comment.