Skip to content

Commit

Permalink
Setting migration owner refs and apply pod names
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Apr 4, 2020
1 parent ecaa52a commit f0d73bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/controller/migration/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func getApplyConfigMap(migrationID string, namespace string, preparedStatement s
return configMap, nil
}

func podNameForMigrationApply(databaseName string, tableName string, migrationID string) string {
podName := fmt.Sprintf("%s-%s-%s-apply", databaseName, tableName, migrationID)
if len(apimachineryvalidation.NameIsDNSSubdomain(podName, false)) > 0 {
podName = fmt.Sprintf("%s-%s-apply", tableName, migrationID)
if len(apimachineryvalidation.NameIsDNSSubdomain(podName, false)) > 0 {
podName = fmt.Sprintf("%s-apply", migrationID)
}
}

return podName
}

func getApplyPod(migrationID string, namespace string, connectionURI string, database *databasesv1alpha3.Database, table *schemasv1alpha3.Table) (*corev1.Pod, error) {
imageName := "schemahero/schemahero:alpha"
nodeSelector := make(map[string]string)
Expand All @@ -58,8 +70,6 @@ func getApplyPod(migrationID string, namespace string, connectionURI string, dat
labels["schemahero-namespace"] = table.Namespace
labels["schemahero-role"] = "apply"

name := fmt.Sprintf("%s-apply", table.Name)

driver := ""
if database.Spec.Connection.Postgres != nil {
driver = "postgres"
Expand All @@ -79,7 +89,7 @@ func getApplyPod(migrationID string, namespace string, connectionURI string, dat

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: podNameForMigrationApply(database.Name, table.Name, migrationID),
Namespace: database.Namespace,
Labels: labels,
},
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/migration/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -58,6 +59,10 @@ func (r *ReconcileMigration) reconcileInstance(ctx context.Context, instance *sc
if err := r.Create(ctx, desiredConfigMap); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create config map")
}

if err := controllerutil.SetControllerReference(instance, desiredConfigMap, r.scheme); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to set owner on configmap")
}
} else if err == nil {
// update it
existingConfigMap.Data = map[string]string{}
Expand All @@ -68,6 +73,9 @@ func (r *ReconcileMigration) reconcileInstance(ctx context.Context, instance *sc
if err = r.Update(ctx, &existingConfigMap); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to update config map")
}
if err := controllerutil.SetControllerReference(instance, &existingConfigMap, r.scheme); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to update owner on configmap")
}
configMapChanged = true
} else {
// something bad is happening here
Expand All @@ -88,6 +96,9 @@ func (r *ReconcileMigration) reconcileInstance(ctx context.Context, instance *sc
if err := r.Create(ctx, desiredPod); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create apply pod")
}
if err := controllerutil.SetControllerReference(instance, desiredPod, r.scheme); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to set owner on pod")
}
} else if err == nil {
// maybe update it
if configMapChanged {
Expand Down

0 comments on commit f0d73bf

Please sign in to comment.