diff options
author | Sebastiaan van Stijn <github@gone.nl> | 2022-02-26 16:14:50 +0100 |
---|---|---|
committer | Sebastiaan van Stijn <github@gone.nl> | 2022-03-17 17:12:27 +0100 |
commit | 382b9865202062b2269d796c428c3cfa89a23846 (patch) | |
tree | d69da50780bd16791308bf01ed70380a004887c1 /registry/service.go | |
parent | 18de76a420b16714c457503cd4312d2f4c379d4c (diff) | |
download | docker-382b9865202062b2269d796c428c3cfa89a23846.tar.gz |
registry: make defaultService.ServiceConfig() more idiomatic
The intent of this function is to return a copy of the service's configuration,
and to copy / dereference the options in its configuration.
The code was doing this in slightly complicated fashion. This patch;
- adds a `copy()` function to serviceConfig
- rewrites the code to use a slightly more idiomatic approach, using one of
the approaches described in "golang SliceTricks" https://github.com/golang/go/wiki/SliceTricks#copy
- changes defaultService.ServiceConfig() to use this function, and updates
its godoc to better describe that it returns a copy.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Diffstat (limited to 'registry/service.go')
-rw-r--r-- | registry/service.go | 25 |
1 files changed, 2 insertions, 23 deletions
diff --git a/registry/service.go b/registry/service.go index cf53472c52..f441d15ee4 100644 --- a/registry/service.go +++ b/registry/service.go @@ -50,32 +50,11 @@ func NewService(options ServiceOptions) (Service, error) { return &defaultService{config: config}, err } -// ServiceConfig returns the public registry service configuration. +// ServiceConfig returns a copy of the public registry service's configuration. func (s *defaultService) ServiceConfig() *registry.ServiceConfig { s.mu.RLock() defer s.mu.RUnlock() - - servConfig := registry.ServiceConfig{ - AllowNondistributableArtifactsCIDRs: make([]*(registry.NetIPNet), 0), - AllowNondistributableArtifactsHostnames: make([]string, 0), - InsecureRegistryCIDRs: make([]*(registry.NetIPNet), 0), - IndexConfigs: make(map[string]*(registry.IndexInfo)), - Mirrors: make([]string, 0), - } - - // construct a new ServiceConfig which will not retrieve s.Config directly, - // and look up items in s.config with mu locked - servConfig.AllowNondistributableArtifactsCIDRs = append(servConfig.AllowNondistributableArtifactsCIDRs, s.config.ServiceConfig.AllowNondistributableArtifactsCIDRs...) - servConfig.AllowNondistributableArtifactsHostnames = append(servConfig.AllowNondistributableArtifactsHostnames, s.config.ServiceConfig.AllowNondistributableArtifactsHostnames...) - servConfig.InsecureRegistryCIDRs = append(servConfig.InsecureRegistryCIDRs, s.config.ServiceConfig.InsecureRegistryCIDRs...) - - for key, value := range s.config.ServiceConfig.IndexConfigs { - servConfig.IndexConfigs[key] = value - } - - servConfig.Mirrors = append(servConfig.Mirrors, s.config.ServiceConfig.Mirrors...) - - return &servConfig + return s.config.copy() } // LoadAllowNondistributableArtifacts loads allow-nondistributable-artifacts registries for Service. |