func()

in cmd/deploy.go [173:280]


func (dc *deployCmd) loadAPIModel() error {
	var caCertificateBytes []byte
	var caKeyBytes []byte
	var err error

	apiloader := &api.Apiloader{
		Translator: &i18n.Translator{
			Locale: dc.locale,
		},
	}

	// do not validate when initially loading the apimodel, validation is done later after autofilling values
	dc.containerService, dc.apiVersion, err = apiloader.LoadContainerServiceFromFile(dc.apimodelPath, false, false, nil)
	if err != nil {
		return errors.Wrap(err, "error parsing the api model")
	}

	if dc.containerService.Properties.MasterProfile == nil {
		return errors.New("MasterProfile can't be nil")
	}

	// consume dc.caCertificatePath and dc.caPrivateKeyPath
	if (dc.caCertificatePath != "" && dc.caPrivateKeyPath == "") || (dc.caCertificatePath == "" && dc.caPrivateKeyPath != "") {
		return errors.New("--ca-certificate-path and --ca-private-key-path must be specified together")
	}

	if dc.caCertificatePath != "" {
		if caCertificateBytes, err = os.ReadFile(dc.caCertificatePath); err != nil {
			return errors.Wrap(err, "failed to read CA certificate file")
		}
		if caKeyBytes, err = os.ReadFile(dc.caPrivateKeyPath); err != nil {
			return errors.Wrap(err, "failed to read CA private key file")
		}

		prop := dc.containerService.Properties
		if prop.CertificateProfile == nil {
			prop.CertificateProfile = &api.CertificateProfile{}
		}
		prop.CertificateProfile.CaCertificate = string(caCertificateBytes)
		prop.CertificateProfile.CaPrivateKey = string(caKeyBytes)
	}

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

	if dc.containerService.Properties.IsCustomCloudProfile() {
		err = dc.containerService.SetCustomCloudProfileEnvironment()
		if err != nil {
			return errors.Wrap(err, "error parsing the api model")
		}
		if err = writeCustomCloudProfile(dc.containerService); err != nil {
			return errors.Wrap(err, "error writing custom cloud profile")
		}

		if dc.containerService.Properties.CustomCloudProfile.IdentitySystem == "" || dc.containerService.Properties.CustomCloudProfile.IdentitySystem != dc.authProvider.getAuthArgs().IdentitySystem {
			if dc.authProvider != nil {
				dc.containerService.Properties.CustomCloudProfile.IdentitySystem = dc.authProvider.getAuthArgs().IdentitySystem
			}
		}

		if dc.containerService.Properties.IsAzureStackCloud() {
			if dc.containerService.Properties.MasterProfile.Distro == api.AKSUbuntu1604 {
				log.Errorln("Distro 'aks-ubuntu-16.04' is not longer supported on Azure Stack Hub, use 'aks-ubuntu-20.04' instead")
				return errors.New("invalid master profile distro 'aks-ubuntu-16.04'")
			} else if dc.containerService.Properties.MasterProfile.Distro == api.AKSUbuntu1804 {
				log.Errorln("Distro 'aks-ubuntu-18.04' is not longer supported on Azure Stack Hub, use 'aks-ubuntu-20.04' instead")
				return errors.New("invalid master profile distro 'aks-ubuntu-18.04'")
			}

			for _, app := range dc.containerService.Properties.AgentPoolProfiles {
				if app.Distro == api.AKSUbuntu1604 {
					log.Errorln("Distro 'aks-ubuntu-16.04' is not longer supported on Azure Stack Hub, use 'aks-ubuntu-20.04' instead")
					return errors.New("invalid agent pool profile distro 'aks-ubuntu-16.04'")
				} else if app.Distro == api.AKSUbuntu1804 {
					log.Errorln("Distro 'aks-ubuntu-18.04' is not longer supported on Azure Stack Hub, use 'aks-ubuntu-20.04' instead")
					return errors.New("invalid agent pool profile distro 'aks-ubuntu-18.04'")
				}
			}
		}
	}

	if err = dc.getAuthArgs().validateAuthArgs(); err != nil {
		return err
	}

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

	if err = autofillApimodel(dc); err != nil {
		return err
	}

	dc.random = rand.New(rand.NewSource(time.Now().UnixNano()))

	return nil
}