Hi there!
The cleanest way I found is to directly use the clamp method of the Vector2 class like this :
var rect = get_viewport_rect()
global_position = global_position.clamp(rect.position, rect.size)
EDIT: I didn’t finished the video at the time I wrote this, but the guy ended up by writing almost the same as me ^^