-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: suppress spurious Kubernetes API server cert updates
With the last changes, `kube-apiserver` certificates are generated based on the assigned `NodeAdresses`, machine configuration, etc. Whenver the certificate is regenerated, `kube-apiserver` is reloaded to pick up the new cert. With Virtual IP enabled, Virtual IP address is included into the certificate from the beginning as it is specified in the machine configuration, but as virtual IP moves between the nodes this causes `NodeAddresses` update, which triggers the controller, generates new certs and reloads `kube-apiserver` at bad time (right after VIP got moved). Even though the cert generated is identical to the previous one, the API server reload makes it unavailable for 30-90 seconds. This change extracts `CertSANs` as a separate resource so that its updates are suppressed if the CertSANs sources change, but the final list stays the same, and in turn prevents final certificate from being updated. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
- Loading branch information
Showing
13 changed files
with
799 additions
and
183 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
internal/app/machined/pkg/controllers/secrets/api_cert_sans.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// 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 ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"go.uber.org/zap" | ||
|
||
"github.com/talos-systems/talos/pkg/resources/k8s" | ||
"github.com/talos-systems/talos/pkg/resources/network" | ||
"github.com/talos-systems/talos/pkg/resources/secrets" | ||
) | ||
|
||
// APICertSANsController manages secrets.APICertSANs based on configuration. | ||
type APICertSANsController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *APICertSANsController) Name() string { | ||
return "secrets.APICertSANsController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
// | ||
//nolint:dupl | ||
func (ctrl *APICertSANsController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: secrets.NamespaceName, | ||
Type: secrets.RootType, | ||
ID: pointer.ToString(secrets.RootOSID), | ||
Kind: controller.InputWeak, | ||
}, | ||
{ | ||
Namespace: network.NamespaceName, | ||
Type: network.HostnameStatusType, | ||
ID: pointer.ToString(network.HostnameID), | ||
Kind: controller.InputWeak, | ||
}, | ||
{ | ||
Namespace: network.NamespaceName, | ||
Type: network.NodeAddressType, | ||
ID: pointer.ToString(network.FilteredNodeAddressID(network.NodeAddressAccumulativeID, k8s.NodeAddressFilterNoK8s)), | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *APICertSANsController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: secrets.CertSANType, | ||
Kind: controller.OutputShared, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *APICertSANsController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
} | ||
|
||
apiRootRes, err := r.Get(ctx, resource.NewMetadata(secrets.NamespaceName, secrets.RootType, secrets.RootOSID, resource.VersionUndefined)) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
if err = ctrl.teardownAll(ctx, r); err != nil { | ||
return fmt.Errorf("error destroying resources: %w", err) | ||
} | ||
|
||
continue | ||
} | ||
|
||
return fmt.Errorf("error getting root k8s secrets: %w", err) | ||
} | ||
|
||
apiRoot := apiRootRes.(*secrets.Root).OSSpec() | ||
|
||
hostnameResource, err := r.Get(ctx, resource.NewMetadata(network.NamespaceName, network.HostnameStatusType, network.HostnameID, resource.VersionUndefined)) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
continue | ||
} | ||
|
||
return err | ||
} | ||
|
||
hostnameStatus := hostnameResource.(*network.HostnameStatus).TypedSpec() | ||
|
||
addressesResource, err := r.Get(ctx, | ||
resource.NewMetadata(network.NamespaceName, network.NodeAddressType, network.FilteredNodeAddressID(network.NodeAddressAccumulativeID, k8s.NodeAddressFilterNoK8s), resource.VersionUndefined)) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
continue | ||
} | ||
|
||
return err | ||
} | ||
|
||
nodeAddresses := addressesResource.(*network.NodeAddress).TypedSpec() | ||
|
||
if err = r.Modify(ctx, secrets.NewCertSAN(secrets.NamespaceName, secrets.CertSANAPIID), func(r resource.Resource) error { | ||
spec := r.(*secrets.CertSAN).TypedSpec() | ||
|
||
spec.AppendIPs(apiRoot.CertSANIPs...) | ||
spec.AppendIPs(nodeAddresses.IPs()...) | ||
|
||
spec.AppendDNSNames(apiRoot.CertSANDNSNames...) | ||
spec.AppendDNSNames(hostnameStatus.Hostname, hostnameStatus.FQDN()) | ||
|
||
spec.FQDN = hostnameStatus.FQDN() | ||
|
||
spec.Sort() | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func (ctrl *APICertSANsController) teardownAll(ctx context.Context, r controller.Runtime) error { | ||
list, err := r.List(ctx, resource.NewMetadata(secrets.NamespaceName, secrets.CertSANType, "", resource.VersionUndefined)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, res := range list.Items { | ||
if res.Metadata().Owner() == ctrl.Name() { | ||
if err = r.Destroy(ctx, res.Metadata()); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.