Merge pull request #54607 from andyzhangx/azure-readonly-fix

Automatic merge from submit-queue (batch tested with PRs 54593, 54607, 54539, 54105). 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>.

fix azure pv crash due to volumeSource.ReadOnly value nil

**What this PR does / why we need it**:
kubelet in agent would crash due to volumeSource.ReadOnly is nil in some condition 

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #54149

**Special notes for your reviewer**:
#54149 is the issue: volumeSource.ReadOnly is nil, make kubelet in azure agent node crash.
"volumeSource.ReadOnly is nil" could be regarded as `false` value
@rootfs 

**Release note**:

```
fix azure pv crash due to volumeSource.ReadOnly value nil
```

/sig azure
pull/6/head
Kubernetes Submit Queue 2017-10-26 11:13:36 -07:00 committed by GitHub
commit 189dfe0577
1 changed files with 9 additions and 5 deletions

View File

@ -43,10 +43,14 @@ var _ volume.Unmounter = &azureDiskUnmounter{}
var _ volume.Mounter = &azureDiskMounter{}
func (m *azureDiskMounter) GetAttributes() volume.Attributes {
volumeSource, _ := getVolumeSource(m.spec)
readOnly := false
volumeSource, err := getVolumeSource(m.spec)
if err != nil && volumeSource.ReadOnly != nil {
readOnly = *volumeSource.ReadOnly
}
return volume.Attributes{
ReadOnly: *volumeSource.ReadOnly,
Managed: !*volumeSource.ReadOnly,
ReadOnly: readOnly,
Managed: !readOnly,
SupportsSELinux: true,
}
}
@ -94,7 +98,7 @@ func (m *azureDiskMounter) SetUpAt(dir string, fsGroup *int64) error {
options := []string{"bind"}
if *volumeSource.ReadOnly {
if volumeSource.ReadOnly != nil && *volumeSource.ReadOnly {
options = append(options, "ro")
}
@ -138,7 +142,7 @@ func (m *azureDiskMounter) SetUpAt(dir string, fsGroup *int64) error {
return mountErr
}
if !*volumeSource.ReadOnly {
if volumeSource.ReadOnly == nil || !*volumeSource.ReadOnly {
volume.SetVolumeOwnership(m, fsGroup)
}