Skip to content

Commit cb91b28

Browse files
authored
[deckhouse-cli] Fix/lts channel fix (#249)
Signed-off-by: Pavel Okhlopkov <[email protected]>
1 parent 7830054 commit cb91b28

File tree

7 files changed

+152
-85
lines changed

7 files changed

+152
-85
lines changed

internal/data/dataexport/util/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package util //nolint
22

33
import (
44
"log/slog"

internal/mirror/cmd/pull/pull.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,22 @@ func setupLogger() *log.SLogger {
121121
return log.NewSLogger(logLevel)
122122
}
123123

124-
func findTagsToMirror(pullParams *params.PullParams, logger *log.SLogger, client registry.Client) ([]string, error) {
124+
func findTagsToMirror(pullParams *params.PullParams, logger *log.SLogger, client registry.Client) ([]string, []string, error) {
125+
strickTags := []string{}
125126
if pullParams.DeckhouseTag != "" {
126-
logger.Infof("Skipped releases lookup as tag %q is specifically requested with --deckhouse-tag", pullParams.DeckhouseTag)
127-
return []string{pullParams.DeckhouseTag}, nil
127+
strickTags = append(strickTags, pullParams.DeckhouseTag)
128128
}
129129

130-
versionsToMirror, err := versionsToMirrorFunc(pullParams, client)
130+
versionsToMirror, channelsToMirror, err := versionsToMirrorFunc(pullParams, client, strickTags)
131131
if err != nil {
132-
return nil, fmt.Errorf("Find versions to mirror: %w", err)
132+
return nil, nil, fmt.Errorf("Find versions to mirror: %w", err)
133133
}
134+
134135
logger.Infof("Deckhouse releases to pull: %+v", versionsToMirror)
135136

136137
return lo.Map(versionsToMirror, func(v semver.Version, _ int) string {
137138
return "v" + v.String()
138-
}), nil
139+
}), channelsToMirror, nil
139140
}
140141

141142
func buildPullParams(logger params.Logger) *params.PullParams {
@@ -365,12 +366,12 @@ func (p *Puller) pullPlatform() error {
365366
return err
366367
}
367368

368-
tagsToMirror, err := findTagsToMirror(p.params, p.logger, c)
369+
tagsToMirror, channelsToMirror, err := findTagsToMirror(p.params, p.logger, c)
369370
if err != nil {
370371
return fmt.Errorf("Find tags to mirror: %w", err)
371372
}
372373

373-
if err = operations.PullDeckhousePlatform(p.params, tagsToMirror, c); err != nil {
374+
if err = operations.PullDeckhousePlatform(p.params, channelsToMirror, tagsToMirror, c); err != nil {
374375
return err
375376
}
376377

@@ -379,19 +380,31 @@ func (p *Puller) pullPlatform() error {
379380
}
380381

381382
// validatePlatformAccess validates access to the platform registry
383+
// check access with strict tag, stable or lts
384+
// if any resource found - access is ok
382385
func (p *Puller) validatePlatformAccess() error {
383-
targetTag := internal.StableChannel
386+
targetTags := []string{}
384387
if p.params.DeckhouseTag != "" {
385-
targetTag = p.params.DeckhouseTag
388+
targetTags = append(targetTags, p.params.DeckhouseTag)
386389
}
387390

388-
imageRef := p.params.DeckhouseRegistryRepo + ":" + targetTag
391+
targetTags = append(targetTags, internal.StableChannel, internal.LTSChannel)
389392

390-
ctx, cancel := context.WithTimeout(p.cmd.Context(), 15*time.Second)
391-
defer cancel()
393+
var accessErr error
394+
for _, targetTag := range targetTags {
395+
imageRef := p.params.DeckhouseRegistryRepo + ":" + targetTag
392396

393-
if err := p.accessValidator.ValidateReadAccessForImage(ctx, imageRef, p.validationOpts...); err != nil {
394-
return fmt.Errorf("Source registry is not accessible: %w", err)
397+
ctx, cancel := context.WithTimeout(p.cmd.Context(), 15*time.Second)
398+
defer cancel()
399+
400+
accessErr = p.accessValidator.ValidateReadAccessForImage(ctx, imageRef, p.validationOpts...)
401+
if accessErr == nil {
402+
break
403+
}
404+
}
405+
406+
if accessErr != nil {
407+
return fmt.Errorf("Source registry is not accessible: %w", accessErr)
395408
}
396409

397410
return nil

internal/mirror/cmd/pull/pull_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,40 @@ func TestFindTagsToMirror(t *testing.T) {
128128

129129
for _, tt := range tests {
130130
t.Run(tt.name, func(t *testing.T) {
131+
var originalVersionsToMirrorFunc func(*params.PullParams, registry.Client, []string) ([]semver.Version, []string, error)
132+
if tt.deckhouseTag == "" {
133+
// Mock for the no tag case to avoid panic
134+
originalVersionsToMirrorFunc = versionsToMirrorFunc
135+
versionsToMirrorFunc = func(pullParams *params.PullParams, client registry.Client, tagsToMirror []string) ([]semver.Version, []string, error) {
136+
return nil, nil, fmt.Errorf("mock error for no tag case")
137+
}
138+
defer func() { versionsToMirrorFunc = originalVersionsToMirrorFunc }()
139+
} else {
140+
// Mock for specific tag case
141+
originalVersionsToMirrorFunc = versionsToMirrorFunc
142+
versionsToMirrorFunc = func(pullParams *params.PullParams, client registry.Client, tagsToMirror []string) ([]semver.Version, []string, error) {
143+
if len(tagsToMirror) > 0 {
144+
v, err := semver.NewVersion(strings.TrimPrefix(tagsToMirror[0], "v"))
145+
if err != nil {
146+
return nil, nil, err
147+
}
148+
return []semver.Version{*v}, []string{"alpha"}, nil
149+
}
150+
return nil, nil, fmt.Errorf("no tags")
151+
}
152+
defer func() { versionsToMirrorFunc = originalVersionsToMirrorFunc }()
153+
}
154+
131155
pullParams := &params.PullParams{
156+
BaseParams: params.BaseParams{
157+
Logger: logger,
158+
},
132159
DeckhouseTag: tt.deckhouseTag,
133160
SinceVersion: tt.sinceVersion,
134161
}
135162

136163
client := mock.NewRegistryClientMock(t)
137-
tags, err := findTagsToMirror(pullParams, logger, client)
164+
tags, _, err := findTagsToMirror(pullParams, logger, client)
138165

139166
if tt.expectError {
140167
assert.Error(t, err)
@@ -960,12 +987,12 @@ func TestFindTagsToMirrorWithVersionsSuccess(t *testing.T) {
960987
defer func() { versionsToMirrorFunc = originalVersionsToMirrorFunc }()
961988

962989
// Mock the function to return successful versions
963-
versionsToMirrorFunc = func(pullParams *params.PullParams, client registry.Client) ([]semver.Version, error) {
990+
versionsToMirrorFunc = func(pullParams *params.PullParams, client registry.Client, tagsToMirror []string) ([]semver.Version, []string, error) {
964991
return []semver.Version{
965992
*semver.MustParse("1.50.0"),
966993
*semver.MustParse("1.51.0"),
967994
*semver.MustParse("1.52.0"),
968-
}, nil
995+
}, nil, nil
969996
}
970997

971998
logger := log.NewSLogger(slog.LevelInfo)
@@ -982,7 +1009,7 @@ func TestFindTagsToMirrorWithVersionsSuccess(t *testing.T) {
9821009
}
9831010

9841011
client := mock.NewRegistryClientMock(t)
985-
tags, err := findTagsToMirror(pullParams, logger, client)
1012+
tags, _, err := findTagsToMirror(pullParams, logger, client)
9861013
assert.NoError(t, err)
9871014
assert.Equal(t, []string{"v1.50.0", "v1.51.0", "v1.52.0"}, tags)
9881015
}
@@ -1419,7 +1446,7 @@ func BenchmarkFindTagsToMirror(b *testing.B) {
14191446
client := mock.NewRegistryClientMock(b)
14201447

14211448
for i := 0; i < b.N; i++ {
1422-
_, _ = findTagsToMirror(pullParams, logger, client)
1449+
_, _, _ = findTagsToMirror(pullParams, logger, client)
14231450
}
14241451
}
14251452

internal/mirror/operations/pull_dkp.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ import (
3636
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/operations/params"
3737
)
3838

39-
func PullDeckhousePlatform(pullParams *params.PullParams, tagsToMirror []string, client registry.Client) error {
39+
func PullDeckhousePlatform(pullParams *params.PullParams, channelsToMirror []string, tagsToMirror []string, client registry.Client) error {
4040
logger := pullParams.Logger
41+
if len(tagsToMirror) == 0 {
42+
return fmt.Errorf("no tags to mirror")
43+
}
44+
4145
tmpDir := filepath.Join(pullParams.WorkingDir, "platform")
4246

4347
logger.Infof("Creating OCI Image Layouts")
@@ -46,7 +50,7 @@ func PullDeckhousePlatform(pullParams *params.PullParams, tagsToMirror []string,
4650
return fmt.Errorf("Create OCI Image Layouts: %w", err)
4751
}
4852

49-
layouts.FillLayoutsWithBasicDeckhouseImages(pullParams, imageLayouts, tagsToMirror)
53+
layouts.FillLayoutsWithBasicDeckhouseImages(pullParams, imageLayouts, channelsToMirror, tagsToMirror)
5054
logger.Infof("Resolving tags")
5155
if err = imageLayouts.TagsResolver.ResolveTagsDigestsForImageLayouts(&pullParams.BaseParams, imageLayouts); err != nil {
5256
return fmt.Errorf("Resolve images tags to digests: %w", err)

internal/mirror/operations/pull_dkp_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestPullDeckhousePlatform_MkdirError(t *testing.T) {
6464
tagsToMirror := []string{"v1.0.0"}
6565
client := mock.NewRegistryClientMock(t)
6666

67-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
67+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
6868
require.Error(t, err)
6969
require.Contains(t, err.Error(), "Create OCI Image Layouts")
7070
}
@@ -77,7 +77,7 @@ func TestPullDeckhousePlatform_ResolveTagsError(t *testing.T) {
7777
tagsToMirror := []string{"v1.0.0"}
7878
client := mock.NewRegistryClientMock(t)
7979

80-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
80+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
8181
require.Error(t, err)
8282
require.Contains(t, err.Error(), "Resolve images tags to digests")
8383
}
@@ -125,7 +125,7 @@ func TestPullDeckhousePlatform_PackError(t *testing.T) {
125125
tagsToMirror := []string{"v1.0.0"}
126126
client := mock.NewRegistryClientMock(t)
127127

128-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
128+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
129129
require.Error(t, err)
130130
// The error will be from resolve tags first, but pack error would occur later
131131
// For now, just verify it fails
@@ -139,7 +139,7 @@ func TestPullDeckhousePlatform_WithDeckhouseTag(t *testing.T) {
139139
tagsToMirror := []string{"v1.0.0"}
140140
client := mock.NewRegistryClientMock(t)
141141

142-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
142+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
143143
// Should succeed or fail based on registry availability
144144
// The important thing is that it doesn't fail due to tag-specific logic
145145
if err != nil {
@@ -153,7 +153,7 @@ func TestPullDeckhousePlatform_EmptyTagsToMirror(t *testing.T) {
153153
tagsToMirror := []string{}
154154
client := mock.NewRegistryClientMock(t)
155155

156-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
156+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
157157
// Should fail due to registry issues, but not due to empty tags
158158
require.Error(t, err)
159159
}
@@ -164,7 +164,7 @@ func TestPullDeckhousePlatform_LoggerCalls(t *testing.T) {
164164
tagsToMirror := []string{"v1.0.0"}
165165
client := mock.NewRegistryClientMock(t)
166166

167-
_ = PullDeckhousePlatform(pullParams, tagsToMirror, client)
167+
_ = PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
168168

169169
// Check that expected logging occurred - adjust expectations based on actual flow
170170
hasCreateLayoutsLog := false
@@ -186,7 +186,7 @@ func TestPullDeckhousePlatform_WorkingDirectoryCleanup(t *testing.T) {
186186
// Track if working directory is used
187187
platformDir := filepath.Join(pullParams.WorkingDir, "platform")
188188

189-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
189+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
190190

191191
// The platform directory should be created and then cleaned up during execution
192192
// Since the function fails early, check that it was attempted
@@ -208,7 +208,7 @@ func TestPullDeckhousePlatform_RegistryAuth(t *testing.T) {
208208
tagsToMirror := []string{"v1.0.0"}
209209
client := mock.NewRegistryClientMock(t)
210210

211-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
211+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
212212
// Should fail due to registry, but auth should be passed through
213213
require.Error(t, err)
214214
}
@@ -221,7 +221,7 @@ func TestPullDeckhousePlatform_InsecureAndTLSSkip(t *testing.T) {
221221
tagsToMirror := []string{"v1.0.0"}
222222
client := mock.NewRegistryClientMock(t)
223223

224-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
224+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
225225
// Should attempt the operation with insecure settings
226226
require.Error(t, err) // Will fail due to no registry, but should not fail due to TLS
227227
}
@@ -233,7 +233,7 @@ func TestPullDeckhousePlatform_BundleChunkSize(t *testing.T) {
233233
tagsToMirror := []string{"v1.0.0"}
234234
client := mock.NewRegistryClientMock(t)
235235

236-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
236+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
237237
// Should use chunked writer when BundleChunkSize > 0
238238
require.Error(t, err) // Will fail due to registry, but chunking should be attempted
239239
}
@@ -247,7 +247,7 @@ func BenchmarkPullDeckhousePlatform(b *testing.B) {
247247

248248
b.ResetTimer()
249249
for i := 0; i < b.N; i++ {
250-
_ = PullDeckhousePlatform(pullParams, tagsToMirror, client)
250+
_ = PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
251251
}
252252
}
253253

@@ -259,7 +259,7 @@ func TestPullDeckhousePlatform_CodeCoverage_ProcessBlocks(t *testing.T) {
259259
tagsToMirror := []string{"v1.0.0"}
260260
client := mock.NewRegistryClientMock(t)
261261

262-
_ = PullDeckhousePlatform(pullParams, tagsToMirror, client)
262+
_ = PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
263263

264264
// Since the function fails at resolve tags, Process blocks won't be reached
265265
// This test verifies the Process logging would work if we got that far
@@ -275,7 +275,7 @@ func TestPullDeckhousePlatform_CodeCoverage_TagPropagation(t *testing.T) {
275275
tagsToMirror := []string{"v1.0.0"}
276276
client := mock.NewRegistryClientMock(t)
277277

278-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
278+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
279279
// Should succeed or fail, but tag propagation logic should be exercised
280280
require.NotNil(t, err) // Will fail due to registry, but logic should run
281281
}
@@ -288,7 +288,7 @@ func TestPullDeckhousePlatform_CodeCoverage_ManifestGeneration(t *testing.T) {
288288
tagsToMirror := []string{"v1.0.0"}
289289
client := mock.NewRegistryClientMock(t)
290290

291-
err := PullDeckhousePlatform(pullParams, tagsToMirror, client)
291+
err := PullDeckhousePlatform(pullParams, []string{}, tagsToMirror, client)
292292
// Should succeed or fail, but manifest generation should be attempted
293293
require.NotNil(t, err) // Will fail due to registry, but manifest generation should be attempted
294294
}

0 commit comments

Comments
 (0)