func()

in pkg/controller/cdstagejenkinsdeployment/cd_stage_jenkins_deployment_controller.go [66:120]


func (r *ReconcileCDStageJenkinsDeployment) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
	log := r.log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
	log.Info("reconciling has been started")

	cdStageJenkinsDeployment := &jenkinsApi.CDStageJenkinsDeployment{}
	if err := r.client.Get(ctx, request.NamespacedName, cdStageJenkinsDeployment); err != nil {
		if k8serrors.IsNotFound(err) {
			log.Info("instance not found")

			return reconcile.Result{}, nil
		}

		return reconcile.Result{}, fmt.Errorf("failed to get CDStageJenkinsDeployment: %w", err)
	}

	defer func() {
		if err := r.updateStatus(ctx, cdStageJenkinsDeployment); err != nil {
			log.Error(err, "error during status updating")
		}
	}()

	if err := r.setOwnerReference(cdStageJenkinsDeployment); err != nil {
		wrappedError := fmt.Errorf("failed to set owner ref for %v CDStageJenkinsDeployment: %w",
			cdStageJenkinsDeployment.Name, err)

		cdStageJenkinsDeployment.SetFailedStatus(wrappedError)

		return reconcile.Result{}, wrappedError
	}

	env, err := helper.GetPlatformTypeEnv()
	if err != nil {
		return reconcile.Result{}, fmt.Errorf("failed to GetPlatformTypeEnv: %w", err)
	}

	platform, err := ps.NewPlatformService(env, r.scheme, r.client)
	if err != nil {
		wrappedError := fmt.Errorf("failed to create platform service: %w", err)

		cdStageJenkinsDeployment.SetFailedStatus(wrappedError)

		return reconcile.Result{}, wrappedError
	}

	if err := chain.CreateDefChain(r.client, platform).ServeRequest(cdStageJenkinsDeployment); err != nil {
		cdStageJenkinsDeployment.SetFailedStatus(err)
		p := r.setReconcilationPeriod(cdStageJenkinsDeployment)

		return reconcile.Result{RequeueAfter: p}, nil
	}

	log.Info("Reconciling has been finished")

	return reconcile.Result{}, nil
}