func (m *defaultResourceManager) findMeshDependency(ctx context.Context, vs *appmesh.VirtualService) (*appmesh.Mesh, error) {
	if vs.Spec.MeshRef == nil {
		return nil, errors.Errorf("meshRef shouldn't be nil, please check webhook setup")
	}
	ms, err := m.referencesResolver.ResolveMeshReference(ctx, *vs.Spec.MeshRef)
	if err != nil {
		return nil, errors.Wrapf(err, "failed to resolve meshRef")
	}
	return ms, nil
}
findMeshDependency find the Mesh dependency for this VirtualService.
package virtualservice
import (
	"context"
	appmesh "github.com/aws/aws-app-mesh-controller-for-k8s/apis/appmesh/v1beta2"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/aws/services"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/conversions"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/k8s"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/mesh"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/references"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/runtime"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/virtualnode"
	"github.com/aws/aws-app-mesh-controller-for-k8s/pkg/virtualrouter"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	appmeshsdk "github.com/aws/aws-sdk-go/service/appmesh"
	"github.com/go-logr/logr"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/pkg/errors"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/conversion"
	"k8s.io/apimachinery/pkg/types"
	"sigs.k8s.io/controller-runtime/pkg/client"
)
type ResourceManager interface {
	Reconcile(ctx context.Context, vs *appmesh.VirtualService) error
	Cleanup(ctx context.Context, vs *appmesh.VirtualService) error
}
func NewDefaultResourceManager(
	k8sClient client.Client,
	appMeshSDK services.AppMesh,
	referencesResolver references.Resolver,
	accountID string,
	log logr.Logger) ResourceManager {
	return &defaultResourceManager{
		k8sClient:          k8sClient,
		appMeshSDK:         appMeshSDK,
		referencesResolver: referencesResolver,
		accountID:          accountID,
		log:                log,
	}
}
type defaultResourceManager struct {
	k8sClient          client.Client
	appMeshSDK         services.AppMesh
	referencesResolver references.Resolver
	accountID          string
	log                logr.Logger
}
func (m *defaultResourceManager) Reconcile(ctx context.Context, vs *appmesh.VirtualService) error {
	ms, err := m.findMeshDependency(ctx, vs)
	if err != nil {
		return err
	}
	if err := m.validateMeshDependencies(ctx, ms); err != nil {
		return err
	}
	vnByKey, err := m.findVirtualNodeDependencies(ctx, vs)
	if err != nil {
		return err
	}
	if err := m.validateVirtualNodeDependencies(ctx, ms, vnByKey); err != nil {
		return err
	}
	vrByKey, err := m.findVirtualRouterDependencies(ctx, vs)
	if err != nil {
		return err
	}
	if err := m.validateVirtualRouterDependencies(ctx, ms, vrByKey); err != nil {
		return err
	}
	sdkVS, err := m.findSDKVirtualService(ctx, ms, vs)
	if err != nil {
		return err
	}
	if sdkVS == nil {
		sdkVS, err = m.createSDKVirtualService(ctx, ms, vs, vnByKey, vrByKey)
		if err != nil {
			return err
		}
	} else {
		sdkVS, err = m.updateSDKVirtualService(ctx, sdkVS, vs, vnByKey, vrByKey)
		if err != nil {
			return err
		}
	}
	return m.updateCRDVirtualService(ctx, vs, sdkVS)
}
func (m *defaultResourceManager) Cleanup(ctx context.Context, vs *appmesh.VirtualService) error {
	ms, err := m.findMeshDependency(ctx, vs)
	if err != nil {
		return err
	}
	sdkVS, err := m.findSDKVirtualService(ctx, ms, vs)
	if err != nil {
		if vs.Status.VirtualServiceARN == nil {
			return nil
		}
		return err
	}
	if sdkVS == nil {
		return nil
	}
	return m.deleteSDKVirtualService(ctx, sdkVS, vs)
}