Skip to content

Commit 83ffd24

Browse files
ldmonsterCopilot
andauthored
[deckhouse-cli] mirror feat: replace registry client pull (#210)
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com> Signed-off-by: Pavel Okhlopkov <36456348+ldmonster@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6875231 commit 83ffd24

File tree

21 files changed

+902
-377
lines changed

21 files changed

+902
-377
lines changed

d8-install.sh

Lines changed: 0 additions & 156 deletions
This file was deleted.

internal/mirror/cmd/pull/pull.go

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ import (
3939
"github.com/deckhouse/deckhouse-cli/internal/mirror/operations"
4040
"github.com/deckhouse/deckhouse-cli/internal/mirror/releases"
4141
"github.com/deckhouse/deckhouse-cli/internal/version"
42+
"github.com/deckhouse/deckhouse-cli/pkg"
4243
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/modules"
4344
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/operations/params"
4445
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/util/log"
4546
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/validation"
47+
"github.com/deckhouse/deckhouse-cli/pkg/registry"
48+
dkplog "github.com/deckhouse/deckhouse/pkg/log"
4649
)
4750

4851
var ErrPullFailed = errors.New("pull failed, see the log for details")
@@ -143,13 +146,13 @@ func setupLogger() *log.SLogger {
143146
return log.NewSLogger(logLevel)
144147
}
145148

146-
func findTagsToMirror(pullParams *params.PullParams, logger *log.SLogger) ([]string, error) {
149+
func findTagsToMirror(pullParams *params.PullParams, logger *log.SLogger, client pkg.RegistryClient) ([]string, error) {
147150
if pullParams.DeckhouseTag != "" {
148151
logger.InfoF("Skipped releases lookup as tag %q is specifically requested with --deckhouse-tag", pullParams.DeckhouseTag)
149152
return []string{pullParams.DeckhouseTag}, nil
150153
}
151154

152-
versionsToMirror, err := versionsToMirrorFunc(pullParams)
155+
versionsToMirror, err := versionsToMirrorFunc(pullParams, client)
153156
if err != nil {
154157
return nil, fmt.Errorf("Find versions to mirror: %w", err)
155158
}
@@ -287,17 +290,42 @@ func (p *Puller) pullPlatform() error {
287290
return nil
288291
}
289292

293+
logger := dkplog.NewNop()
294+
295+
if log.DebugLogLevel() >= 3 {
296+
logger = dkplog.NewLogger(dkplog.WithLevel(slog.LevelDebug))
297+
}
298+
299+
// Create registry client for module operations
300+
clientOpts := &registry.ClientOptions{
301+
Insecure: p.params.Insecure,
302+
TLSSkipVerify: p.params.SkipTLSVerification,
303+
Logger: logger,
304+
}
305+
306+
if p.params.RegistryAuth != nil {
307+
clientOpts.Auth = p.params.RegistryAuth
308+
}
309+
310+
var client pkg.RegistryClient
311+
client = registry.NewClientWithOptions(p.params.DeckhouseRegistryRepo, clientOpts)
312+
313+
// Scope to the registry path and modules suffix
314+
if p.params.RegistryPath != "" {
315+
client = client.WithSegment(p.params.RegistryPath)
316+
}
317+
290318
return p.logger.Process("Pull Deckhouse Kubernetes Platform", func() error {
291319
if err := p.validatePlatformAccess(); err != nil {
292320
return err
293321
}
294322

295-
tagsToMirror, err := findTagsToMirror(p.params, p.logger)
323+
tagsToMirror, err := findTagsToMirror(p.params, p.logger, client)
296324
if err != nil {
297325
return fmt.Errorf("Find tags to mirror: %w", err)
298326
}
299327

300-
if err = operations.PullDeckhousePlatform(p.params, tagsToMirror); err != nil {
328+
if err = operations.PullDeckhousePlatform(p.params, tagsToMirror, client); err != nil {
301329
return err
302330
}
303331
return nil
@@ -327,6 +355,31 @@ func (p *Puller) pullSecurityDatabases() error {
327355
return nil
328356
}
329357

358+
logger := dkplog.NewNop()
359+
360+
if log.DebugLogLevel() >= 3 {
361+
logger = dkplog.NewLogger(dkplog.WithLevel(slog.LevelDebug))
362+
}
363+
364+
// Create registry client for module operations
365+
clientOpts := &registry.ClientOptions{
366+
Insecure: p.params.Insecure,
367+
TLSSkipVerify: p.params.SkipTLSVerification,
368+
Logger: logger,
369+
}
370+
371+
if p.params.RegistryAuth != nil {
372+
clientOpts.Auth = p.params.RegistryAuth
373+
}
374+
375+
var client pkg.RegistryClient
376+
client = registry.NewClientWithOptions(p.params.DeckhouseRegistryRepo, clientOpts)
377+
378+
// Scope to the registry path and modules suffix
379+
if p.params.RegistryPath != "" {
380+
client = client.WithSegment(p.params.RegistryPath)
381+
}
382+
330383
return p.logger.Process("Pull Security Databases", func() error {
331384
ctx, cancel := context.WithTimeout(p.cmd.Context(), 15*time.Second)
332385
defer cancel()
@@ -341,7 +394,7 @@ func (p *Puller) pullSecurityDatabases() error {
341394
return fmt.Errorf("Source registry is not accessible: %w", err)
342395
}
343396

344-
if err := operations.PullSecurityDatabases(p.params); err != nil {
397+
if err := operations.PullSecurityDatabases(p.params, client); err != nil {
345398
return err
346399
}
347400
return nil
@@ -359,6 +412,35 @@ func (p *Puller) pullModules() error {
359412
processName = "Pull Extra Images"
360413
}
361414

415+
logger := dkplog.NewNop()
416+
417+
if log.DebugLogLevel() >= 3 {
418+
logger = dkplog.NewLogger(dkplog.WithLevel(slog.LevelDebug))
419+
}
420+
421+
// Create registry client for module operations
422+
clientOpts := &registry.ClientOptions{
423+
Insecure: p.params.Insecure,
424+
TLSSkipVerify: p.params.SkipTLSVerification,
425+
Logger: logger,
426+
}
427+
428+
if p.params.RegistryAuth != nil {
429+
clientOpts.Auth = p.params.RegistryAuth
430+
}
431+
432+
var client pkg.RegistryClient
433+
client = registry.NewClientWithOptions(p.params.DeckhouseRegistryRepo, clientOpts)
434+
435+
// Scope to the registry path and modules suffix
436+
if p.params.RegistryPath != "" {
437+
client = client.WithSegment(p.params.RegistryPath)
438+
}
439+
440+
if p.params.ModulesPathSuffix != "" {
441+
client = client.WithSegment(p.params.ModulesPathSuffix)
442+
}
443+
362444
return p.logger.Process(processName, func() error {
363445
if err := p.validateModulesAccess(); err != nil {
364446
return err
@@ -369,7 +451,7 @@ func (p *Puller) pullModules() error {
369451
return err
370452
}
371453

372-
return operations.PullModules(p.params, filter)
454+
return operations.PullModules(p.params, filter, client)
373455
})
374456
}
375457

internal/mirror/cmd/pull/pull_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ import (
3434
"github.com/stretchr/testify/require"
3535

3636
"github.com/deckhouse/deckhouse-cli/internal/mirror"
37+
"github.com/deckhouse/deckhouse-cli/pkg"
3738
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/operations/params"
3839
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/util/log"
3940
"github.com/deckhouse/deckhouse-cli/pkg/libmirror/validation"
41+
mock "github.com/deckhouse/deckhouse-cli/pkg/mock"
4042
)
4143

4244
func TestNewCommand(t *testing.T) {
@@ -130,7 +132,8 @@ func TestFindTagsToMirror(t *testing.T) {
130132
SinceVersion: tt.sinceVersion,
131133
}
132134

133-
tags, err := findTagsToMirror(pullParams, logger)
135+
client := mock.NewRegistryClientMock(t)
136+
tags, err := findTagsToMirror(pullParams, logger, client)
134137

135138
if tt.expectError {
136139
assert.Error(t, err)
@@ -956,7 +959,7 @@ func TestFindTagsToMirrorWithVersionsSuccess(t *testing.T) {
956959
defer func() { versionsToMirrorFunc = originalVersionsToMirrorFunc }()
957960

958961
// Mock the function to return successful versions
959-
versionsToMirrorFunc = func(pullParams *params.PullParams) ([]semver.Version, error) {
962+
versionsToMirrorFunc = func(pullParams *params.PullParams, client pkg.RegistryClient) ([]semver.Version, error) {
960963
return []semver.Version{
961964
*semver.MustParse("1.50.0"),
962965
*semver.MustParse("1.51.0"),
@@ -977,7 +980,8 @@ func TestFindTagsToMirrorWithVersionsSuccess(t *testing.T) {
977980
SinceVersion: nil,
978981
}
979982

980-
tags, err := findTagsToMirror(pullParams, logger)
983+
client := mock.NewRegistryClientMock(t)
984+
tags, err := findTagsToMirror(pullParams, logger, client)
981985
assert.NoError(t, err)
982986
assert.Equal(t, []string{"v1.50.0", "v1.51.0", "v1.52.0"}, tags)
983987
}
@@ -1411,8 +1415,10 @@ func BenchmarkFindTagsToMirror(b *testing.B) {
14111415
DeckhouseTag: "v1.57.3",
14121416
}
14131417

1418+
client := mock.NewRegistryClientMock(b)
1419+
14141420
for i := 0; i < b.N; i++ {
1415-
_, _ = findTagsToMirror(pullParams, logger)
1421+
_, _ = findTagsToMirror(pullParams, logger, client)
14161422
}
14171423
}
14181424

0 commit comments

Comments
 (0)