Skip to content

Commit

Permalink
Merge pull request #135 from schemahero/0.8.0-alpha.2
Browse files Browse the repository at this point in the history
Setting migration owner refs and apply pod names
  • Loading branch information
marccampbell committed Apr 5, 2020
2 parents 13294e9 + 0ec3dfe commit d750869
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Replicated, Inc.
Copyright 2020 Replicated, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -12,4 +12,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
*/
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 d750869

Please sign in to comment.