func getClusterInfoFromKubeadmConfigMap()

in kubernetes/metadata/metadata.go [145:175]


func getClusterInfoFromKubeadmConfigMap(client k8sclient.Interface, kubeadm bool) (ClusterInfo, error) {
	clusterInfo := ClusterInfo{}
	if client == nil {
		return clusterInfo, fmt.Errorf("unable to get cluster identifiers from kubeadm-config")
	}
	if !kubeadm {
		return clusterInfo, nil
	}
	cm, err := client.CoreV1().ConfigMaps("kube-system").Get(context.TODO(), "kubeadm-config", metav1.GetOptions{})
	if err != nil {
		return clusterInfo, fmt.Errorf("unable to get cluster identifiers from kubeadm-config: %w", err)
	}
	p, ok := cm.Data["ClusterConfiguration"]
	if !ok {
		return clusterInfo, fmt.Errorf("unable to get cluster identifiers from ClusterConfiguration")
	}

	cc := &ClusterConfiguration{}
	err = yaml.Unmarshal([]byte(p), cc)
	if err != nil {
		return ClusterInfo{}, err
	}
	if cc.ClusterName != "" {
		clusterInfo.Name = cc.ClusterName
	}
	if cc.ControlPlaneEndpoint != "" {
		clusterInfo.URL = cc.ControlPlaneEndpoint
	}

	return clusterInfo, nil
}