This is common in many democracies across the world.
The fact that someone in your government is forced to step down over something as basic as having an affair (our president has had a number of them over the last 4 years) shows that USA is still pretty high up as far as government morals go. Bravo!
So it would be a double standard for management, etc., and he would have been known to have broken the code of conduct. Not broken it with the affair, but broken it by not disclosing a compromising situation.
Not to mention, it was leaked to the media, everyone already knows about it. How exactly could that be used to blackmail someone if the entire world already knows about it?
I suppose you think Clinton should have resigned (instead of lying about the affair, which made everything worse)?
Now, maybe you think those rules are stupid. Frankly, I think some of them don't make a lot of sense (or at least fail a cost/benefit analysis), but I don't make the rules. Petreus agreed to follow those rules. If he didn't like them, he could have refused the job. Or he could have worked to change them. But he didn't. So he has to live by them. And he failed to do that.
Also, the people involved have probably learned from painful experience that traditional institutional rules are usually better guides than they sometimes appear to be at the moment of decision.
If anything I would guess that he was fired for allowing his secret to leak (which means incompetency).
Realistically though, all that scandal is just a cover for real reason why Petraeus was fired - probably something like policy disagreement with superiors.
Actually leaving the military, if you're an officer, requires resigning your commission. For enlisted, one generally signs up for a limited term of enlistment, and is out of the military at the end of that enlistment unless they choose to reenlist. Once an enlisted servicemember reaches 20 years of service, they become eligible to retire at their current rank as well.
I'm not an American and I don't live in the States, but from what I can tell a bigger f*ck-up was that Mrs. Broadwell gained access to Petraeus's personal email account. God only knows (or in this case the FBI) what was in there, but I suspect it was not only about baseball newsletters and photos with Petraeus's nephews and kids. This coming from the man that heads the CIA is a major no-no, IMHO.