To put it simply, the Ottomans did invade Morocco multiple times, and they were also allies with various Moroccan Dynasties too. The relations boil down to rivaling Dynasties in Morocco in the 16th Century, with the pro-Ottoman Saadians dynasty in the south and the Fez backed Wattassid dynasty in the north. A series of wars were fought between the dynasties for control of the region culminating in the battle of Battle of Ksar El Kebir where the Ottoman backed forces defeated the Northern Army backed by Portugal at the time. The aftermath is summed as follows:
Although the Ottomans contributed to the final establishment of a stable Moroccan rule, Morocco was never nominally a part of the Ottoman Empire and remained independent thereafter. As the Ottoman Empire dominated Northern Africa, Morocco was the exception to that domination.
The peaceful neighbors continued peaceful relations until the fall of the Ottoman Empire.
Further on Ottoman Expansion during the period:
After the formation of a stable government of Morocco in 1578 the Ottoman empire was dealing with a period known as the Revolts and revival (1566–1683). This period was rife with instability in the Ottoman Domain with a series of almost a dozen sultans in a century. During this time it is not hard to imagine that the regimes in power were more concerned with securing their throne locally than expanding into friendly neighbors. But this didn't stop multiple failed attempts, which resulted in the Empire ceding territories to Persia and failing to invade the Polish Commonwealth (Moldavian Magnate Wars).