Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry credentials configuration #1724

Merged
merged 6 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions addons/machinecontroller/machine-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,20 @@ data:
"ca.crt": |
{{ .Certificates.KubernetesCA | b64enc | indent 4 }}

{{ if .RegistryCredentials }}
---
apiVersion: v1
kind: Secret
metadata:
name: kubeone-registry-credentials
namespace: kube-system
data:
{{ range .RegistryCredentials }}
{{ .RegistryName }}: |
{{ mustToRawJson .Auth | b64enc | indent 4 }}
{{ end }}
{{ end }}

---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
Expand Down
17 changes: 16 additions & 1 deletion docs/api_reference/v1beta2.en.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
+++
title = "v1beta2 API Reference"
date = 2022-01-10T17:50:56+01:00
date = 2022-01-11T11:06:38+02:00
weight = 11
+++
## v1beta2
Expand All @@ -20,6 +20,7 @@ weight = 11
* [ContainerRuntimeContainerd](#containerruntimecontainerd)
* [ContainerRuntimeDocker](#containerruntimedocker)
* [ContainerdRegistry](#containerdregistry)
* [ContainerdRegistryAuthConfig](#containerdregistryauthconfig)
* [ContainerdTLSConfig](#containerdtlsconfig)
* [ControlPlaneConfig](#controlplaneconfig)
* [DNSConfig](#dnsconfig)
Expand Down Expand Up @@ -237,6 +238,20 @@ ContainerdRegistry defines endpoints and security for given container registry
| ----- | ----------- | ------ | -------- |
| mirrors | List of registry mirrors to use | []string | false |
| tlsConfig | TLSConfig for the registry | *[ContainerdTLSConfig](#containerdtlsconfig) | false |
| auth | Registry authentication | *[ContainerdRegistryAuthConfig](#containerdregistryauthconfig) | false |

[Back to Group](#v1beta2)

### ContainerdRegistryAuthConfig

Containerd per-registry credentials config

| Field | Description | Scheme | Required |
| ----- | ----------- | ------ | -------- |
| username | | string | false |
| password | | string | false |
| auth | | string | false |
| identityToken | | string | false |

[Back to Group](#v1beta2)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/iancoleman/orderedmap v0.2.0
github.com/imdario/mergo v0.3.12
github.com/koron-go/prefixw v0.0.0-20181013140428-271b207a7572
github.com/kubermatic/machine-controller v1.40.1
github.com/kubermatic/machine-controller v1.41.0
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
Expand Down
194 changes: 7 additions & 187 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions pkg/addons/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/fs"
"os"
"sort"
"strings"

"github.com/MakeNowJust/heredoc/v2"
Expand Down Expand Up @@ -66,11 +67,17 @@ type templateData struct {
CSIMigration bool
CSIMigrationFeatureGates string
MachineControllerCredentialsEnvVars string
RegistryCredentials []registryCredentialsContainer
InternalImages *internalImages
Resources map[string]string
Params map[string]string
}

type registryCredentialsContainer struct {
RegistryName string
Auth kubeoneapi.ContainerdRegistryAuthConfig
}

func newAddonsApplier(s *state.State) (*applier, error) {
var localFS fs.FS

Expand Down Expand Up @@ -155,6 +162,28 @@ func newAddonsApplier(s *state.State) (*applier, error) {
}
}

regCredentials := []registryCredentialsContainer{}

if s.Cluster.ContainerRuntime.Containerd != nil {
regNames := []string{}

for reg := range s.Cluster.ContainerRuntime.Containerd.Registries {
regNames = append(regNames, reg)
}

sort.Strings(regNames)

for _, reg := range regNames {
regConfig := s.Cluster.ContainerRuntime.Containerd.Registries[reg]
if regConfig.Auth != nil {
regCredentials = append(regCredentials, registryCredentialsContainer{
RegistryName: reg,
Auth: *regConfig.Auth,
})
}
}
}

data := templateData{
Config: s.Cluster,
Certificates: map[string]string{
Expand All @@ -170,6 +199,7 @@ func newAddonsApplier(s *state.State) (*applier, error) {
CSIMigration: csiMigration,
CSIMigrationFeatureGates: csiMigrationFeatureGates,
MachineControllerCredentialsEnvVars: string(credsEnvVarsMC),
RegistryCredentials: regCredentials,
InternalImages: &internalImages{
pauseImage: s.PauseImage,
resolver: s.Images.Get,
Expand Down
20 changes: 18 additions & 2 deletions pkg/apis/kubeone/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"github.com/pkg/errors"
)

const (
credentialSecretName = "kube-system/kubeone-registry-credentials" //nolint:gosec
)

// Leader returns the first configured host. Only call this after
// validating the cluster config to ensure a leader exists.
func (c KubeOneCluster) Leader() (HostConfig, error) {
Expand Down Expand Up @@ -100,9 +104,11 @@ func (crc ContainerRuntimeConfig) MachineControllerFlags() []string {
// -node-containerd-registry-mirrors=k8s.gcr.io=http://somewhere
// -node-insecure-registries=docker.io,k8s.gcr.io
var (
registryNames []string
insecureSet = map[string]struct{}{}
registryNames []string
insecureSet = map[string]struct{}{}
registryCredentialsSecretFlag bool
)

for registry := range crc.Containerd.Registries {
registryNames = append(registryNames, registry)
}
Expand All @@ -121,6 +127,16 @@ func (crc ContainerRuntimeConfig) MachineControllerFlags() []string {
fmt.Sprintf("-node-containerd-registry-mirrors=%s=%s", registryName, mirror),
)
}

if containerdRegistry.Auth != nil {
registryCredentialsSecretFlag = true
}
}

if registryCredentialsSecretFlag {
mcFlags = append(mcFlags,
fmt.Sprintf("-node-registry-credentials-secret=%s", credentialSecretName),
)
}

if len(insecureSet) > 0 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/kubeone/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ type ContainerdRegistry struct {

// TLSConfig for the registry
TLSConfig *ContainerdTLSConfig `json:"tlsConfig,omitempty"`

// Registry authentication
Auth *ContainerdRegistryAuthConfig `json:"auth,omitempty"`
}

// Containerd per-registry credentials config
type ContainerdRegistryAuthConfig struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Auth string `json:"auth,omitempty"`
IdentityToken string `json:"identityToken,omitempty"`
}

// Configures containerd TLS for a registry
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/kubeone/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ type ContainerdRegistry struct {

// TLSConfig for the registry
TLSConfig *ContainerdTLSConfig `json:"tlsConfig,omitempty"`

// Registry authentication
Auth *ContainerdRegistryAuthConfig `json:"auth,omitempty"`
}

// Containerd per-registry credentials config
type ContainerdRegistryAuthConfig struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Auth string `json:"auth,omitempty"`
IdentityToken string `json:"identityToken,omitempty"`
}

// Configures containerd TLS for a registry
Expand Down
38 changes: 38 additions & 0 deletions pkg/apis/kubeone/v1beta2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/apis/kubeone/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/apis/kubeone/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ containerRuntime:
# docker.io:
# mirrors:
# - http://plain-text2.tld
# auth:
# # all of the following fields are optional
# username: "u5er"
# password: "myc00lp455w0rd"
# auth: "base64(user:password)"
# identityToken: ""
# "*":
# mirrors:
# - https://secure.tld
Expand Down
21 changes: 20 additions & 1 deletion pkg/containerruntime/containerd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ type containerdRegistryMirror struct {
}

type containerdRegistryConfig struct {
TLS *containerdRegistryTLSConfig `toml:"tls"`
TLS *containerdRegistryTLSConfig `toml:"tls"`
Auth *containerdRegistryAuth `toml:"auth"`
}

type containerdRegistryAuth struct {
Username string `toml:"username"`
Password string `toml:"password"`
Auth string `toml:"auth"`
IdentityToken string `toml:"identitytoken"`
}

type containerdRegistryTLSConfig struct {
Expand Down Expand Up @@ -118,6 +126,17 @@ func marshalContainerdConfig(cluster *kubeone.KubeOneCluster) (string, error) {
},
}
}

if registry.Auth != nil {
regConfig := criPlugin.Registry.Configs[registryName]
regConfig.Auth = &containerdRegistryAuth{
Username: registry.Auth.Username,
Password: registry.Auth.Password,
Auth: registry.Auth.Auth,
IdentityToken: registry.Auth.IdentityToken,
}
criPlugin.Registry.Configs[registryName] = regConfig
}
}
}

Expand Down
Loading