Peter Bergen is CNN's national security analyst, a vice president at New America, and a professor of practice at Arizona State University. He is writing a book for Penguin/Random House about the Trump administration's national security team and its policies. The opinions expressed in this commentary are his own. View more opinion at CNN.

(CNN) Few who work at senior levels in the administration of President Donald Trump leave with their reputations unsullied. Even fewer leave on their own terms.

Jim Mattis is one of the few.

In the wake of Mattis' resignation as defense secretary Thursday, it's worth recalling that the relationship between Trump and Mattis started well. Mattis met Trump for the first time at Trump's golf club in Bedminster, New Jersey in mid-November 2016 to talk with the President-elect about the Secretary of Defense job.

After the meeting, Trump came out to talk to the press. Framed by the imposing, white columns of his Bedminster estate, Trump posed for pictures with Mattis.

Reporters shouted questions at Trump about whether Mattis was being considered for a cabinet job. Trump replied , "He's just a brilliant, wonderful man. What a career! We're going to see what happens, but he is the real deal."

During Mattis' first interview as Secretary of Defense on CBS's "Face the Nation," host John Dickerson asked Mattis, "What keeps you awake at night?"

Without missing a beat, Mattis replied , "Nothing. I keep other people awake at night."

Bromance over burgers

Trump loves this kind of smack and, before their bromance faded, he would invite Mattis to the White House to eat a casual dinner of burgers and to shoot the breeze about the state of the world.

In picking Mattis to be his Secretary of Defense , Trump said he had found his "Gen. George Patton." Because of his ferocity on the battlefield, Mattis is nicknamed "Mad Dog," a moniker Trump rejoiced in but Mattis himself doesn't love.

Mattis is also a "warrior monk." An American four-star general is likely to move a dozen times during a long career, bringing family and household effects from one post to the next. Gen. Mattis, who has never married, instead moved his books — all 7,000 of them.

In 2003 during the Iraq War, Mattis explained in an email to a fellow officer why deep reading about the history of warfare can save American lives on the battlefield. "By reading, you learn through others' experience — generally a better way to do business — especially in our line of work where the consequences of incompetence are so final for young men," he wrote.

By contrast, Trump reads little except, perhaps, hagiographic works by his most hardcore supporters.

As a candidate, Trump said he received information about military affairs from "the shows" on TV. Fox News continues to be a key briefing source.

But the differences between Trump and Mattis go far beyond their preferred means of absorbing information. The break between Mattis and Trump represents the most consequential split between those in the Trump administration who value US international alliances and commitments and those, like Trump, who do not.

Of course, others went before Mattis. Trump's former national security adviser, Lt. Gen. H.R. McMaster, understood American alliances and commitments, having served alongside allies in Iraq and Afghanistan. He also led the fight within the administration not to pull all US troops out of Afghanistan, which was Trump's first instinct.

The break between Mattis and Trump represents the most consequential split between those in the Trump administration who value the United States' international alliances and commitments and those, like Trump, who do not. Peter Bergen

But Mattis is the longest-serving senior cabinet official to leave the administration, and he is resigning on principle, in particular, over how Trump treats America's international alliances.

Trump believes that longtime American allies are somehow "ripping off" the US rather than contributing to world order and helping the US continue as the world's only superpower. At the same time, Trump declares his "love" for the North Korean dictator Kim Jong Un and fawns over Russian President Vladimir Putin, longtime American adversaries.

None of this makes any sense, of course. And Mattis' resignation underlines the fact that Emperor Donald Trump I is wearing absolutely no clothes.

Storied military career

Mattis enlisted in the Marine Corps Reserve when he was only 18 in 1969 at the height of the Vietnam War. He was commissioned a second lieutenant when he graduated from college in 1971.

Yet it was many years later that he first came to prominence. In the months after the 9/11 attacks, he led his Marines in the deepest aerial assault in US combat history near the de facto capital of the Taliban, Kandahar in southern Afghanistan. The Marines then seized the key Kandahar Airport.

Mattis, Gen. James Kelly, Trump's now-departing chief of staff, and Gen. Joseph Dunford, Trump's chairman of the Joint Chiefs of Staff, have worked closely in various roles since the 2003 invasion of Iraq.

Mattis, Kelly and Dunford led the Marine force that invaded Iraq. They helped overthrow Saddam Hussein's regime in a matter of weeks.

Mattis co-authored with Gen. David Petraeus the 2006 counterinsurgency manual that prompted a new US approach to the Iraq War, which had become a quagmire after the successful initial invasion. The manual was highly influential in military and national security circles and even became an unlikely New York Times bestseller

Clash with Obama team

Mattis ran Central Command (CENTCOM) for President Barack Obama, overseeing American military operations in the Middle East. He pressed for military action against targets in Iran after Iranian proxy forces killed American soldiers in Iraq in 2011. This sparked a furious debate at the White House. Obama wanted to complete the negotiations for an Iranian nuclear deal, and an attack on Iranian territory would surely undercut that.

JUST WATCHED Who is Gen. James 'Mad Dog' Mattis? Replay More Videos ... MUST WATCH Who is Gen. James 'Mad Dog' Mattis? 02:34

Mattis was forced by the Obama team to retire from the military five months early as a result of his hostility to Iran. Mattis learned about his defenestration when an aide passed him a note saying the Pentagon was announcing his replacement at CENTCOM. Neither the White House nor the Pentagon had bothered to give Mattis notice of his termination.

Surely this experience helped inform Mattis' choice to leave the Trump team on his own terms and make clear he was quitting on principle rather than being fired. After all, Mattis' close ally, former Secretary of State Rex Tillerson, learned of his likely firing on a trip to Africa. Tillerson hurried back only to be informed by a Trump tweet that he was out of job.

Disagreements from the start

The Trump team and Mattis had substantive disagreements almost from the start about both personnel and policy.

Mattis wanted to appoint experienced, competent officials in the top spots at the Pentagon. For the key job of undersecretary for policy, Mattis wanted to bring on Mary Beth Long, a former CIA operations officer who had held senior jobs at the Pentagon under President George W. Bush. But during the campaign, Long had signed a "Never Trump" letter, and the Trump team nixed her.

For the same key position, Mattis then tried to install Ann Patterson , a recently retired top US diplomat. The White House rejected her. As US ambassador in Egypt, Patterson had performed typical functions with the democratically elected Islamist government of Mohammed Morsi, which made her suspect to Trump officials and supporters.

In June 2017, Trump endorsed the Saudi-led blockade of Qatar, its neighbor and a US ally.

As the former CENTCOM commander, Mattis knew that in many ways the most important US base overseas is the Al Udeid Air Base in Qatar. It is not only the "forward" headquarters of CENTCOM but is also where the war against ISIS is coordinated. The massive base sprawls for miles in the Qatari desert and is home to around 11,000 American military servicemen and women.

Trump didn't seem to know about these facts or care about them when he applauded the Saudi blockade of Qatar. Mattis pushed back heavily on the blockade to no avail, while the Saudis have persisted with it, confident that Trump has their back.

A month after the beginning of the Qatar blockade, the US military brass was blindsided by a Trump tweet ordering the banning of transgender individuals serving in the US military.

This tweet came less than a month after Mattis had announced a six-month review of the matter.

Tensions between Mattis and Trump amplify

Trump's idiosyncratic "America First" foreign policy has gathered steam during the past year and it created further tensions between the president and his secretary of defense.

In June, Trump attended a G7 summit in Canada. As he left the summit on Air Force One, Trump dumped on his host , Prime Minister Justin Trudeau, accusing him on Twitter of being "very dishonest and weak" and making "false statements."

Trump feuded publicly with major Western allies at a NATO summit in Brussels a month later. Departing for his trip to the NATO summit and then on to the UK before a planned meeting with Putin, Trump said, "I have NATO. I have the UK, which is in, somewhat, turmoil. And I have Putin. Frankly, Putin may be the easiest of them all. Who would think?"

This all contrasted with Mattis' longstanding support of NATO. On his first day in office, Mattis made a point to call the NATO secretary-general, the British defense secretary and the Canadian defense minister to emphasize the continuing US commitment to the alliance. It is one of the most successful in modern history and contributed to the peaceful implosion of the Soviet Union.

Seventeen years later, NATO continues to assist and train the Afghan army.

After treating longtime US allies with contempt, Trump has gone out of his way to give passes to and shower praise on longtime American adversaries.

Trump kowtowed to Putin at the Helsinki summit in July, saying that he didn't "see any reason why" Russia would be responsible for interfering in the US election, going against the consensus of his own intelligence community.

By contrast, earlier this month Mattis said publicly at the Reagan Defense Forum in California that "we simply cannot trust" Putin.

In June, Trump attended a much-ballyhooed summit in Singapore with Kim and later declared, "We fell in love."

This love affair has had scant impact on the dictator's nuclear ambitions. According to the New York Times last month, the North Koreans have accelerated their ballistic missile program at more than a dozen secret bases.

In June, Trump canceled joint US-South Korea military exercises, a staple of the alliance for decades. The move blindsided Mattis and was a gift to the North Koreans.

Final straws

A sign of the fraying relationship between Trump and Mattis came in October when Trump told Lesley Stahl on CBS's "60 Minutes" that Mattis was "sort of a Democrat."

Then came the twofer last week. First, Trump announced the pullout of 2,000 soldiers from Syria. This was an early Christmas present to American enemies such as Syrian dictator Bashar al Assad and his close allies Putin and Iran, as well as to ISIS and al Qaeda's affiliate in Syria. It also leaves the Kurdish forces that did almost all the fighting against ISIS on behalf of the US vulnerable to attack by the powerful Turkish military, which regards them as terrorists. In effect, Trump's decision risks abandoning an American ally on the battlefield. This was the decision that precipitated Mattis quitting.

Stay up to date... Sign up for our new newsletter. Join us on Twitter and Facebook

And then came the reports of Trump's plan for a pullout of some 7,000 US troops from Afghanistan, about half the US troops in the country.

That is hardly a politically useful message to send to the struggling Afghan government, which will embark on a critical presidential election next year. Or to Afghanistan's many problematic neighbors. Or, indeed, to the Taliban, which recently started negotiating directly with the US.

A principal Taliban demand is the withdrawal of US forces from Afghanistan. Trump styles himself as a great dealmaker, but he is giving the enemy half of what they want without exacting any concessions at all.

Mattis was in favor of the Afghan policy that Trump announced last August, which included a troop increase and a "conditions-based" timeline for their withdrawal. This is all unraveling now.

What is particularly bizarre about these policy shifts is they do exactly what Trump repeatedly warned against during his campaign. They give America's enemies an early heads up about military plans and withdrawal dates.

During the campaign, Trump also critiqued the total US troop withdrawal from Iraq under Obama as paving the way for ISIS.

Now comes Mattis' resignation letter, which called out Trump on the need for "showing respect" to longtime allies.

Mattis reminded Trump that 29 NATO countries have fought "alongside" the US since 9/11.

Mattis also gently took Trump to task for his mollycoddling of Russia and its "authoritarian model."

In a final dig, Mattis reminded the president that he based his critique of Trump's policies on "over four decades of immersion in these issues." That was a not-so-subtle reminder that Trump is the first US president with no political or military service and a national security neophyte.

Trump won't listen, of course. But good for Mattis that he resigned on principle, a public servant in the court of Donald Trump who left with his reputation intact.

Qualified candidates to succeed Mattis must look at Trump's abrupt and unilateral decisions on Syria and Afghanistan and ask if they want to be part of the continuing chaos.

"Chaos," incidentally, was Mattis " call sign " on the battlefield.

Mattis may have brought chaos to enemies, but he tried to keep a lid on it during his two years in the Trump administration.

What comes next? Already, Trump has stuffed his national security team with advisers who are likely to go along for the ride, including his National Security Adviser John Bolton, Secretary of State Mike Pompeo, chief Middle East adviser and son-in-law Jared Kushner, and his nominee as US ambassador to the UN, Heather Nauert.

In the court of Donald Trump, there seems to be no room for independent thinkers.

Who knows what the Trump show will now produce.