Image copyright Reuters Image caption Top Canadian public servant Michael Wernick to resign

Canada's top public servant has announced he will step down over his handling of a political crisis that has embroiled PM Justin Trudeau.

Privy Council clerk Michael Wernick said "recent events" led him to conclude he must retire before the upcoming general election.

He had faced calls for his resignation over claims he acted in a partisan manner during the SNC-Lavalin affair.

Fallout from the crisis has already cost Mr Trudeau two top ministers.

The prime minister's former attorney general and justice minister, Jody Wilson-Raybould, has said she faced attempts at political interference from Mr Trudeau and his inner circle - including Mr Wernick - over whether to make a deal that would have suspended prosecution against SNC-Lavalin.

Mr Wernick also made two combative appearances before a parliamentary committee looking into the matter where he defended the government's actions.

The prime minister and the top bureaucrat have both denied any wrongdoing in the handling of the SNC-Lavalin affair.

In a letter to the prime minister on Monday, Mr Wernick said it was clear "that there is no path for me to have a relationship of mutual trust and respect" with opposition leaders.

He added that it was essential that Canadians continue to see the public service as non-partisan.

The main role of the clerk of the privy council is to act as a link between the prime minister and cabinet and the public service.

Mr Wernick was also one of the key officials tasked with warning Canadians of possible interference in the coming general election.

In his letter, he said it is essential the clerk of the privy council "be seen by all political parties as an impartial arbiter of whether serious foreign interference has occurred".

A number of opinion polls have indicated that Mr Trudeau's popularity is slipping in the run-up to the election scheduled for October as he struggles to contain the political crisis.

Liberal MP Jane Philpott quit cabinet earlier this month in protest with how the government has handled the matter. Ms Wilson-Raybould resigned from cabinet in February. One of Mr Trudeau's most trusted and senior aides also quit amid the affair.

Quebec-based SNC-Lavalin is one of the world's largest engineering and construction companies.

It is facing allegations that former executives paid bribes to win contracts in Libya under Muammar Gaddafi's regime, which fell in 2011.

It has been lobbying for an agreement that would have allowed the firm to avoid a criminal trial and instead agree to alternative terms or conditions, like penalties or enhanced compliance measures.