Jesus knew His purpose before He came into the world, based on the fact that salvation through Jesus was ordained before the foundation of the world (1 Peter 1:19-20; Revelation 13:8; 1 Corinthians 2:7) and He existed before the world began (John 17:24).
Philippians 2:6-8 shows how Jesus was equal with God the Father and yet became one of us, and Hebrews 10:5 says, "Therefore, when He came into the world, He said: Sacrifice and offering You did not desire, but a body You have prepared for me."
And in Mark 10:45, Jesus said about Himself, "For even the Son of Man did not come to be served, but to serve, and to give His life a ransom for many."
Even though He knew He would be crucified, He went to the cross willingly (John 10:18; John 15:13; 1 Corinthians 11:24) because of His love for us.