|
| 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "log/slog" |
| 23 | + "os" |
| 24 | + "path" |
| 25 | + "path/filepath" |
| 26 | + "slices" |
| 27 | + "strings" |
| 28 | + |
| 29 | + dkplog "github.com/deckhouse/deckhouse/pkg/log" |
| 30 | + "github.com/deckhouse/deckhouse/pkg/registry" |
| 31 | + |
| 32 | + "github.com/deckhouse/deckhouse-cli/internal/mirror/pusher" |
| 33 | + "github.com/deckhouse/deckhouse-cli/pkg/libmirror/util/log" |
| 34 | +) |
| 35 | + |
| 36 | +// PushOptions contains options for pushing module images |
| 37 | +type PushOptions struct { |
| 38 | + BundleDir string |
| 39 | + WorkingDir string |
| 40 | +} |
| 41 | + |
| 42 | +// PushService handles pushing module images to registry |
| 43 | +type PushService struct { |
| 44 | + client registry.Client |
| 45 | + pusherService *pusher.Service |
| 46 | + options *PushOptions |
| 47 | + logger *dkplog.Logger |
| 48 | + userLogger *log.SLogger |
| 49 | +} |
| 50 | + |
| 51 | +// NewPushService creates a new modules push service |
| 52 | +func NewPushService( |
| 53 | + client registry.Client, |
| 54 | + options *PushOptions, |
| 55 | + logger *dkplog.Logger, |
| 56 | + userLogger *log.SLogger, |
| 57 | +) *PushService { |
| 58 | + if options == nil { |
| 59 | + options = &PushOptions{} |
| 60 | + } |
| 61 | + |
| 62 | + return &PushService{ |
| 63 | + client: client, |
| 64 | + pusherService: pusher.NewService(logger, userLogger), |
| 65 | + options: options, |
| 66 | + logger: logger, |
| 67 | + userLogger: userLogger, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Push pushes all module packages to the registry |
| 72 | +func (svc *PushService) Push(ctx context.Context) error { |
| 73 | + modulePackages, err := svc.findModulePackages() |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("find module packages: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + if len(modulePackages) == 0 { |
| 79 | + svc.userLogger.InfoLn("No module packages found, skipping") |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + pushed := make(map[string]struct{}) |
| 84 | + for _, moduleName := range modulePackages { |
| 85 | + if _, ok := pushed[moduleName]; ok { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + if err := svc.pushModule(ctx, moduleName); err != nil { |
| 90 | + svc.userLogger.WarnLn(err) |
| 91 | + continue |
| 92 | + } |
| 93 | + pushed[moduleName] = struct{}{} |
| 94 | + } |
| 95 | + |
| 96 | + if len(pushed) > 0 { |
| 97 | + names := make([]string, 0, len(pushed)) |
| 98 | + for name := range pushed { |
| 99 | + names = append(names, name) |
| 100 | + } |
| 101 | + slices.Sort(names) |
| 102 | + svc.userLogger.Infof("Modules pushed: %s", strings.Join(names, ", ")) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (svc *PushService) findModulePackages() ([]string, error) { |
| 109 | + entries, err := os.ReadDir(svc.options.BundleDir) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("list bundle directory: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + modules := make([]string, 0, len(entries)) |
| 115 | + for _, entry := range entries { |
| 116 | + name := entry.Name() |
| 117 | + |
| 118 | + // Skip non-module files |
| 119 | + if !strings.HasPrefix(name, "module-") { |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + // Only process .tar and .chunk files |
| 124 | + ext := filepath.Ext(name) |
| 125 | + if ext != ".tar" && ext != ".chunk" { |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + // Extract module name: "module-foo.tar" -> "foo" |
| 130 | + // Handle chunked files: "module-foo.tar.chunk000" -> "foo" |
| 131 | + moduleName := strings.TrimPrefix(name, "module-") |
| 132 | + moduleName = strings.TrimSuffix(moduleName, ext) |
| 133 | + moduleName = strings.TrimSuffix(moduleName, ".tar") |
| 134 | + |
| 135 | + modules = append(modules, moduleName) |
| 136 | + } |
| 137 | + |
| 138 | + return modules, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (svc *PushService) pushModule(ctx context.Context, moduleName string) error { |
| 142 | + return svc.pusherService.PushPackage(ctx, pusher.PackagePushConfig{ |
| 143 | + PackageName: "module-" + moduleName, |
| 144 | + ProcessName: "Push module: " + moduleName, |
| 145 | + WorkingDir: filepath.Join(svc.options.WorkingDir, "modules"), |
| 146 | + BundleDir: svc.options.BundleDir, |
| 147 | + Client: svc.client.WithSegment(moduleName), |
| 148 | + // New pull creates: module/, release/, extra/ |
| 149 | + MandatoryLayoutsFunc: func(packageDir string) map[string]string { |
| 150 | + return map[string]string{ |
| 151 | + "module root layout": filepath.Join(packageDir, "module"), |
| 152 | + "module release channels layout": filepath.Join(packageDir, "release"), |
| 153 | + } |
| 154 | + }, |
| 155 | + // Dynamic layout discovery after unpacking |
| 156 | + LayoutsFunc: svc.buildModuleLayouts, |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +// buildModuleLayouts returns the list of layouts for a module, including dynamic extra discovery |
| 161 | +func (svc *PushService) buildModuleLayouts(packageDir string) []pusher.LayoutMapping { |
| 162 | + layouts := []pusher.LayoutMapping{ |
| 163 | + {LayoutPath: "module", Segment: ""}, // Root module images |
| 164 | + {LayoutPath: "release", Segment: "release"}, // Release channels |
| 165 | + } |
| 166 | + |
| 167 | + // Check if extra directory exists |
| 168 | + extraDir := filepath.Join(packageDir, "extra") |
| 169 | + if _, err := os.Stat(extraDir); os.IsNotExist(err) { |
| 170 | + return layouts |
| 171 | + } |
| 172 | + |
| 173 | + // Add root extra layout |
| 174 | + layouts = append(layouts, pusher.LayoutMapping{ |
| 175 | + LayoutPath: "extra", |
| 176 | + Segment: "extra", |
| 177 | + }) |
| 178 | + |
| 179 | + // Discover nested extra layouts |
| 180 | + entries, err := os.ReadDir(extraDir) |
| 181 | + if err != nil { |
| 182 | + svc.logger.Warn("Error reading extra dir", slog.Any("error", err)) |
| 183 | + return layouts |
| 184 | + } |
| 185 | + |
| 186 | + for _, entry := range entries { |
| 187 | + if entry.IsDir() { |
| 188 | + svc.logger.Debug("Found extra layout", slog.String("layout", entry.Name())) |
| 189 | + layouts = append(layouts, pusher.LayoutMapping{ |
| 190 | + LayoutPath: path.Join("extra", entry.Name()), |
| 191 | + Segment: path.Join("extra", entry.Name()), |
| 192 | + }) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return layouts |
| 197 | +} |
0 commit comments