Skip to content

Commit

Permalink
Delete resources' cr if pulsar cluster doesn't exist (#236)
Browse files Browse the repository at this point in the history
* Delete resources cr if pulsar cluster doesn't exist

* Declare an error function and deal with topic

* Lint code
  • Loading branch information
yaalsn authored Sep 4, 2024
1 parent 0a10ea2 commit baaba51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
9 changes: 9 additions & 0 deletions pkg/admin/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package admin

import (
"errors"
"net"

"github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest"
)

Expand Down Expand Up @@ -70,3 +73,9 @@ func IsAlreadyExist(err error) bool {
func IsInternalServerError(err error) bool {
return ErrorReason(err) == ReasonInternalServerError
}

// IsNoSuchHostError returns true if operator cannot connect the resource host
func IsNoSuchHostError(err error) bool {
var dnsErr *net.DNSError
return errors.As(err, &dnsErr) && dnsErr.Err == "no such host"
}
8 changes: 6 additions & 2 deletions pkg/connection/reconcile_geo_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ func (r *PulsarGeoReplicationReconciler) ReconcileGeoReplication(ctx context.Con
// Delete the cluster that created with destination cluster info.
// TODO it can only be deleted after the cluster has been removed from the tenant, namespace, and topic
if err := pulsarAdmin.DeleteCluster(destClusterName); err != nil && !admin.IsNotFound(err) {
log.Error(err, "Failed to delete geo replication cluster")
return err
if admin.IsNoSuchHostError(err) {
log.Info("Pulsar cluster has been deleted")
} else {
log.Error(err, "Failed to delete geo replication cluster")
return err
}
}
}
controllerutil.RemoveFinalizer(geoReplication, resourcev1alpha1.FinalizerName)
Expand Down
22 changes: 15 additions & 7 deletions pkg/connection/reconcile_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,28 @@ func (r *PulsarNamespaceReconciler) ReconcileNamespace(ctx context.Context, puls
if namespace.Status.GeoReplicationEnabled {
log.Info("GeoReplication is enabled. Reset namespace cluster", "LifecyclePolicy", namespace.Spec.LifecyclePolicy, "ClusterName", r.conn.connection.Spec.ClusterName)
if err := pulsarAdmin.SetNamespaceClusters(namespace.Spec.Name, []string{r.conn.connection.Spec.ClusterName}); err != nil {
log.Error(err, "Failed to reset the cluster for namespace")
return err
if admin.IsNoSuchHostError(err) {
log.Info("Pulsar cluster has been deleted")
} else {
log.Error(err, "Failed to reset the cluster for namespace")
return err
}
}
}

if namespace.Spec.LifecyclePolicy != resourcev1alpha1.KeepAfterDeletion {
if err := pulsarAdmin.DeleteNamespace(namespace.Spec.Name); err != nil && !admin.IsNotFound(err) {
log.Error(err, "Failed to delete namespace")
meta.SetStatusCondition(&namespace.Status.Conditions, *NewErrorCondition(namespace.Generation, err.Error()))
if err := r.conn.client.Status().Update(ctx, namespace); err != nil {
log.Error(err, "Failed to update the geo replication status")
if admin.IsNoSuchHostError(err) {
log.Info("Pulsar cluster has been deleted")
} else {
log.Error(err, "Failed to delete namespace")
meta.SetStatusCondition(&namespace.Status.Conditions, *NewErrorCondition(namespace.Generation, err.Error()))
if err := r.conn.client.Status().Update(ctx, namespace); err != nil {
log.Error(err, "Failed to update the geo replication status")
return err
}
return err
}
return err
}
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/connection/reconcile_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ func (r *PulsarTenantReconciler) ReconcileTenant(ctx context.Context, pulsarAdmi
log.Info("Deleting tenant", "LifecyclePolicy", tenant.Spec.LifecyclePolicy)
if tenant.Spec.LifecyclePolicy != resourcev1alpha1.KeepAfterDeletion {
if err := pulsarAdmin.DeleteTenant(tenant.Spec.Name); err != nil && !admin.IsNotFound(err) {
log.Error(err, "Failed to delete tenant")
return err
if admin.IsNoSuchHostError(err) {
log.Info("Pulsar cluster has been deleted")
} else {
log.Error(err, "Failed to delete tenant")
return err
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/connection/reconcile_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ func (r *PulsarTopicReconciler) ReconcileTopic(ctx context.Context, pulsarAdmin
if topic.Status.GeoReplicationEnabled {
log.Info("GeoReplication is enabled. Reset topic cluster first", "LifecyclePolicy", topic.Spec.LifecyclePolicy, "ClusterName", r.conn.connection.Spec.ClusterName)
if err := pulsarAdmin.SetTopicClusters(topic.Spec.Name, topic.Spec.Persistent, []string{r.conn.connection.Spec.ClusterName}); err != nil {
log.Error(err, "Failed to reset the cluster for topic")
return err
if admin.IsNoSuchHostError(err) {
log.Info("Pulsar cluster has been deleted")
} else {
log.Error(err, "Failed to reset the cluster for topic")
return err
}
}
}

Expand Down

0 comments on commit baaba51

Please sign in to comment.