Type IV pili (T4P) produced by the pathogen Pseudomonas aeruginosa play a pivotal role in adhesion, surface motility, biofilm formation, and infection in humans. Despite the significance of T4P as a potential therapeutic target, key details of their dynamic assembly and underlying molecular mechanisms of pilus extension and retraction remain elusive, primarily due to challenges in isolating intact T4P machines from the bacterial cell envelope. Here, we combine cryo-electron tomography with subtomogram averaging and integrative modelling to resolve in-situ architectural details of the dynamic T4P machine in P. aeruginosa cells. The T4P machine forms 7-fold symmetric cage-like structures anchored in the cell envelope, providing a molecular framework for the rapid exchange of major pilin subunits during pilus extension and retraction. Our data suggest that the T4P adhesin PilY1 forms a champagne-cork-shaped structure, effectively blocking the secretin channel in the outer membrane whereas the minor-pilin complex in the periplasm appears to contact PilY1 via the central pore of the secretin gate. These findings point to a hypothetical model where the interplay between the secretin protein PilQ and the PilY1-minor-pilin priming complex is important for optimizing conformations of the T4P machine in P. aeruginosa, suggesting a gate-keeping mechanism that regulates pilus dynamics.