newServiceConfig creates a new service config with the given options.
(registries []string)
| 97 | |
| 98 | // newServiceConfig creates a new service config with the given options. |
| 99 | func newServiceConfig(registries []string) (*serviceConfig, error) { |
| 100 | if len(registries) == 0 { |
| 101 | return &serviceConfig{}, nil |
| 102 | } |
| 103 | // Localhost is by default considered as an insecure registry. This is a |
| 104 | // stop-gap for people who are running a private registry on localhost. |
| 105 | registries = append(registries, "::1/128", "127.0.0.0/8") |
| 106 | |
| 107 | var ( |
| 108 | insecureRegistryCIDRs = make([]*net.IPNet, 0) |
| 109 | indexConfigs = make(map[string]*registry.IndexInfo) |
| 110 | ) |
| 111 | |
| 112 | skip: |
| 113 | for _, r := range registries { |
| 114 | if scheme, host, ok := strings.Cut(r, "://"); ok { |
| 115 | switch strings.ToLower(scheme) { |
| 116 | case "http", "https": |
| 117 | log.G(context.TODO()).Warnf("insecure registry %[1]s should not contain '%[2]s' and '%[2]ss' has been removed from the insecure registry config", r, scheme) |
| 118 | r = host |
| 119 | default: |
| 120 | // unsupported scheme |
| 121 | return nil, invalidParam(fmt.Errorf("insecure registry %s should not contain '://'", r)) |
| 122 | } |
| 123 | } |
| 124 | // Check if CIDR was passed to --insecure-registry |
| 125 | _, ipnet, err := net.ParseCIDR(r) |
| 126 | if err == nil { |
| 127 | // Valid CIDR. If ipnet is already in config.InsecureRegistryCIDRs, skip. |
| 128 | for _, value := range insecureRegistryCIDRs { |
| 129 | if value.IP.String() == ipnet.IP.String() && value.Mask.String() == ipnet.Mask.String() { |
| 130 | continue skip |
| 131 | } |
| 132 | } |
| 133 | // ipnet is not found, add it in config.InsecureRegistryCIDRs |
| 134 | insecureRegistryCIDRs = append(insecureRegistryCIDRs, ipnet) |
| 135 | } else { |
| 136 | if err := validateHostPort(r); err != nil { |
| 137 | return nil, invalidParam(fmt.Errorf("insecure registry %s is not valid: %w", r, err)) |
| 138 | } |
| 139 | // Assume `host:port` if not CIDR. |
| 140 | indexConfigs[r] = ®istry.IndexInfo{ |
| 141 | Name: r, |
| 142 | Secure: false, |
| 143 | Official: false, |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Configure public registry. |
| 149 | indexConfigs[IndexName] = ®istry.IndexInfo{ |
| 150 | Name: IndexName, |
| 151 | Secure: true, |
| 152 | Official: true, |
| 153 | } |
| 154 | |
| 155 | return &serviceConfig{ |
| 156 | indexConfigs: indexConfigs, |
searching dependent graphs…