mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 23:23:23 +09:00
A converted chart reads each account's stored value from an account Secret the platform keeps, and nothing writes those Secrets for the applications that exist before the chart switches. Without a conversion every existing account would stop logging in at the switch. The command has two halves. seed runs from the platform's pre-upgrade hook, before any component chart changes version: it writes the account Secrets unowned, with the stored form of the password the tenant could read until now and origin Migrated, records each release's chart version, and marks a release for seeding only when every account got its Secret, so the CredentialTemplate controller never seeds a random password over one the release's clients still use. It takes the kind's format and accounts path from flags, because the hook runs before the converted chart renders any template. finish runs from a Job once the charts have switched: per release it waits for the converted chart to be Ready, strips any password left in the values and deletes the old source Secret. seed fails closed. Only a release whose accounts cannot be resolved from its values is left out; any other error fails the hook and the upgrade retries, since a left-out release whose source Helm then deletes would keep neither an account Secret nor the plaintext. finish holds a release recorded without a chart version rather than retiring it as soon as it renders, old chart included. A transient API or connection error is retried on the same release a few times. If it persists, the release is recorded for the incomplete alert but announced with CredentialsMigrationRetrying, a Normal event, since the Job's restart retries it; only an error no retry fixes records the Warning CredentialsMigrationSkipped "left this release out", which reads as a release that needs a person. While any release is left out, the state ConfigMap carries credentials.cozystack.io/incomplete=true. The stored form comes from the platform's own format package, so a migrated release and a mint store the same value. Co-authored-by: IvanHunters <xorokhotnikov@gmail.com> Co-authored-by: Andrei Kvapil <kvapss@gmail.com> Assisted-by: LLM Signed-off-by: Timofei Larkin <lllamnyp@gmail.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
100 lines
3.7 KiB
Go
100 lines
3.7 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
// reasonRetrying names an Event for a release that hit a transient error and
|
|
// is converted on a later attempt, unlike reasonSkipped, which needs a person.
|
|
const reasonRetrying = "CredentialsMigrationRetrying"
|
|
|
|
// transientAttempts and transientDelay bound the in-process retries of a
|
|
// release that hit a transient error, before the Job's own restart retries it.
|
|
var (
|
|
transientAttempts = 3
|
|
transientDelay = 10 * time.Second
|
|
)
|
|
|
|
// isTransient reports whether err comes from the API server or the
|
|
// connection to it rather than from the release: retrying the same release
|
|
// can succeed without anyone changing anything.
|
|
func isTransient(err error) bool {
|
|
switch {
|
|
case apierrors.IsServerTimeout(err), apierrors.IsTimeout(err), apierrors.IsTooManyRequests(err),
|
|
apierrors.IsServiceUnavailable(err), apierrors.IsInternalError(err), apierrors.IsUnexpectedServerError(err),
|
|
apierrors.IsConflict(err):
|
|
return true
|
|
case errors.Is(err, io.EOF), errors.Is(err, io.ErrUnexpectedEOF),
|
|
errors.Is(err, syscall.ECONNRESET), errors.Is(err, syscall.ECONNREFUSED), errors.Is(err, syscall.EPIPE):
|
|
return true
|
|
}
|
|
var netErr net.Error
|
|
if errors.As(err, &netErr) {
|
|
return true
|
|
}
|
|
// client-go wraps a body read that failed mid-response this way.
|
|
return strings.Contains(err.Error(), "unexpected error when reading response body")
|
|
}
|
|
|
|
// finishReleaseRetrying runs finishRelease, retrying a transient error on a
|
|
// fresh read of the release.
|
|
func (m *migrator) finishReleaseRetrying(ctx context.Context, hr *helmv2.HelmRelease, rec *releaseState) error {
|
|
var err error
|
|
for attempt := 1; ; attempt++ {
|
|
if err = m.finishRelease(ctx, hr, rec); err == nil || !isTransient(err) || attempt >= transientAttempts {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "%s/%s: transient error, retrying: %v\n", hr.Namespace, hr.Name, err)
|
|
time.Sleep(transientDelay)
|
|
if gerr := m.c.Get(ctx, client.ObjectKeyFromObject(hr), hr); gerr != nil && !isTransient(gerr) {
|
|
return gerr
|
|
}
|
|
}
|
|
}
|
|
|
|
// leaveOut records a release finish could not convert. A transient error is
|
|
// recorded for the alert like any other, but announced as a retry rather
|
|
// than as a release left out, since the Job's restart converts it.
|
|
func (m *migrator) leaveOut(ctx context.Context, st *state, hr *helmv2.HelmRelease, cause error) {
|
|
if !isTransient(cause) {
|
|
m.skip(ctx, st, hr, cause)
|
|
return
|
|
}
|
|
key := hr.Namespace + "/" + hr.Name
|
|
st.Skipped[key] = fmt.Sprintf("transient error, retried by the Job: %s at %s", cause.Error(), m.now().UTC().Format(time.RFC3339))
|
|
fmt.Fprintf(os.Stderr, "%s: transient error, retried at the next attempt: %v\n", key, cause)
|
|
ev := &corev1.Event{
|
|
ObjectMeta: metav1.ObjectMeta{GenerateName: hr.Name + ".", Namespace: hr.Namespace},
|
|
InvolvedObject: corev1.ObjectReference{
|
|
APIVersion: helmv2.GroupVersion.String(), Kind: helmv2.HelmReleaseKind,
|
|
Name: hr.Name, Namespace: hr.Namespace, UID: hr.UID,
|
|
},
|
|
Reason: reasonRetrying,
|
|
Message: fmt.Sprintf("credentials migration %d hit a transient error on this release and retries it: %v", m.o.migration, cause),
|
|
Type: corev1.EventTypeNormal,
|
|
Source: corev1.EventSource{Component: "credentials-migrate"},
|
|
FirstTimestamp: metav1.NewTime(m.now()),
|
|
LastTimestamp: metav1.NewTime(m.now()),
|
|
Count: 1,
|
|
}
|
|
if err := m.c.Create(ctx, ev); err != nil {
|
|
fmt.Fprintf(os.Stderr, "%s: failed to record event: %v\n", key, err)
|
|
}
|
|
}
|