func importCmdCommand()

in cmd/config.go [122:181]


func importCmdCommand() *cobra.Command {
	var subscriptionID string
	var resourceGroup string
	var clusterName string

	virtualMachineScaleSetVMs := func() (map[string]*utils.VirtualMachineScaleSetVM, error) {
		utils.DefaultSpinner.Start()
		utils.DefaultSpinner.Suffix = " Importing..."

		if subscriptionID != "" && resourceGroup != "" && clusterName != "" {
			vms, err := utils.VirtualMachineScaleSetVMsViaAzureAPI(subscriptionID, resourceGroup, clusterName)
			utils.DefaultSpinner.Stop()
			if err != nil {
				return nil, fmt.Errorf("getting VMSS VMs via Azure API: %w", err)
			}
			return vms, nil
		}

		vms, err := utils.VirtualMachineScaleSetVMsViaKubeconfig()
		utils.DefaultSpinner.Stop()
		if err != nil {
			logrus.Warn("Could not get VMSS VMs via Kubernetes API")
			logrus.Warnf("Please provide '--%s', '--%s' and '--%s' flags to get VMSS VMs via Azure API",
				utils.SubscriptionIDKey, utils.ResourceGroupKey, utils.ClusterNameKey)
			return nil, fmt.Errorf("getting VMSS VMs via Kuberntes API: %w", err)
		}

		return vms, nil
	}

	cmd := &cobra.Command{
		Use:   "import",
		Short: "Import Kubernetes nodes in the configuration",
		Long: fmt.Sprintf("Import Kubernetes nodes in the configuration"+"\n\n"+
			"It uses kubeconfig by default, but it can also use Azure API to get VMSS VMs."+"\n"+
			"In case of Azure API, you need to provide '--%s', '--%s' and '--%s' flags.",
			utils.SubscriptionIDKey, utils.ResourceGroupKey, utils.ClusterNameKey),
		SilenceUsage: true,
		Args:         cobra.NoArgs,
		RunE: func(cmd *cobra.Command, args []string) error {
			vms, err := virtualMachineScaleSetVMs()
			if err != nil {
				return err
			}
			cfg := config.New()
			for nn, vm := range vms {
				if err = cfg.SetNodeConfigWithVMSSInfoFlag(nn, vm.SubscriptionID, vm.NodeResourceGroup, vm.VMScaleSet, vm.InstanceID); err != nil {
					return fmt.Errorf("setting node config for %s: %w", nn, err)
				}
			}
			return nil
		},
	}

	cmd.Flags().StringVarP(&subscriptionID, utils.SubscriptionIDKey, "", "", "Subscription ID of the cluster (only needed with Azure API)")
	cmd.Flags().StringVarP(&resourceGroup, utils.ResourceGroupKey, "", "", "Resource group of the cluster (only needed with Azure API)")
	cmd.Flags().StringVarP(&clusterName, utils.ClusterNameKey, "", "", "Name of the cluster (only needed with Azure API)")

	return cmd
}