This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathutils.go
170 lines (144 loc) · 4.14 KB
/
utils.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
package exposestrategy
import (
"bytes"
"encoding/json"
"net"
"strings"
"text/template"
"github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/strategicpatch"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
func findHttpProtocol(svc *api.Service, hostName string) string {
// default to http
protocol := "http"
// if a port is on the hostname check is its a default http / https port
_, port, err := net.SplitHostPort(hostName)
if err == nil {
if port == "443" || port == "8443" {
protocol = "https"
}
}
// check if the service port has a name of https
for _, port := range svc.Spec.Ports {
if port.Name == "https" {
protocol = port.Name
}
}
return protocol
}
func addServiceAnnotation(svc *api.Service, hostName string) (*api.Service, error) {
protocol := findHttpProtocol(svc, hostName)
return addServiceAnnotationWithProtocol(svc, hostName, protocol)
}
func addServiceAnnotationWithProtocol(svc *api.Service, hostName string, protocol string) (*api.Service, error) {
if svc.Annotations == nil {
svc.Annotations = map[string]string{}
}
exposeURL := protocol + "://" + hostName
path := svc.Annotations[ApiServicePathAnnotationKey]
if len(path) > 0 {
exposeURL = urlJoin(exposeURL, path)
}
svc.Annotations[ExposeAnnotationKey] = exposeURL
if key := svc.Annotations[ExposeHostNameAsAnnotationKey]; len(key) > 0 {
svc.Annotations[key] = hostName
}
return svc, nil
}
// urlJoin joins the given URL paths so that there is a / separating them but not a double //
func urlJoin(repo string, path string) string {
return strings.TrimSuffix(repo, "/") + "/" + strings.TrimPrefix(path, "/")
}
func removeServiceAnnotation(svc *api.Service) *api.Service {
delete(svc.Annotations, ExposeAnnotationKey)
if key := svc.Annotations[ExposeHostNameAsAnnotationKey]; len(key) > 0 {
delete(svc.Annotations, key)
}
return svc
}
func createPatch(a runtime.Object, b runtime.Object, encoder runtime.Encoder, dataStruct interface{}) ([]byte, error) {
var aBuf bytes.Buffer
err := encoder.Encode(a, &aBuf)
if err != nil {
return nil, errors.Wrapf(err, "failed to encode object: %v", a)
}
var bBuf bytes.Buffer
err = encoder.Encode(b, &bBuf)
if err != nil {
return nil, errors.Wrapf(err, "failed to encode object: %v", b)
}
aBytes := aBuf.Bytes()
bBytes := bBuf.Bytes()
if bytes.Compare(aBytes, bBytes) == 0 {
return nil, nil
}
patch, err := strategicpatch.CreateTwoWayMergePatch(aBytes, bBytes, dataStruct)
if err != nil {
return nil, errors.Wrap(err, "failed to create patch")
}
return patch, nil
}
type masterType string
const (
openShift masterType = "OpenShift"
kubernetes masterType = "Kubernetes"
)
func typeOfMaster(c *client.Client) (masterType, error) {
res, err := c.Get().AbsPath("").DoRaw()
if err != nil {
errors.Wrap(err, "could not discover the type of your installation")
}
var rp unversioned.RootPaths
err = json.Unmarshal(res, &rp)
if err != nil {
errors.Wrap(err, "could not discover the type of your installation")
}
for _, p := range rp.Paths {
if p == "/oapi" {
return openShift, nil
}
}
return kubernetes, nil
}
type urlTemplateParts struct {
Service string
Namespace string
Domain string
}
func getURLFormat(urltemplate string) (string, error) {
if urltemplate == "" {
urltemplate = "{{.Service}}.{{.Namespace}}.{{.Domain}}"
}
placeholders := urlTemplateParts{"%[1]s", "%[2]s", "%[3]s"}
tmpl, err := template.New("format").Parse(urltemplate)
if err != nil {
errors.Wrap(err, "Failed to parse UrlTemplate")
}
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, placeholders)
if err != nil {
errors.Wrap(err, "Failed to execute UrlTemplate")
}
return buffer.String(), nil
}
// UrlJoin joins the given paths so that there is only ever one '/' character between the paths
func UrlJoin(paths ...string) string {
var buffer bytes.Buffer
last := len(paths) - 1
for i, path := range paths {
p := path
if i > 0 {
buffer.WriteString("/")
p = strings.TrimPrefix(p, "/")
}
if i < last {
p = strings.TrimSuffix(p, "/")
}
buffer.WriteString(p)
}
return buffer.String()
}