From 9a33881b14a2357fcb19e8ee2f114c48911a656c Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 16 Sep 2024 19:13:04 +1000 Subject: [PATCH] fix: correct service owner references (#2691) --- .../scaling/k8sscaling/deployment_provisioner.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/controller/scaling/k8sscaling/deployment_provisioner.go b/backend/controller/scaling/k8sscaling/deployment_provisioner.go index 530dba18e5..768df7fa9f 100644 --- a/backend/controller/scaling/k8sscaling/deployment_provisioner.go +++ b/backend/controller/scaling/k8sscaling/deployment_provisioner.go @@ -218,7 +218,7 @@ func (r *DeploymentProvisioner) handleNewDeployment(ctx context.Context, dep *sc service.Name = name service.Labels["app"] = name - deployment.OwnerReferences = []v1.OwnerReference{{APIVersion: "apps/v1", Kind: "deployment", Name: thisDeploymentName, UID: deployment.UID}} + service.OwnerReferences = []v1.OwnerReference{{APIVersion: "apps/v1", Kind: "deployment", Name: thisDeploymentName, UID: deployment.UID}} service.Spec.Selector = map[string]string{"app": name} _, err = r.Client.CoreV1().Services(r.Namespace).Create(ctx, service, v1.CreateOptions{}) if err != nil { @@ -369,6 +369,15 @@ func (r *DeploymentProvisioner) deleteMissingDeployments(ctx context.Context) { logger.Errorf(err, "failed to delete deployment %s", deployment.Name) } + // With owner references the service should be deleted automatically + // However there was a bug so we need this in prod for a bit to clean up + logger.Infof("deleting service %s", deployment.Name) + err = r.Client.CoreV1().Services(r.Namespace).Delete(ctx, deployment.Name, v1.DeleteOptions{}) + if err != nil { + if !errors.IsNotFound(err) { + logger.Errorf(err, "failed to delete service %s", deployment.Name) + } + } } } }