Let $p:E \to M$ be a vector bundle over a smooth manifold $M$, $M\times 0$ be the image of its zero section of $p$, $\mathcal{X}(M)$ be the space of vector fields on $M$, and $\Gamma(E)$ be the space of sections of $E$.
Suppose $f:E \to\mathbb{R}$ is a smooth function such that
1) $M \times 0$ coincides with the set of critical points of $f$, however it does not necessarily coincide with the entire level set $f^{-1}(c)$ for some $c\in\mathbb{R}$.
2) the restriction of $f$ to each fibre $E_x$ over $x\in M$ has no critical points except for $x\times 0$. This in particular implies that the level sets of $f$ are transversal to the fibres.
Question: does there exist either
a linear connection $\nabla:\mathcal{X}(M) \times\Gamma(E) \to \Gamma(E)$, or, more generally,
a compete (=having path lifting property) Ehresmann connection $\nabla: E \to J^1(E)$, (a smooth section of the $1$-jet bundle over $E$)
on this vector bundle which preserves the function $f$ in the following sense: if $\gamma:[0,1]\to M$ is a smooth curve, $(\gamma(0), v) \in E_{\gamma(0)}$ is an any point in a fibre over $\gamma(0)$, and $\widehat{\gamma}:[0,1]\to E$ is a unique lifting of $\gamma$ with respect to $\nabla$ such that $\widehat{\gamma}=(\gamma(0), v)$, then $f \circ \widehat{\gamma}(t) = f \circ \widehat{\gamma}(0)$ for all $t\in[0,1]$.
Example when this situation appears. 1) Let $E = M \times \mathbb{R}^n \to M$ be a trivial bundle, $g:\mathbb{R}^n\to\mathbb{R}$ be a smooth function with a unique critical point at the origin, and $f:E \to \mathbb{R}$ be given by $f(x,v) = g(v)$. Then one can take a linear connection $\nabla$ to be the zero map.
2) Probably this holds if $\langle\cdot,\cdot\rangle$ is a smooth scalar product on $E$ and $f(x,v) = \langle v,v\rangle$ is the square of the corresponding norm.
At least for the tanent bundle of a Riemannian manifold $M$ the corresponding Levi-Civita connection preserves the square of the norm of tangent vectors in the above sense.
Actually I am looking for an answer in the situation when $f: N \to \mathbb{R}$ is a Bott function, $M$ a critical submanifold of $f$, and $E$ is a tubular neighborhood of $f$. Example 2) corresponds then to the case when $f$ takes minimum along $M$.