func()

in controllers/manager/staticgatewayconfiguration_controller.go [185:253]


func (r *StaticGatewayConfigurationReconciler) ensureDeleted(
	ctx context.Context,
	gwConfig *egressgatewayv1alpha1.StaticGatewayConfiguration,
) error {
	log := log.FromContext(ctx)
	log.Info(fmt.Sprintf("Reconciling staticGatewayConfiguration deletion %s/%s", gwConfig.Namespace, gwConfig.Name))

	if !controllerutil.ContainsFinalizer(gwConfig, consts.SGCFinalizerName) {
		log.Info("gwConfig does not have finalizer, no additional cleanup needed")
		return nil
	}

	mc := metrics.NewMetricsContext(
		os.Getenv(consts.PodNamespaceEnvKey),
		"delete_static_gateway_configuration",
		"n/a",
		"n/a",
		strings.ToLower(fmt.Sprintf("%s/%s", gwConfig.Namespace, gwConfig.Name)),
	) // no subscription_id/resource_group for SGC reconciler
	succeeded := false
	defer func() { mc.ObserveControllerReconcileMetrics(succeeded) }()

	secretDeleted := false
	log.Info("Deleting wireguard key")
	secret := &corev1.Secret{
		ObjectMeta: metav1.ObjectMeta{
			Name:      fmt.Sprintf("sgw-%s", string(gwConfig.UID)),
			Namespace: r.SecretNamespace,
		},
	}
	if err := r.Delete(ctx, secret); err != nil {
		if !apierrors.IsNotFound(err) {
			log.Error(err, "failed to delete existing gateway LB configuration")
			return err
		} else {
			secretDeleted = true
		}
	}

	lbConfigDeleted := false
	log.Info("Deleting gateway LB configuration")
	lbConfig := &egressgatewayv1alpha1.GatewayLBConfiguration{
		ObjectMeta: metav1.ObjectMeta{
			Name:      gwConfig.Name,
			Namespace: gwConfig.Namespace,
		},
	}
	if err := r.Delete(ctx, lbConfig); err != nil {
		if !apierrors.IsNotFound(err) {
			log.Error(err, "failed to delete existing gateway LB configuration")
			return err
		} else {
			lbConfigDeleted = true
		}
	}

	if secretDeleted && lbConfigDeleted {
		log.Info("Secret and LBConfig are deleted, removing finalizer")
		controllerutil.RemoveFinalizer(gwConfig, consts.SGCFinalizerName)
		if err := r.Update(ctx, gwConfig); err != nil {
			log.Error(err, "failed to remove finalizer")
			return err
		}
	}

	log.Info("staticGatewayConfiguration deletion reconciled")
	succeeded = true
	return nil
}