func()

in cmd/addpool.go [201:304]


func (apc *addPoolCmd) run(cmd *cobra.Command, args []string) error {
	if err := apc.validate(cmd); err != nil {
		return errors.Wrap(err, "failed to validate addpool command")
	}
	if err := apc.load(); err != nil {
		return errors.Wrap(err, "failed to load existing container service")
	}
	apCount := len(apc.containerService.Properties.AgentPoolProfiles)

	ctx, cancel := context.WithTimeout(context.Background(), armhelpers.DefaultARMOperationTimeout)
	defer cancel()
	orchestratorInfo := apc.containerService.Properties.OrchestratorProfile
	winPoolIndex := -1

	translator := engine.Context{
		Translator: &i18n.Translator{
			Locale: apc.locale,
		},
	}
	templateGenerator, err := engine.InitializeTemplateGenerator(translator)
	if err != nil {
		return errors.Wrap(err, "failed to initialize template generator")
	}

	apc.containerService.Properties.AgentPoolProfiles = []*api.AgentPoolProfile{apc.nodePool}

	_, err = apc.containerService.SetPropertiesDefaults(api.PropertiesDefaultsParams{
		IsScale:    true,
		IsUpgrade:  false,
		PkiKeySize: helpers.DefaultPkiKeySize,
	})
	if err != nil {
		return errors.Wrapf(err, "error in SetPropertiesDefaults template %s", apc.apiModelPath)
	}
	template, parameters, err := templateGenerator.GenerateTemplateV2(apc.containerService, engine.DefaultGeneratorCode, BuildTag)
	if err != nil {
		return errors.Wrapf(err, "error generating template %s", apc.apiModelPath)
	}

	if template, err = transform.PrettyPrintArmTemplate(template); err != nil {
		return errors.Wrap(err, "error pretty printing template")
	}

	templateJSON := make(map[string]interface{})
	parametersJSON := make(map[string]interface{})

	err = json.Unmarshal([]byte(template), &templateJSON)
	if err != nil {
		return errors.Wrap(err, "error unmarshaling template")
	}

	err = json.Unmarshal([]byte(parameters), &parametersJSON)
	if err != nil {
		return errors.Wrap(err, "error unmarshaling parameters")
	}

	if apc.nodePool.OSType == api.Windows {
		winPoolIndex = apCount
	}

	// The agent pool is set to index 0 for the scale operation, we need to overwrite the template variables that rely on pool index.
	if winPoolIndex != -1 {
		templateJSON["variables"].(map[string]interface{})[apc.nodePool.Name+"Index"] = winPoolIndex
		templateJSON["variables"].(map[string]interface{})[apc.nodePool.Name+"VMNamePrefix"] = apc.containerService.Properties.GetAgentVMPrefix(apc.nodePool, winPoolIndex)
	}
	transformer := transform.Transformer{Translator: translator.Translator}

	if orchestratorInfo.KubernetesConfig.LoadBalancerSku == api.StandardLoadBalancerSku {
		err = transformer.NormalizeForK8sSLBScalingOrUpgrade(apc.logger, templateJSON)
		if err != nil {
			return errors.Wrapf(err, "error transforming the template for scaling with SLB %s", apc.apiModelPath)
		}
	}

	err = transformer.NormalizeForK8sAddVMASPool(apc.logger, templateJSON)
	if err != nil {
		return errors.Wrap(err, "error transforming the template to add a VMAS node pool")
	}

	random := rand.New(rand.NewSource(time.Now().UnixNano()))
	deploymentSuffix := random.Int31()

	_, err = apc.client.DeployTemplate(
		ctx,
		apc.resourceGroupName,
		fmt.Sprintf("%s-%d", apc.resourceGroupName, deploymentSuffix),
		templateJSON,
		parametersJSON)
	if err != nil {
		return err
	}
	if apc.nodes != nil {
		nodes, err := operations.GetNodes(apc.client, apc.logger, apc.apiserverURL, apc.kubeconfig, time.Duration(5)*time.Minute, apc.nodePool.Name, apc.nodePool.Count)
		if err == nil && nodes != nil {
			apc.nodes = nodes
			apc.logger.Infof("Nodes in pool '%s' after scaling:\n", apc.nodePool.Name)
			operations.PrintNodes(apc.nodes)
		} else {
			apc.logger.Warningf("Unable to get nodes in pool %s after scaling:\n", apc.nodePool.Name)
		}
	}

	return apc.saveAPIModel()
}