forked from openshift/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recreateobjects.go
172 lines (158 loc) · 4.98 KB
/
recreateobjects.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
package v1migration
import (
"bufio"
"encoding/json"
"io"
"os"
"runtime"
"sync"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
contributils "github.com/openshift/hive/contrib/pkg/utils"
hivev1alpha1 "github.com/openshift/hive/pkg/apis/hive/v1alpha1"
)
// RecreateObjectsOptions is the set of options for the re-creating Hive objects.
type RecreateObjectsOptions struct {
fileName string
}
// NewRecreateObjectsCommand creates a command that executes the migration utility to re-create Hive objects stored in a file.
func NewRecreateObjectsCommand() *cobra.Command {
opt := &RecreateObjectsOptions{}
cmd := &cobra.Command{
Use: "recreate-objects JSON_FILE_NAME",
Short: "re-create the Hive objects stored in the JSON file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.InfoLevel)
if err := opt.Complete(cmd, args); err != nil {
log.WithError(err).Fatal("Error")
}
if err := opt.Validate(cmd); err != nil {
log.WithError(err).Fatal("Error")
}
if err := opt.Run(); err != nil {
log.WithError(err).Fatal("Error")
}
},
}
return cmd
}
// Complete finishes parsing arguments for the command
func (o *RecreateObjectsOptions) Complete(cmd *cobra.Command, args []string) error {
o.fileName = args[0]
return nil
}
// Validate ensures that option values make sense
func (o *RecreateObjectsOptions) Validate(cmd *cobra.Command) error {
return nil
}
// Run executes the command
func (o *RecreateObjectsOptions) Run() error {
clientConfig, err := contributils.GetClientConfig()
if err != nil {
return errors.Wrap(err, "could not get the client config")
}
client, err := dynamic.NewForConfig(clientConfig)
if err != nil {
return errors.Wrap(err, "could not create kube client")
}
file, err := os.Open(o.fileName)
if err != nil {
errors.Wrap(err, "could not open the JSON file")
}
defer file.Close()
logger := log.StandardLogger()
objChan := make(chan *unstructured.Unstructured)
var recreateWG sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ {
recreateWG.Add(1)
go recreateObjects(client, objChan, &recreateWG, logger)
}
decoder := json.NewDecoder(bufio.NewReader(file))
for {
var objFromFile unstructured.Unstructured
if err := decoder.Decode(&objFromFile); err != nil {
if err == io.EOF {
break
}
return errors.Wrap(err, "could not decode JSON from file")
}
objChan <- &objFromFile
}
close(objChan)
recreateWG.Wait()
return nil
}
func recreateObjects(client dynamic.Interface, objChan chan *unstructured.Unstructured, wg *sync.WaitGroup, logger log.FieldLogger) {
defer wg.Done()
for objFromFile := range objChan {
apiVersion := objFromFile.GetAPIVersion()
kind := objFromFile.GetKind()
namespace := objFromFile.GetNamespace()
name := objFromFile.GetName()
logger := log.WithFields(log.Fields{
"apiVersion": apiVersion,
"kind": kind,
"name": name,
})
if namespace != "" {
logger = logger.WithField("namespace", namespace)
}
if apiVersion != hivev1alpha1.SchemeGroupVersion.String() {
logger.Warn("object in JSON file is not a Hive v1alpha1 resource")
continue
}
switch kind {
case "DNSEndpoint":
logger.Info("re-creation skipped since not used in v1")
continue
case "HiveConfig":
logger.Info("re-creation skipped since HiveConfig has already been restored")
continue
}
gvr := hivev1alpha1.SchemeGroupVersion.WithResource(resourceForHiveKind(kind))
var resourceClient dynamic.ResourceInterface
if namespace != "" {
resourceClient = client.Resource(gvr).Namespace(namespace)
} else {
resourceClient = client.Resource(gvr)
}
clearResourceVersion(objFromFile)
removeHiveOwnerReferences(objFromFile)
removeKubectlLastAppliedAnnotation(objFromFile)
newObj, err := resourceClient.Create(objFromFile, metav1.CreateOptions{})
if err != nil {
logger.WithError(err).Error("could not create object")
continue
}
objFromFile.SetUID(newObj.GetUID())
objFromFile.SetResourceVersion(newObj.GetResourceVersion())
if _, err := resourceClient.Update(objFromFile, metav1.UpdateOptions{}); err != nil {
logger.WithError(err).Error("could not update re-created object with status")
continue
}
logger.Info("created object")
}
}
func clearResourceVersion(obj *unstructured.Unstructured) {
obj.SetResourceVersion("")
}
func removeHiveOwnerReferences(obj *unstructured.Unstructured) {
var ownerReferences []metav1.OwnerReference
for _, r := range obj.GetOwnerReferences() {
if r.APIVersion == hivev1alpha1.SchemeGroupVersion.String() {
continue
}
ownerReferences = append(ownerReferences, r)
}
obj.SetOwnerReferences(ownerReferences)
}
func removeKubectlLastAppliedAnnotation(obj *unstructured.Unstructured) {
annotations := obj.GetAnnotations()
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
obj.SetAnnotations(annotations)
}