This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
volumesource.go
168 lines (147 loc) · 5.06 KB
/
volumesource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package volumesource
import (
"errors"
"fmt"
"io"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/api/v1"
appsapi "k8s.io/kubernetes/pkg/apis/apps/v1beta1"
batchapi "k8s.io/kubernetes/pkg/apis/batch/v1"
batchapi2 "k8s.io/kubernetes/pkg/apis/batch/v2alpha1"
extensionsapi "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
settingsapi "k8s.io/kubernetes/pkg/apis/settings/v1alpha1"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"github.com/openshift/origin/pkg/cmd/admin/migrate"
"github.com/openshift/origin/pkg/cmd/templates"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
deployapi "github.com/openshift/origin/pkg/deploy/apis/apps/v1"
"github.com/spf13/cobra"
)
var (
internalMigrateVolumeSourceLong = templates.LongDesc(`
Confirm that all objects that contain volumeSource.metadata do not have it set
volumeSource.metadata is deprecated and will no longer be available in the next release.
Thus all objects that contain a non-nil volumeSource.metadata must be manually converted
to prevent data loss.
The following resource types are checked by this command:
* cronjobs.batch
* daemonsets.extensions
* deploymentconfigs.apps.openshift.io
* deployments.extensions
* jobs.batch
* podpresets.settings.k8s.io
* pods
* podtemplates
* replicasets.extensions
* replicationcontrollers
* statefulsets.apps
No resources are mutated.`)
errVolumeSourceMetadataIsNotNil = errors.New("volumeSource.metadata is not nil")
)
type MigrateVolumeSourceOptions struct {
migrate.ResourceOptions
}
func NewCmdMigrateVolumeSource(name, fullName string, f *clientcmd.Factory, in io.Reader, out, errout io.Writer) *cobra.Command {
options := &MigrateVolumeSourceOptions{
ResourceOptions: migrate.ResourceOptions{
In: in,
Out: out,
ErrOut: errout,
AllNamespaces: true,
Include: []string{
"cronjobs.batch",
"daemonsets.extensions",
"deploymentconfigs.apps.openshift.io",
"deployments.extensions",
"jobs.batch",
"podpresets.settings.k8s.io",
"pods",
"podtemplates",
"replicasets.extensions",
"replicationcontrollers",
"statefulsets.apps",
},
},
}
cmd := &cobra.Command{
Use: name,
Short: "Confirm that all objects that contain volumeSource.metadata do not have it set",
Long: internalMigrateVolumeSourceLong,
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(options.Complete(name, f, cmd, args))
kcmdutil.CheckErr(options.Validate())
kcmdutil.CheckErr(options.Run())
},
}
return cmd
}
func (o *MigrateVolumeSourceOptions) Complete(name string, f *clientcmd.Factory, c *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%s takes no positional arguments", name)
}
return o.ResourceOptions.Complete(f, c)
}
func (o MigrateVolumeSourceOptions) Validate() error {
return o.ResourceOptions.Validate()
}
func (o MigrateVolumeSourceOptions) Run() error {
return o.ResourceOptions.Visitor().Visit(func(info *resource.Info) (migrate.Reporter, error) {
// get the object as we want the external type without any transformations
versions := &runtime.VersionedObjects{}
if err := info.Client.Get().
Resource(info.Mapping.Resource).
NamespaceIfScoped(info.Namespace, info.Mapping.Scope.Name() == meta.RESTScopeNameNamespace).
Name(info.Name).Do().Into(versions); err != nil {
return migrate.NotChanged, err
}
return checkVolumeSourceMetadataIsNil(versions.First()) // check the object as it is serialized on the wire
})
}
// checkVolumeSourceMetadataIsNil confirms that all objects that contain volumeSource.metadata do not have it set.
// It returns an error any volumeSource.metadata is not nil.
func checkVolumeSourceMetadataIsNil(obj runtime.Object) (migrate.Reporter, error) {
var (
err error
spec *kapi.PodTemplateSpec
)
switch t := obj.(type) {
case *kapi.Pod:
spec = &kapi.PodTemplateSpec{Spec: t.Spec} // fake a PodTemplateSpec using the Pod's Spec
case *settingsapi.PodPreset:
spec = &kapi.PodTemplateSpec{Spec: kapi.PodSpec{Volumes: t.Spec.Volumes}} // fake a PodTemplateSpec using the PodPreset's Volumes
case *kapi.PodTemplate:
spec = &t.Template
case *kapi.ReplicationController:
spec = t.Spec.Template
case *extensionsapi.DaemonSet:
spec = &t.Spec.Template
case *extensionsapi.Deployment:
spec = &t.Spec.Template
case *extensionsapi.ReplicaSet:
spec = &t.Spec.Template
case *appsapi.StatefulSet:
spec = &t.Spec.Template
case *batchapi.Job:
spec = &t.Spec.Template
case *deployapi.DeploymentConfig:
spec = t.Spec.Template
case *batchapi2.CronJob:
spec = &t.Spec.JobTemplate.Spec.Template
default:
return nil, nil // indicate that we ignored the object
}
if specHasVolumeSourceMetadata(spec) {
err = errVolumeSourceMetadataIsNotNil
}
return migrate.NotChanged, err // we only perform read operations
}
func specHasVolumeSourceMetadata(spec *kapi.PodTemplateSpec) bool {
for _, volume := range spec.Spec.Volumes {
if volume.VolumeSource.Metadata != nil {
return true
}
}
return false
}