It's a common narrative, especially among a lot of libertarians, that the war on Ukraine wouldn't be happening if Ukraine weren't in talks with NATO and Ukraine could end this all right now if they signed a treaty with Russia to ensure that they wouldn't attempt to join NATO.
Like most clearly oversimplified and generally wrong takes, there's some truth to this. After the fall of the Soviet Union there were conversations with Gorbachev about keeping the West from knocking on Russia's door. However, it's a myth that assurances were made that NATO wouldn't expand eastward.
If there is a betrayal here, it's entirely on the part of the Russians. Until 1994 Ukraine had the third largest nuclear arsenal in the world. That year, Ukraine agreed to give up her nuclear arsenal with assurances of protection by both the USA and Russia.
The potential of Ukraine becoming a member of NATO was never a threat of Western invasion of Russia. Even if it were, Putin wouldn't admit to being worried about it. NATO wouldn't be putting nuclear weapons into Ukraine like the USA did with Turkey.
So, I'm going to argue that the potential of Ukraine joining NATO isn't the reason why this is happening. Rather, it's why it's happening now.
Putin has made clear that he views Ukraine similarly to how Hamas views Israel. He thinks that Ukraine is rightfully Russian land that was taken from his people after a tragic loss.
Russia isn't in a good place right now. The population is declining. Record numbers of people (even for Russia) are dying from alcohol abuse. Drivers in Russia are using dash cams because people jumping in front of cars and suing drivers has become common place. This was all before the sanctions.
Ukraine has a lot of resources that Russia could use, including her people. You combine that with the fact that Putin views Ukraine as being rightfully his and you can see why, when the Ukrainian government shifted to being less friendly with Putin (unlike Belarus), they might look for support from NATO.
I think that Putin saw this as now or never. He wanted to take Ukraine.
Still, being evil and psychotic doesn't make a person stupid. He knew that taking Ukraine would result in international backlash. With the possibility of Ukraine joining NATO, there could be enough spin to convince some people that NATO started it. What's more important is that, if he waited and Ukraine did join NATO, his invasion of Ukraine would have brought several countries, including the USA, into the conflict on the side of Ukraine. That would be a war that zero people would want and that Putin wouldn't have a prayer of winning.
Maybe one could make the argument that, if NATO weren't a factor, Putin would have kept kicking the can down the road until he ended up kicking the bucket and none of this would have happened. I'm more pessimistic. I think that this was always going to happen.