Skip to content

Commit

Permalink
Cleaning up migration pods
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Apr 5, 2020
1 parent d265b9b commit 5399cc3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
14 changes: 12 additions & 2 deletions pkg/controller/migration/migration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *ReconcileMigration) Reconcile(request reconcile.Request) (reconcile.Res
// so this function is simply an entrypoint that executes the right reconcile loop
instance, instanceErr := r.getInstance(request)
if instanceErr == nil {
result, err := r.reconcileInstance(context.Background(), instance)
result, err := r.reconcileMigration(context.Background(), instance)
if err != nil {
logger.Error(err)
}
Expand All @@ -117,7 +117,7 @@ func (r *ReconcileMigration) Reconcile(request reconcile.Request) (reconcile.Res
pod := &corev1.Pod{}
podErr := r.Get(context.Background(), request.NamespacedName, pod)
if podErr == nil {
result, err := r.reconcilePod(pod)
result, err := r.reconcilePod(context.Background(), pod)
if err != nil {
logger.Error(err)
}
Expand All @@ -127,6 +127,16 @@ func (r *ReconcileMigration) Reconcile(request reconcile.Request) (reconcile.Res
return reconcile.Result{}, errors.New("unknown error in migration reconciler")
}

func (r *ReconcileMigration) getInstance(request reconcile.Request) (*schemasv1alpha3.Migration, error) {
v1alpha3instance := &schemasv1alpha3.Migration{}
err := r.Get(context.Background(), request.NamespacedName, v1alpha3instance)
if err != nil {
return nil, err // don't wrap
}

return v1alpha3instance, nil
}

func (r *ReconcileMigration) readConnectionURI(database *databasesv1alpha3.Database) (string, error) {
var valueOrValueFrom *databasesv1alpha3.ValueOrValueFrom

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcileMigration) getInstance(request reconcile.Request) (*schemasv1alpha3.Migration, error) {
v1alpha3instance := &schemasv1alpha3.Migration{}
err := r.Get(context.Background(), request.NamespacedName, v1alpha3instance)
if err != nil {
return nil, err // don't wrap
}

return v1alpha3instance, nil
}

func (r *ReconcileMigration) reconcileInstance(ctx context.Context, instance *schemasv1alpha3.Migration) (reconcile.Result, error) {
func (r *ReconcileMigration) reconcileMigration(ctx context.Context, instance *schemasv1alpha3.Migration) (reconcile.Result, error) {
logger.Debug("reconciling migration",
zap.String("kind", instance.Kind),
zap.String("name", instance.Name),
Expand Down Expand Up @@ -122,13 +112,3 @@ func (r *ReconcileMigration) reconcileInstance(ctx context.Context, instance *sc

return reconcile.Result{}, nil
}

func (r *ReconcileMigration) reconcilePod(pod *corev1.Pod) (reconcile.Result, error) {
// podLabels := pod.GetObjectMeta().GetLabels()
// role, ok := podLabels["schemahero-role"]
// if !ok {
// return reconcile.Result{}, nil
// }

return reconcile.Result{}, nil
}
41 changes: 41 additions & 0 deletions pkg/controller/migration/reconcile_pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package migration

import (
"context"

"github.com/pkg/errors"
"github.com/schemahero/schemahero/pkg/logger"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcileMigration) reconcilePod(ctx context.Context, pod *corev1.Pod) (reconcile.Result, error) {
podLabels := pod.GetObjectMeta().GetLabels()
role, ok := podLabels["schemahero-role"]
if !ok {
return reconcile.Result{}, nil
}

if role != "" && role != "apply" {
// make sure we are filtering to relevant events
return reconcile.Result{}, nil
}

logger.Debug("reconciling schemahero pod",
zap.String("kind", pod.Kind),
zap.String("name", pod.Name),
zap.String("role", role),
zap.String("podPhase", string(pod.Status.Phase)))

if pod.Status.Phase != corev1.PodSucceeded {
return reconcile.Result{}, nil
}

// delete it
if err := r.Delete(ctx, pod); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to delete apply pod")
}

return reconcile.Result{}, nil
}
10 changes: 5 additions & 5 deletions pkg/controller/table/reconcile_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func (r *ReconcileTable) reconcilePod(ctx context.Context, pod *corev1.Pod) (rec
return reconcile.Result{}, nil
}

if role != "table" && role != "plan" {
// we want to avoid migration pods in this reconciler
return reconcile.Result{}, nil
}

logger.Debug("reconciling schemahero pod",
zap.String("kind", pod.Kind),
zap.String("name", pod.Name),
zap.String("role", role),
zap.String("podPhase", string(pod.Status.Phase)))

if role != "table" && role != "plan" {
// we want to avoid migration pods in this reconciler
return reconcile.Result{}, nil
}

if pod.Status.Phase != corev1.PodSucceeded {
return reconcile.Result{}, nil
}
Expand Down
File renamed without changes.

0 comments on commit 5399cc3

Please sign in to comment.