Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy: refactor getResource to return NotFound for both cases #1046

Conversation

ykaliuta
Copy link
Contributor

@ykaliuta ykaliuta commented Jun 10, 2024

Avoid returning (nil, nil), convert error if CRD not present to NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Description

ref: https://issues.redhat.com/browse/RHOAIENG-8297

How Has This Been Tested?

Merge criteria:

  • The commits are squashed in a cohesive manner and have meaningful messages.
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has manually tested the changes and verified that the changes work

@openshift-ci openshift-ci bot requested review from adelton and biswassri June 10, 2024 07:43
@ykaliuta ykaliuta requested review from bartoszmajsak and zdtsw and removed request for adelton and biswassri June 10, 2024 07:44
// Create resource if it doesn't exist
if found == nil {
return createResource(ctx, cli, obj, owner)
if err == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be if found == nil ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've exchanged createResource/updateResource to avoid found == nil check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, meant to say
if found != nil { // do update }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work as well, but if there was no error (err == nil), found is not nil in getResource() so we can stay on checking err only

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be more clear actually:

func manageResource(ctx context.Context, cli client.Client, obj *unstructured.Unstructured, owner metav1.Object, applicationNamespace, componentName string, enabled bool) error {
	// Return if resource is of Kind: Namespace and Name: odhApplicationsNamespace
	if obj.GetKind() == "Namespace" && obj.GetName() == applicationNamespace {
		return nil
	}

	found, err := getResource(ctx, cli, obj)

	// err == nil means found
	if err == nil {
		if enabled {
			return updateResource(ctx, cli, obj, found, owner, componentName)
		}
		return handleDisabledComponent(ctx, cli, found, componentName)
	}

	if apierrs.IsNotFound(err) {
		// Create resource if it doesn't exist
		return createResource(ctx, cli, obj, owner)
	}

	return err
}

Copy link
Contributor

@bartoszmajsak bartoszmajsak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@openshift-ci openshift-ci bot added the lgtm label Jun 10, 2024
Avoid returning (nil, nil), convert error if CRD not present to
NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Signed-off-by: Yauheni Kaliuta <[email protected]>
@ykaliuta ykaliuta force-pushed the getresource-returns-notfound branch from 78d1b32 to 974218f Compare June 10, 2024 09:23
@openshift-ci openshift-ci bot removed the lgtm label Jun 10, 2024
Copy link
Member

@VaishnaviHire VaishnaviHire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @ykaliuta

/lgtm

Copy link

openshift-ci bot commented Jun 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bartoszmajsak, VaishnaviHire, zdtsw

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [VaishnaviHire,zdtsw]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit 9651b76 into opendatahub-io:incubation Jun 10, 2024
8 checks passed
VaishnaviHire pushed a commit to VaishnaviHire/opendatahub-operator that referenced this pull request Jun 10, 2024
…atahub-io#1046)

Avoid returning (nil, nil), convert error if CRD not present to
NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Signed-off-by: Yauheni Kaliuta <[email protected]>
(cherry picked from commit 9651b76)
VaishnaviHire pushed a commit to VaishnaviHire/opendatahub-operator that referenced this pull request Jun 10, 2024
- since we already have these resource for create, list, delete etc, no
harm to add get on them

Signed-off-by: Wen Zhou <[email protected]>
(cherry picked from commit bd9b83c)

fix: when delete resource if no CRD in the cluster (opendatahub-io#1044)

Signed-off-by: Wen Zhou <[email protected]>
(cherry picked from commit c040f77)

deploy: refactor getResource to return NotFound for both cases (opendatahub-io#1046)

Avoid returning (nil, nil), convert error if CRD not present to
NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Signed-off-by: Yauheni Kaliuta <[email protected]>
(cherry picked from commit 9651b76)

Handle Kserve exception

Fix csv
VaishnaviHire pushed a commit to VaishnaviHire/opendatahub-operator that referenced this pull request Jun 10, 2024
- since we already have these resource for create, list, delete etc, no
harm to add get on them

Signed-off-by: Wen Zhou <[email protected]>
(cherry picked from commit bd9b83c)

fix: when delete resource if no CRD in the cluster (opendatahub-io#1044)

Signed-off-by: Wen Zhou <[email protected]>
(cherry picked from commit c040f77)

deploy: refactor getResource to return NotFound for both cases (opendatahub-io#1046)

Avoid returning (nil, nil), convert error if CRD not present to
NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Signed-off-by: Yauheni Kaliuta <[email protected]>
(cherry picked from commit 9651b76)

Handle Kserve exception

Fix csv

(cherry picked from commit e5f65bd)
zdtsw pushed a commit to zdtsw-forking/rhods-operator that referenced this pull request Jun 18, 2024
…atahub-io#1046)

Avoid returning (nil, nil), convert error if CRD not present to
NotFound and handle them in the caller code.

Avoid checking for found == nil but handle only error code.

This is more idiomatic and less error prone.

Signed-off-by: Yauheni Kaliuta <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants