LOS ANGELES (AP) ' Santa Maria police Officer Matt Kline had to make a quick decision as he watched two sergeants try to wrestle a loaded handgun from a fellow officer.

Concerned someone might get shot, Kline drew his .45-caliber semi-automatic handgun and fired three rounds, striking and killing the gunman. Kline was soon told he had killed his friend, Officer Alberto Covarrubias, who was being investigated for having sex with a 17-year-old girl.

Santa Barbara County District Attorney Joyce Dudley said Tuesday that Kline, a five-year veteran, was legally justified in killing Covarrubias.

"Fearing for the safety of others, and believing he needed to act immediately to preserve the sergeants' lives, he shot and killed," Covarrubias, Dudley wrote. "I've concluded Officer Kline was justified in using deadly force, and, therefore this shooting was justified" under California law.

The 18-page report reveals a detailed account of what transpired on Jan. 28, but doesn't address the actions of police that night when they tried to arrest Covarrubias while he was armed with his service pistol.

Covarrubias, 29, was ending his shift at a DUI checkpoint when he was shot in a scuffle as police moved in to arrest him for investigation of having sex with the girl, a member of the department's Police Explorer program.

Police learned about a possible inappropriate relationship between the girl and Covarrubias two days before the shooting. The girl told investigators she had sex with Covarrubias and agreed to participate in a ruse where she would tell him she might be pregnant with his child, according to the report.

Shortly before the shooting, the girl called Covarrubias and told him about the pregnancy and that he was the only person who could be the father of the child.

Covarrubias asked the girl to deny they had sex and said he would kill himself if their relationship was made public, the report said.

"'You know, I'll be honest with you, I won't go to jail,'" Covarrubias tells the girl, according to the report. "'I'll go out, but I won't go to jail.'"

Covarrubias adds: "'But like I said, if it gets out that it's mine, if you tell anybody, or this is a set up ... I'm done girl.'"

Police then made the decision to arrest Covarrubias at the DUI checkpoint, even though at least one superior was concerned about arresting an armed officer among other officers, most of whom had no idea that Covarrubias was under investigation.

Another high-ranking officer said the reason for making an arrest immediately was because Covarrubias "may be suicidal and had told someone that he wasn't going to be taken alive," the report said.

Two sergeants ' one of them the officer's cousin ' approached Covarrubias but he backed away. Covarrubias drew his gun and fired one shot, but didn't hit anyone, the report said. Security camera video from a nearby business captured some of the struggle.

One of the sergeants pleaded for help from fellow officers as Covarrubias did not let go of his gun. When Kline noticed the two sergeants were in "the fight for life," according to the report, he pulled out his weapon and shot and killed Covarrubias.

"'Was it Cova?'" Kline, who was Covarrubias's best men at his wedding three weeks earlier, asked one of the sergeants. When Kline was told he had shot his friend, he became even more upset, the report said.

"'I (expletive) shot him. I had to shoot him. He pulled a gun out and shot at the sergeants,'" Kline said.

The Office of Independent Review, a Los Angeles County law enforcement watchdog, is conducting a review of Santa Maria's police procedures. It's not known when the agency will finish its investigation.