func()

in controllers/endpointconfig/endpointconfig_controller.go [125:200]


func (r *EndpointConfigReconciler) reconcileEndpointConfig(ctx reconcileRequestContext) error {

	var err error

	// Update initial status.
	if ctx.EndpointConfig.Status.Status == "" {
		if err = r.updateStatus(ctx, InitializingJobStatus); err != nil {
			return err
		}
	}

	if err = r.initializeContext(&ctx); err != nil {
		return r.updateStatusAndReturnError(ctx, ErrorStatus, errors.Wrap(err, "Unable to initialize operator"))
	}

	// Add finalizer if the EndpointConfig is not marked for deletion.
	if !HasDeletionTimestamp(ctx.EndpointConfig.ObjectMeta) {
		if !ContainsString(ctx.EndpointConfig.ObjectMeta.GetFinalizers(), SageMakerResourceFinalizerName) {
			ctx.EndpointConfig.ObjectMeta.Finalizers = append(ctx.EndpointConfig.ObjectMeta.Finalizers, SageMakerResourceFinalizerName)
			if err := r.Update(ctx, ctx.EndpointConfig); err != nil {
				return errors.Wrap(err, "Failed to add finalizer")
			}
			ctx.Log.Info("Finalizer has been added")
		}
	}

	// Get SageMaker endpointConfig description.
	if ctx.EndpointConfigDescription, err = ctx.SageMakerClient.DescribeEndpointConfig(ctx, generateEndpointConfigName(ctx.EndpointConfig)); err != nil {
		return r.updateStatusAndReturnError(ctx, ErrorStatus, errors.Wrap(err, "Unable to get SageMaker EndpointConfig description"))
	}

	// Determine action needed for endpointConfig and endpointConfig description.
	var action ReconcileAction
	if action, err = r.determineActionForEndpointConfig(ctx.EndpointConfig, ctx.EndpointConfigDescription); err != nil {
		return r.updateStatusAndReturnError(ctx, ErrorStatus, errors.Wrap(err, "Unable to determine action for SageMaker EndpointConfig."))
	}
	ctx.Log.Info("Determined action for endpointConfig", "action", action)

	// If update or delete, delete the existing endpointConfig.
	if action == NeedsDelete || action == NeedsUpdate {
		if err = r.reconcileDeletion(ctx, ctx.SageMakerClient, generateEndpointConfigName(ctx.EndpointConfig)); err != nil {
			return r.updateStatusAndReturnError(ctx, ErrorStatus, errors.Wrap(err, "Unable to delete SageMaker endpointConfig"))
		}

		// Delete succeeded, set EndpointConfigDescription to nil.
		ctx.EndpointConfigDescription = nil
	}

	// If update or create, create the desired endpointConfig.
	if action == NeedsCreate || action == NeedsUpdate {
		if ctx.EndpointConfigDescription, err = r.reconcileCreation(ctx, ctx.SageMakerClient, ctx.EndpointConfig.Spec, generateEndpointConfigName(ctx.EndpointConfig)); err != nil {
			return r.updateStatusAndReturnError(ctx, ErrorStatus, errors.Wrap(err, "Unable to create SageMaker endpointConfig"))
		}
	}

	// Update the status accordingly.
	status := CreatedStatus
	if ctx.EndpointConfigDescription == nil {
		status = DeletedStatus
	}

	if err = r.updateStatus(ctx, status); err != nil {
		return err
	}

	// Remove finalizer on deletion.
	if HasDeletionTimestamp(ctx.EndpointConfig.ObjectMeta) {
		ctx.EndpointConfig.ObjectMeta.Finalizers = RemoveString(ctx.EndpointConfig.ObjectMeta.Finalizers, SageMakerResourceFinalizerName)
		if err := r.Update(ctx, ctx.EndpointConfig); err != nil {
			return errors.Wrap(err, "Failed to remove finalizer")
		}
		ctx.Log.Info("Finalizer has been removed")
	}

	return nil
}