func()

in controllers/hostingautoscalingpolicy/hostingautoscalingpolicy_controller.go [245:313]


func (r *Reconciler) initializeContext(ctx *reconcileRequestContext) error {
	var err error

	// Ensure we are using the job name specified in the spec
	if ctx.HostingAutoscalingPolicy.Spec.PolicyName != nil && len(*ctx.HostingAutoscalingPolicy.Spec.PolicyName) > 0 {
		ctx.PolicyName = *ctx.HostingAutoscalingPolicy.Spec.PolicyName
	} else {
		ctx.PolicyName = DefaultAutoscalingPolicyName
		ctx.HostingAutoscalingPolicy.Spec.PolicyName = &ctx.PolicyName

		if err := r.Update(ctx, ctx.HostingAutoscalingPolicy); err != nil {
			ctx.Log.Info("Error while updating HostingAutoscalingPolicy policyName in spec")
			return err
		}
	}

	// Save the ResourceIDs into ctx as usable strings
	if err = r.getResourceIDListfromInputSpec(ctx); err != nil {
		ctx.Log.Error(err, "Error reading the ResourceIDs from Spec")
		return err
	}

	if ctx.HostingAutoscalingPolicy.Status.ResourceIDList == nil {
		ctx.HostingAutoscalingPolicy.Status.ResourceIDList = []string{}
	}

	// Initialize other values to defaults if not in Spec
	if ctx.HostingAutoscalingPolicy.Spec.ScalableDimension == nil {
		namespace := sdkutil.HostingAutoscalingPolicyServiceNamespace
		ctx.HostingAutoscalingPolicy.Spec.ServiceNamespace = &namespace
	}

	if ctx.HostingAutoscalingPolicy.Spec.ScalableDimension == nil {
		dimension := ScalableDimension
		ctx.HostingAutoscalingPolicy.Spec.ScalableDimension = &dimension
	}

	if ctx.HostingAutoscalingPolicy.Spec.PolicyType == nil {
		policyType := PolicyType
		ctx.HostingAutoscalingPolicy.Spec.PolicyType = &policyType
	}

	if ctx.HostingAutoscalingPolicy.Spec.SuspendedState == nil {
		ctx.HostingAutoscalingPolicy.Spec.SuspendedState = &commonv1.HAPSuspendedState{}
	}

	if ctx.HostingAutoscalingPolicy.Spec.SuspendedState.DynamicScalingInSuspended == nil {
		ctx.HostingAutoscalingPolicy.Spec.SuspendedState.DynamicScalingInSuspended = controllertest.ToBoolPtr(DefaultSuspendedStateAttributeValue)
	}

	if ctx.HostingAutoscalingPolicy.Spec.SuspendedState.DynamicScalingOutSuspended == nil {
		ctx.HostingAutoscalingPolicy.Spec.SuspendedState.DynamicScalingOutSuspended = controllertest.ToBoolPtr(DefaultSuspendedStateAttributeValue)
	}

	if ctx.HostingAutoscalingPolicy.Spec.SuspendedState.ScheduledScalingSuspended == nil {
		ctx.HostingAutoscalingPolicy.Spec.SuspendedState.ScheduledScalingSuspended = controllertest.ToBoolPtr(DefaultSuspendedStateAttributeValue)
	}

	awsConfig, err := r.awsConfigLoader.LoadAWSConfigWithOverrides(ctx.HostingAutoscalingPolicy.Spec.Region, ctx.HostingAutoscalingPolicy.Spec.SageMakerEndpoint)
	if err != nil {
		ctx.Log.Error(err, "Error loading AWS config")
		return err
	}

	ctx.ApplicationAutoscalingClient = r.createApplicationAutoscalingClient(awsConfig)
	ctx.Log.Info("Loaded AWS config")

	return nil
}