This repository has been archived by the owner on May 23, 2023. It is now read-only.
generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from vshn/feat/pg-reboot
Add UpdateStrategy to VSHNPostgreSQL
- Loading branch information
Showing
9 changed files
with
1,128 additions
and
9 deletions.
There are no files selected for viewing
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,143 @@ | ||
package vshnpostgres | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
sgv1 "github.com/vshn/appcat-comp-functions/apis/stackgres/v1" | ||
"github.com/vshn/appcat-comp-functions/runtime" | ||
vshnv1 "github.com/vshn/component-appcat/apis/vshn/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/pointer" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
var sgDbOpsRestartRetention time.Duration = 30 * 24 * time.Hour | ||
|
||
// TransformRestart triggers a restart of the postgreqsql instance if there is a pending restart and the composite is configured to restart on update. | ||
func TransformRestart(ctx context.Context, iof *runtime.Runtime) runtime.Result { | ||
return transformRestart(ctx, iof, time.Now) | ||
} | ||
|
||
func transformRestart(ctx context.Context, iof *runtime.Runtime, now func() time.Time) runtime.Result { | ||
comp := vshnv1.VSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(ctx, &comp) | ||
if err != nil { | ||
return runtime.NewFatal(ctx, err.Error()) | ||
} | ||
err = keepRecentRestartOps(ctx, iof, comp.GetName(), now) | ||
if err != nil { | ||
return runtime.NewFatal(ctx, err.Error()) | ||
} | ||
|
||
if comp.Spec.Parameters.UpdateStrategy.Type == vshnv1.VSHNPostgreSQLUpdateStrategyTypeOnRestart { | ||
return runtime.NewNormal() | ||
} | ||
|
||
restartTime, err := getPendingRestart(ctx, iof) | ||
if err != nil { | ||
return runtime.NewWarning(ctx, err.Error()) | ||
} | ||
|
||
if restartTime.IsZero() { | ||
return runtime.NewNormal() | ||
} | ||
|
||
err = scheduleRestart(ctx, iof, comp.GetName(), restartTime) | ||
if err != nil { | ||
return runtime.NewFatal(ctx, err.Error()) | ||
} | ||
|
||
return runtime.NewNormal() | ||
} | ||
|
||
func getPendingRestart(ctx context.Context, iof *runtime.Runtime) (time.Time, error) { | ||
cluster := sgv1.SGCluster{} | ||
|
||
err := iof.Observed.GetFromObject(ctx, &cluster, "cluster") | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
log := controllerruntime.LoggerFrom(ctx).WithValues("cluster", cluster.Name) | ||
if cluster.Status.Conditions == nil { | ||
return time.Time{}, nil | ||
} | ||
|
||
for _, cond := range *cluster.Status.Conditions { | ||
if cond.Type == nil || *cond.Type != sgv1.SGClusterConditionTypePendingRestart || cond.Status == nil || cond.LastTransitionTime == nil { | ||
continue | ||
} | ||
status, err := strconv.ParseBool(*cond.Status) | ||
if err != nil || !status { | ||
continue | ||
} | ||
|
||
log.WithValues("at", cond.LastTransitionTime).Info("PendingRestart") | ||
|
||
restartTime, err := time.Parse(time.RFC3339, *cond.LastTransitionTime) | ||
if err != nil { | ||
continue | ||
} | ||
return restartTime, nil | ||
} | ||
|
||
return time.Time{}, nil | ||
} | ||
|
||
func scheduleRestart(ctx context.Context, iof *runtime.Runtime, compName string, restartTime time.Time) error { | ||
name := fmt.Sprintf("pg-restart-%d", restartTime.Unix()) | ||
ops := sgv1.SGDbOps{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: fmt.Sprintf("vshn-postgresql-%s", compName), | ||
}, | ||
Spec: sgv1.SGDbOpsSpec{ | ||
Op: sgv1.SGDbOpsOpRestart, | ||
SgCluster: compName, | ||
Restart: &sgv1.SGDbOpsSpecRestart{ | ||
Method: pointer.String(sgv1.SGDbOpsRestartMethodInPlace), | ||
OnlyPendingRestart: pointer.Bool(true), | ||
}, | ||
RunAt: pointer.String(restartTime.Format(time.RFC3339)), | ||
}, | ||
} | ||
return iof.Desired.PutIntoObject(ctx, &ops, fmt.Sprintf("%s-%s", compName, name)) | ||
} | ||
|
||
func keepRecentRestartOps(ctx context.Context, iof *runtime.Runtime, compName string, now func() time.Time) error { | ||
|
||
for _, r := range iof.Observed.List(ctx) { | ||
if !strings.HasPrefix(r.GetName(), fmt.Sprintf("%s-pg-restart", compName)) { | ||
continue | ||
} | ||
|
||
op := sgv1.SGDbOps{} | ||
err := iof.Observed.GetFromObject(ctx, &op, r.GetName()) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
if op.Spec.Op != "restart" || op.Spec.RunAt == nil { | ||
continue | ||
} | ||
|
||
runAt, err := time.Parse(time.RFC3339, *op.Spec.RunAt) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
if runAt.Before(now().Add(-1 * sgDbOpsRestartRetention)) { | ||
continue | ||
} | ||
|
||
err = iof.Desired.PutIntoObject(ctx, &op, r.GetName()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,92 @@ | ||
package vshnpostgres | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
sgv1 "github.com/vshn/appcat-comp-functions/apis/stackgres/v1" | ||
"github.com/vshn/appcat-comp-functions/runtime" | ||
vshnv1 "github.com/vshn/component-appcat/apis/vshn/v1" | ||
|
||
fnv1aplha1 "github.com/crossplane/crossplane/apis/apiextensions/fn/io/v1alpha1" | ||
) | ||
|
||
func TestTransformRestart_NoopNoPending(t *testing.T) { | ||
iof := loadRuntimeFromFile(t, "restart/01-NoPendingReboot.yaml") | ||
require.NoError(t, runtime.AddToScheme(sgv1.SchemeBuilder.SchemeBuilder)) | ||
|
||
res := TransformRestart(context.TODO(), iof) | ||
assert.Equal(t, fnv1aplha1.SeverityNormal, res.Resolve().Severity, res.Resolve().Message) | ||
|
||
comp := &vshnv1.XVSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(context.TODO(), comp) | ||
assert.NoError(t, err) | ||
|
||
assert.Len(t, iof.Desired.List(context.TODO()), 0) | ||
} | ||
func TestTransformRestart_NoopPendingOnRestart(t *testing.T) { | ||
iof := loadRuntimeFromFile(t, "restart/02-PendingRebootNoRestart.yaml") | ||
require.NoError(t, runtime.AddToScheme(sgv1.SchemeBuilder.SchemeBuilder)) | ||
|
||
res := TransformRestart(context.TODO(), iof) | ||
assert.Equal(t, fnv1aplha1.SeverityNormal, res.Resolve().Severity, res.Resolve().Message) | ||
|
||
comp := &vshnv1.XVSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(context.TODO(), comp) | ||
assert.NoError(t, err) | ||
|
||
assert.Len(t, iof.Desired.List(context.TODO()), 0) | ||
} | ||
|
||
func TestTransformRestart_RestartPending(t *testing.T) { | ||
iof := loadRuntimeFromFile(t, "restart/02-PendingReboot.yaml") | ||
require.NoError(t, runtime.AddToScheme(sgv1.SchemeBuilder.SchemeBuilder)) | ||
|
||
res := TransformRestart(context.TODO(), iof) | ||
assert.Equal(t, fnv1aplha1.SeverityNormal, res.Resolve().Severity, res.Resolve().Message) | ||
|
||
comp := &vshnv1.XVSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(context.TODO(), comp) | ||
assert.NoError(t, err) | ||
|
||
assert.Len(t, iof.Desired.List(context.TODO()), 1) | ||
|
||
sgrest := sgv1.SGDbOps{} | ||
assert.NoError(t, iof.Desired.GetFromObject(context.TODO(), &sgrest, "pgsql-gc9x4-pg-restart-1682587342")) | ||
assert.Equal(t, "restart", sgrest.Spec.Op) | ||
if assert.NotNil(t, sgrest.Spec.RunAt) { | ||
assert.Equal(t, "2023-04-27T09:22:22Z", *sgrest.Spec.RunAt) | ||
} | ||
if assert.NotNil(t, sgrest.Spec.Restart) { | ||
if assert.NotNil(t, sgrest.Spec.Restart.Method) { | ||
assert.Equal(t, "InPlace", *sgrest.Spec.Restart.Method) | ||
} | ||
if assert.NotNil(t, sgrest.Spec.Restart.OnlyPendingRestart) { | ||
assert.True(t, *sgrest.Spec.Restart.OnlyPendingRestart) | ||
} | ||
} | ||
assert.Equal(t, "restart", sgrest.Spec.Op) | ||
} | ||
|
||
func TestTransformRestart_KeepRecentReboots(t *testing.T) { | ||
iof := loadRuntimeFromFile(t, "restart/03-KeepRecentReboots.yaml") | ||
require.NoError(t, runtime.AddToScheme(sgv1.SchemeBuilder.SchemeBuilder)) | ||
|
||
now := func() time.Time { | ||
n, err := time.Parse(time.RFC3339, "2023-04-27T10:04:05Z") | ||
assert.NoError(t, err) | ||
return n | ||
} | ||
|
||
res := transformRestart(context.TODO(), iof, now) | ||
assert.Equal(t, fnv1aplha1.SeverityNormal, res.Resolve().Severity, res.Resolve().Message) | ||
|
||
comp := &vshnv1.XVSHNPostgreSQL{} | ||
err := iof.Desired.GetComposite(context.TODO(), comp) | ||
assert.NoError(t, err) | ||
|
||
assert.Len(t, iof.Desired.List(context.TODO()), 2) | ||
} |
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
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
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
Oops, something went wrong.