Skip to content

Commit

Permalink
Merge pull request #381 from camptocamp/add-supplemental-groups
Browse files Browse the repository at this point in the history
feat: copy securitycontext from manager to agents
  • Loading branch information
Vampouille authored Sep 26, 2023
2 parents 577505d + 7cc1f46 commit e9f13e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.5.1
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISg
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
30 changes: 30 additions & 0 deletions pkg/orchestrators/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ func (o *KubernetesOrchestrator) DeployAgent(image string, cmd, envs []string, v
node = ""
}

managerPod, err := o.getManagerPod()
if err != nil {
err = fmt.Errorf("failed to create agent: %s", err)
}

pod, err := o.client.CoreV1().Pods(v.Namespace).Create(&apiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "bivac-agent-",
Expand All @@ -219,6 +224,10 @@ func (o *KubernetesOrchestrator) DeployAgent(image string, cmd, envs []string, v
RestartPolicy: "Never",
Volumes: kvs,
ServiceAccountName: o.config.AgentServiceAccount,
SecurityContext: &apiv1.PodSecurityContext{
SupplementalGroups: managerPod.Spec.SecurityContext.SupplementalGroups,
},

Containers: []apiv1.Container{
{
Name: "bivac-agent",
Expand Down Expand Up @@ -615,3 +624,24 @@ func (o *KubernetesOrchestrator) getAgentAnnotations() map[string]string {
}
return agentAnnotations
}

func (o *KubernetesOrchestrator) getManagerPod() (pod *apiv1.Pod, err error) {
podName := os.Getenv("HOSTNAME")

// get the namespace
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
namespace, _, err := kubeconfig.Namespace()
if err != nil {
err = fmt.Errorf("failed to get namespace: %v", err)
return
}

pod, err = o.client.CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{})
if err != nil {
err = fmt.Errorf("failed to get manager pod: %s", err)
}
return
}

0 comments on commit e9f13e0

Please sign in to comment.