From 6ec1ebd6ce65b7b05f195551e89649428d8c2738 Mon Sep 17 00:00:00 2001 From: Xing Yang Date: Fri, 7 Sep 2018 11:07:47 -0700 Subject: [PATCH] Make APIGroup optional and modify validation --- pkg/apis/core/types.go | 7 +++++-- pkg/apis/core/validation/validation.go | 22 +++++++++++++++------ pkg/apis/core/validation/validation_test.go | 2 +- staging/src/k8s.io/api/core/v1/types.go | 7 +++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/pkg/apis/core/types.go b/pkg/apis/core/types.go index 1574a83340f..702ffa4d432 100644 --- a/pkg/apis/core/types.go +++ b/pkg/apis/core/types.go @@ -3976,8 +3976,11 @@ type LocalObjectReference struct { // TypedLocalObjectReference contains enough information to let you locate the typed referenced object inside the same namespace. type TypedLocalObjectReference struct { - // APIGroup is the group for the resource being referenced - APIGroup string + // APIGroup is the group for the resource being referenced. + // If APIGroup is not specified, the specified Kind must be in the core API group. + // For any other third-party types, APIGroup is required. + // +optional + APIGroup *string // Kind is the type of resource being referenced Kind string // Name is the name of resource being referenced diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index 8f2e6db5026..ab81fe45d33 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -36,6 +36,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/sets" @@ -1501,8 +1502,9 @@ var supportedReclaimPolicy = sets.NewString(string(core.PersistentVolumeReclaimD var supportedVolumeModes = sets.NewString(string(core.PersistentVolumeBlock), string(core.PersistentVolumeFilesystem)) -var supportedDataSourceKinds = sets.NewString(string("VolumeSnapshot")) -var supportedDataSourceAPIGroups = sets.NewString(string("snapshot.storage.k8s.io")) +var supportedDataSourceAPIGroupKinds = map[schema.GroupKind]bool{ + {Group: "snapshot.storage.k8s.io", Kind: "VolumeSnapshot"}: true, +} func ValidatePersistentVolume(pv *core.PersistentVolume) field.ErrorList { metaPath := field.NewPath("metadata") @@ -1833,10 +1835,18 @@ func ValidatePersistentVolumeClaimSpec(spec *core.PersistentVolumeClaimSpec, fld } else if spec.DataSource != nil { if len(spec.DataSource.Name) == 0 { allErrs = append(allErrs, field.Required(fldPath.Child("dataSource", "name"), "")) - } else if !supportedDataSourceKinds.Has(string(spec.DataSource.Kind)) { - allErrs = append(allErrs, field.NotSupported(fldPath.Child("dataSource"), spec.DataSource.Kind, supportedDataSourceKinds.List())) - } else if !supportedDataSourceAPIGroups.Has(string(spec.DataSource.APIGroup)) { - allErrs = append(allErrs, field.NotSupported(fldPath.Child("dataSource"), spec.DataSource.APIGroup, supportedDataSourceAPIGroups.List())) + } + + groupKind := schema.GroupKind{Group: "", Kind: spec.DataSource.Kind} + if spec.DataSource.APIGroup != nil { + groupKind.Group = string(*spec.DataSource.APIGroup) + } + groupKindList := make([]string, 0, len(supportedDataSourceAPIGroupKinds)) + for grp := range supportedDataSourceAPIGroupKinds { + groupKindList = append(groupKindList, grp.String()) + } + if !supportedDataSourceAPIGroupKinds[groupKind] { + allErrs = append(allErrs, field.NotSupported(fldPath.Child("dataSource"), groupKind.String(), groupKindList)) } } diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 7a5a8b08a91..64c75344277 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -726,7 +726,7 @@ func testVolumeSnapshotDataSourceInSpec(name string, kind string, apiGroup strin }, StorageClassName: &scName, DataSource: &core.TypedLocalObjectReference{ - APIGroup: apiGroup, + APIGroup: &apiGroup, Kind: kind, Name: name, }, diff --git a/staging/src/k8s.io/api/core/v1/types.go b/staging/src/k8s.io/api/core/v1/types.go index a316657dc8e..1ed9377244d 100644 --- a/staging/src/k8s.io/api/core/v1/types.go +++ b/staging/src/k8s.io/api/core/v1/types.go @@ -4495,8 +4495,11 @@ type LocalObjectReference struct { // TypedLocalObjectReference contains enough information to let you locate the // typed referenced object inside the same namespace. type TypedLocalObjectReference struct { - // APIGroup is the group for the resource being referenced - APIGroup string `json:"apiGroup" protobuf:"bytes,1,opt,name=apiGroup"` + // APIGroup is the group for the resource being referenced. + // If APIGroup is not specified, the specified Kind must be in the core API group. + // For any other third-party types, APIGroup is required. + // +optional + APIGroup *string `json:"apiGroup" protobuf:"bytes,1,opt,name=apiGroup"` // Kind is the type of resource being referenced Kind string `json:"kind" protobuf:"bytes,2,opt,name=kind"` // Name is the name of resource being referenced