A characterization of harmonic foliations by variations of the metric