-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_openshift_route.go
288 lines (258 loc) · 9.6 KB
/
resource_openshift_route.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package openshift
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
api "github.com/openshift/api/route/v1"
client_v1 "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
)
func resourceOpenshiftRoute() *schema.Resource {
return &schema.Resource{
Create: resourceOpenshiftRouteCreate,
Read: resourceOpenshiftRouteRead,
Update: resourceOpenshiftRouteUpdate,
Delete: resourceOpenshiftRouteDelete,
Exists: resourceOpenshiftRouteExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("route", true),
"spec": {
Type: schema.TypeList,
Description: "Spec defines the behavior of a route.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Description: "(string) host is an alias/DNS that points to the service. Optional. If not specified a route name will typically be automatically chosen. Must follow DNS952 subdomain conventions.",
Computed: true,
Optional: true,
},
"path": {
Type: schema.TypeString,
Description: "(string) Path that the router watches for, to route traffic for to the service. Optional",
Optional: true,
},
"port": {
Type: schema.TypeList,
Description: "(v1.RoutePort) If specified, the port to be used by the router. Most routers will use all endpoints exposed by the service by default - set this value to instruct routers which port to use.",
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_port": {
Type: schema.TypeString,
Description: "(intstr.IntOrString) The target port on pods selected by the service this route points to. If this is a string, it will be looked up as a named port in the target endpoints port list. Required",
Required: true,
},
},
},
},
"tls": {
Type: schema.TypeList,
Description: "(v1.TLSConfig) The tls field provides the ability to configure certificates and termination for the route.",
MaxItems: 1,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"termination": {
Type: schema.TypeString,
Description: "(string) termination indicates termination type.",
Computed: true,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"edge", "passthrough", "reencrypt"}, false),
},
"certificate": {
Type: schema.TypeString,
Description: "(string) certificate provides certificate contents",
Optional: true,
},
"key": {
Type: schema.TypeString,
Description: "(string) key provides key file contents",
Optional: true,
},
"ca_certificate": {
Type: schema.TypeString,
Description: "(string) caCertificate provides the cert authority certificate contents",
Optional: true,
},
"destination_ca_certificate": {
Type: schema.TypeString,
Description: "(string) destinationCACertificate provides the contents of the ca certificate of the final destination. When using reencrypt termination this file should be provided in order to have routers use it for health checks on the secure connection. If this field is not specified, the router may provide its own destination CA and perform hostname validation using the short service name (service.namespace.svc), which allows infrastructure generated certificates to automatically verify.",
Optional: true,
},
"insecure_edge_termination_policy": {
Type: schema.TypeString,
Description: "(string) insecureEdgeTerminationPolicy indicates the desired behavior for insecure connections to a route. While each router may make its own decisions on which ports to expose, this is normally port 80.",
Computed: true,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"None", "Allow", "Redirect"}, false),
},
},
},
},
"to": {
Type: schema.TypeList,
Description: "(v1.RouteTargetReference) to is an object the route should use as the primary backend. Only the Service kind is allowed, and it will be defaulted to Service. If the weight field (0-256 default 1) is set to zero, no traffic will be sent to this backend.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kind": {
Type: schema.TypeString,
Description: "(string) The kind of target that the route is referring to. Currently, only 'Service' is allowed",
Required: true,
},
"name": {
Type: schema.TypeString,
Description: "(string) name of the service/target that is being referred to. e.g. name of the service",
Required: true,
},
"weight": {
Type: schema.TypeInt,
Description: "(integer) weight as an integer between 0 and 256, default 1, that specifies the target's relative weight against other target reference objects. 0 suppresses requests to this backend.",
Computed: true,
Optional: true,
},
},
},
},
"wildcard_policy": {
Type: schema.TypeString,
Description: "(string) Wildcard policy if any for the route. Currently only 'Subdomain' or 'None' is allowed.",
Optional: true,
Default: "None",
ValidateFunc: validation.StringInSlice([]string{"Subdomain", "None"}, false),
},
},
},
},
},
}
}
func resourceOpenshiftRouteCreate(d *schema.ResourceData, meta interface{}) error {
client, err := client_v1.NewForConfig(meta.(*rest.Config))
if err != nil {
return err
}
metadata := expandMetadata(d.Get("metadata").([]interface{}))
route := api.Route{
ObjectMeta: metadata,
Spec: expandRouteSpec(d.Get("spec").([]interface{})),
}
log.Printf("[INFO] Creating new route: %#v", route)
out, err := client.Routes(metadata.Namespace).Create(&route)
if err != nil {
return fmt.Errorf("Failed to create route: %s", err)
}
log.Printf("[INFO] Submitted new route: %#v", out)
d.SetId(buildId(out.ObjectMeta))
return resourceOpenshiftRouteRead(d, meta)
}
func resourceOpenshiftRouteRead(d *schema.ResourceData, meta interface{}) error {
client, err := client_v1.NewForConfig(meta.(*rest.Config))
if err != nil {
return err
}
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Reading route %s", name)
route, err := client.Routes(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return err
}
log.Printf("[INFO] Received route: %#v", route)
err = d.Set("metadata", flattenMetadata(route.ObjectMeta, d))
if err != nil {
return err
}
spec, err := flattenRouteSpec(route.Spec)
if err != nil {
return err
}
err = d.Set("spec", spec)
if err != nil {
return err
}
return nil
}
func resourceOpenshiftRouteUpdate(d *schema.ResourceData, meta interface{}) error {
client, err := client_v1.NewForConfig(meta.(*rest.Config))
if err != nil {
return err
}
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("spec") {
diffOps, err := patchRouteSpec("spec.0.", "/spec/", d)
if err != nil {
return err
}
ops = append(ops, diffOps...)
}
data, err := ops.MarshalJSON()
if err != nil {
return fmt.Errorf("Failed to marshal update operations: %s", err)
}
log.Printf("[INFO] Updating route %q: %v", name, string(data))
out, err := client.Routes(namespace).Patch(name, pkgApi.JSONPatchType, data)
if err != nil {
return fmt.Errorf("Failed to update route: %s", err)
}
log.Printf("[INFO] Submitted updated route: %#v", out)
d.SetId(buildId(out.ObjectMeta))
return resourceOpenshiftRouteRead(d, meta)
}
func resourceOpenshiftRouteDelete(d *schema.ResourceData, meta interface{}) error {
client, err := client_v1.NewForConfig(meta.(*rest.Config))
if err != nil {
return err
}
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
log.Printf("[INFO] Deleting route: %#v", name)
err = client.Routes(namespace).Delete(name, &meta_v1.DeleteOptions{})
if err != nil {
return fmt.Errorf("Failed to delete route: %s", err)
}
d.SetId("")
return nil
}
func resourceOpenshiftRouteExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client, err := client_v1.NewForConfig(meta.(*rest.Config))
if err != nil {
return false, err
}
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}
log.Printf("[INFO] Checking route %s", name)
_, err = client.Routes(namespace).Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
}
log.Printf("[DEBUG] Received error: %#v", err)
}
return true, err
}