odo
odo copied to clipboard
Ability to specify ServiceAccount for `odo dev`
/kind feature
Which functionality do you think we should add?
When running the odo dev
command, I would like to be able to specify the Kubernetes ServiceAccount the pod should run as.
Why is this needed?
Currently, if you use odo dev
the pod will start as the default service account. Some workloads running on Kubernetes need to run as a specific service account in order to have the proper permissions to work properly. One common use case is a Kubernetes Operator.
Notes from the discussion on this in the odo contributors' call:
- This should ideally be addressed at Devfile API level such that a
container
component has aserviceAccount
attribute. - In absence of 1, odo can use
annotations
field incontainer
component so that a service account specified in this field is used by odo to set proper information inside the Pod created for such component.
As part of this Sprint, the team will be investigating how to implement pod-overrides and container-overrides annotations in the Devfile library (as a more generic way that would allow us to support user-defined ServiceAccounts), as discussed in https://github.com/devfile/api/issues/920#issuecomment-1244059075.
Then on the odo
side, it would "just" be a matter of updating the Devfile library version.
Related issue: https://github.com/devfile/api/issues/936
To better estimate the work that needs to be done, we are investigating if there exists some library that could help with merging JSON data.
/assign @feloy
The gojq (github.com/itchyny/gojq) can be used:
package main
import (
"errors"
"fmt"
"github.com/itchyny/gojq"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)
func main() {
pod := corev1.Pod{
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(1001),
},
},
}
unstructuredPod, err := ConvertResourceToUnstructured(&pod)
if err != nil {
panic(err)
}
query, err := gojq.Parse(".spec.securityContext = {\"runAsUser\": 1000, \"runAsGroup\": 3000, \"fsGroup\": 2000}")
if err != nil {
panic(err)
}
iter := query.Run(unstructuredPod)
v, ok := iter.Next()
if !ok {
panic(errors.New("error"))
}
json := v.(map[string]interface{})
ConvertUnstructuredToResource(unstructured.Unstructured{Object: json}, &pod)
fmt.Printf("%v\n", pod)
}
func ConvertUnstructuredToResource(u unstructured.Unstructured, obj interface{}) error {
return runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), obj)
}
func ConvertResourceToUnstructured(obj interface{}) (map[string]interface{}, error) {
return runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
}
https://github.com/devfile/api/blob/fe7c10eaa530b12b19cfb0e22e221e753391304c/pkg/attributes/attributes.go#L14
I've opened https://github.com/devfile/library/pull/155 for container-overrides
part.
TODO (as of Nov-23, 2022):
- [ ] Continue work on
container-overrides
PR above: https://github.com/devfile/library/pull/155 - [ ] PR for supporting
pod-overrides