forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
226 lines (207 loc) · 6.2 KB
/
exporter.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package export
import (
"fmt"
"reflect"
"strings"
"github.com/golang/glog"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
osautil "github.com/openshift/origin/pkg/serviceaccounts/util"
)
var ErrExportOmit = fmt.Errorf("object is omitted")
type Exporter interface {
AddExportOptions(*pflag.FlagSet)
Export(obj runtime.Object, exact bool) error
}
type DefaultExporter struct{}
func (e *DefaultExporter) AddExportOptions(flags *pflag.FlagSet) {
}
func exportObjectMeta(objMeta metav1.Object, exact bool) {
objMeta.SetUID("")
if !exact {
objMeta.SetNamespace("")
}
objMeta.SetCreationTimestamp(metav1.Time{})
objMeta.SetDeletionTimestamp(nil)
objMeta.SetResourceVersion("")
objMeta.SetSelfLink("")
if len(objMeta.GetGenerateName()) > 0 && !exact {
objMeta.SetName("")
}
}
func (e *DefaultExporter) Export(obj runtime.Object, exact bool) error {
if meta, err := meta.Accessor(obj); err == nil {
exportObjectMeta(meta, exact)
} else {
glog.V(4).Infof("Object of type %v does not have ObjectMeta: %v", reflect.TypeOf(obj), err)
}
switch t := obj.(type) {
case *kapi.Endpoints:
case *kapi.ResourceQuota:
t.Status = kapi.ResourceQuotaStatus{}
case *kapi.LimitRange:
// TODO: this needs to be fixed
// limitrange.Strategy.PrepareForCreate(obj)
case *kapi.Node:
if exact {
return nil
}
// Nodes are the only resources that allow direct status edits, therefore
// we clear that without exact so that the node value can be reused.
t.Status = kapi.NodeStatus{}
case *kapi.Namespace:
case *kapi.PersistentVolumeClaim:
t.Status = kapi.PersistentVolumeClaimStatus{}
case *kapi.PersistentVolume:
case *kapi.ReplicationController:
t.Status = kapi.ReplicationControllerStatus{}
case *kapi.Pod:
t.Status = kapi.PodStatus{}
case *kapi.PodTemplate:
case *kapi.Service:
// TODO: service does not yet have a strategy
t.Status = kapi.ServiceStatus{}
if exact {
return nil
}
if t.Spec.ClusterIP != kapi.ClusterIPNone {
t.Spec.ClusterIP = ""
}
if t.Spec.Type == kapi.ServiceTypeNodePort {
for i := range t.Spec.Ports {
t.Spec.Ports[i].NodePort = 0
}
}
case *kapi.Secret:
if exact {
return nil
}
// secrets that are tied to the UID of a service account cannot be exported anyway
if t.Type == kapi.SecretTypeServiceAccountToken || len(t.Annotations[kapi.ServiceAccountUIDKey]) > 0 {
return ErrExportOmit
}
case *kapi.ServiceAccount:
if exact {
return nil
}
dockercfgSecretPrefix := osautil.GetDockercfgSecretNamePrefix(t)
newImagePullSecrets := []kapi.LocalObjectReference{}
for _, secretRef := range t.ImagePullSecrets {
if strings.HasPrefix(secretRef.Name, dockercfgSecretPrefix) {
continue
}
newImagePullSecrets = append(newImagePullSecrets, secretRef)
}
t.ImagePullSecrets = newImagePullSecrets
tokenSecretPrefix := osautil.GetTokenSecretNamePrefix(t)
newMountableSecrets := []kapi.ObjectReference{}
for _, secretRef := range t.Secrets {
if strings.HasPrefix(secretRef.Name, dockercfgSecretPrefix) ||
strings.HasPrefix(secretRef.Name, tokenSecretPrefix) {
continue
}
newMountableSecrets = append(newMountableSecrets, secretRef)
}
t.Secrets = newMountableSecrets
case *appsapi.DeploymentConfig:
t.Status = appsapi.DeploymentConfigStatus{}
case *buildapi.BuildConfig:
t.Status = buildapi.BuildConfigStatus{}
for i := range t.Spec.Triggers {
if p := t.Spec.Triggers[i].ImageChange; p != nil {
p.LastTriggeredImageID = ""
}
}
case *buildapi.Build:
// TODO: should be handled by prepare for create
t.Status.Duration = 0
t.Status.Phase = buildapi.BuildPhaseNew
t.Status.StartTimestamp = nil
t.Status.CompletionTimestamp = nil
if exact {
return nil
}
if t.Status.Config != nil {
t.Status.Config = &kapi.ObjectReference{Name: t.Status.Config.Name}
}
case *routeapi.Route:
case *imageapi.Image:
case *imageapi.ImageStream:
if exact {
return nil
}
// if we point to a docker image repository upstream, copy only the spec tags
if len(t.Spec.DockerImageRepository) > 0 {
t.Status = imageapi.ImageStreamStatus{}
break
}
// create an image stream that mirrors (each spec tag points to the remote image stream)
if len(t.Status.DockerImageRepository) > 0 {
ref, err := imageapi.ParseDockerImageReference(t.Status.DockerImageRepository)
if err != nil {
return err
}
newSpec := imageapi.ImageStreamSpec{
Tags: map[string]imageapi.TagReference{},
}
for name, tag := range t.Status.Tags {
if len(tag.Items) > 0 {
// copy annotations
existing := t.Spec.Tags[name]
// point directly to that registry
ref.Tag = name
existing.From = &kapi.ObjectReference{
Kind: "DockerImage",
Name: ref.String(),
}
newSpec.Tags[name] = existing
}
}
for name, ref := range t.Spec.Tags {
if _, ok := t.Status.Tags[name]; ok {
continue
}
// TODO: potentially trim some of these
newSpec.Tags[name] = ref
}
t.Spec = newSpec
t.Status = imageapi.ImageStreamStatus{
Tags: map[string]imageapi.TagEventList{},
}
break
}
// otherwise, try to snapshot the most recent image as spec items
newSpec := imageapi.ImageStreamSpec{
Tags: map[string]imageapi.TagReference{},
}
for name, tag := range t.Status.Tags {
if len(tag.Items) > 0 {
// copy annotations
existing := t.Spec.Tags[name]
existing.From = &kapi.ObjectReference{
Kind: "DockerImage",
Name: tag.Items[0].DockerImageReference,
}
newSpec.Tags[name] = existing
}
}
t.Spec = newSpec
t.Status = imageapi.ImageStreamStatus{
Tags: map[string]imageapi.TagEventList{},
}
case *imageapi.ImageStreamTag:
exportObjectMeta(&t.Image.ObjectMeta, exact)
case *imageapi.ImageStreamImage:
exportObjectMeta(&t.Image.ObjectMeta, exact)
default:
glog.V(4).Infof("No export strategy defined for objects of type %v", reflect.TypeOf(obj))
}
return nil
}