mirror of
				https://github.com/siderolabs/talos.git
				synced 2025-11-03 18:01:29 +01:00 
			
		
		
		
	That's the recommended syntax: https://golangci-lint.run/usage/false-positives/ Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
		
			
				
	
	
		
			83 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// This Source Code Form is subject to the terms of the Mozilla Public
 | 
						|
// License, v. 2.0. If a copy of the MPL was not distributed with this
 | 
						|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
						|
 | 
						|
// +build integration_k8s
 | 
						|
 | 
						|
package base
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"time"
 | 
						|
 | 
						|
	v1 "k8s.io/api/core/v1"
 | 
						|
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						|
	"k8s.io/client-go/kubernetes"
 | 
						|
	"k8s.io/client-go/tools/clientcmd"
 | 
						|
	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
 | 
						|
 | 
						|
	"github.com/talos-systems/talos/pkg/cluster"
 | 
						|
	"github.com/talos-systems/talos/pkg/machinery/client"
 | 
						|
	"github.com/talos-systems/talos/pkg/machinery/constants"
 | 
						|
)
 | 
						|
 | 
						|
//nolint:gocyclo
 | 
						|
func discoverNodesK8s(client *client.Client, suite *TalosSuite) (cluster.Info, error) {
 | 
						|
	ctx, ctxCancel := context.WithTimeout(context.Background(), time.Minute)
 | 
						|
	defer ctxCancel()
 | 
						|
 | 
						|
	kubeconfig, err := client.Kubeconfig(ctx)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	config, err := clientcmd.BuildConfigFromKubeconfigGetter("", func() (*clientcmdapi.Config, error) {
 | 
						|
		return clientcmd.Load(kubeconfig)
 | 
						|
	})
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// patch timeout
 | 
						|
	config.Timeout = time.Minute
 | 
						|
	if suite.K8sEndpoint != "" {
 | 
						|
		config.Host = suite.K8sEndpoint
 | 
						|
	}
 | 
						|
 | 
						|
	clientset, err := kubernetes.NewForConfig(config)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	result := &infoWrapper{}
 | 
						|
 | 
						|
	for _, node := range nodes.Items {
 | 
						|
		var address string
 | 
						|
 | 
						|
		for _, nodeAddress := range node.Status.Addresses {
 | 
						|
			if nodeAddress.Type == v1.NodeInternalIP {
 | 
						|
				address = nodeAddress.Address
 | 
						|
 | 
						|
				break
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if address == "" {
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		if _, ok := node.Labels[constants.LabelNodeRoleMaster]; ok {
 | 
						|
			result.masterNodes = append(result.masterNodes, address)
 | 
						|
		} else {
 | 
						|
			result.workerNodes = append(result.workerNodes, address)
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	return result, nil
 | 
						|
}
 |