Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/kubernetes_service: Switch targetPort to string #154

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func resourceKubernetesService() *schema.Resource {
Default: "TCP",
},
"target_port": {
Type: schema.TypeInt,
Type: schema.TypeString,
Description: "Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. This field is ignored for services with `cluster_ip = \"None\"`. More info: http://kubernetes.io/docs/user-guide/services#defining-a-service",
Optional: true,
Computed: true,
Expand Down
56 changes: 56 additions & 0 deletions kubernetes/resource_kubernetes_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,33 @@ func TestAccKubernetesService_noTargetPort(t *testing.T) {
})
}

func TestAccKubernetesService_stringTargetPort(t *testing.T) {
var conf api.Service
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfNoLoadBalancersAvailable(t) },
IDRefreshName: "kubernetes_service.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesServiceConfig_stringTargetPort(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesServiceExists("kubernetes_service.test", &conf),
testAccCheckServicePorts(&conf, []api.ServicePort{
{
Port: int32(8080),
Protocol: api.ProtocolTCP,
TargetPort: intstr.FromString("http-server"),
},
}),
),
},
},
})
}

func TestAccKubernetesService_externalName(t *testing.T) {
var conf api.Service
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -575,6 +602,35 @@ resource "kubernetes_service" "test" {
}`, name, name)
}

func testAccKubernetesServiceConfig_stringTargetPort(name string) string {
return fmt.Sprintf(`
resource "kubernetes_service" "test" {
metadata {
name = "%s"

labels {
app = "helloweb"
tier = "frontend"
}
}

spec {
type = "LoadBalancer"

selector {
app = "helloweb"
tier = "frontend"
}

port {
port = 8080
target_port = "http-server"
}
}
}
`, name)
}

func testAccKubernetesServiceConfig_noTargetPort(name string) string {
return fmt.Sprintf(`
resource "kubernetes_service" "test" {
Expand Down
12 changes: 2 additions & 10 deletions kubernetes/structure_service_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import (

// Flatteners

func flattenIntOrString(in intstr.IntOrString) int {
return in.IntValue()
}

func flattenServicePort(in []v1.ServicePort) []interface{} {
att := make([]interface{}, len(in), len(in))
for i, n := range in {
m := make(map[string]interface{})
m["name"] = n.Name
m["protocol"] = string(n.Protocol)
m["port"] = int(n.Port)
m["target_port"] = flattenIntOrString(n.TargetPort)
m["target_port"] = n.TargetPort.String()
m["node_port"] = int(n.NodePort)

att[i] = m
Expand Down Expand Up @@ -76,10 +72,6 @@ func flattenLoadBalancerIngress(in []v1.LoadBalancerIngress) []interface{} {

// Expanders

func expandIntOrString(in int) intstr.IntOrString {
return intstr.FromInt(in)
}

func expandServicePort(l []interface{}) []v1.ServicePort {
if len(l) == 0 || l[0] == nil {
return []v1.ServicePort{}
Expand All @@ -89,7 +81,7 @@ func expandServicePort(l []interface{}) []v1.ServicePort {
cfg := n.(map[string]interface{})
obj[i] = v1.ServicePort{
Port: int32(cfg["port"].(int)),
TargetPort: expandIntOrString(cfg["target_port"].(int)),
TargetPort: intstr.Parse(cfg["target_port"].(string)),
}
if v, ok := cfg["name"].(string); ok {
obj[i].Name = v
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ The following arguments are supported:
* `node_port` - (Optional) The port on each node on which this service is exposed when `type` is `NodePort` or `LoadBalancer`. Usually assigned by the system. If specified, it will be allocated to the service if unused or else creation of the service will fail. Default is to auto-allocate a port if the `type` of this service requires one. More info: http://kubernetes.io/docs/user-guide/services#type--nodeport
* `port` - (Required) The port that will be exposed by this service.
* `protocol` - (Optional) The IP protocol for this port. Supports `TCP` and `UDP`. Default is `TCP`.
* `target_port` - (Required) Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. This field is ignored for services with `cluster_ip = "None"`. More info: http://kubernetes.io/docs/user-guide/services#defining-a-service
* `target_port` - (Optional) Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. This field is ignored for services with `cluster_ip = "None"`. More info: http://kubernetes.io/docs/user-guide/services#defining-a-service

## Attributes

Expand Down