Spike Lee is an extraordinarily gifted filmmaker, but sometimes his passion has overwhelmed his discipline, and the resultant pictures have been hectoring messes. That doesn’t happen in this case. There’s plenty of righteous indignation throughout “BlacKkKlansman,” but only at the end, when Lee chooses to accentuate the contemporary relevance of his period tale with news footage, does it arguably lapse into didactic overkill. The result is the most compelling—and funniest—movie Lee’s made in years, as well as one of his most effective in terms of messaging.

The actual case on which the script is based is disarmingly cheeky. Ron Stallworth, the first African-American cop in the Colorado Springs police department, in 1978 convinced his superiors to let him run a sting operation after seeing a recruitment ad for the KKK in the local paper. He contacted the organizers for a one-and-one meeting, for which a white officer was assigned to impersonate him. The operation continued for nine months, during which Stallworth made direct contact with David Duke and even served as the Grand Wizard’s bodyguard when he came to Colorado Springs. Though it yielded no arrests, the operation did reveal the Klan affiliation of some military personnel.

Lee and his writing colleagues Charles Wachtel, David Rabinowitz and Kevin Willmott have added some major fictional touches to the tale—adding a romantic interest for Stallworth and making his white partner Jewish, for example—but it’s commonplace to allow such embellishments to increase the urgency of a film “based on a true story,” and no more inappropriate here than elsewhere.

The important things is that Lee handles this remarkable tale with the same cinematic dexterity he demonstrated in the most stylistically skillful of his previous pictures—from “Do the Right Thing” through “Malcolm X” and “25th Hour” to “The Inside Man.” Visually it’s rawer than those pictures—Chayse Irvin’s cinematography aims for a gritty period look rather than slickness—but that’s the right choice for this material.

Even more important is the tonal balance Lee brings to the story, juxtaposing drama and comedy with considerable finesse. That’s apparent in the finely drawn performances of John David Washington as Stallworth and Adam Driver as his white partner, here called Finn Zimmerman, who actually takes most of the risks in the operation. Washington has enormous fun in his phone calls, particularly the ones he has with Duke (played with flair by Topher Grace, who suggests the nastiness behind the man’s carefully contrived amiability), in which he uses what in the recent “Sorry to Bother You” would be called his “white voice.” He also brings out the guilt Stallworth feels about hiding his “pig” status from Patrice (Laura Harrier), the Colorado State Black Student Union president whose organization he infiltrates—and who becomes his girlfriend.

In many respects Driver has the more difficult task, just as Stallworth’s real-life partner did, handling much of the heavier dramatics as Zimmerman is challenged about his commitment by his fellow Klansmen and struggles with embracing his Jewish identity in the process. But even so, he brings an air of hangdog likableness to his performance even as he tackles the role’s darker aspects.

One sees the canny melding of tone throughout the film, but a number of scenes stand out. Those in which Zimmerman interacts with the local Klansmen encompass both the clownishness of one called Ivanhoe (Paul Walter Hauser) and the savagery of perpetually furious Felix (Jasper Paakkonen), whose use of his dottily devoted wife Connie (Ashlie Atkinson) evokes both chuckles and horror—as well as the steely pragmatism of the cell’s leader Walter (Ryan Eggold). And that Klan meeting for which Duke comes to Colorado includes both a farcical note when Stallworth asks the Grand Wizard to take a picture with him (it might make you recall the end of the vintage “All in the Family” episode with Sammy Davis, Jr.) and a chillingly dangerous one when an outsider (Nicholas Turturro) is ready to blow Zimmerman’s cover. The look on the face of a black waiter hired for the gathering adds to the off-kilter feeling.

Of course, Lee’s anger over the country’s history of racial discrimination is very much in evidence as well. A subplot about a cop who shakes down black motorists and harasses Stallworth is a running thread (ending, one might think, a little too positively) and even the original interview Stallworth has with the police chief (Robert John Burke) and a politician (Isiah Whitlock, Jr.) foreshadows the trouble he’ll face.

But it really takes center stage in some powerful set-pieces that are dropped into the narrative at strategic points; some will dismiss these as digressions, but they reinforce the idea that the Stallworth story is but a small part of a long-running theme in American history. There are, for instance, the episodes that bookend the film–a prologue introduced by a clip from “Gone With the Wind,” in which Alec Baldwin, as a right-wing bloviator, records one of his vitriolic tirades, and that concluding montage of recent news footage about Charlottesville and other tragedies.

And there’s more: a stinging oration delivered by Kwane Ture, the erstwhile Stokely Carmichael (Corey Hawkins) at an event sponsored by Patrice’s group, and a second presentation at the Union’s center, this time a poignant recollection on lynching by an elderly activist played by Harry Belafonte. At the opposite end of the ideological spectrum is a screening of D.W. Griffith’s “Birth of a Nation” for the Klan and their assembled family and guests at an initiation ceremony.

All of this might sound like a bit much, but Lee and his editor Barry Alexander Brown weave the various pieces together in a whole that only occasionally feels unwieldy. “BlacKkKlansman” is an impassioned take on racial bigotry in America, but unlike some of Lee’s other films, it shows a level of directorial control that keeps it engaging without dulling the message.