Skip to content

Commit 5d49a62

Browse files
authored
Merge pull request kubernetes#95035 from harche/selinux_kubelet
Add SELinux labels for kubelet on Fedora CoreOS
2 parents 4b59044 + a4cd6f1 commit 5d49a62

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

test/e2e_node/remote/node_e2e.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"os/exec"
2323
"path/filepath"
24+
"regexp"
2425
"strings"
2526
"time"
2627

@@ -101,23 +102,57 @@ func prependMemcgNotificationFlag(args string) string {
101102
return "--kubelet-flags=--kernel-memcg-notification=true " + args
102103
}
103104

104-
// updateOSSpecificKubeletFlags updates the Kubelet args with OS specific
105-
// settings.
106-
func updateOSSpecificKubeletFlags(args, host, workspace string) (string, error) {
107-
output, err := SSH(host, "cat", "/etc/os-release")
105+
// osSpecificActions takes OS specific actions required for the node tests
106+
func osSpecificActions(args, host, workspace string) (string, error) {
107+
output, err := getOSDistribution(host)
108108
if err != nil {
109109
return "", fmt.Errorf("issue detecting node's OS via node's /etc/os-release. Err: %v, Output:\n%s", err, output)
110110
}
111111
switch {
112-
case strings.Contains(output, "ID=gci"), strings.Contains(output, "ID=cos"):
112+
case strings.Contains(output, "fedora"), strings.Contains(output, "rhcos"),
113+
strings.Contains(output, "centos"), strings.Contains(output, "rhel"):
114+
return args, setKubeletSELinuxLabels(host, workspace)
115+
case strings.Contains(output, "gci"), strings.Contains(output, "cos"):
113116
args = prependMemcgNotificationFlag(args)
114117
return prependCOSMounterFlag(args, host, workspace)
115-
case strings.Contains(output, "ID=ubuntu"):
118+
case strings.Contains(output, "ubuntu"):
116119
return prependMemcgNotificationFlag(args), nil
117120
}
118121
return args, nil
119122
}
120123

124+
// setKubeletSELinuxLabels set the appropriate SELinux labels for the
125+
// kubelet on Fedora CoreOS distribution
126+
func setKubeletSELinuxLabels(host, workspace string) error {
127+
cmd := getSSHCommand(" && ",
128+
fmt.Sprintf("/usr/bin/chcon -u system_u -r object_r -t bin_t %s", filepath.Join(workspace, "kubelet")),
129+
fmt.Sprintf("/usr/bin/chcon -u system_u -r object_r -t bin_t %s", filepath.Join(workspace, "e2e_node.test")),
130+
fmt.Sprintf("/usr/bin/chcon -u system_u -r object_r -t bin_t %s", filepath.Join(workspace, "ginkgo")),
131+
fmt.Sprintf("/usr/bin/chcon -u system_u -r object_r -t bin_t %s", filepath.Join(workspace, "mounter")),
132+
fmt.Sprintf("/usr/bin/chcon -R -u system_u -r object_r -t bin_t %s", filepath.Join(workspace, "cni", "bin/")),
133+
)
134+
output, err := SSH(host, "sh", "-c", cmd)
135+
if err != nil {
136+
return fmt.Errorf("Unable to apply SELinux labels. Err: %v, Output:\n%s", err, output)
137+
}
138+
return nil
139+
}
140+
141+
func getOSDistribution(host string) (string, error) {
142+
output, err := SSH(host, "cat", "/etc/os-release")
143+
if err != nil {
144+
return "", fmt.Errorf("issue detecting node's OS via node's /etc/os-release. Err: %v, Output:\n%s", err, output)
145+
}
146+
147+
var re = regexp.MustCompile(`(?m)^ID="?(\w+)"?`)
148+
subMatch := re.FindStringSubmatch(output)
149+
if len(subMatch) > 0 {
150+
return subMatch[1], nil
151+
}
152+
153+
return "", fmt.Errorf("Unable to parse os-release for the host, %s", host)
154+
}
155+
121156
// RunTest runs test on the node.
122157
func (n *NodeE2ERemote) RunTest(host, workspace, results, imageDesc, junitFilePrefix, testArgs, ginkgoArgs, systemSpecName, extraEnvs string, timeout time.Duration) (string, error) {
123158
// Install the cni plugins and add a basic CNI configuration.
@@ -134,7 +169,7 @@ func (n *NodeE2ERemote) RunTest(host, workspace, results, imageDesc, junitFilePr
134169
// Kill any running node processes
135170
cleanupNodeProcesses(host)
136171

137-
testArgs, err := updateOSSpecificKubeletFlags(testArgs, host, workspace)
172+
testArgs, err := osSpecificActions(testArgs, host, workspace)
138173
if err != nil {
139174
return "", err
140175
}

0 commit comments

Comments
 (0)