|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package modules |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 24 | + "github.com/google/go-containerregistry/pkg/v1/layout" |
| 25 | + |
| 26 | + "github.com/deckhouse/deckhouse-cli/internal" |
| 27 | + "github.com/deckhouse/deckhouse-cli/internal/mirror/puller" |
| 28 | + regimage "github.com/deckhouse/deckhouse-cli/pkg/registry/image" |
| 29 | +) |
| 30 | + |
| 31 | +type ImageDownloadList struct { |
| 32 | + rootURL string |
| 33 | + |
| 34 | + Modules map[string]*puller.ImageMeta |
| 35 | + ModulesReleaseChannels map[string]*puller.ImageMeta |
| 36 | + ModulesExtra map[string]*puller.ImageMeta |
| 37 | +} |
| 38 | + |
| 39 | +func NewImageDownloadList(rootURL string) *ImageDownloadList { |
| 40 | + return &ImageDownloadList{ |
| 41 | + rootURL: rootURL, |
| 42 | + |
| 43 | + Modules: make(map[string]*puller.ImageMeta), |
| 44 | + ModulesReleaseChannels: make(map[string]*puller.ImageMeta), |
| 45 | + ModulesExtra: make(map[string]*puller.ImageMeta), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (l *ImageDownloadList) FillModulesImages(modules []string) { |
| 50 | + for _, module := range modules { |
| 51 | + l.Modules[filepath.Join(l.rootURL, internal.ModulesSegment, module)+":latest"] = nil |
| 52 | + l.ModulesReleaseChannels[filepath.Join(l.rootURL, internal.ModulesSegment, module, internal.ModulesReleasesSegment)+":latest"] = nil |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (l *ImageDownloadList) FillForTag(tag string) { |
| 57 | + // If we are to pull only the specific requested version, we should not pull any release channels at all. |
| 58 | + if tag != "" { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // For modules, release channels might be handled differently |
| 63 | + // TODO: implement if needed |
| 64 | +} |
| 65 | + |
| 66 | +type ImageLayouts struct { |
| 67 | + platform v1.Platform |
| 68 | + workingDir string |
| 69 | + |
| 70 | + Modules *regimage.ImageLayout |
| 71 | + ModulesReleaseChannels *regimage.ImageLayout |
| 72 | + ModulesExtra *regimage.ImageLayout |
| 73 | +} |
| 74 | + |
| 75 | +func NewImageLayouts(rootFolder string) *ImageLayouts { |
| 76 | + l := &ImageLayouts{ |
| 77 | + workingDir: rootFolder, |
| 78 | + platform: v1.Platform{Architecture: "amd64", OS: "linux"}, |
| 79 | + } |
| 80 | + |
| 81 | + return l |
| 82 | +} |
| 83 | + |
| 84 | +func (l *ImageLayouts) setLayoutByMirrorType(rootFolder string, mirrorType internal.MirrorType) error { |
| 85 | + layoutPath := filepath.Join(rootFolder, internal.InstallPathByMirrorType(mirrorType)) |
| 86 | + |
| 87 | + layout, err := regimage.NewImageLayout(layoutPath) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to create image layout: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + switch mirrorType { |
| 93 | + case internal.MirrorTypeModules: |
| 94 | + l.Modules = layout |
| 95 | + case internal.MirrorTypeModulesReleaseChannels: |
| 96 | + l.ModulesReleaseChannels = layout |
| 97 | + case internal.MirrorTypeModulesExtra: |
| 98 | + l.ModulesExtra = layout |
| 99 | + default: |
| 100 | + return fmt.Errorf("wrong mirror type in modules image layout: %v", mirrorType) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// AsList returns a list of layout.Path's in it. Undefined path's are not included in the list. |
| 107 | +func (l *ImageLayouts) AsList() []layout.Path { |
| 108 | + paths := make([]layout.Path, 0) |
| 109 | + if l.Modules != nil { |
| 110 | + paths = append(paths, l.Modules.Path()) |
| 111 | + } |
| 112 | + if l.ModulesReleaseChannels != nil { |
| 113 | + paths = append(paths, l.ModulesReleaseChannels.Path()) |
| 114 | + } |
| 115 | + if l.ModulesExtra != nil { |
| 116 | + paths = append(paths, l.ModulesExtra.Path()) |
| 117 | + } |
| 118 | + return paths |
| 119 | +} |
0 commit comments