|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "sync" |
| 11 | + |
| 12 | + catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1" |
| 13 | + |
| 14 | + "github.com/operator-framework/operator-controller/internal/catalogmetadata/client" |
| 15 | +) |
| 16 | + |
| 17 | +var _ client.Fetcher = &filesystemCache{} |
| 18 | + |
| 19 | +// NewFilesystemCache returns a client.Fetcher implementation that uses a |
| 20 | +// local filesystem to cache Catalog contents. When fetching the Catalog contents |
| 21 | +// it will: |
| 22 | +// - Check if the Catalog is cached |
| 23 | +// - IF !cached it will fetch from the catalogd HTTP server and cache the response |
| 24 | +// - IF cached it will verify the cache is up to date. If it is up to date it will return |
| 25 | +// the cached contents, if not it will fetch the new contents from the catalogd HTTP |
| 26 | +// server and update the cached contents. |
| 27 | +func NewFilesystemCache(cachePath string, client *http.Client) client.Fetcher { |
| 28 | + return &filesystemCache{ |
| 29 | + cachePath: cachePath, |
| 30 | + mutex: sync.RWMutex{}, |
| 31 | + client: client, |
| 32 | + cacheDataByCatalogName: map[string]cacheData{}, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// cacheData holds information about a catalog |
| 37 | +// other than it's contents that is used for |
| 38 | +// making decisions on when to attempt to refresh |
| 39 | +// the cache. |
| 40 | +type cacheData struct { |
| 41 | + ResolvedRef string |
| 42 | +} |
| 43 | + |
| 44 | +// FilesystemCache is a cache that |
| 45 | +// uses the local filesystem for caching |
| 46 | +// catalog contents. It will fetch catalog |
| 47 | +// contents if the catalog does not already |
| 48 | +// exist in the cache. |
| 49 | +type filesystemCache struct { |
| 50 | + mutex sync.RWMutex |
| 51 | + cachePath string |
| 52 | + client *http.Client |
| 53 | + cacheDataByCatalogName map[string]cacheData |
| 54 | +} |
| 55 | + |
| 56 | +// FetchCatalogContents implements the client.Fetcher interface and |
| 57 | +// will fetch the contents for the provided Catalog from the filesystem. |
| 58 | +// If the provided Catalog has not yet been cached, it will make a GET |
| 59 | +// request to the Catalogd HTTP server to get the Catalog contents and cache |
| 60 | +// them. The cache will be updated automatically if a Catalog is noticed to |
| 61 | +// have a different resolved image reference. |
| 62 | +// The Catalog provided to this function is expected to: |
| 63 | +// - Be non-nil |
| 64 | +// - Have a non-nil Catalog.Status.ResolvedSource.Image |
| 65 | +// This ensures that we are only attempting to fetch catalog contents for Catalog |
| 66 | +// resources that have been successfully reconciled, unpacked, and are being served. |
| 67 | +// These requirements help ensure that we can rely on status conditions to determine |
| 68 | +// when to issue a request to update the cached Catalog contents. |
| 69 | +func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *catalogd.Catalog) (io.ReadCloser, error) { |
| 70 | + if catalog == nil { |
| 71 | + return nil, fmt.Errorf("error: provided catalog must be non-nil") |
| 72 | + } |
| 73 | + |
| 74 | + if catalog.Status.ResolvedSource == nil { |
| 75 | + return nil, fmt.Errorf("error: catalog %q has a nil status.resolvedSource value", catalog.Name) |
| 76 | + } |
| 77 | + |
| 78 | + if catalog.Status.ResolvedSource.Image == nil { |
| 79 | + return nil, fmt.Errorf("error: catalog %q has a nil status.resolvedSource.image value", catalog.Name) |
| 80 | + } |
| 81 | + |
| 82 | + cacheDir := filepath.Join(fsc.cachePath, catalog.Name) |
| 83 | + cacheFilePath := filepath.Join(cacheDir, "data.json") |
| 84 | + |
| 85 | + fsc.mutex.RLock() |
| 86 | + if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok { |
| 87 | + if catalog.Status.ResolvedSource.Image.Ref == data.ResolvedRef { |
| 88 | + fsc.mutex.RUnlock() |
| 89 | + return os.Open(cacheFilePath) |
| 90 | + } |
| 91 | + } |
| 92 | + fsc.mutex.RUnlock() |
| 93 | + |
| 94 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, catalog.Status.ContentURL, nil) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("error forming request: %s", err) |
| 97 | + } |
| 98 | + |
| 99 | + resp, err := fsc.client.Do(req) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("error performing request: %s", err) |
| 102 | + } |
| 103 | + defer resp.Body.Close() |
| 104 | + |
| 105 | + if resp.StatusCode != http.StatusOK { |
| 106 | + return nil, fmt.Errorf("error: received unexpected response status code %d", resp.StatusCode) |
| 107 | + } |
| 108 | + |
| 109 | + fsc.mutex.Lock() |
| 110 | + defer fsc.mutex.Unlock() |
| 111 | + |
| 112 | + // make sure we only write if this info hasn't been updated |
| 113 | + // by another thread. The check here, if multiple threads are |
| 114 | + // updating this, has no way to tell if the current ref is the |
| 115 | + // newest possible ref. If another thread has already updated |
| 116 | + // this to be the same value, skip the write logic and return |
| 117 | + // the cached contents |
| 118 | + if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok { |
| 119 | + if data.ResolvedRef == catalog.Status.ResolvedSource.Image.Ref { |
| 120 | + return os.Open(cacheFilePath) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if err = os.MkdirAll(cacheDir, os.ModePerm); err != nil { |
| 125 | + return nil, fmt.Errorf("error creating cache directory for Catalog %q: %s", catalog.Name, err) |
| 126 | + } |
| 127 | + |
| 128 | + file, err := os.Create(cacheFilePath) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("error creating cache file for Catalog %q: %s", catalog.Name, err) |
| 131 | + } |
| 132 | + |
| 133 | + if _, err := io.Copy(file, resp.Body); err != nil { |
| 134 | + return nil, fmt.Errorf("error writing contents to cache file for Catalog %q: %s", catalog.Name, err) |
| 135 | + } |
| 136 | + |
| 137 | + if err = file.Sync(); err != nil { |
| 138 | + return nil, fmt.Errorf("error syncing contents to cache file for Catalog %q: %s", catalog.Name, err) |
| 139 | + } |
| 140 | + |
| 141 | + if _, err = file.Seek(0, 0); err != nil { |
| 142 | + return nil, fmt.Errorf("error resetting offset for cache file reader for Catalog %q: %s", catalog.Name, err) |
| 143 | + } |
| 144 | + |
| 145 | + fsc.cacheDataByCatalogName[catalog.Name] = cacheData{ |
| 146 | + ResolvedRef: catalog.Status.ResolvedSource.Image.Ref, |
| 147 | + } |
| 148 | + |
| 149 | + return file, nil |
| 150 | +} |
0 commit comments