Under the Law of Moses, only an Israelite was to be selected as king over Israel.
Although it was GOD’s will that He serve as Israel’s King, He foreknew their hardheartedness and eventual desire to select a man to rule over themselves.
In anticipation of this eventuality, GOD gave Moses guardrails to the forefathers of that generation, written within the Law, to safeguard the people and the eventual leaders, so that the people might be blessed in spite of their foolish decision and rejection of Him as their King.
We previously established that GOD was to select the king.
GOD also instructed that the king come from the people of Israel:
14 “If you enter the land the Lord your God is giving you as an inheritance, and you inherit it and dwell in it, and say, ‘I will set a ruler over me like the remaining nations around me,’ 15 you shall surely set a ruler over you whom the Lord your God chooses; one from among your brethren you shall set as a ruler over yourself; you cannot set a foreigner over yourself, because he is not your brother.Deuteronomy 17:14-15 OSB