-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split
config.Registry
into the separate resource
Required for #9614 Closes #9766 Signed-off-by: Dmitriy Matrenichev <[email protected]>
- Loading branch information
Showing
30 changed files
with
2,686 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
internal/app/machined/pkg/controllers/cri/registries_config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cri | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/controller/generic/transform" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/constants" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/cri" | ||
) | ||
|
||
// RegistriesConfigController watches v1alpha1.Config, updates registry.RegistriesConfig. | ||
type RegistriesConfigController = transform.Controller[*config.MachineConfig, *cri.RegistriesConfig] | ||
|
||
// NewRegistriesConfigController creates new config controller. | ||
// | ||
//nolint:gocyclo | ||
func NewRegistriesConfigController() *RegistriesConfigController { | ||
return transform.NewController( | ||
transform.Settings[*config.MachineConfig, *cri.RegistriesConfig]{ | ||
Name: "cri.RegistriesConfigController", | ||
MapMetadataOptionalFunc: func(cfg *config.MachineConfig) optional.Optional[*cri.RegistriesConfig] { | ||
if cfg.Metadata().ID() != config.V1Alpha1ID { | ||
return optional.None[*cri.RegistriesConfig]() | ||
} | ||
|
||
return optional.Some(cri.NewRegistriesConfig()) | ||
}, | ||
TransformFunc: func(ctx context.Context, r controller.Reader, logger *zap.Logger, cfg *config.MachineConfig, res *cri.RegistriesConfig) error { | ||
imageCacheConfig, err := safe.ReaderGetByID[*cri.ImageCacheConfig](ctx, r, cri.ImageCacheConfigID) | ||
if err != nil && !state.IsNotFoundError(err) { | ||
return fmt.Errorf("failed to get image cache config: %w", err) | ||
} | ||
|
||
spec := res.TypedSpec() | ||
|
||
spec.RegistryConfig = clearInit(spec.RegistryConfig) | ||
spec.RegistryMirrors = clearInit(spec.RegistryMirrors) | ||
|
||
if cfg != nil && cfg.Config().Machine() != nil { | ||
// This is breaking our interface abstraction, but we need to get the underlying types for protobuf | ||
// encoding to work correctly. | ||
mr := cfg.Provider().RawV1Alpha1().MachineConfig.MachineRegistries | ||
|
||
for k, v := range mr.RegistryConfig { | ||
spec.RegistryConfig[k] = &cri.RegistryConfig{ | ||
RegistryTLS: &cri.RegistryTLSConfig{ | ||
TLSClientIdentity: v.RegistryTLS.TLSClientIdentity, | ||
TLSCA: v.RegistryTLS.TLSCA, | ||
TLSInsecureSkipVerify: v.RegistryTLS.TLSInsecureSkipVerify, | ||
}, | ||
RegistryAuth: &cri.RegistryAuthConfig{ | ||
RegistryUsername: v.RegistryAuth.RegistryUsername, | ||
RegistryPassword: v.RegistryAuth.RegistryPassword, | ||
RegistryAuth: v.RegistryAuth.RegistryAuth, | ||
RegistryIdentityToken: v.RegistryAuth.RegistryIdentityToken, | ||
}, | ||
} | ||
} | ||
|
||
for k, v := range mr.RegistryMirrors { | ||
spec.RegistryMirrors[k] = &cri.RegistryMirrorConfig{ | ||
MirrorEndpoints: v.MirrorEndpoints, | ||
MirrorOverridePath: v.MirrorOverridePath, | ||
MirrorSkipFallback: v.MirrorSkipFallback, | ||
} | ||
} | ||
} | ||
|
||
if imageCacheConfig != nil && imageCacheConfig.TypedSpec().Status == cri.ImageCacheStatusReady { | ||
// if the '*' was configured, we just use it, otherwise create it so that we can inject the registryd | ||
if _, hasStar := spec.RegistryMirrors["*"]; !hasStar { | ||
spec.RegistryMirrors["*"] = &cri.RegistryMirrorConfig{} | ||
} | ||
|
||
// inject the registryd mirror endpoint as the first one for all registries | ||
for registry := range spec.RegistryMirrors { | ||
spec.RegistryMirrors[registry].MirrorEndpoints = append( | ||
[]string{"http://" + constants.RegistrydListenAddress}, | ||
spec.RegistryMirrors[registry].MirrorEndpoints..., | ||
) | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
transform.WithExtraInputs( | ||
controller.Input{ | ||
Namespace: cri.NamespaceName, | ||
Type: cri.ImageCacheConfigType, | ||
ID: optional.Some(cri.ImageCacheConfigID), | ||
Kind: controller.InputWeak, | ||
}, | ||
), | ||
) | ||
} | ||
|
||
func clearInit[M ~map[K]V, K comparable, V any](m M) M { | ||
if m == nil { | ||
return make(M) | ||
} | ||
|
||
clear(m) | ||
|
||
return m | ||
} |
113 changes: 113 additions & 0 deletions
113
internal/app/machined/pkg/controllers/cri/registries_config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package cri_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/cri" | ||
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/ctest" | ||
"github.com/siderolabs/talos/pkg/machinery/config/container" | ||
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1" | ||
"github.com/siderolabs/talos/pkg/machinery/constants" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
crires "github.com/siderolabs/talos/pkg/machinery/resources/cri" | ||
) | ||
|
||
type ConfigSuite struct { | ||
ctest.DefaultSuite | ||
} | ||
|
||
func (suite *ConfigSuite) TestRegistry() { | ||
cfg := config.NewMachineConfig(container.NewV1Alpha1(&v1alpha1.Config{ | ||
ConfigVersion: "v1alpha1", | ||
MachineConfig: &v1alpha1.MachineConfig{ | ||
MachineType: "controlplane", | ||
MachineRegistries: v1alpha1.RegistriesConfig{ | ||
RegistryMirrors: map[string]*v1alpha1.RegistryMirrorConfig{ | ||
"docker.io": {MirrorEndpoints: []string{"https://mirror.io"}}, | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
suite.Require().NoError(suite.State().Create(suite.Ctx(), cfg)) | ||
|
||
ctest.AssertResource(suite, crires.RegistriesConfigID, func(r *crires.RegistriesConfig, a *assert.Assertions) { | ||
spec := r.TypedSpec() | ||
|
||
a.Equal( | ||
map[string]*crires.RegistryMirrorConfig{ | ||
"docker.io": {MirrorEndpoints: []string{"https://mirror.io"}}, | ||
}, | ||
spec.RegistryMirrors, | ||
) | ||
}) | ||
|
||
ic := crires.NewImageCacheConfig() | ||
ic.TypedSpec().Roots = []string{"/imagecache"} | ||
ic.TypedSpec().Status = crires.ImageCacheStatusReady | ||
|
||
suite.Require().NoError(suite.State().Create(suite.Ctx(), ic)) | ||
|
||
ctest.AssertResource(suite, crires.RegistriesConfigID, func(r *crires.RegistriesConfig, a *assert.Assertions) { | ||
spec := r.TypedSpec() | ||
|
||
a.Equal( | ||
map[string]*crires.RegistryMirrorConfig{ | ||
"*": {MirrorEndpoints: []string{ | ||
"http://" + constants.RegistrydListenAddress, | ||
}}, | ||
"docker.io": {MirrorEndpoints: []string{ | ||
"http://" + constants.RegistrydListenAddress, | ||
"https://mirror.io", | ||
}}, | ||
}, | ||
spec.RegistryMirrors, | ||
) | ||
}) | ||
} | ||
|
||
func (suite *ConfigSuite) TestRegistryNoMachineConfig() { | ||
cfg := config.NewMachineConfig(container.NewV1Alpha1(nil)) | ||
|
||
suite.Require().NoError(suite.State().Create(suite.Ctx(), cfg)) | ||
|
||
ic := crires.NewImageCacheConfig() | ||
ic.TypedSpec().Roots = []string{"/imagecache"} | ||
ic.TypedSpec().Status = crires.ImageCacheStatusReady | ||
|
||
suite.Require().NoError(suite.State().Create(suite.Ctx(), ic)) | ||
|
||
ctest.AssertResource(suite, crires.RegistriesConfigID, func(r *crires.RegistriesConfig, a *assert.Assertions) { | ||
spec := r.TypedSpec() | ||
|
||
a.Equal( | ||
map[string]*crires.RegistryMirrorConfig{ | ||
"*": {MirrorEndpoints: []string{ | ||
"http://" + constants.RegistrydListenAddress, | ||
}}, | ||
}, | ||
spec.RegistryMirrors, | ||
) | ||
}) | ||
} | ||
|
||
func TestConfigSuite(t *testing.T) { | ||
t.Parallel() | ||
|
||
suite.Run(t, &ConfigSuite{ | ||
DefaultSuite: ctest.DefaultSuite{ | ||
Timeout: 5 * time.Second, | ||
AfterSetup: func(s *ctest.DefaultSuite) { | ||
s.Require().NoError(s.Runtime().RegisterController(cri.NewRegistriesConfigController())) | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.