func()

in controllers/controllers/resource/fetcher.go [121:166]


func (r *CapiResourceFetcher) fetchClusterForRef(ctx context.Context, refId types.NamespacedName, kind string) (*anywherev1.Cluster, error) {
	clusters := &anywherev1.ClusterList{}
	o := &client.ListOptions{Raw: &metav1.ListOptions{TypeMeta: metav1.TypeMeta{Kind: anywherev1.ClusterKind, APIVersion: anywherev1.GroupVersion.String()}}, Namespace: refId.Namespace}
	err := r.client.List(ctx, clusters, o)
	if err != nil {
		return nil, err
	}
	for _, c := range clusters.Items {
		if kind == anywherev1.VSphereDatacenterKind || kind == anywherev1.DockerDatacenterKind {
			if c.Spec.DatacenterRef.Name == refId.Name {
				if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
					return &c, nil
				}
			}
		}
		if kind == anywherev1.VSphereMachineConfigKind {
			for _, machineRef := range c.Spec.WorkerNodeGroupConfigurations {
				if machineRef.MachineGroupRef != nil && machineRef.MachineGroupRef.Name == refId.Name {
					if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
						return &c, nil
					}
				}
			}
			if c.Spec.ControlPlaneConfiguration.MachineGroupRef != nil && c.Spec.ControlPlaneConfiguration.MachineGroupRef.Name == refId.Name {
				if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
					return &c, nil
				}
			}
			if c.Spec.ExternalEtcdConfiguration != nil && c.Spec.ExternalEtcdConfiguration.MachineGroupRef != nil && c.Spec.ExternalEtcdConfiguration.MachineGroupRef.Name == refId.Name {
				if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
					return &c, nil
				}
			}
		}
		if kind == anywherev1.AWSIamConfigKind {
			for _, indentityProviderRef := range c.Spec.IdentityProviderRefs {
				if indentityProviderRef.Name == refId.Name {
					if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
						return &c, nil
					}
				}
			}
		}
	}
	return nil, fmt.Errorf("eksa cluster not found for %s: %v", kind, refId)
}