Skip to content

Commit

Permalink
chore: refactor: extract ClusterConfig
Browse files Browse the repository at this point in the history
Extract ClusterConfig and related types.
Make one huge file a bit smaller.

Signed-off-by: Alexey Palazhchenko <[email protected]>
  • Loading branch information
AlekSi authored and talos-bot committed Mar 29, 2021
1 parent 0328518 commit 35598f3
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 367 deletions.
6 changes: 4 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ issues:
- path: internal/app/machined/pkg/system/services
linters:
- dupl
- path: pkg/config/types/v1alpha1

# pkg/machinery module
- path: config/types/v1alpha1
linters:
- lll
- dupl # some interfaces and implementation are nearly identical (e.g. APIServer, ControllerManager, and Scheduler)

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
Expand Down
2 changes: 2 additions & 0 deletions pkg/machinery/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ type ClusterNetwork interface {
PodCIDR() string
ServiceCIDR() string
DNSDomain() string
// APIServerIPs returns kube-apiserver IPs in the ServiceCIDR.
APIServerIPs() ([]net.IP, error)
// DNSServiceIPs returns DNS service IPs in the ServiceCIDR.
DNSServiceIPs() ([]net.IP, error)
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_apiserverconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 v1alpha1

import (
"fmt"

"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/constants"
)

// Image implements the config.APIServer interface.
func (a *APIServerConfig) Image() string {
image := a.ContainerImage

if image == "" {
image = fmt.Sprintf("%s:v%s", constants.KubernetesAPIServerImage, constants.DefaultKubernetesVersion)
}

return image
}

// ExtraArgs implements the config.APIServer interface.
func (a *APIServerConfig) ExtraArgs() map[string]string {
return a.ExtraArgsConfig
}

// ExtraVolumes implements the config.APIServer interface.
func (a *APIServerConfig) ExtraVolumes() []config.VolumeMount {
volumes := make([]config.VolumeMount, 0, len(a.ExtraVolumesConfig))

for _, volume := range a.ExtraVolumesConfig {
volumes = append(volumes, volume)
}

return volumes
}
242 changes: 242 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_clusterconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// 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 v1alpha1

import (
"fmt"
"net"
"net/url"
"strings"

"github.com/talos-systems/crypto/x509"
talosnet "github.com/talos-systems/net"

"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/machinery/constants"
)

// ClusterConfig implements config.ClusterConfig, config.Token, and config.ClusterNetwork interfaces.

// Name implements the config.ClusterConfig interface.
func (c *ClusterConfig) Name() string {
return c.ClusterName
}

// APIServer implements the config.ClusterConfig interface.
func (c *ClusterConfig) APIServer() config.APIServer {
if c.APIServerConfig == nil {
return &APIServerConfig{}
}

return c.APIServerConfig
}

// ControllerManager implements the config.ClusterConfig interface.
func (c *ClusterConfig) ControllerManager() config.ControllerManager {
if c.ControllerManagerConfig == nil {
return &ControllerManagerConfig{}
}

return c.ControllerManagerConfig
}

// Proxy implements the config.ClusterConfig interface.
func (c *ClusterConfig) Proxy() config.Proxy {
if c.ProxyConfig == nil {
return &ProxyConfig{}
}

return c.ProxyConfig
}

// Scheduler implements the config.ClusterConfig interface.
func (c *ClusterConfig) Scheduler() config.Scheduler {
if c.SchedulerConfig == nil {
return &SchedulerConfig{}
}

return c.SchedulerConfig
}

// Endpoint implements the config.ClusterConfig interface.
func (c *ClusterConfig) Endpoint() *url.URL {
return c.ControlPlane.Endpoint.URL
}

// Token implements the config.ClusterConfig interface.
func (c *ClusterConfig) Token() config.Token {
return c
}

// CertSANs implements the config.ClusterConfig interface.
func (c *ClusterConfig) CertSANs() []string {
return c.APIServerConfig.CertSANs
}

// CA implements the config.ClusterConfig interface.
func (c *ClusterConfig) CA() *x509.PEMEncodedCertificateAndKey {
return c.ClusterCA
}

// AggregatorCA implements the config.ClusterConfig interface.
func (c *ClusterConfig) AggregatorCA() *x509.PEMEncodedCertificateAndKey {
return c.ClusterAggregatorCA
}

// ServiceAccount implements the config.ClusterConfig interface.
func (c *ClusterConfig) ServiceAccount() *x509.PEMEncodedKey {
return c.ClusterServiceAccount
}

// AESCBCEncryptionSecret implements the config.ClusterConfig interface.
func (c *ClusterConfig) AESCBCEncryptionSecret() string {
return c.ClusterAESCBCEncryptionSecret
}

// Config implements the config.ClusterConfig interface.
func (c *ClusterConfig) Config(t machine.Type) (string, error) {
return "", nil
}

// Etcd implements the config.ClusterConfig interface.
func (c *ClusterConfig) Etcd() config.Etcd {
if c.EtcdConfig == nil {
c.EtcdConfig = &EtcdConfig{}
}

return c.EtcdConfig
}

// Network implements the config.ClusterConfig interface.
func (c *ClusterConfig) Network() config.ClusterNetwork {
return c
}

// LocalAPIServerPort implements the config.ClusterConfig interface.
func (c *ClusterConfig) LocalAPIServerPort() int {
if c.ControlPlane.LocalAPIServerPort == 0 {
return constants.DefaultControlPlanePort
}

return c.ControlPlane.LocalAPIServerPort
}

// CoreDNS implements the config.ClusterConfig interface.
func (c *ClusterConfig) CoreDNS() config.CoreDNS {
if c.CoreDNSConfig == nil {
return &CoreDNS{}
}

return c.CoreDNSConfig
}

// ExtraManifestURLs implements the config.ClusterConfig interface.
func (c *ClusterConfig) ExtraManifestURLs() []string {
return c.ExtraManifests
}

// ExtraManifestHeaderMap implements the config.ClusterConfig interface.
func (c *ClusterConfig) ExtraManifestHeaderMap() map[string]string {
return c.ExtraManifestHeaders
}

// AdminKubeconfig implements the config.ClusterConfig interface.
func (c *ClusterConfig) AdminKubeconfig() config.AdminKubeconfig {
return c.AdminKubeconfigConfig
}

// ScheduleOnMasters implements the config.ClusterConfig interface.
func (c *ClusterConfig) ScheduleOnMasters() bool {
return c.AllowSchedulingOnMasters
}

// ID implements the config.Token interface.
func (c *ClusterConfig) ID() string {
parts := strings.Split(c.BootstrapToken, ".")
if len(parts) != 2 {
return ""
}

return parts[0]
}

// Secret implements the config.Token interface.
func (c *ClusterConfig) Secret() string {
parts := strings.Split(c.BootstrapToken, ".")
if len(parts) != 2 {
return ""
}

return parts[1]
}

// CNI implements the config.ClusterNetwork interface.
func (c *ClusterConfig) CNI() config.CNI {
switch {
case c.ClusterNetwork == nil:
fallthrough

case c.ClusterNetwork.CNI == nil:
return &CNIConfig{
CNIName: constants.DefaultCNI,
}
}

return c.ClusterNetwork.CNI
}

// PodCIDR implements the config.ClusterNetwork interface.
func (c *ClusterConfig) PodCIDR() string {
switch {
case c.ClusterNetwork == nil:
fallthrough
case len(c.ClusterNetwork.PodSubnet) == 0:
return constants.DefaultIPv4PodNet
}

return strings.Join(c.ClusterNetwork.PodSubnet, ",")
}

// ServiceCIDR implements the config.ClusterNetwork interface.
func (c *ClusterConfig) ServiceCIDR() string {
switch {
case c.ClusterNetwork == nil:
fallthrough
case len(c.ClusterNetwork.ServiceSubnet) == 0:
return constants.DefaultIPv4ServiceNet
}

return strings.Join(c.ClusterNetwork.ServiceSubnet, ",")
}

// DNSDomain implements the config.ClusterNetwork interface.
func (c *ClusterConfig) DNSDomain() string {
if c.ClusterNetwork == nil {
return constants.DefaultDNSDomain
}

return c.ClusterNetwork.DNSDomain
}

// APIServerIPs implements the config.ClusterNetwork interface.
func (c *ClusterConfig) APIServerIPs() ([]net.IP, error) {
serviceCIDRs, err := talosnet.SplitCIDRs(c.ServiceCIDR())
if err != nil {
return nil, fmt.Errorf("failed to process Service CIDRs: %w", err)
}

return talosnet.NthIPInCIDRSet(serviceCIDRs, 1)
}

// DNSServiceIPs implements the config.ClusterNetwork interface.
func (c *ClusterConfig) DNSServiceIPs() ([]net.IP, error) {
serviceCIDRs, err := talosnet.SplitCIDRs(c.ServiceCIDR())
if err != nil {
return nil, fmt.Errorf("failed to process Service CIDRs: %w", err)
}

return talosnet.NthIPInCIDRSet(serviceCIDRs, 10)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 v1alpha1

import (
"fmt"

"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/constants"
)

// Image implements the config.ControllerManager interface.
func (c *ControllerManagerConfig) Image() string {
image := c.ContainerImage

if image == "" {
image = fmt.Sprintf("%s:v%s", constants.KubernetesControllerManagerImage, constants.DefaultKubernetesVersion)
}

return image
}

// ExtraArgs implements the config.ControllerManager interface.
func (c *ControllerManagerConfig) ExtraArgs() map[string]string {
return c.ExtraArgsConfig
}

// ExtraVolumes implements the config.ControllerManager interface.
func (c *ControllerManagerConfig) ExtraVolumes() []config.VolumeMount {
volumes := make([]config.VolumeMount, 0, len(c.ExtraVolumesConfig))

for _, volume := range c.ExtraVolumesConfig {
volumes = append(volumes, volume)
}

return volumes
}
44 changes: 44 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_etcdconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 v1alpha1

import (
"fmt"
goruntime "runtime"

"github.com/talos-systems/crypto/x509"

"github.com/talos-systems/talos/pkg/machinery/constants"
)

// Image implements the config.Etcd interface.
func (e *EtcdConfig) Image() string {
image := e.ContainerImage
suffix := ""

if goruntime.GOARCH == "arm64" {
suffix = "-arm64"
}

if image == "" {
image = fmt.Sprintf("%s:%s%s", constants.EtcdImage, constants.DefaultEtcdVersion, suffix)
}

return image
}

// CA implements the config.Etcd interface.
func (e *EtcdConfig) CA() *x509.PEMEncodedCertificateAndKey {
return e.RootCA
}

// ExtraArgs implements the config.Etcd interface.
func (e *EtcdConfig) ExtraArgs() map[string]string {
if e.EtcdExtraArgs == nil {
e.EtcdExtraArgs = make(map[string]string)
}

return e.EtcdExtraArgs
}
Loading

0 comments on commit 35598f3

Please sign in to comment.