mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Change SecretVolumeSource to use a secret name instead of ObjRef
This commit is contained in:
parent
1dc7bcf53b
commit
e40ba109a1
@ -265,8 +265,8 @@ type GitRepoVolumeSource struct {
|
|||||||
// The contents of the target Secret's Data field will be presented in a volume
|
// The contents of the target Secret's Data field will be presented in a volume
|
||||||
// as files using the keys in the Data field as the file names.
|
// as files using the keys in the Data field as the file names.
|
||||||
type SecretVolumeSource struct {
|
type SecretVolumeSource struct {
|
||||||
// Reference to a Secret
|
// Name of the secret in the pod's namespace to use
|
||||||
Target ObjectReference `json:"target"`
|
SecretName string `json:"secretName"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NFSVolumeSource represents an NFS Mount that lasts the lifetime of a pod
|
// NFSVolumeSource represents an NFS Mount that lasts the lifetime of a pod
|
||||||
|
@ -1377,6 +1377,14 @@ func init() {
|
|||||||
out.PodID = in.Name
|
out.PodID = in.Name
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
func(in *newer.SecretVolumeSource, out *SecretVolumeSource, s conversion.Scope) error {
|
||||||
|
out.Target.ID = in.SecretName
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(in *SecretVolumeSource, out *newer.SecretVolumeSource, s conversion.Scope) error {
|
||||||
|
out.SecretName = in.Target.ID
|
||||||
|
return nil
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If one of the conversion functions is malformed, detect it immediately.
|
// If one of the conversion functions is malformed, detect it immediately.
|
||||||
|
@ -462,3 +462,31 @@ func TestEndpointsConversion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSecretVolumeSourceConversion(t *testing.T) {
|
||||||
|
given := current.SecretVolumeSource{
|
||||||
|
Target: current.ObjectReference{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := newer.SecretVolumeSource{
|
||||||
|
SecretName: "foo",
|
||||||
|
}
|
||||||
|
|
||||||
|
got := newer.SecretVolumeSource{}
|
||||||
|
if err := Convert(&given, &got); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got.SecretName != expected.SecretName {
|
||||||
|
t.Errorf("Expected %v; got %v", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
got2 := current.SecretVolumeSource{}
|
||||||
|
if err := Convert(&got, &got2); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got2.Target.ID != given.Target.ID {
|
||||||
|
t.Errorf("Expected %v; got %v", given, got2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -171,7 +171,8 @@ type GitRepoVolumeSource struct {
|
|||||||
|
|
||||||
// SecretVolumeSource adapts a Secret into a VolumeSource
|
// SecretVolumeSource adapts a Secret into a VolumeSource
|
||||||
type SecretVolumeSource struct {
|
type SecretVolumeSource struct {
|
||||||
// Reference to a Secret
|
// Reference to a Secret to use. Only the ID field of this reference is used; a
|
||||||
|
// secret can only be used by pods in its namespace.
|
||||||
Target ObjectReference `json:"target" description:"target is a reference to a secret"`
|
Target ObjectReference `json:"target" description:"target is a reference to a secret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1305,6 +1305,14 @@ func init() {
|
|||||||
out.PodID = in.Name
|
out.PodID = in.Name
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
func(in *newer.SecretVolumeSource, out *SecretVolumeSource, s conversion.Scope) error {
|
||||||
|
out.Target.ID = in.SecretName
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(in *SecretVolumeSource, out *newer.SecretVolumeSource, s conversion.Scope) error {
|
||||||
|
out.SecretName = in.Target.ID
|
||||||
|
return nil
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If one of the conversion functions is malformed, detect it immediately.
|
// If one of the conversion functions is malformed, detect it immediately.
|
||||||
|
@ -281,3 +281,31 @@ func TestEndpointsConversion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSecretVolumeSourceConversion(t *testing.T) {
|
||||||
|
given := current.SecretVolumeSource{
|
||||||
|
Target: current.ObjectReference{
|
||||||
|
ID: "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := newer.SecretVolumeSource{
|
||||||
|
SecretName: "foo",
|
||||||
|
}
|
||||||
|
|
||||||
|
got := newer.SecretVolumeSource{}
|
||||||
|
if err := newer.Scheme.Convert(&given, &got); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got.SecretName != expected.SecretName {
|
||||||
|
t.Errorf("Expected %v; got %v", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
got2 := current.SecretVolumeSource{}
|
||||||
|
if err := newer.Scheme.Convert(&got, &got2); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got2.Target.ID != given.Target.ID {
|
||||||
|
t.Errorf("Expected %v; got %v", given, got2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -110,7 +110,8 @@ const (
|
|||||||
//
|
//
|
||||||
// https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/secrets.md
|
// https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/secrets.md
|
||||||
type SecretVolumeSource struct {
|
type SecretVolumeSource struct {
|
||||||
// Reference to a Secret
|
// Reference to a Secret to use. Only the ID field of this reference is used; a
|
||||||
|
// secret can only be used by pods in its namespace.
|
||||||
Target ObjectReference `json:"target" description:"target is a reference to a secret"`
|
Target ObjectReference `json:"target" description:"target is a reference to a secret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,9 +274,11 @@ type GitRepoVolumeSource struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SecretVolumeSource adapts a Secret into a VolumeSource
|
// SecretVolumeSource adapts a Secret into a VolumeSource
|
||||||
|
//
|
||||||
|
// https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/secrets.md
|
||||||
type SecretVolumeSource struct {
|
type SecretVolumeSource struct {
|
||||||
// Reference to a Secret
|
// Name of the secret in the pod's namespace to use
|
||||||
Target ObjectReference `json:"target" description:"target is a reference to a secret"`
|
SecretName string `json:"secretName" description:"secretName is the name of a secret in the pod's namespace"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NFSVolumeSource represents an NFS mount that lasts the lifetime of a pod
|
// NFSVolumeSource represents an NFS mount that lasts the lifetime of a pod
|
||||||
|
@ -331,14 +331,8 @@ func validateGCEPersistentDiskVolumeSource(PD *api.GCEPersistentDiskVolumeSource
|
|||||||
|
|
||||||
func validateSecretVolumeSource(secretSource *api.SecretVolumeSource) errs.ValidationErrorList {
|
func validateSecretVolumeSource(secretSource *api.SecretVolumeSource) errs.ValidationErrorList {
|
||||||
allErrs := errs.ValidationErrorList{}
|
allErrs := errs.ValidationErrorList{}
|
||||||
if secretSource.Target.Name == "" {
|
if secretSource.SecretName == "" {
|
||||||
allErrs = append(allErrs, errs.NewFieldRequired("target.name"))
|
allErrs = append(allErrs, errs.NewFieldRequired("secretName"))
|
||||||
}
|
|
||||||
if secretSource.Target.Namespace == "" {
|
|
||||||
allErrs = append(allErrs, errs.NewFieldRequired("target.namespace"))
|
|
||||||
}
|
|
||||||
if secretSource.Target.Kind != "Secret" {
|
|
||||||
allErrs = append(allErrs, errs.NewFieldInvalid("target.kind", secretSource.Target.Kind, "Secret"))
|
|
||||||
}
|
}
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
@ -211,7 +211,7 @@ func TestValidateVolumes(t *testing.T) {
|
|||||||
{Name: "empty", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "empty", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
{Name: "gcepd", VolumeSource: api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDiskVolumeSource{"my-PD", "ext4", 1, false}}},
|
{Name: "gcepd", VolumeSource: api.VolumeSource{GCEPersistentDisk: &api.GCEPersistentDiskVolumeSource{"my-PD", "ext4", 1, false}}},
|
||||||
{Name: "gitrepo", VolumeSource: api.VolumeSource{GitRepo: &api.GitRepoVolumeSource{"my-repo", "hashstring"}}},
|
{Name: "gitrepo", VolumeSource: api.VolumeSource{GitRepo: &api.GitRepoVolumeSource{"my-repo", "hashstring"}}},
|
||||||
{Name: "secret", VolumeSource: api.VolumeSource{Secret: &api.SecretVolumeSource{api.ObjectReference{Namespace: api.NamespaceDefault, Name: "my-secret", Kind: "Secret"}}}},
|
{Name: "secret", VolumeSource: api.VolumeSource{Secret: &api.SecretVolumeSource{"my-secret"}}},
|
||||||
}
|
}
|
||||||
names, errs := validateVolumes(successCase)
|
names, errs := validateVolumes(successCase)
|
||||||
if len(errs) != 0 {
|
if len(errs) != 0 {
|
||||||
|
@ -62,7 +62,7 @@ func (plugin *secretPlugin) NewBuilder(spec *api.Volume, podRef *api.ObjectRefer
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *secretPlugin) newBuilderInternal(spec *api.Volume, podRef *api.ObjectReference) (volume.Builder, error) {
|
func (plugin *secretPlugin) newBuilderInternal(spec *api.Volume, podRef *api.ObjectReference) (volume.Builder, error) {
|
||||||
return &secretVolume{spec.Name, *podRef, plugin, spec.Secret.Target}, nil
|
return &secretVolume{spec.Name, *podRef, plugin, spec.Secret.SecretName}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *secretPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cleaner, error) {
|
func (plugin *secretPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cleaner, error) {
|
||||||
@ -70,16 +70,16 @@ func (plugin *secretPlugin) NewCleaner(volName string, podUID types.UID) (volume
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *secretPlugin) newCleanerInternal(volName string, podUID types.UID) (volume.Cleaner, error) {
|
func (plugin *secretPlugin) newCleanerInternal(volName string, podUID types.UID) (volume.Cleaner, error) {
|
||||||
return &secretVolume{volName, api.ObjectReference{UID: podUID}, plugin, api.ObjectReference{}}, nil
|
return &secretVolume{volName, api.ObjectReference{UID: podUID}, plugin, ""}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// secretVolume handles retrieving secrets from the API server
|
// secretVolume handles retrieving secrets from the API server
|
||||||
// and placing them into the volume on the host.
|
// and placing them into the volume on the host.
|
||||||
type secretVolume struct {
|
type secretVolume struct {
|
||||||
volName string
|
volName string
|
||||||
podRef api.ObjectReference
|
podRef api.ObjectReference
|
||||||
plugin *secretPlugin
|
plugin *secretPlugin
|
||||||
secretRef api.ObjectReference
|
secretName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sv *secretVolume) SetUp() error {
|
func (sv *secretVolume) SetUp() error {
|
||||||
@ -109,9 +109,9 @@ func (sv *secretVolume) SetUpAt(dir string) error {
|
|||||||
return fmt.Errorf("Cannot setup secret volume %v because kube client is not configured", sv)
|
return fmt.Errorf("Cannot setup secret volume %v because kube client is not configured", sv)
|
||||||
}
|
}
|
||||||
|
|
||||||
secret, err := kubeClient.Secrets(sv.podRef.Namespace).Get(sv.secretRef.Name)
|
secret, err := kubeClient.Secrets(sv.podRef.Namespace).Get(sv.secretName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Couldn't get secret %v/%v", sv.secretRef.Namespace, sv.secretRef.Name)
|
glog.Errorf("Couldn't get secret %v/%v", sv.podRef.Namespace, sv.secretName)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ func TestCanSupport(t *testing.T) {
|
|||||||
if plugin.Name() != secretPluginName {
|
if plugin.Name() != secretPluginName {
|
||||||
t.Errorf("Wrong name: %s", plugin.Name())
|
t.Errorf("Wrong name: %s", plugin.Name())
|
||||||
}
|
}
|
||||||
if !plugin.CanSupport(&api.Volume{VolumeSource: api.VolumeSource{Secret: &api.SecretVolumeSource{Target: api.ObjectReference{}}}}) {
|
if !plugin.CanSupport(&api.Volume{VolumeSource: api.VolumeSource{Secret: &api.SecretVolumeSource{SecretName: ""}}}) {
|
||||||
t.Errorf("Expected true")
|
t.Errorf("Expected true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,10 +69,7 @@ func TestPlugin(t *testing.T) {
|
|||||||
Name: testVolumeName,
|
Name: testVolumeName,
|
||||||
VolumeSource: api.VolumeSource{
|
VolumeSource: api.VolumeSource{
|
||||||
Secret: &api.SecretVolumeSource{
|
Secret: &api.SecretVolumeSource{
|
||||||
Target: api.ObjectReference{
|
SecretName: testName,
|
||||||
Namespace: testNamespace,
|
|
||||||
Name: testName,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -81,11 +81,7 @@ var _ = Describe("Secrets", func() {
|
|||||||
Name: volumeName,
|
Name: volumeName,
|
||||||
VolumeSource: api.VolumeSource{
|
VolumeSource: api.VolumeSource{
|
||||||
Secret: &api.SecretVolumeSource{
|
Secret: &api.SecretVolumeSource{
|
||||||
Target: api.ObjectReference{
|
SecretName: name,
|
||||||
Kind: "Secret",
|
|
||||||
Namespace: ns,
|
|
||||||
Name: name,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user