President Donald Trump just fired Secretary of State Rex Tillerson. National Security Adviser H.R. McMaster could be fired at literally any moment. Chief of Staff John Kelly and Attorney General Jeff Sessions are rumored to be on the potential chopping block, as are at least five other members of Trump’s Cabinet.

But Defense Secretary Jim Mattis shared virtually every policy view as Tillerson, which in part got Tillerson fired. In a sense, Mattis does many things that would usually anger Trump, yet he’s so far avoided the president’s ire.

Consider the following: Mattis purposefully slow-walked Trump’s transgender military ban. He agreed with Tillerson on issues ranging from the Iran nuclear deal to the relocation of the Jerusalem embassy — all positions that contributed to Trump’s fury with the now-former secretary of state.

Mattis hasn’t met Tillerson’s fate even though he’s not a Trump sycophant, and he famously refused to praise Trump during the administration’s first North Korean propaganda-like Cabinet meeting. Mattis is also linked to the massive Theranos scandal, in which the company lied about its blood test technology for years, because he’d served on the company’s board of directors after retiring from the military.

And the secretary’s past as a general doesn’t explain why Trump still likes him. Kelly is a retired Marine four-star, and McMaster is currently an Army three-star general — but Trump reportedly can’t stand either of them. It seems that Kelly’s push for order in the White House irks the president, and Trump is unhappy with McMaster’s long briefings, among other things.

Put together, it seems like Mattis should be a Trump target, even though he clearly isn’t. Experts think they know why: Mattis has figured out the way of working for the volatile president, which combines staying out of the public limelight and showing there’s no daylight with Trump on key issues.

“In the military, you never get to pick the bosses that you’re going to work for,” James Carafano, a national security policy expert at the conservative Heritage Foundation who served on Trump’s transition team, told me. “One of the skills you learn is how your boss works, and you adapt to that.”

“It doesn’t mean that you can’t argue or fight with the boss, but it damn sure means that you can’t try to play rope-a-dope and not be supportive,” he continued. “Mattis is smart enough to figure that out.”

Of course, Trump could change his mind and ultimately choose to fire Mattis — it’s Trump, after all. But as the president looks to reset his administration, it’s noteworthy that Mattis has so far sidestepped Trump’s rage.

Mattis and Trump diverge on pretty much everything. Trump likes him anyway.

Trump and Mattis seemingly have a good relationship. It’s somewhat perplexing why that is.

Take their major policy disagreements. Last year, Trump reportedly spent weeks asking Mattis to impose a ban on transgender members of the military, which would block trans civilians from enlisting and reverse an Obama-era directive that allowed roughly 15,500 trans troops to serve openly. But Mattis ignored Trump and took no action.

That prompted Trump to take matters into his own hands. Last July, he tweeted his order to implement the transgender ban policy. Trump then tasked Mattis and Kelly, who then was the secretary of homeland security, to lead a months-long review of the issue. But in February, Mattis reportedly recommended to Trump that he allow trans people in and out of the military to serve. And on February 23, the first openly trans recruit joined the military — all while Trump stayed silent.

But that’s not all. Mattis and Tillerson famously formed a united front to push back against the president on key national security issues — angering Trump along the way — and lost again and again. For example, Trump threatened to pull the US out of the Iran nuclear deal in May and still plans to move the US Embassy in Israel to Jerusalem. Yet Trump only chose to oust Tillerson.

“We got along actually quite well, but we disagreed on things,” Trump told reporters on March 13, never mentioning the similar policy chasm between him and Mattis.

Keep in mind that Mattis has proved unwilling to play to the president’s ego. During the administration’s first Cabinet meeting last June, most of the president’s aides paid homage to the commander in chief. Mattis famously didn’t. Instead, he said it was his honor to represent the men and women of the Department of Defense.

And it’s not clear Mattis’s status as a retired Marine general is the reason Trump keeps him around. After all, Trump is reportedly considering removing Kelly, in part for imposing too strict a system on the White House and for botching the firing of a White House staffer twice accused of domestic assault (although Trump and Kelly seem on better footing now). Before Kelly, aides could flow more freely in and out of the Oval Office.

And rumors swirl constantly that McMaster’s ouster is imminent. As the Washington Post puts it, “The president has complained that McMaster is too rigid and that his briefings go on too long and seem irrelevant.” It’s unclear, though, if either Kelly or McMaster will head out the door anytime soon.

Finally, Trump doesn’t like it when his staffers are mired in scandal — but Mattis is linked to one now. As Vox’s Matt Yglesias notes, Mattis is implicated in one of the largest business scandals of the past decades, described by the Securities and Exchange Commission as an “elaborate, years-long fraud” through which Theranos, led by CEO Elizabeth Holmes and president Ramesh Balwani, “exaggerated or made false statements about the company’s technology, business, and financial performance.”

Yglesias continues:

Mattis not only served on Theranos’s board during some of the years it was perpetrating the fraud, after he retired from US military service, he earlier served as a key advocate of putting the company’s [fake blood test] technology ... to use inside the military while he was still serving as a general.

All that said, Mattis maintains widespread support on the Hill — including from Democrats — and many feel he is helping to check Trump’s worst foreign policy impulses. That could eventually pose a problem, though, as Trump increasingly feels he doesn’t need supervision.

And Trump could eventually turn on Mattis if Mattis continues to push policies he disagrees with. Trump’s likely decision in May to remove the US from the Iran nuclear deal could be a climactic moment. It’s when he will decide once and for all whether to stick with the Mattis view to stay in the agreement or throw that aside.

However, their policy fight will surely be behind the scenes. In public, Mattis will more than likely defend whatever the president decides.

There are reasons to think Trump should be mad at Mattis, but that just isn’t the case. That’s because the secretary is a surprisingly shrewd political operator for a man who spent decades in the nominally apolitical US military and never held a high-profile government job before.

Mattis purposely keeps his head down — and defends Trump when he has to

One of the keys to Mattis’s success may be how he deals with the press.

Mattis has yet to conduct a press conference of his own in the Pentagon briefing room. He’s only appeared on one Sunday news show. He has yet to sit down for a profile of him. And he’s scaled back the number of reporters that travel with him from about 11 to six (or eight, if a television crew comes along). In effect, Mattis doesn’t aim to make news — which helps keep him off the TV his boss watches so often.

That’s purposeful, Doug Wilson, a Pentagon senior spokesperson in the Obama administration, told me. “I think the way he’s dealing with the press both reflects his own natural instincts about not wanting to be in the limelight,” he said, “and it also reflects the realities of this administration where focusing on your job and putting your nose to the grindstone is the best way to ensure you get to keep doing your job.”

Dana White, the Pentagon’s top spokesperson, told me the secretary’s “sole focus has been and continues to be the warfighter” and that “he uses his position to ensure they have what they need before they need it.” In practice, that means he cares less about seeking media attention and prefers to focus on the intricacies of his job.

Other recent secretaries of defense — with the exception of Donald Rumsfeld — also didn’t seek the spotlight. Robert Gates, whose served as defense secretary under Presidents George W. Bush and Barack Obama, didn’t like when he was the subject of a story.

In effect, Wilson thinks Mattis formed the press relationship he needed to because he has to keep his head down in this administration. “I think Jim’s balancing act is very appropriate, and I would not fault him for things where in other administrations — under other circumstances — you might’ve expected more,” he told me.

That said, Mattis doesn’t mind once in a while dropping in for surprise appearances with the Pentagon press corps in their bullpen, and is also unfailingly polite and even-tempered in his dealings with reporters. Yet when he does face the media, he either defends his relationship with the president or stays out of the political fray.

For example, last August, someone recorded Mattis speaking to US troops in Jordan. In his remarks, he spoke about societal problems in America, which were widely interpreted as the secretary rebuking Trump. Here’s why: Mattis’s comments came shortly after the president’s remarks about Charlottesville, Virginia, when Trump said “both sides” were to blame for violence during a white supremacist rally, even though a neo-Nazi killed a protester.

“It’s [the country has] got problems that we don’t have in the military, and you just hold the line, my fine young soldiers, sailors, airmen, Marines,” Mattis told the troops. “Just hold the line until our country gets back to understanding and respecting each other and showing it.”

When a reporter asked about the viral video — and whether his remarks were subtly aimed at Trump — Mattis demurred. “If you’ll remember, the first, I don’t know, three, four, five, six paragraphs was about America coming together,” he said. “And people took — literally, I — I’m using the president’s thoughts and they thought that I was distancing from the president. So I mean, it shows how ludicrous this really is.”

Or take what happened later that month. On August 30, 2017, Trump tweeted that “Talking is not the answer!” when it comes to North Korea. (Of course, he’s now planning for a potential summit with North Korean leader Kim Jong Un by the end of May.)

After the tweet, a reporter asked Mattis if the US had no diplomatic options left to defuse a standoff with Pyongyang. “No, we’re never out of diplomatic solutions,” Mattis responded.

That led to speculation that Mattis publicly broke with Trump on a top national security issue — a narrative Mattis denied the next day. “I’ll do my best to call it like I see it, but right now, if I say six and the president says half a dozen, they’re going to say I disagree with him,” he told reporters. “So let’s just get over that.”

So perhaps the answer is less that Trump doesn’t care about where he and Mattis diverge, and more that Trump knows Mattis is ultimately a team player. It’s looking more and more like Mattis has broken the code to working — and surviving — in the Trump administration.