Kānat is a past defective verb. Bi-l-samāʾ serves as the preposed predicate of kāna, while ʿillatun is its postponed nominative subject.
In introduces the condition, and in lam takun in the following sentence negates it, producing the contrasting clear-sky case.