func()

in pkg/controllers/cloudmap_controller.go [71:113]


func (r *CloudMapReconciler) reconcileNamespace(ctx context.Context, namespaceName string) error {
	r.Log.Debug("syncing namespace", "namespace", namespaceName)

	desiredServices, err := r.Cloudmap.ListServices(ctx, namespaceName)
	if err != nil {
		r.Log.Error(err, "failed to fetch the list Services")
		return err
	}

	serviceImports := v1alpha1.ServiceImportList{}
	if err = r.Client.List(ctx, &serviceImports, client.InNamespace(namespaceName)); err != nil {
		r.Log.Error(err, "failed to reconcile namespace", "namespace", namespaceName)
		return nil
	}

	existingImportsMap := make(map[string]v1alpha1.ServiceImport)
	for _, svc := range serviceImports.Items {
		existingImportsMap[svc.Namespace+"/"+svc.Name] = svc
	}

	for _, svc := range desiredServices {
		if len(svc.Endpoints) == 0 {
			// skip empty services
			continue
		}

		if err = r.reconcileService(ctx, svc); err != nil {
			r.Log.Error(err, "error when syncing service", "namespace", svc.Namespace, "name", svc.Name)
		}
		delete(existingImportsMap, svc.Namespace+"/"+svc.Name)
	}

	// delete remaining imports that have not been matched
	for _, i := range existingImportsMap {
		if err = r.Client.Delete(ctx, &i); err != nil {
			r.Log.Error(err, "error deleting ServiceImport", "namespace", i.Namespace, "name", i.Name)
			continue
		}
		r.Log.Info("delete ServiceImport", "namespace", i.Namespace, "name", i.Name)
	}

	return nil
}