def get_kubernetes_containers( self, docker_volumes: Sequence[DockerVolume], system_paasta_config: SystemPaastaConfig, aws_ebs_volumes: Sequence[AwsEbsVolume], service_namespace_config: ServiceNamespaceConfig, ) -> Sequence[V1Container]: service_container = V1Container( image=self.get_docker_url(), command=self.get_cmd(), args=self.get_args(), env=self.get_container_env(), resources=self.get_resource_requirements(), lifecycle=V1Lifecycle(pre_stop=V1Handler(_exec=V1ExecAction( command=[ "/bin/sh", "-c", "sleep 30", ], ), ), ), name=self.get_sanitised_deployment_name(), liveness_probe=self.get_liveness_probe(service_namespace_config), ports=[ V1ContainerPort(container_port=8888, ), ], volume_mounts=self.get_volume_mounts( docker_volumes=docker_volumes, aws_ebs_volumes=aws_ebs_volumes, persistent_volumes=self.get_persistent_volumes(), ), ) containers = [service_container] + self.get_sidecar_containers( system_paasta_config=system_paasta_config) return containers
def get_sidecar_containers( self, system_paasta_config: SystemPaastaConfig) -> List[V1Container]: registrations = " ".join(self.get_registrations()) # s_m_j currently asserts that services are healthy in smartstack before # continuing a bounce. this readiness check lets us achieve the same thing readiness_probe: Optional[V1Probe] if system_paasta_config.get_enable_nerve_readiness_check(): readiness_probe = V1Probe( _exec=V1ExecAction(command=[ system_paasta_config.get_nerve_readiness_check_script(), ] + self.get_registrations(), ), initial_delay_seconds=10, period_seconds=10, ) else: readiness_probe = None hacheck_sidecar = V1Container( image=system_paasta_config.get_hacheck_sidecar_image_url(), lifecycle=V1Lifecycle(pre_stop=V1Handler(_exec=V1ExecAction( command=[ "/bin/sh", "-c", f"/usr/bin/hadown {registrations}; sleep 31", ], ), ), ), name="hacheck", env=self.get_kubernetes_environment(), ports=[ V1ContainerPort(container_port=6666, ), ], readiness_probe=readiness_probe, ) return [hacheck_sidecar]
def get_kubernetes_containers( self, volumes: Sequence[DockerVolume], system_paasta_config: SystemPaastaConfig, ) -> Sequence[V1Container]: service_container = V1Container( image=self.get_docker_url(), command=self.get_cmd(), args=self.get_args(), env=self.get_container_env(), lifecycle=V1Lifecycle( pre_stop=V1Handler( _exec=V1ExecAction( command=[ "/bin/sh", "-c", "sleep 30", ], ), ), ), name="{service}-{instance}".format( service=self.get_sanitised_service_name(), instance=self.get_sanitised_instance_name(), ), liveness_probe=V1Probe( failure_threshold=10, http_get=V1HTTPGetAction( path="/status", port=8888, ), initial_delay_seconds=15, period_seconds=10, timeout_seconds=5, ), ports=[ V1ContainerPort( container_port=8888, ), ], volume_mounts=self.get_volume_mounts(volumes=volumes), ) containers = [service_container] + self.get_sidecar_containers(system_paasta_config=system_paasta_config) return containers
def test_get_kubernetes_containers(self): with mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_docker_url', autospec=True, ) as mock_get_docker_url, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_cmd', autospec=True, ) as mock_get_cmd, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_args', autospec=True, ) as mock_get_args, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_container_env', autospec=True, ) as mock_get_container_env, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_sanitised_service_name', autospec=True, return_value='kurupt', ), mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_sanitised_instance_name', autospec=True, return_value='fm', ), mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_volume_mounts', autospec=True, ) as mock_get_volume_mounts, mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_sidecar_containers', autospec=True, return_value=['mock_sidecar'], ): mock_system_config = mock.Mock() mock_docker_volumes: Sequence[DockerVolume] = [] mock_aws_ebs_volumes: Sequence[AwsEbsVolume] = [] expected = [ V1Container( args=mock_get_args.return_value, command=mock_get_cmd.return_value, env=mock_get_container_env.return_value, image=mock_get_docker_url.return_value, lifecycle=V1Lifecycle(pre_stop=V1Handler( _exec=V1ExecAction(command=[ '/bin/sh', '-c', 'sleep 30', ], ), ), ), liveness_probe=V1Probe( failure_threshold=10, http_get=V1HTTPGetAction( path='/status', port=8888, ), initial_delay_seconds=15, period_seconds=10, timeout_seconds=5, ), name='kurupt-fm', ports=[V1ContainerPort(container_port=8888)], volume_mounts=mock_get_volume_mounts.return_value, ), 'mock_sidecar', ] assert self.deployment.get_kubernetes_containers( docker_volumes=mock_docker_volumes, system_paasta_config=mock_system_config, aws_ebs_volumes=mock_aws_ebs_volumes, ) == expected
def test_get_sidecar_containers(self): with mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_registrations', autospec=True, return_value=['universal.credit'], ), mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_kubernetes_environment', autospec=True, return_value={}, ), mock.patch( 'paasta_tools.kubernetes_tools.KubernetesDeploymentConfig.get_sanitised_volume_name', autospec=True, return_value='sane-name', ): mock_system_config = mock.Mock( get_enable_nerve_readiness_check=mock.Mock(return_value=False), get_nerve_readiness_check_script=mock.Mock( return_value='/nail/blah.sh'), get_hacheck_sidecar_image_url=mock.Mock( return_value='some-docker-image'), ) ret = self.deployment.get_sidecar_containers(mock_system_config) expected = [ V1Container( env={}, image='some-docker-image', lifecycle=V1Lifecycle(pre_stop=V1Handler( _exec=V1ExecAction(command=[ '/bin/sh', '-c', '/usr/bin/hadown ' 'universal.credit; sleep ' '31', ], ), ), ), name='hacheck', ports=[V1ContainerPort(container_port=6666)], ), ] assert ret == expected mock_system_config = mock.Mock( get_enable_nerve_readiness_check=mock.Mock(return_value=True), get_nerve_readiness_check_script=mock.Mock( return_value='/nail/blah.sh'), get_hacheck_sidecar_image_url=mock.Mock( return_value='some-docker-image'), ) ret = self.deployment.get_sidecar_containers(mock_system_config) expected = [ V1Container( env={}, image='some-docker-image', lifecycle=V1Lifecycle(pre_stop=V1Handler( _exec=V1ExecAction(command=[ '/bin/sh', '-c', '/usr/bin/hadown ' 'universal.credit; sleep ' '31', ], ), ), ), name='hacheck', ports=[V1ContainerPort(container_port=6666)], readiness_probe=V1Probe( _exec=V1ExecAction( command=['/nail/blah.sh', 'universal.credit'], ), initial_delay_seconds=10, period_seconds=10, ), ), ] assert ret == expected