func()

in cmd/addpool.go [114:199]


func (apc *addPoolCmd) load() error {
	logger := log.New()
	logger.Formatter = new(prefixed.TextFormatter)
	apc.logger = log.NewEntry(log.New())
	var err error

	ctx, cancel := context.WithTimeout(context.Background(), armhelpers.DefaultARMOperationTimeout)
	defer cancel()

	if _, err = os.Stat(apc.apiModelPath); os.IsNotExist(err) {
		return errors.Errorf("specified api model does not exist (%s)", apc.apiModelPath)
	}

	apiloader := &api.Apiloader{
		Translator: &i18n.Translator{
			Locale: apc.locale,
		},
	}
	apc.containerService, apc.apiVersion, err = apiloader.LoadContainerServiceFromFile(apc.apiModelPath, true, true, nil)
	if err != nil {
		return errors.Wrap(err, "error parsing the api model")
	}

	if _, err = os.Stat(apc.nodePoolPath); os.IsNotExist(err) {
		return errors.Errorf("specified agent pool spec does not exist (%s)", apc.nodePoolPath)
	}

	apc.nodePool, err = apiloader.LoadAgentpoolProfileFromFile(apc.nodePoolPath)
	if err != nil {
		return errors.Wrap(err, "error parsing the agent pool")
	}

	if apc.containerService.Properties.IsCustomCloudProfile() {
		if err = writeCustomCloudProfile(apc.containerService); err != nil {
			return errors.Wrap(err, "error writing custom cloud profile")
		}
		if err = apc.containerService.Properties.SetCustomCloudSpec(api.AzureCustomCloudSpecParams{IsUpgrade: false, IsScale: true}); err != nil {
			return errors.Wrap(err, "error parsing the api model")
		}
	}

	for _, p := range apc.containerService.Properties.AgentPoolProfiles {
		if strings.EqualFold(p.Name, apc.nodePool.Name) {
			return errors.Errorf("node pool %s already exists", p.Name)
		}
		if !strings.EqualFold(p.AvailabilityProfile, apc.nodePool.AvailabilityProfile) {
			return errors.New("mixed mode availability profiles are not allowed, all node pools should have the same availabilityProfile")
		}
	}

	if err = apc.authArgs.validateAuthArgs(); err != nil {
		return err
	}

	// Set env var if custom cloud profile is not nil
	var env *api.Environment
	if apc.containerService != nil &&
		apc.containerService.Properties != nil &&
		apc.containerService.Properties.CustomCloudProfile != nil {
		env = apc.containerService.Properties.CustomCloudProfile.Environment
	}
	if apc.client, err = apc.authArgs.getClient(env); err != nil {
		return errors.Wrap(err, "failed to get client")
	}

	_, err = apc.client.EnsureResourceGroup(ctx, apc.resourceGroupName, apc.location, nil)
	if err != nil {
		return err
	}

	if apc.containerService.Location == "" {
		apc.containerService.Location = apc.location
	} else if apc.containerService.Location != apc.location {
		return errors.New("--location does not match api model location")
	}

	//allows to identify VMs in the resource group that belong to this cluster.
	apc.nameSuffix = apc.containerService.Properties.GetClusterID()
	log.Debugf("Cluster ID used in all agent pools: %s", apc.nameSuffix)

	apc.kubeconfig, err = engine.GenerateKubeConfig(apc.containerService.Properties, apc.location)
	if err != nil {
		return errors.New("Unable to derive kubeconfig from api model")
	}
	return nil
}