// Package gke add by blacknc <blacknc@163.com> at 2025/01/15 18:02:56
package gke

import (
	"fmt"

	"github.com/tidbcloud/eks-provider/api/v1alpha1"
	"github.com/tidbcloud/eks-provider/controllers/dm/task"
	"github.com/tidbcloud/infra-provider/pkg/namer"
	infrautil "github.com/tidbcloud/infra-provider/pkg/util"
)

func getGCPNodePoolServiceAccountEmail(t *task.BaseTask) (string, error) {
	if t.ClusterRole == nil {
		return "", fmt.Errorf("cluster role is nil: may be not initialized or does not exist")
	}
	return t.ClusterRole.CloudIamIdentity, nil
}

func getNodePoolName(t *task.BaseTask, dmSpec *v1alpha1.DMSpec) string {
	originalString := fmt.Sprintf("%s/%s/%s/%s/%s",
		dmSpec.MemberType,
		dmSpec.SpecCode,
		t.GetClusterScopeName(),
		t.DMJob.Name,
		task.ComponentName)
	prefix := nodePoolNamePrefix + "-" + infrautil.FnvHash(originalString)
	return namer.UniqueIdentifier(t.DMJob.ObjectMeta, namer.WithPrefix(prefix), namer.WithLimit(40))
}
