func connectionDetails(cluster *container.Cluster) managed.ConnectionDetails {
	config, err := gke.GenerateClientConfig(cluster)
	if err != nil {
		return nil
	}
	rawConfig, err := clientcmd.Write(config)
	if err != nil {
		return nil
	}
	cd := managed.ConnectionDetails{
		xpv1.ResourceCredentialsSecretEndpointKey:   []byte(config.Clusters[cluster.Name].Server),
		xpv1.ResourceCredentialsSecretUserKey:       []byte(config.AuthInfos[cluster.Name].Username),
		xpv1.ResourceCredentialsSecretPasswordKey:   []byte(config.AuthInfos[cluster.Name].Password),
		xpv1.ResourceCredentialsSecretCAKey:         config.Clusters[cluster.Name].CertificateAuthorityData,
		xpv1.ResourceCredentialsSecretClientCertKey: config.AuthInfos[cluster.Name].ClientCertificateData,
		xpv1.ResourceCredentialsSecretClientKeyKey:  config.AuthInfos[cluster.Name].ClientKeyData,
		xpv1.ResourceCredentialsSecretKubeconfigKey: rawConfig,
	}
	return cd
}
connectionSecret return secret object for cluster instance
package container
import (
	"context"
	"time"
	"github.com/google/go-cmp/cmp"
	container "google.golang.org/api/container/v1"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/workqueue"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
	"github.com/crossplane/crossplane-runtime/pkg/errors"
	"github.com/crossplane/crossplane-runtime/pkg/event"
	"github.com/crossplane/crossplane-runtime/pkg/logging"
	"github.com/crossplane/crossplane-runtime/pkg/meta"
	"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
	"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
	"github.com/crossplane/crossplane-runtime/pkg/resource"
	"github.com/crossplane/provider-gcp/apis/container/v1beta2"
	gcp "github.com/crossplane/provider-gcp/pkg/clients"
	gke "github.com/crossplane/provider-gcp/pkg/clients/cluster"
)
const (
	errNewClient            = "cannot create new GKE container client"
	errManagedUpdateFailed  = "cannot update Cluster custom resource"
	errNotCluster           = "managed resource is not a Cluster"
	errGetCluster           = "cannot get GKE cluster"
	errCreateCluster        = "cannot create GKE cluster"
	errUpdateCluster        = "cannot update GKE cluster"
	errDeleteCluster        = "cannot delete GKE cluster"
	errCheckClusterUpToDate = "cannot determine if GKE cluster is up to date"
)
func SetupCluster(mgr ctrl.Manager, l logging.Logger, rl workqueue.RateLimiter, poll time.Duration) error {
	name := managed.ControllerName(v1beta2.ClusterGroupKind)
	return ctrl.NewControllerManagedBy(mgr).
		Named(name).
		WithOptions(controller.Options{
			RateLimiter: ratelimiter.NewDefaultManagedRateLimiter(rl),
		}).
		For(&v1beta2.Cluster{}).
		Complete(managed.NewReconciler(mgr,
			resource.ManagedKind(v1beta2.ClusterGroupVersionKind),
			managed.WithExternalConnecter(&clusterConnector{kube: mgr.GetClient()}),
			managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())),
			managed.WithPollInterval(poll),
			managed.WithLogger(l.WithValues("controller", name)),
			managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name)))))
}
type clusterConnector struct {
	kube client.Client
}
func (c *clusterConnector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
	projectID, opts, err := gcp.GetAuthInfo(ctx, c.kube, mg)
	if err != nil {
		return nil, err
	}
	s, err := container.NewService(ctx, opts)
	if err != nil {
		return nil, errors.Wrap(err, errNewClient)
	}
	return &clusterExternal{cluster: s, projectID: projectID, kube: c.kube}, errors.Wrap(err, errNewClient)
}
type clusterExternal struct {
	kube      client.Client
	cluster   *container.Service
	projectID string
}
func (e *clusterExternal) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {  
	cr, ok := mg.(*v1beta2.Cluster)
	if !ok {
		return managed.ExternalObservation{}, errors.New(errNotCluster)
	}
	existing, err := e.cluster.Projects.Locations.Clusters.Get(gke.GetFullyQualifiedName(e.projectID, cr.Spec.ForProvider, meta.GetExternalName(cr))).Context(ctx).Do()
	if err != nil {
		return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errGetCluster)
	}
	cr.Status.AtProvider = gke.GenerateObservation(*existing)
	currentSpec := cr.Spec.ForProvider.DeepCopy()
	gke.LateInitializeSpec(&cr.Spec.ForProvider, *existing)
	if !cmp.Equal(currentSpec, &cr.Spec.ForProvider) {
		if err := e.kube.Update(ctx, cr); err != nil {
			return managed.ExternalObservation{}, errors.Wrap(err, errManagedUpdateFailed)
		}
	}
	switch cr.Status.AtProvider.Status {
	case v1beta2.ClusterStateRunning, v1beta2.ClusterStateReconciling:
		cr.Status.SetConditions(xpv1.Available())
	case v1beta2.ClusterStateProvisioning:
		cr.Status.SetConditions(xpv1.Creating())
	case v1beta2.ClusterStateUnspecified, v1beta2.ClusterStateDegraded, v1beta2.ClusterStateError:
		cr.Status.SetConditions(xpv1.Unavailable())
	}
	u, _, err := gke.IsUpToDate(meta.GetExternalName(cr), &cr.Spec.ForProvider, existing)
	if err != nil {
		return managed.ExternalObservation{}, errors.Wrap(err, errCheckClusterUpToDate)
	}
	return managed.ExternalObservation{
		ResourceExists:    true,
		ResourceUpToDate:  u,
		ConnectionDetails: connectionDetails(existing),
	}, nil
}
func (e *clusterExternal) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
	cr, ok := mg.(*v1beta2.Cluster)
	if !ok {
		return managed.ExternalCreation{}, errors.New(errNotCluster)
	}
	cr.SetConditions(xpv1.Creating())
	if cr.Status.AtProvider.Status == v1beta2.ClusterStateProvisioning {
		return managed.ExternalCreation{}, nil
	}
	cluster := &container.Cluster{}
	gke.GenerateCluster(meta.GetExternalName(cr), cr.Spec.ForProvider, cluster)
	if cluster.Autopilot == nil || !cluster.Autopilot.Enabled {
		gke.AddNodePoolForCreate(cluster)
	}
	create := &container.CreateClusterRequest{
		Cluster: cluster,
	}
	_, err := e.cluster.Projects.Locations.Clusters.Create(gke.GetFullyQualifiedParent(e.projectID, cr.Spec.ForProvider), create).Context(ctx).Do()
	return managed.ExternalCreation{}, errors.Wrap(err, errCreateCluster)
}
func (e *clusterExternal) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
	cr, ok := mg.(*v1beta2.Cluster)
	if !ok {
		return managed.ExternalUpdate{}, errors.New(errNotCluster)
	}
	if cr.Status.AtProvider.Status == v1beta2.ClusterStateReconciling || cr.Status.AtProvider.Status == v1beta2.ClusterStateProvisioning {
		return managed.ExternalUpdate{}, nil
	}
	existing, err := e.cluster.Projects.Locations.Clusters.Get(gke.GetFullyQualifiedName(e.projectID, cr.Spec.ForProvider, meta.GetExternalName(cr))).Context(ctx).Do()
	if err != nil {
		return managed.ExternalUpdate{}, errors.Wrap(err, errGetCluster)
	}
	u, fn, err := gke.IsUpToDate(meta.GetExternalName(cr), &cr.Spec.ForProvider, existing)
	if err != nil {
		return managed.ExternalUpdate{}, errors.Wrap(err, errCheckClusterUpToDate)
	}
	if u {
		return managed.ExternalUpdate{}, nil
	}
	_, err = fn(ctx, e.cluster, gke.GetFullyQualifiedName(e.projectID, cr.Spec.ForProvider, meta.GetExternalName(cr)))
	return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateCluster)
}
func (e *clusterExternal) Delete(ctx context.Context, mg resource.Managed) error {
	cr, ok := mg.(*v1beta2.Cluster)
	if !ok {
		return errors.New(errNotCluster)
	}
	cr.SetConditions(xpv1.Deleting())
	if cr.Status.AtProvider.Status == v1beta2.ClusterStateStopping {
		return nil
	}
	_, err := e.cluster.Projects.Locations.Clusters.Delete(gke.GetFullyQualifiedName(e.projectID, cr.Spec.ForProvider, meta.GetExternalName(cr))).Context(ctx).Do()
	return errors.Wrap(resource.Ignore(gcp.IsErrorNotFound, err), errDeleteCluster)
}