2015-04-14 20:15:42 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
2015-04-14 20:15:42 +00:00
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 persistent_claim
import (
"fmt"
2015-09-14 09:51:40 +00:00
2015-08-05 22:05:17 +00:00
"github.com/golang/glog"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/types"
"k8s.io/kubernetes/pkg/volume"
2015-04-14 20:15:42 +00:00
)
func ProbeVolumePlugins ( ) [ ] volume . VolumePlugin {
2015-06-29 16:54:43 +00:00
return [ ] volume . VolumePlugin { & persistentClaimPlugin { host : nil } }
2015-04-14 20:15:42 +00:00
}
type persistentClaimPlugin struct {
2015-06-29 16:54:43 +00:00
host volume . VolumeHost
readOnly bool
2015-04-14 20:15:42 +00:00
}
var _ volume . VolumePlugin = & persistentClaimPlugin { }
const (
persistentClaimPluginName = "kubernetes.io/persistent-claim"
)
func ( plugin * persistentClaimPlugin ) Init ( host volume . VolumeHost ) {
plugin . host = host
}
func ( plugin * persistentClaimPlugin ) Name ( ) string {
return persistentClaimPluginName
}
func ( plugin * persistentClaimPlugin ) CanSupport ( spec * volume . Spec ) bool {
2015-08-12 19:11:03 +00:00
return spec . Volume != nil && spec . Volume . PersistentVolumeClaim != nil
2015-04-14 20:15:42 +00:00
}
2015-09-14 09:51:40 +00:00
func ( plugin * persistentClaimPlugin ) NewBuilder ( spec * volume . Spec , pod * api . Pod , opts volume . VolumeOptions ) ( volume . Builder , error ) {
2015-08-12 19:11:03 +00:00
claim , err := plugin . host . GetKubeClient ( ) . PersistentVolumeClaims ( pod . Namespace ) . Get ( spec . Volume . PersistentVolumeClaim . ClaimName )
2015-04-14 20:15:42 +00:00
if err != nil {
2015-08-12 19:11:03 +00:00
glog . Errorf ( "Error finding claim: %+v\n" , spec . Volume . PersistentVolumeClaim . ClaimName )
2015-04-14 20:15:42 +00:00
return nil , err
}
2015-05-13 00:44:29 +00:00
if claim . Spec . VolumeName == "" {
2015-04-25 06:44:37 +00:00
return nil , fmt . Errorf ( "The claim %+v is not yet bound to a volume" , claim . Name )
}
2015-05-13 00:44:29 +00:00
pv , err := plugin . host . GetKubeClient ( ) . PersistentVolumes ( ) . Get ( claim . Spec . VolumeName )
2015-04-14 20:15:42 +00:00
if err != nil {
glog . Errorf ( "Error finding persistent volume for claim: %+v\n" , claim . Name )
return nil , err
}
2015-05-13 00:44:29 +00:00
if pv . Spec . ClaimRef == nil {
glog . Errorf ( "The volume is not yet bound to the claim. Expected to find the bind on volume.Spec.ClaimRef: %+v" , pv )
return nil , err
}
if pv . Spec . ClaimRef . UID != claim . UID {
2015-06-17 21:51:42 +00:00
glog . Errorf ( "Expected volume.Spec.ClaimRef.UID %+v but have %+v" , pv . Spec . ClaimRef . UID , claim . UID )
2015-05-13 00:44:29 +00:00
return nil , err
}
2015-09-14 09:51:40 +00:00
builder , err := plugin . host . NewWrapperBuilder ( volume . NewSpecFromPersistentVolume ( pv , spec . ReadOnly ) , pod , opts )
2015-04-14 20:15:42 +00:00
if err != nil {
glog . Errorf ( "Error creating builder for claim: %+v\n" , claim . Name )
return nil , err
}
return builder , nil
}
2015-06-29 16:54:43 +00:00
func ( plugin * persistentClaimPlugin ) IsReadOnly ( ) bool {
return plugin . readOnly
}
2015-09-14 09:51:40 +00:00
func ( plugin * persistentClaimPlugin ) NewCleaner ( _ string , _ types . UID ) ( volume . Cleaner , error ) {
2015-04-14 20:15:42 +00:00
return nil , fmt . Errorf ( "This will never be called directly. The PV backing this claim has a cleaner. Kubelet uses that cleaner, not this one, when removing orphaned volumes." )
}