-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60516 from joelsmith/automated-cherry-pick-of-#58…
…720-#60342-upstream-release-1.7 Automatic merge from submit-queue. [1.7] Automated cherry pick of #58720 #60342 Cherry pick of #58720 #60342 on release-1.7. #58720: Ensure that the runtime mounts RO volumes read-only #60342: Fix nested volume mounts for read-only API data volumes ```release-note Containers now mount secret, configMap, downwardAPI and projected volumes read-only. Previously, container modifications to files in these types of volumes were temporary and reverted by the kubelet during volume sync. Until version 1.11, setting the feature gate ReadOnlyAPIDataVolumes=false will preserve the old behavior. ```
- Loading branch information
Showing
11 changed files
with
399 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/kubernetes/pkg/api/v1" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// getNestedMountpoints returns a list of mountpoint directories that should be created | ||
// for the volume indicated by name. | ||
// note: the returned list is relative to baseDir | ||
func getNestedMountpoints(name, baseDir string, pod v1.Pod) ([]string, error) { | ||
var retval []string | ||
checkContainer := func(container *v1.Container) error { | ||
var allMountPoints []string // all mount points in this container | ||
var myMountPoints []string // mount points that match name | ||
for _, vol := range container.VolumeMounts { | ||
cleaned := filepath.Clean(vol.MountPath) | ||
allMountPoints = append(allMountPoints, cleaned) | ||
if vol.Name == name { | ||
myMountPoints = append(myMountPoints, cleaned) | ||
} | ||
} | ||
sort.Strings(allMountPoints) | ||
parentPrefix := ".." + string(os.PathSeparator) | ||
// Examine each place where this volume is mounted | ||
for _, myMountPoint := range myMountPoints { | ||
if strings.HasPrefix(myMountPoint, parentPrefix) { | ||
// Don't let a container trick us into creating directories outside of its rootfs | ||
return fmt.Errorf("Invalid container mount point %v", myMountPoint) | ||
} | ||
myMPSlash := myMountPoint + string(os.PathSeparator) | ||
// The previously found nested mountpoint (or "" if none found yet) | ||
prevNestedMP := "" | ||
// examine each mount point to see if it's nested beneath this volume | ||
// (but skip any that are double-nested beneath this volume) | ||
// For example, if this volume is mounted as /dir and other volumes are mounted | ||
// as /dir/nested and /dir/nested/other, only create /dir/nested. | ||
for _, mp := range allMountPoints { | ||
if !strings.HasPrefix(mp, myMPSlash) { | ||
continue // skip -- not nested beneath myMountPoint | ||
} | ||
if prevNestedMP != "" && strings.HasPrefix(mp, prevNestedMP) { | ||
continue // skip -- double nested beneath myMountPoint | ||
} | ||
// since this mount point is nested, remember it so that we can check that following ones aren't nested beneath this one | ||
prevNestedMP = mp + string(os.PathSeparator) | ||
retval = append(retval, mp[len(myMPSlash):]) | ||
} | ||
} | ||
return nil | ||
} | ||
for _, container := range pod.Spec.InitContainers { | ||
if err := checkContainer(&container); err != nil { | ||
return nil, err | ||
} | ||
} | ||
for _, container := range pod.Spec.Containers { | ||
if err := checkContainer(&container); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return retval, nil | ||
} | ||
|
||
// MakeNestedMountpoints creates mount points in baseDir for volumes mounted beneath name | ||
func MakeNestedMountpoints(name, baseDir string, pod v1.Pod) error { | ||
dirs, err := getNestedMountpoints(name, baseDir, pod) | ||
if err != nil { | ||
return err | ||
} | ||
for _, dir := range dirs { | ||
err := os.MkdirAll(path.Join(baseDir, dir), 0755) | ||
if err != nil { | ||
return fmt.Errorf("Unable to create nested volume mountpoints: %v", err) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.