Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit afd01c0

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#57561 from dims/enable-privileged-container-for-apiserver-and-controller
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Enable privileged containers for apiserver and controller **What this PR does / why we need it**: In OpenStack environment, when there is no metadata service, we look at the config drive to figure out the metadata. Since we need to run commands like blkid, we need to ensure that api server and kube controller are running in the privileged mode. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes kubernetes#47392 Fixes kubernetes/kubeadm#588 **Special notes for your reviewer**: **Release note**: ```release-note Fix issue when using OpenStack config drive for node metadata ```
2 parents 8cc5ccf + 658a27c commit afd01c0

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

cmd/kubeadm/app/apis/kubeadm/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ type MasterConfiguration struct {
3939
NodeName string
4040
AuthorizationModes []string
4141

42+
// Mark the controller and api server pods as privileged as some cloud
43+
// controllers like openstack need escalated privileges under some conditions
44+
// example - loading a config drive to fetch node information
45+
PrivilegedPods bool
46+
4247
Token string
4348
TokenTTL *metav1.Duration
4449

cmd/kubeadm/app/apis/kubeadm/v1alpha1/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ type MasterConfiguration struct {
3939
NodeName string `json:"nodeName"`
4040
AuthorizationModes []string `json:"authorizationModes,omitempty"`
4141

42+
// Mark the controller and api server pods as privileged as some cloud
43+
// controllers like openstack need escalated privileges under some conditions
44+
// example - loading a config drive to fetch node information
45+
PrivilegedPods bool `json:"privilegedPods"`
46+
4247
Token string `json:"token"`
4348
TokenTTL *metav1.Duration `json:"tokenTTL,omitempty"`
4449

cmd/kubeadm/app/apis/kubeadm/v1alpha1/zz_generated.conversion.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/kubeadm/app/cmd/upgrade/common_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestPrintConfiguration(t *testing.T) {
5959
podSubnet: ""
6060
serviceSubnet: ""
6161
nodeName: ""
62+
privilegedPods: false
6263
token: ""
6364
unifiedControlPlaneImage: ""
6465
`),
@@ -92,6 +93,7 @@ func TestPrintConfiguration(t *testing.T) {
9293
podSubnet: ""
9394
serviceSubnet: 10.96.0.1/12
9495
nodeName: ""
96+
privilegedPods: false
9597
token: ""
9698
unifiedControlPlaneImage: ""
9799
`),
@@ -135,6 +137,7 @@ func TestPrintConfiguration(t *testing.T) {
135137
podSubnet: ""
136138
serviceSubnet: ""
137139
nodeName: ""
140+
privilegedPods: false
138141
token: ""
139142
unifiedControlPlaneImage: ""
140143
`),

cmd/kubeadm/app/phases/controlplane/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ go_library(
4444
"//cmd/kubeadm/app/util/staticpod:go_default_library",
4545
"//pkg/kubeapiserver/authorizer/modes:go_default_library",
4646
"//pkg/master/reconcilers:go_default_library",
47+
"//pkg/util/pointer:go_default_library",
4748
"//pkg/util/version:go_default_library",
4849
"//vendor/k8s.io/api/core/v1:go_default_library",
4950
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",

cmd/kubeadm/app/phases/controlplane/manifests.go

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
staticpodutil "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod"
3636
authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
3737
"k8s.io/kubernetes/pkg/master/reconcilers"
38+
utilpointer "k8s.io/kubernetes/pkg/util/pointer"
3839
"k8s.io/kubernetes/pkg/util/version"
3940
)
4041

@@ -104,6 +105,18 @@ func GetStaticPodSpecs(cfg *kubeadmapi.MasterConfiguration, k8sVersion *version.
104105
}, mounts.GetVolumes(kubeadmconstants.KubeScheduler)),
105106
}
106107

108+
// Some cloud providers need extra privileges for example to load node information from a config drive
109+
// TODO: when we fully to external cloud providers and the api server and controller manager do not need
110+
// to call out to cloud provider code, we can remove the support for the PrivilegedPods
111+
if cfg.PrivilegedPods {
112+
staticPodSpecs[kubeadmconstants.KubeAPIServer].Spec.Containers[0].SecurityContext = &v1.SecurityContext{
113+
Privileged: utilpointer.BoolPtr(true),
114+
}
115+
staticPodSpecs[kubeadmconstants.KubeControllerManager].Spec.Containers[0].SecurityContext = &v1.SecurityContext{
116+
Privileged: utilpointer.BoolPtr(true),
117+
}
118+
}
119+
107120
return staticPodSpecs
108121
}
109122

cmd/kubeadm/app/phases/controlplane/manifests_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,58 @@ func TestCreateStaticPodFilesAndWrappers(t *testing.T) {
135135
}
136136
}
137137

138+
func TestCreatePrivilegedContainerForOpenStack(t *testing.T) {
139+
// Creates a Master Configuration with OpenStack cloud provider
140+
var staticPodNames = []string{
141+
kubeadmconstants.KubeAPIServer,
142+
kubeadmconstants.KubeControllerManager,
143+
}
144+
var assertions = []struct {
145+
cloudProvider string
146+
privilegedPods bool
147+
expectedPrivilege bool
148+
}{
149+
{
150+
cloudProvider: "",
151+
expectedPrivilege: false,
152+
},
153+
{
154+
cloudProvider: "aws",
155+
expectedPrivilege: false,
156+
},
157+
{
158+
cloudProvider: "openstack",
159+
privilegedPods: true,
160+
expectedPrivilege: true,
161+
},
162+
}
163+
164+
for _, assertion := range assertions {
165+
cfg := &kubeadmapi.MasterConfiguration{
166+
KubernetesVersion: "v1.9.0",
167+
CloudProvider: assertion.cloudProvider,
168+
PrivilegedPods: assertion.privilegedPods,
169+
}
170+
171+
k8sVersion, _ := version.ParseSemantic(cfg.KubernetesVersion)
172+
specs := GetStaticPodSpecs(cfg, k8sVersion)
173+
174+
for _, podname := range staticPodNames {
175+
spec, _ := specs[podname]
176+
sc := spec.Spec.Containers[0].SecurityContext
177+
if assertion.expectedPrivilege == true {
178+
if sc == nil || sc.Privileged == nil || *sc.Privileged == false {
179+
t.Errorf("GetStaticPodSpecs did not enable privileged containers in %s pod for provider %s", podname, assertion.cloudProvider)
180+
}
181+
} else {
182+
if sc != nil && sc.Privileged != nil && *sc.Privileged == true {
183+
t.Errorf("GetStaticPodSpecs enabled privileged containers in %s pod for provider %s", podname, assertion.cloudProvider)
184+
}
185+
}
186+
}
187+
}
188+
}
189+
138190
func TestGetAPIServerCommand(t *testing.T) {
139191
var tests = []struct {
140192
cfg *kubeadmapi.MasterConfiguration

0 commit comments

Comments
 (0)