Switch to use TypedLocalObjectReference in core API

This commit is contained in:
Xing Yang
2018-08-30 06:23:03 -07:00
parent d50a9a9a66
commit c422fbde7c
6 changed files with 31 additions and 45 deletions

View File

@@ -71,7 +71,7 @@ type VolumeSnapshotSpec struct {
// In Alpha version, only PersistentVolumeClaim is supported as the source. // In Alpha version, only PersistentVolumeClaim is supported as the source.
// If not specified, user can create VolumeSnapshotContent and bind it with VolumeSnapshot manually. // If not specified, user can create VolumeSnapshotContent and bind it with VolumeSnapshot manually.
// +optional // +optional
Source *TypedLocalObjectReference `json:"source" protobuf:"bytes,1,opt,name=source"` Source *core_v1.TypedLocalObjectReference `json:"source" protobuf:"bytes,1,opt,name=source"`
// SnapshotContentName binds the VolumeSnapshot object with the VolumeSnapshotContent // SnapshotContentName binds the VolumeSnapshot object with the VolumeSnapshotContent
// +optional // +optional
@@ -110,15 +110,6 @@ type VolumeSnapshotStatus struct {
Error *storage.VolumeError `json:"error,omitempty" protobuf:"bytes,4,opt,name=error,casttype=VolumeError"` Error *storage.VolumeError `json:"error,omitempty" protobuf:"bytes,4,opt,name=error,casttype=VolumeError"`
} }
// TypedLocalObjectReference contains enough information to let you locate the typed referenced object inside the same namespace.
// TODO: After TypedLocalObjectReference is merged into the in-tree core API, this will be replaced.
type TypedLocalObjectReference struct {
// Name of the referent.
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`
// Kind of the referent.
Kind string `json:"kind,omitempty" protobuf:"bytes,2,opt,name=kind"`
}
// +genclient // +genclient
// +genclient:nonNamespaced // +genclient:nonNamespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

View File

@@ -52,22 +52,6 @@ func (in *CSIVolumeSnapshotSource) DeepCopy() *CSIVolumeSnapshotSource {
return out return out
} }
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TypedLocalObjectReference) DeepCopyInto(out *TypedLocalObjectReference) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TypedLocalObjectReference.
func (in *TypedLocalObjectReference) DeepCopy() *TypedLocalObjectReference {
if in == nil {
return nil
}
out := new(TypedLocalObjectReference)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *VolumeSnapshot) DeepCopyInto(out *VolumeSnapshot) { func (in *VolumeSnapshot) DeepCopyInto(out *VolumeSnapshot) {
*out = *in *out = *in
@@ -313,8 +297,8 @@ func (in *VolumeSnapshotSpec) DeepCopyInto(out *VolumeSnapshotSpec) {
*out = *in *out = *in
if in.Source != nil { if in.Source != nil {
in, out := &in.Source, &out.Source in, out := &in.Source, &out.Source
*out = new(TypedLocalObjectReference) *out = new(v1.TypedLocalObjectReference)
**out = **in (*in).DeepCopyInto(*out)
} }
if in.VolumeSnapshotClassName != nil { if in.VolumeSnapshotClassName != nil {
in, out := &in.VolumeSnapshotClassName, &out.VolumeSnapshotClassName in, out := &in.VolumeSnapshotClassName, &out.VolumeSnapshotClassName

View File

@@ -24,15 +24,14 @@ import (
runtime "k8s.io/apimachinery/pkg/runtime" runtime "k8s.io/apimachinery/pkg/runtime"
schema "k8s.io/apimachinery/pkg/runtime/schema" schema "k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer" serializer "k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
) )
var scheme = runtime.NewScheme() var scheme = runtime.NewScheme()
var codecs = serializer.NewCodecFactory(scheme) var codecs = serializer.NewCodecFactory(scheme)
var parameterCodec = runtime.NewParameterCodec(scheme) var parameterCodec = runtime.NewParameterCodec(scheme)
var localSchemeBuilder = runtime.SchemeBuilder{
func init() { volumesnapshotv1alpha1.AddToScheme,
v1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(scheme)
} }
// AddToScheme adds all types of this clientset into the given scheme. This allows composition // AddToScheme adds all types of this clientset into the given scheme. This allows composition
@@ -45,10 +44,13 @@ func init() {
// ) // )
// //
// kclientset, _ := kubernetes.NewForConfig(c) // kclientset, _ := kubernetes.NewForConfig(c)
// aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) // _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
// //
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
// correctly. // correctly.
func AddToScheme(scheme *runtime.Scheme) { var AddToScheme = localSchemeBuilder.AddToScheme
volumesnapshotv1alpha1.AddToScheme(scheme)
func init() {
v1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})
utilruntime.Must(AddToScheme(scheme))
} }

View File

@@ -24,15 +24,14 @@ import (
runtime "k8s.io/apimachinery/pkg/runtime" runtime "k8s.io/apimachinery/pkg/runtime"
schema "k8s.io/apimachinery/pkg/runtime/schema" schema "k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer" serializer "k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
) )
var Scheme = runtime.NewScheme() var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme) var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme) var ParameterCodec = runtime.NewParameterCodec(Scheme)
var localSchemeBuilder = runtime.SchemeBuilder{
func init() { volumesnapshotv1alpha1.AddToScheme,
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
AddToScheme(Scheme)
} }
// AddToScheme adds all types of this clientset into the given scheme. This allows composition // AddToScheme adds all types of this clientset into the given scheme. This allows composition
@@ -45,10 +44,13 @@ func init() {
// ) // )
// //
// kclientset, _ := kubernetes.NewForConfig(c) // kclientset, _ := kubernetes.NewForConfig(c)
// aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme) // _ = aggregatorclientsetscheme.AddToScheme(clientsetscheme.Scheme)
// //
// After this, RawExtensions in Kubernetes types will serialize kube-aggregator types // After this, RawExtensions in Kubernetes types will serialize kube-aggregator types
// correctly. // correctly.
func AddToScheme(scheme *runtime.Scheme) { var AddToScheme = localSchemeBuilder.AddToScheme
volumesnapshotv1alpha1.AddToScheme(scheme)
func init() {
v1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
utilruntime.Must(AddToScheme(Scheme))
} }

View File

@@ -806,7 +806,7 @@ func newSnapshot(name, className, boundToContent, snapshotUID, claimName string,
SelfLink: "/apis/snapshot.storage.k8s.io/v1alpha1/namespaces/" + testNamespace + "/volumesnapshots/" + name, SelfLink: "/apis/snapshot.storage.k8s.io/v1alpha1/namespaces/" + testNamespace + "/volumesnapshots/" + name,
}, },
Spec: crdv1.VolumeSnapshotSpec{ Spec: crdv1.VolumeSnapshotSpec{
Source: &crdv1.TypedLocalObjectReference{ Source: &v1.TypedLocalObjectReference{
Name: claimName, Name: claimName,
Kind: "PersistentVolumeClaim", Kind: "PersistentVolumeClaim",
}, },

View File

@@ -77,6 +77,7 @@ import (
// In the future version, a retry policy will be added. // In the future version, a retry policy will be added.
const pvcKind = "PersistentVolumeClaim" const pvcKind = "PersistentVolumeClaim"
const apiGroup = ""
const controllerUpdateFailMsg = "snapshot controller failed to update" const controllerUpdateFailMsg = "snapshot controller failed to update"
const IsDefaultSnapshotClassAnnotation = "snapshot.storage.kubernetes.io/is-default-class" const IsDefaultSnapshotClassAnnotation = "snapshot.storage.kubernetes.io/is-default-class"
@@ -824,13 +825,19 @@ func (ctrl *csiSnapshotController) SetDefaultSnapshotClass(snapshot *crdv1.Volum
// getClaimFromVolumeSnapshot is a helper function to get PVC from VolumeSnapshot. // getClaimFromVolumeSnapshot is a helper function to get PVC from VolumeSnapshot.
func (ctrl *csiSnapshotController) getClaimFromVolumeSnapshot(snapshot *crdv1.VolumeSnapshot) (*v1.PersistentVolumeClaim, error) { func (ctrl *csiSnapshotController) getClaimFromVolumeSnapshot(snapshot *crdv1.VolumeSnapshot) (*v1.PersistentVolumeClaim, error) {
if snapshot.Spec.Source == nil || snapshot.Spec.Source.Kind != pvcKind { if snapshot.Spec.Source == nil {
return nil, fmt.Errorf("The snapshot source is not the right type. Expected %s, Got %v", pvcKind, snapshot.Spec.Source) return nil, fmt.Errorf("the snapshot source is not specified.")
}
if snapshot.Spec.Source.Kind != pvcKind {
return nil, fmt.Errorf("the snapshot source is not the right type. Expected %s, Got %v", pvcKind, snapshot.Spec.Source.Kind)
} }
pvcName := snapshot.Spec.Source.Name pvcName := snapshot.Spec.Source.Name
if pvcName == "" { if pvcName == "" {
return nil, fmt.Errorf("the PVC name is not specified in snapshot %s", snapshotKey(snapshot)) return nil, fmt.Errorf("the PVC name is not specified in snapshot %s", snapshotKey(snapshot))
} }
if snapshot.Spec.Source.APIGroup != nil && *(snapshot.Spec.Source.APIGroup) != apiGroup {
return nil, fmt.Errorf("the snapshot source does not have the right APIGroup. Expected empty string, Got %s", *(snapshot.Spec.Source.APIGroup))
}
pvc, err := ctrl.client.CoreV1().PersistentVolumeClaims(snapshot.Namespace).Get(pvcName, metav1.GetOptions{}) pvc, err := ctrl.client.CoreV1().PersistentVolumeClaims(snapshot.Namespace).Get(pvcName, metav1.GetOptions{})
if err != nil { if err != nil {