-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ephemeral and PV backed volumes.
- Loading branch information
Showing
6 changed files
with
167 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Copyright 2024 Google LLC | ||
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 | ||
https://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 webhook | ||
|
||
import ( | ||
"errors" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func (si *SidecarInjector) IsPreprovisionCSIVolume(csiDriver string, pvc *corev1.PersistentVolumeClaim) (bool, *corev1.PersistentVolume, error) { | ||
// IsPreprovisionCSIVolume checks whether the volume is a pre-provisioned volume for the desired csiDriver. | ||
if csiDriver == "" { | ||
return false, nil, errors.New("failed to check IsPreprovisionCSIVolume, csiDriver is empty") | ||
} | ||
|
||
if pvc == nil { | ||
return false, nil, errors.New("failed to check IsPreprovisionCSIVolume, pvsi is nil") | ||
} | ||
|
||
if pvc.Spec.VolumeName == "" { | ||
return false, nil, nil | ||
} | ||
|
||
// GetPV returns an error if pvc.Spec.VolumeName is not empty and the associated PV object is not found in the API server. | ||
pv, err := si.GetPV(pvc.Spec.VolumeName) | ||
if err != nil { | ||
return false, nil, err // no additional context needed for error. | ||
} | ||
|
||
if pv.Spec.CSI == nil { | ||
return false, nil, nil | ||
} | ||
|
||
// PV is using dynamic mounting. See details: https://cloud.google.com/storage/docs/gcsfuse-mount#dynamic-mount | ||
if pv.Spec.CSI.VolumeHandle == "_" { | ||
return false, nil, nil | ||
} | ||
|
||
if pv.Spec.CSI.Driver == csiDriver { | ||
return true, pv, nil | ||
} | ||
|
||
// Returns false when PV - PVC pair was created for a different csi driver or different storage type. | ||
return false, nil, nil | ||
} | ||
|
||
func (si *SidecarInjector) GetPV(name string) (*corev1.PersistentVolume, error) { | ||
pv, err := si.PvLister.Get(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pv, nil | ||
} | ||
|
||
func (si *SidecarInjector) GetPVC(namespace, name string) (*corev1.PersistentVolumeClaim, error) { | ||
pvc, err := si.PvcLister.PersistentVolumeClaims(namespace).Get(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pvc, nil | ||
} |
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,64 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Copyright 2022 Google LLC | ||
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 | ||
https://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 webhook | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
gcsFuseCsiDriverName = "gcsfuse.csi.storage.gke.io" | ||
) | ||
|
||
func (si *SidecarInjector) isGcsFuseCSIVolume(volume corev1.Volume, namespace string) (bool, map[string]string, error) { | ||
// Check if it is ephemeral volume. | ||
if volume.CSI != nil { | ||
if volume.CSI.Driver == gcsFuseCsiDriverName { | ||
return true, volume.CSI.VolumeAttributes, nil | ||
} | ||
|
||
return false, nil, nil | ||
} | ||
|
||
// Check if it's a persistent volume. | ||
pvc := volume.PersistentVolumeClaim | ||
if pvc == nil { | ||
return false, nil, nil | ||
} | ||
pvcName := pvc.ClaimName | ||
pvcObj, err := si.GetPVC(namespace, pvcName) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
// Check if the PVC is a preprovisioned parallelstore volume. | ||
isPreprovisionGcsFuseCsi, pv, err := si.IsPreprovisionCSIVolume(gcsFuseCsiDriverName, pvcObj) | ||
if err != nil { | ||
klog.Warningf("unable to determine if PVC %s/%s is a pre-provisioned gcsfuse volume: %v", namespace, pvcName, err) | ||
|
||
return false, nil, nil | ||
} | ||
if isPreprovisionGcsFuseCsi { | ||
return true, pv.Spec.CSI.VolumeAttributes, nil | ||
} | ||
|
||
klog.Infof("PVC %s/%s is not referring to a pre-provisioned gcsfuse volume", namespace, pvcName) | ||
|
||
return false, nil, nil | ||
} |