func GetKubeConfigPath()

in action/k8s/utils/file.go [16:41]


func GetKubeConfigPath() (string, error) {
	// Read the configuration file
	file, err := os.ReadFile(ConfigFileName)
	if err != nil {
		return "", fmt.Errorf("failed to read config.yml:" + err.Error())
	}
	var config model.Config
	err = yaml.Unmarshal(file, &config)
	if err != nil {
		return "", fmt.Errorf("unmarshal failed" + err.Error())
	}
	// Retrieve the context name
	contextName := config.Context.Kubernetes
	var contextPath string
	// Find the matching KubeConfig path based on the context
	for _, cluster := range config.Kubernetes.Cluster {
		if cluster.Name == contextName {
			contextPath = cluster.KubeConfigPath
		}
	}
	// If no matching context is found, return an error
	if contextPath == "" {
		return "", fmt.Errorf("failed to find context in config.yml")
	}
	return contextPath, err
}