func()

in cmd/upgrade.go [146:291]


func (uc *upgradeCmd) loadCluster() error {
	var err error

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

	// Load apimodel from the directory.
	if uc.apiModelPath == "" {
		uc.apiModelPath = filepath.Join(uc.deploymentDirectory, apiModelFilename)
	}

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

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

	// Load the container service.
	uc.containerService, uc.apiVersion, err = apiloader.LoadContainerServiceFromFile(uc.apiModelPath, true, true, nil)
	if err != nil {
		return errors.Wrap(err, "error parsing the api model")
	}

	// Ensure there aren't known-breaking API model configurations
	if uc.containerService.Properties.MasterProfile.AvailabilityProfile == api.VirtualMachineScaleSets {
		return errors.Errorf("clusters with a VMSS control plane are not upgradable using `aks-engine upgrade`")
	}
	if uc.containerService.Properties.OrchestratorProfile != nil &&
		uc.containerService.Properties.OrchestratorProfile.KubernetesConfig != nil &&
		to.Bool(uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.EnableEncryptionWithExternalKms) &&
		to.Bool(uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.UseManagedIdentity) &&
		uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.UserAssignedID == "" {
		return errors.Errorf("clusters with enableEncryptionWithExternalKms=true and system-assigned identity are not upgradable using `aks-engine upgrade`")
	}

	// Set 60 minutes cordonDrainTimeout for Azure Stack Cloud to give it enough time to move around resources during Node Drain,
	// especially disk detach/attach operations. We still honor the user's input.
	if uc.cordonDrainTimeout == nil && uc.containerService.Properties.IsAzureStackCloud() {
		cordonDrainTimeout := time.Duration(60) * time.Minute
		uc.cordonDrainTimeout = &cordonDrainTimeout
	}

	// Use the Windows VHD associated with the aks-engine version if upgradeWindowsVHD is set to "true"
	if uc.upgradeWindowsVHD && uc.containerService.Properties.WindowsProfile != nil {
		windowsProfile := uc.containerService.Properties.WindowsProfile
		if api.ImagePublisherAndOfferMatch(windowsProfile, api.AKSWindowsServer2019ContainerDOSImageConfig) && strings.Contains(windowsProfile.WindowsSku, ctrdWindowsImageIdentifier) {
			windowsProfile.ImageVersion = api.AKSWindowsServer2019ContainerDOSImageConfig.ImageVersion
			windowsProfile.WindowsSku = api.AKSWindowsServer2019ContainerDOSImageConfig.ImageSku
		} else if api.ImagePublisherAndOfferMatch(windowsProfile, api.AKSWindowsServer2019OSImageConfig) && strings.Contains(windowsProfile.WindowsSku, smalldiskWindowsImageIdentifier) {
			windowsProfile.ImageVersion = api.AKSWindowsServer2019OSImageConfig.ImageVersion
			windowsProfile.WindowsSku = api.AKSWindowsServer2019OSImageConfig.ImageSku
		} else if api.ImagePublisherAndOfferMatch(windowsProfile, api.WindowsServer2019OSImageConfig) {
			windowsProfile.ImageVersion = api.WindowsServer2019OSImageConfig.ImageVersion
			windowsProfile.WindowsSku = api.WindowsServer2019OSImageConfig.ImageSku
		}
	}

	// Update the masterProfile and agentPoolProfiles distro for AzureStackCloud to use aks-ubuntu-18.04 instead of aks-ubuntu-16.04
	if uc.containerService.Properties.IsAzureStackCloud() {
		if uc.containerService.Properties.MasterProfile.Distro == api.AKSUbuntu1604 {
			log.Infoln("Distro 'aks-ubuntu-16.04' is not longer supported on Azure Stack Hub, overwriting master profile distro to 'aks-ubuntu-22.04'")
			uc.containerService.Properties.MasterProfile.Distro = api.AKSUbuntu2204
		} else if uc.containerService.Properties.MasterProfile.Distro == api.AKSUbuntu1804 {
			log.Infoln("Distro 'aks-ubuntu-18.04' is not longer supported on Azure Stack Hub, overwriting master profile distro to 'aks-ubuntu-22.04'")
			uc.containerService.Properties.MasterProfile.Distro = api.AKSUbuntu2204
		} else if uc.containerService.Properties.MasterProfile.Distro == api.AKSUbuntu2004 {
			log.Infoln("Distro 'aks-ubuntu-20.04' is not longer supported on Azure Stack Hub, overwriting master profile distro to 'aks-ubuntu-22.04'")
			uc.containerService.Properties.MasterProfile.Distro = api.AKSUbuntu2204
		}

		for _, app := range uc.containerService.Properties.AgentPoolProfiles {
			if app.Distro == api.AKSUbuntu1604 {
				log.Infoln(fmt.Sprintf("Distro 'aks-ubuntu-16.04' is not longer supported on Azure Stack Hub, overwriting agent pool profile %s distro to 'aks-ubuntu-22.04'", app.Name))
				app.Distro = api.AKSUbuntu2204
			} else if app.Distro == api.AKSUbuntu1804 {
				log.Infoln(fmt.Sprintf("Distro 'aks-ubuntu-18.04' is not longer supported on Azure Stack Hub, overwriting agent pool profile %s distro to 'aks-ubuntu-22.04'", app.Name))
				app.Distro = api.AKSUbuntu2204
			} else if app.Distro == api.AKSUbuntu2004 {
				log.Infoln(fmt.Sprintf("Distro 'aks-ubuntu-20.04' is not longer supported on Azure Stack Hub, overwriting agent pool profile %s distro to 'aks-ubuntu-22.04'", app.Name))
				app.Distro = api.AKSUbuntu2204
			}
		}
	}

	// Enforce UseCloudControllerManager for Kubernetes 1.21+ on Azure Stack cloud
	if uc.containerService.Properties.IsAzureStackCloud() && common.IsKubernetesVersionGe(uc.upgradeVersion, "1.21.0") {
		log.Infoln("The in-tree cloud provider is not longer supported on Azure Stack Hub for v1.21+ clusters, overwriting UseCloudControllerManager to 'true'")
		uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager = to.BoolPtr(true)
	}

	// Only containerd runtime is allowed for Kubernetes 1.24+ on Azure Stack cloud
	if uc.containerService.Properties.IsAzureStackCloud() && strings.EqualFold(uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.ContainerRuntime, "docker") && common.IsKubernetesVersionGe(uc.upgradeVersion, "1.24.0") {
		log.Infoln("The docker runtime is no longer supported for v1.24+ clusters, overwriting ContainerRuntime to 'containerd'")
		uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.ContainerRuntime = "containerd"
	}

	// The cluster-init component is a cluster create-only feature, temporarily disable if enabled
	if i := api.GetComponentsIndexByName(uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.Components, common.ClusterInitComponentName); i > -1 {
		if uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.Components[i].IsEnabled() {
			uc.disableClusterInitComponentDuringUpgrade = true
			uc.containerService.Properties.OrchestratorProfile.KubernetesConfig.Components[i].Enabled = to.BoolPtr(false)
		}
	}

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

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

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

	_, err = uc.client.EnsureResourceGroup(ctx, uc.resourceGroupName, uc.location, nil)
	if err != nil {
		return errors.Wrap(err, "error ensuring resource group")
	}

	err = uc.initialize()
	if err != nil {
		return errors.Wrap(err, "error validating the api model")
	}
	return nil
}