func addNodeFlags()

in cmd/utils/flags.go [66:168]


func addNodeFlags(command *cobra.Command, useFlagsOnly bool) {
	command.PersistentFlags().StringVarP(
		&node,
		NodeKey, "",
		"",
		"Kubernetes node name.",
	)
	command.PersistentFlags().StringVarP(
		&subscriptionID,
		SubscriptionIDKey, "",
		"",
		"Subscription ID.",
	)
	command.PersistentFlags().StringVarP(
		&nodeResourceGroup,
		NodeResourceGroupKey, "",
		"",
		"Node resource group name.",
	)
	command.PersistentFlags().StringVarP(
		&vmss,
		VMSSKey, "",
		"",
		"Virtual machine scale set name.",
	)
	command.PersistentFlags().StringVarP(
		&vmssInstanceID,
		VMSSInstanceIDKey, "",
		"",
		"VM scale set instance ID.",
	)
	command.PersistentFlags().StringVarP(
		&resourceID,
		ResourceIDKey, "",
		"",
		`Resource ID containing all information of the VMSS instance using format:
		e.g. /subscriptions/mySubID/resourceGroups/myRG/providers/myProvider/virtualMachineScaleSets/myVMSS/virtualMachines/myInsID.
		Notice it is not case sensitive.`,
	)

	command.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
		// If node or resource ID is set, we don't need to read the config file
		// nor the environment variables because the CLI flags have precedence.
		if !useFlagsOnly && node == "" && resourceID == "" {
			config := config.New()

			if cc, ok := config.CurrentConfig(); ok {
				config = cc
			}
			// bind environment variables
			config.AutomaticEnv()
			config.SetEnvPrefix("kubectl_aks")
			config.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

			// bind CLI flags
			if err := config.BindPFlags(cmd.PersistentFlags()); err != nil {
				return fmt.Errorf("binding flags: %w", err)
			}

			// set the values with precedence:
			// (1) CLI flag
			// (2) environment variable
			// (3) config file
			node = config.GetString(NodeKey)
			subscriptionID = config.GetString(SubscriptionIDKey)
			nodeResourceGroup = config.GetString(NodeResourceGroupKey)
			vmss = config.GetString(VMSSKey)
			vmssInstanceID = config.GetString(VMSSInstanceIDKey)
			resourceID = config.GetString(ResourceIDKey)
		}

		// validate the parameters
		var nodeSet, vmssInfoSet, resourceIDSet bool
		if node != "" {
			nodeSet = true
		}
		if subscriptionID != "" && nodeResourceGroup != "" && vmss != "" && vmssInstanceID != "" {
			vmssInfoSet = true
		}
		if resourceID != "" {
			resourceIDSet = true
		}
		if !nodeSet && !vmssInfoSet && !resourceIDSet {
			if subscriptionID != "" || nodeResourceGroup != "" || vmss != "" || vmssInstanceID != "" {
				return errors.New("specify complete VMMS instance information ('subscription', 'node-resource-group', 'vmss' and 'instance-id')")
			}
			return errors.New("specify either 'node' or 'id' or VMMS instance information ('subscription', 'node-resource-group', 'vmss' and 'instance-id')")
		} else if nodeSet {
			if vmssInfoSet {
				return errors.New("specify either 'node' or VMMS instance information ('subscription', 'node-resource-group', 'vmss' and 'instance-id')")
			}
			if resourceIDSet {
				return errors.New("specify either 'node' or 'id'")
			}
		} else if vmssInfoSet {
			if resourceIDSet {
				return errors.New("specify either VMMS instance information ('subscription', 'node-resource-group', 'vmss' and 'instance-id') or 'id'")
			}
		}

		return nil
	}
}