flinkk8soperator icon indicating copy to clipboard operation
flinkk8soperator copied to clipboard

Support securityContext settings in jobManager and taskManager spec

Open gdasbach opened this issue 5 years ago • 8 comments

It would be great if the jobManager and taskManager specs in the CRD supported setting other Pod specs, like securityContext. For some environments there are requirements to runAsNonRoot or running as a specific uid. Since the operator handles the pod and container creation, there is no way at the moment to provide those specs to the generated objects.

gdasbach avatar Nov 13 '19 14:11 gdasbach

@gdasbach Correct me if I am wrong, securityContext can be only applied to pods right? The Flink operator only interacts with deployment objects. In this case you will have to implement some sort of MutatingAdmissionWebhook(https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/) to inject into the pods

anandswaminathan avatar Nov 21 '19 23:11 anandswaminathan

@anandswaminathan The securitycontext settings can be placed in the PodSpec or the ContainerSpec. The deployments created in the job_manager_controller and task_manager_controller contain the PodSpec and reference the ContainerSpec created in the templating functions. Essentially it would mean adding the security context settings to the FlinkApplication CRD spec and then translating those into either the PodSpec or ContainerSpec objects when they are created by the controllers.

gdasbach avatar Nov 26 '19 17:11 gdasbach

Following up on this, I agree with @gdasbach that this should be a trivial change here:

https://github.com/lyft/flinkk8soperator/blob/master/pkg/controller/flink/job_manager_controller.go#L329

and here:

https://github.com/lyft/flinkk8soperator/blob/master/pkg/controller/flink/task_manager_controller.go#L205

I think the main decision that would need to be made is how to most correctly add this behavior to the CRD spec. I see two options:

  1. Specify a single, top-level security context (similar to how the serviceAccountName is being done today and apply it uniformly to both Task and Job managers
  2. JobManager and TaskManager configs each accepts a separate security context.

@anandswaminathan, do you have any thoughts on this? It seems that this might be a breaking change per the warning in the controllers, but I'm not sure. Would you be willing to take this change if I worked on it?

kelly-sm avatar Jan 13 '20 21:01 kelly-sm

@kelly-sm I feel we can proceed with option (1).

cc @mwylde @glaksh100

anandswaminathan avatar Jan 13 '20 22:01 anandswaminathan

Sounds good, I'll start putting together a pull request. Here's my plan to make sure we're on the same page:

v1beta1/types.go

type FlinkApplicationSpec struct {
	Image              string                       `json:"image,omitempty" protobuf:"bytes,2,opt,name=image"`
	ImagePullPolicy    apiv1.PullPolicy             `json:"imagePullPolicy,omitempty" protobuf:"bytes,14,opt,name=imagePullPolicy,casttype=PullPolicy"`
	ImagePullSecrets   []apiv1.LocalObjectReference `json:"imagePullSecrets,omitempty" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,15,rep,name=imagePullSecrets"`
        // ADD
	SecurityContext    apiv1.PodSecurityContext			`json:"securityContext,omitempty"`

controller/flink/job_manager_controller.go and controller/flink/task_manager_controller.go

Spec: coreV1.PodSpec{
	Containers: []coreV1.Container{
		*jobManagerContainer,
	},
	SecurityContext: 	&app.Spec.SecurityContext, // <- ADD
	Volumes:          app.Spec.Volumes,
	ImagePullSecrets: app.Spec.ImagePullSecrets,
	NodeSelector:     app.Spec.JobManagerConfig.NodeSelector,
},

Seems to work so far.

kelly-sm avatar Jan 13 '20 23:01 kelly-sm

Here's my PR: https://github.com/lyft/flinkk8soperator/pull/158

kelly-sm avatar Jan 14 '20 21:01 kelly-sm

I just verified that this is working in my production environment for: docker.io/lyft/flinkk8soperator:f8b0a121eae65a33bda0231b9d4f77171080918d

This issue can likely be closed, though I don't believe this change made it into the 0.4.0 release.

kelly-sm avatar Jan 18 '20 00:01 kelly-sm

The changes did not make it into the 0.4.0 release. Using the container referenced above worked.

jdp1 avatar Mar 09 '20 13:03 jdp1