This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversion.go
142 lines (131 loc) · 4.09 KB
/
conversion.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
package crd
import (
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/solo-io/gloo-api/pkg/api/types/v1"
crdv1 "github.com/solo-io/gloo-storage/crd/solo.io/v1"
"github.com/solo-io/gloo/pkg/protoutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func UpstreamToCrd(namespace string, upstream *v1.Upstream) (*crdv1.Upstream, error) {
name := upstream.Name
var status *v1.Status
var ok bool
if upstream.Status != nil {
status, ok = proto.Clone(upstream.Status).(*v1.Status)
if !ok {
return nil, errors.New("internal error: output of proto.Clone was not expected type")
}
}
var resourceVersion string
var annotations map[string]string
if upstream.Metadata != nil {
resourceVersion = upstream.Metadata.ResourceVersion
if upstream.Metadata.Namespace != "" {
namespace = upstream.Metadata.Namespace
}
annotations = upstream.Metadata.Annotations
}
// clone and remove fields
upstreamClone, ok := proto.Clone(upstream).(*v1.Upstream)
if !ok {
return nil, errors.New("internal error: output of proto.Clone was not expected type")
}
upstreamClone.Metadata = nil
upstreamClone.Name = ""
upstreamClone.Status = nil
spec, err := protoutil.MarshalMap(upstreamClone)
if err != nil {
return nil, errors.Wrap(err, "failed to convert proto upstream to map[string]interface{}")
}
copySpec := crdv1.Spec(spec)
return &crdv1.Upstream{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
ResourceVersion: resourceVersion,
Annotations: annotations,
},
Status: status,
Spec: ©Spec,
}, nil
}
func UpstreamFromCrd(upstreamCrd *crdv1.Upstream) (*v1.Upstream, error) {
var upstream v1.Upstream
if upstreamCrd.Spec != nil {
err := protoutil.UnmarshalMap(*upstreamCrd.Spec, &upstream)
if err != nil {
return nil, errors.Wrap(err, "failed to convert crd spec to upstream")
}
}
// add removed fields to the internal object
upstream.Name = upstreamCrd.Name
upstream.Metadata = &v1.Metadata{
ResourceVersion: upstreamCrd.ResourceVersion,
Namespace: upstreamCrd.Namespace,
Annotations: upstreamCrd.Annotations,
}
upstream.Status = upstreamCrd.Status
return &upstream, nil
}
func VirtualHostToCrd(namespace string, virtualHost *v1.VirtualHost) (*crdv1.VirtualHost, error) {
name := virtualHost.Name
var status *v1.Status
var ok bool
if virtualHost.Status != nil {
status, ok = proto.Clone(virtualHost.Status).(*v1.Status)
if !ok {
return nil, errors.New("internal error: output of proto.Clone was not expected type")
}
}
var resourceVersion string
var annotations map[string]string
if virtualHost.Metadata != nil {
resourceVersion = virtualHost.Metadata.ResourceVersion
if virtualHost.Metadata.Namespace != "" {
namespace = virtualHost.Metadata.Namespace
}
annotations = virtualHost.Metadata.Annotations
}
// clone and remove fields
vHostClone, ok := proto.Clone(virtualHost).(*v1.VirtualHost)
if !ok {
return nil, errors.New("internal error: output of proto.Clone was not expected type")
}
vHostClone.Metadata = nil
vHostClone.Name = ""
vHostClone.Status = nil
spec, err := protoutil.MarshalMap(vHostClone)
if err != nil {
return nil, errors.Wrap(err, "failed to convert proto upstream to map[string]interface{}")
}
copySpec := crdv1.Spec(spec)
return &crdv1.VirtualHost{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
ResourceVersion: resourceVersion,
Annotations: annotations,
},
Status: status,
Spec: ©Spec,
}, nil
}
func VirtualHostFromCrd(vHostCrd *crdv1.VirtualHost) (*v1.VirtualHost, error) {
var virtualHost v1.VirtualHost
if vHostCrd.Spec != nil {
err := protoutil.UnmarshalMap(*vHostCrd.Spec, &virtualHost)
if err != nil {
return nil, errors.Wrap(err, "failed to convert crd spec to virtualhost")
}
}
// add removed fields to the internal object
virtualHost.Name = vHostCrd.Name
virtualHost.Metadata = &v1.Metadata{
ResourceVersion: vHostCrd.ResourceVersion,
Namespace: vHostCrd.Namespace,
Annotations: vHostCrd.Annotations,
}
virtualHost.Status = vHostCrd.Status
return &virtualHost, nil
}