Skip to content

Conversation

shreyashastantram
Copy link
Contributor

@shreyashastantram shreyashastantram commented Oct 14, 2025

Reason for Change:

Adding check to MTPNC to ensure that CNS doesn't respond back with the ipaddress if mtpnc is set for deletion.

Issue Fixed:

Requirements:

Notes:

if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
return v1alpha1.MultitenantPodNetworkConfig{}, types.UnexpectedError, errors.Wrap(err, errGetMTPNC.Error()).Error()
}
if mtpnc.DeletionTimestamp != nil {
Copy link
Member

Choose a reason for hiding this comment

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

!IsZero() seems to be the way to check this. DeletionTimestamp is Time.
it may be helpful to add a func something like IsTerminating similar to IsReady that we already have on mtpnc.

I also feel we shouldn't be adding these checks inside getMTPNC func. This func should return the MTPNC object and the caller should look at it and decide how to proceed however it wants. (different callers may call getMTPNC and use different fields to decide what to do next)

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 can add a function similar to isready, but we cannot use isZero since the data will be nil if it's not populated with timestamp. it's either nil or timestamp.

Copy link
Member

Choose a reason for hiding this comment

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

if the time.Time is absent, it will default to zero and IsZero() will be true

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 @ashvindeodhar is right, this doesn't belong in the getMTPNC. Return the MTPNC and let the caller decide what to do if it's Deleting.

.DeletionTimestamp.IsZero() is canonically the correct way to determine if an Object is deleting. ref https://book.kubebuilder.io/reference/using-finalizers.html

Copy link
Member

@isaac-dasan isaac-dasan Oct 16, 2025

Choose a reason for hiding this comment

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

nit: I would recommend a rename of isTerminating -> isDeleting everywhere (++ errMessage)
isDeleting ⇒ metadata.deletionTimestamp != nil
isTerminating ⇒ your CR’s .status.phase == "Terminating" (or whatever phase name your API defines) which doesn't apply to MTPNC

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 do not see deleting being used anywhere in Kubernetes, keeping it, terminating will keep it consistent with the pod state terminating.

Copy link
Member

Choose a reason for hiding this comment

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

The pod status changes to Terminating until it’s fully removed,

  • Sends SIGTERM to containers.
  • Waits for the termination grace period (default 30s).
  • Runs any presto hooks.
  • Finally sends SIGKILL if containers don’t exit in time.

Unlike pod (where there is an actual termination phase), MTPNC doesn't have a termination phase. So, there is no need for us to keep it consistent with pod status. We should keep the name to the face value of the operation, since the errMessage is something, customers can see.

errGetMTPNC = errors.New(NetworkNotReadyErrorMsg + " - failed to get MTPNC")
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
errDeleteTimestampSet = errors.New("MTPNC deletion timestamp is set")
Copy link
Collaborator

Choose a reason for hiding this comment

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

deletion timestamp being set is an implementation detail. the important thing is that the object is deleting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will change it to mtpnc terminating

if err := k.Cli.Get(ctx, mtpncNamespacedName, &mtpnc); err != nil {
return v1alpha1.MultitenantPodNetworkConfig{}, types.UnexpectedError, errors.Wrap(err, errGetMTPNC.Error()).Error()
}
if mtpnc.DeletionTimestamp != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 @ashvindeodhar is right, this doesn't belong in the getMTPNC. Return the MTPNC and let the caller decide what to do if it's Deleting.

.DeletionTimestamp.IsZero() is canonically the correct way to determine if an Object is deleting. ref https://book.kubebuilder.io/reference/using-finalizers.html

@shreyashastantram shreyashastantram marked this pull request as ready for review October 15, 2025 23:10
@shreyashastantram shreyashastantram requested review from a team as code owners October 15, 2025 23:10
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds validation to prevent IP configuration requests for pods with terminating MultitenantPodNetworkConfig (MTPNC) resources by checking if the deletion timestamp is set.

  • Introduces an IsTerminating() method to check if a MTPNC resource has a deletion timestamp
  • Adds validation in the middleware to reject requests when MTPNC is terminating
  • Includes test coverage for the terminating state scenario

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
crd/multitenancy/api/v1alpha1/utils.go Adds IsTerminating() method to check deletion timestamp
cns/middlewares/k8sSwiftV2.go Implements validation to reject IP config requests when MTPNC is terminating
cns/middlewares/mock/mockClient.go Adds mock data for testing MTPNC terminating state
cns/middlewares/k8sSwiftV2_linux_test.go Adds test case to verify terminating state is properly handled

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@shreyashastantram shreyashastantram changed the title Adding delete timestamp check fix: Adding delete timestamp check Oct 15, 2025
Co-authored-by: Copilot <[email protected]>
Signed-off-by: shreyashastantram <[email protected]>
errGetMTPNC = errors.New(NetworkNotReadyErrorMsg + " - failed to get MTPNC")
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
errDeleteTimestampSet = errors.New("MTPNC deletion timestamp is set")
Copy link
Contributor

Choose a reason for hiding this comment

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

these are still user-visible messages as this will be the error shown after "network not ready" to the cx on CNI
message should be more meaningful to the Cx - e.g.: "found mtpnc for previous pod pending deletion, waiting for new mtpnc to be ready"

Copy link
Contributor

Choose a reason for hiding this comment

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

arguably we could just return the "mtpncnotready" - but since the old mtpnc object is still visible, I think its better to have the separate error message.

Copy link
Contributor

Choose a reason for hiding this comment

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

this still applies to "mtpnc is terminating" - its not untrue, but it does not tell you why this is treated as a transient "network not ready" instead of a terminal error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With that change, we are tagging this error with stateful set pod creation, if user removes ownership ref and tries deleting mtpnc for some reason, pod restart would result in the same error which is not the case. Yes, it's not the expected behavior but if user can do it, should we not consider that case? I want to keep it general so that it doesn't change with context

Copy link
Contributor

Choose a reason for hiding this comment

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

if user removes ownership and tries deleting before pod starts they'll get this error - which tells them this will not succeed without a new mtpnc on ready state. and if the mtpnc completes delete on the next CNI restart they'll get "mtpnc not ready"

errGetMTPNC = errors.New(NetworkNotReadyErrorMsg + " - failed to get MTPNC")
errInvalidSWIFTv2NICType = errors.New("invalid NIC type for SWIFT v2 scenario")
errInvalidMTPNCPrefixLength = errors.New("invalid prefix length for MTPNC primaryIP, must be 32")
errMTPNCTerminating = errors.New("MTPNC in terminating state")
Copy link
Member

Choose a reason for hiding this comment

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

Update to deleting && add NetworkNotReadyErrorMsg
Without NetworkNotReadyErrorMsg kubelet won't retry faster for the new pod. You can check my PR when I added NetworkNotReadyErrorMsg for the reasoning behind this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants