Cohen was always a snake and there's no way Trump didn't know that. Re-read Art of the Deal, Trump literally says flat out he hires people he knows are crooks as long as they are useful, because they are usually the best at their respective fields. Trump probably leaked info and tricked the NY AG into raiding Cohen's office to get rid of him once he outlived his usefulness. Cohen never flipped since he was never really on Trump's side to begin with.
He likely pleaded guilty just now because Trump contradicted his testimony in his written answers to Mueller with iron clad proof. Cohen is now seeking the least amount of legal repercussions for pissing off POTUS, congress, and Mueller.