talos/pkg/resources/secrets/kubernetes.go
Andrey Smirnov b914398154 refactor: split kubernetes/etcd resource generation into subresources
Fixes #3062

There's no user-visible change in this PR.

It carefully separates generated secrets (e.g. certs) from source
secrets from the config (e.g. CAs), so that certs are generated on
config changes which actually affect cert input.

And same way separates etcd and Kubernetes PKI, so if etcd CA got
changed, only etcd certs will be regenerated.

This should have noticeable impact with RSA-based PKI as it reduces
number of times PKI gets generated.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2021-02-18 22:01:28 -08:00

83 lines
2.2 KiB
Go

// 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 secrets
import (
"fmt"
"github.com/talos-systems/crypto/x509"
"github.com/talos-systems/os-runtime/pkg/resource"
"github.com/talos-systems/os-runtime/pkg/resource/core"
)
// KubernetesType is type of Kubernetes resource.
const KubernetesType = resource.Type("secrets/kubernetes")
// KubernetesID is a resource ID of singleton instance.
const KubernetesID = resource.ID("k8s-certs")
// Kubernetes contains K8s generated secrets.
type Kubernetes struct {
md resource.Metadata
spec interface{}
}
// KubernetesCertsSpec describes generated Kubernetes certificates.
type KubernetesCertsSpec struct {
APIServer *x509.PEMEncodedCertificateAndKey `yaml:"apiServer"`
APIServerKubeletClient *x509.PEMEncodedCertificateAndKey `yaml:"apiServerKubeletClient"`
FrontProxy *x509.PEMEncodedCertificateAndKey `yaml:"frontProxy"`
AdminKubeconfig string `yaml:"adminKubeconfig"`
}
// NewKubernetes initializes a Kubernetes resource.
func NewKubernetes() *Kubernetes {
r := &Kubernetes{
md: resource.NewMetadata(NamespaceName, KubernetesType, KubernetesID, resource.VersionUndefined),
spec: &KubernetesCertsSpec{},
}
r.md.BumpVersion()
return r
}
// Metadata implements resource.Resource.
func (r *Kubernetes) Metadata() *resource.Metadata {
return &r.md
}
// Spec implements resource.Resource.
func (r *Kubernetes) Spec() interface{} {
return r.spec
}
func (r *Kubernetes) String() string {
return fmt.Sprintf("secrets.Kubernetes(%q)", r.md.ID())
}
// DeepCopy implements resource.Resource.
func (r *Kubernetes) DeepCopy() resource.Resource {
return &Kubernetes{
md: r.md,
spec: r.spec,
}
}
// ResourceDefinition implements core.ResourceDefinitionProvider interface.
func (r *Kubernetes) ResourceDefinition() core.ResourceDefinitionSpec {
return core.ResourceDefinitionSpec{
Type: KubernetesType,
Aliases: []resource.Type{"k8sSecret", "k8sSecrets"},
DefaultNamespace: NamespaceName,
}
}
// Certs returns .spec.
func (r *Kubernetes) Certs() *KubernetesCertsSpec {
return r.spec.(*KubernetesCertsSpec)
}