Skip to content

Commit 9a3bf07

Browse files
author
Aleksandr Rybolovlev
authored
Add app_protocol attribute to kubernetes_service (#1668)
* Add 'app_protocol' attribute of resource and data source 'kubernetes_service'
1 parent 9544e7c commit 9a3bf07

9 files changed

+75
-46
lines changed

kubernetes/data_source_kubernetes_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func dataSourceKubernetesService() *schema.Resource {
6262
Computed: true,
6363
Elem: &schema.Resource{
6464
Schema: map[string]*schema.Schema{
65+
"app_protocol": {
66+
Type: schema.TypeString,
67+
Description: "The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per RFC-6335 and http://www.iana.org/assignments/service-names). Non-standard protocols should use prefixed names such as mycompany.com/my-custom-protocol.",
68+
Optional: true,
69+
},
6570
"name": {
6671
Type: schema.TypeString,
6772
Description: "The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.",

kubernetes/data_source_kubernetes_service_test.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
func TestAccKubernetesDataSourceService_basic(t *testing.T) {
1212
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
13+
resourceName := "kubernetes_service.test"
14+
dataSourceName := "data.kubernetes_service.test"
1315

1416
resource.Test(t, resource.TestCase{
1517
PreCheck: func() { testAccPreCheck(t) },
@@ -18,42 +20,44 @@ func TestAccKubernetesDataSourceService_basic(t *testing.T) {
1820
{
1921
Config: testAccKubernetesDataSourceServiceConfig_basic(name),
2022
Check: resource.ComposeAggregateTestCheckFunc(
21-
resource.TestCheckResourceAttr("kubernetes_service.test", "metadata.0.name", name),
22-
resource.TestCheckResourceAttrSet("kubernetes_service.test", "metadata.0.generation"),
23-
resource.TestCheckResourceAttrSet("kubernetes_service.test", "metadata.0.resource_version"),
24-
resource.TestCheckResourceAttrSet("kubernetes_service.test", "metadata.0.uid"),
25-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.#", "1"),
26-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.#", "1"),
27-
resource.TestCheckResourceAttrSet("kubernetes_service.test", "spec.0.cluster_ip"),
28-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.name", ""),
29-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.node_port", "0"),
30-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.port", "8080"),
31-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.protocol", "TCP"),
32-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.target_port", "80"),
33-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.session_affinity", "None"),
34-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.type", "ClusterIP"),
35-
resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.health_check_node_port", "0"),
23+
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
24+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
25+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
26+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
27+
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
28+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.#", "1"),
29+
resource.TestCheckResourceAttrSet(resourceName, "spec.0.cluster_ip"),
30+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.name", ""),
31+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.node_port", "0"),
32+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.port", "8080"),
33+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.protocol", "TCP"),
34+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.target_port", "80"),
35+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.app_protocol", "http"),
36+
resource.TestCheckResourceAttr(resourceName, "spec.0.session_affinity", "None"),
37+
resource.TestCheckResourceAttr(resourceName, "spec.0.type", "ClusterIP"),
38+
resource.TestCheckResourceAttr(resourceName, "spec.0.health_check_node_port", "0"),
3639
),
3740
},
3841
{
3942
Config: testAccKubernetesDataSourceServiceConfig_basic(name) +
4043
testAccKubernetesDataSourceServiceConfig_read(),
4144
Check: resource.ComposeAggregateTestCheckFunc(
42-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "metadata.0.name", name),
43-
resource.TestCheckResourceAttrSet("data.kubernetes_service.test", "metadata.0.generation"),
44-
resource.TestCheckResourceAttrSet("data.kubernetes_service.test", "metadata.0.resource_version"),
45-
resource.TestCheckResourceAttrSet("data.kubernetes_service.test", "metadata.0.uid"),
46-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.#", "1"),
47-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.#", "1"),
48-
resource.TestCheckResourceAttrSet("data.kubernetes_service.test", "spec.0.cluster_ip"),
49-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.0.name", ""),
50-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.0.node_port", "0"),
51-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.0.port", "8080"),
52-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.0.protocol", "TCP"),
53-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.port.0.target_port", "80"),
54-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.session_affinity", "None"),
55-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.type", "ClusterIP"),
56-
resource.TestCheckResourceAttr("data.kubernetes_service.test", "spec.0.health_check_node_port", "0"),
45+
resource.TestCheckResourceAttr(dataSourceName, "metadata.0.name", name),
46+
resource.TestCheckResourceAttrSet(dataSourceName, "metadata.0.generation"),
47+
resource.TestCheckResourceAttrSet(dataSourceName, "metadata.0.resource_version"),
48+
resource.TestCheckResourceAttrSet(dataSourceName, "metadata.0.uid"),
49+
resource.TestCheckResourceAttr(dataSourceName, "spec.#", "1"),
50+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.#", "1"),
51+
resource.TestCheckResourceAttrSet(dataSourceName, "spec.0.cluster_ip"),
52+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.name", ""),
53+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.node_port", "0"),
54+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.port", "8080"),
55+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.protocol", "TCP"),
56+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.target_port", "80"),
57+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.port.0.app_protocol", "http"),
58+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.session_affinity", "None"),
59+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.type", "ClusterIP"),
60+
resource.TestCheckResourceAttr(dataSourceName, "spec.0.health_check_node_port", "0"),
5761
),
5862
},
5963
},
@@ -76,8 +80,9 @@ func testAccKubernetesDataSourceServiceConfig_basic(name string) string {
7680
}
7781
spec {
7882
port {
79-
port = 8080
80-
target_port = 80
83+
port = 8080
84+
target_port = 80
85+
app_protocol = "http"
8186
}
8287
}
8388
}

kubernetes/resource_kubernetes_service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func resourceKubernetesServiceSchemaV1() map[string]*schema.Schema {
109109
MinItems: 1,
110110
Elem: &schema.Resource{
111111
Schema: map[string]*schema.Schema{
112+
"app_protocol": {
113+
Type: schema.TypeString,
114+
Description: "The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per RFC-6335 and http://www.iana.org/assignments/service-names). Non-standard protocols should use prefixed names such as mycompany.com/my-custom-protocol.",
115+
Optional: true,
116+
},
112117
"name": {
113118
Type: schema.TypeString,
114119
Description: "The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.",

kubernetes/resource_kubernetes_service_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,27 +391,31 @@ func TestAccKubernetesService_nodePort(t *testing.T) {
391391
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.port", "10222"),
392392
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.protocol", "TCP"),
393393
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.target_port", "22"),
394+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.0.app_protocol", "ssh"),
394395
resource.TestCheckResourceAttr(resourceName, "spec.0.port.1.name", "second"),
395396
resource.TestCheckResourceAttrSet(resourceName, "spec.0.port.1.node_port"),
396397
resource.TestCheckResourceAttr(resourceName, "spec.0.port.1.port", "10333"),
397398
resource.TestCheckResourceAttr(resourceName, "spec.0.port.1.protocol", "TCP"),
398399
resource.TestCheckResourceAttr(resourceName, "spec.0.port.1.target_port", "33"),
400+
resource.TestCheckResourceAttr(resourceName, "spec.0.port.1.app_protocol", "terraform.io/kubernetes"),
399401
resource.TestCheckResourceAttr(resourceName, "spec.0.selector.%", "1"),
400402
resource.TestCheckResourceAttr(resourceName, "spec.0.selector.App", "MyApp"),
401403
resource.TestCheckResourceAttr(resourceName, "spec.0.session_affinity", "ClientIP"),
402404
resource.TestCheckResourceAttr(resourceName, "spec.0.type", "NodePort"),
403405
testAccCheckServicePorts(&conf, []api.ServicePort{
404406
{
405-
Name: "first",
406-
Port: int32(10222),
407-
Protocol: api.ProtocolTCP,
408-
TargetPort: intstr.FromInt(22),
407+
AppProtocol: ptrToString("ssh"),
408+
Name: "first",
409+
Port: int32(10222),
410+
Protocol: api.ProtocolTCP,
411+
TargetPort: intstr.FromInt(22),
409412
},
410413
{
411-
Name: "second",
412-
Port: int32(10333),
413-
Protocol: api.ProtocolTCP,
414-
TargetPort: intstr.FromInt(33),
414+
AppProtocol: ptrToString("terraform.io/kubernetes"),
415+
Name: "second",
416+
Port: int32(10333),
417+
Protocol: api.ProtocolTCP,
418+
TargetPort: intstr.FromInt(33),
415419
},
416420
}),
417421
),
@@ -1165,15 +1169,17 @@ func testAccKubernetesServiceConfig_nodePort(name string) string {
11651169
session_affinity = "ClientIP"
11661170
11671171
port {
1168-
name = "first"
1169-
port = 10222
1170-
target_port = 22
1172+
name = "first"
1173+
port = 10222
1174+
target_port = 22
1175+
app_protocol = "ssh"
11711176
}
11721177
11731178
port {
1174-
name = "second"
1175-
port = 10333
1176-
target_port = 33
1179+
name = "second"
1180+
port = 10333
1181+
target_port = 33
1182+
app_protocol = "terraform.io/kubernetes"
11771183
}
11781184
11791185
type = "NodePort"

kubernetes/structure_service_spec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func flattenServicePort(in []v1.ServicePort) []interface{} {
1414
att := make([]interface{}, len(in), len(in))
1515
for i, n := range in {
1616
m := make(map[string]interface{})
17+
m["app_protocol"] = n.AppProtocol
1718
m["name"] = n.Name
1819
m["protocol"] = string(n.Protocol)
1920
m["port"] = int(n.Port)
@@ -96,6 +97,9 @@ func expandServicePort(l []interface{}, removeNodePort bool) []v1.ServicePort {
9697
Port: int32(cfg["port"].(int)),
9798
TargetPort: intstr.Parse(cfg["target_port"].(string)),
9899
}
100+
if v, ok := cfg["app_protocol"].(string); ok && v != "" {
101+
obj[i].AppProtocol = &v
102+
}
99103
if v, ok := cfg["name"].(string); ok {
100104
obj[i].Name = v
101105
}

website/docs/d/service.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The following arguments are supported:
6060

6161
#### Attributes
6262

63+
* `app_protocol` - (Optional) The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per [RFC-6335](https://datatracker.ietf.org/doc/html/rfc6335) and [IANA standard service names](http://www.iana.org/assignments/service-names)). Non-standard protocols should use prefixed names such as `mycompany.com/my-custom-protocol`. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol)
6364
* `name` - The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.
6465
* `node_port` - 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. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#type--nodeport)
6566
* `port` - The port that will be exposed by this service.

website/docs/d/service_v1.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The following arguments are supported:
6060

6161
#### Attributes
6262

63+
* `app_protocol` - (Optional) The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per [RFC-6335](https://datatracker.ietf.org/doc/html/rfc6335) and [IANA standard service names](http://www.iana.org/assignments/service-names)). Non-standard protocols should use prefixed names such as `mycompany.com/my-custom-protocol`. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol)
6364
* `name` - The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.
6465
* `node_port` - 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. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#type--nodeport)
6566
* `port` - The port that will be exposed by this service.

website/docs/r/service.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ The following arguments are supported:
170170

171171
#### Arguments
172172

173+
* `app_protocol` - (Optional) The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per [RFC-6335](https://datatracker.ietf.org/doc/html/rfc6335) and [IANA standard service names](http://www.iana.org/assignments/service-names)). Non-standard protocols should use prefixed names such as `mycompany.com/my-custom-protocol`. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol)
173174
* `name` - (Optional) The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.
174175
* `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. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#type--nodeport)
175176
* `port` - (Required) The port that will be exposed by this service.

website/docs/r/service_v1.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ The following arguments are supported:
170170

171171
#### Arguments
172172

173+
* `app_protocol` - (Optional) The application protocol for this port. This field follows standard Kubernetes label syntax. Un-prefixed names are reserved for IANA standard service names (as per [RFC-6335](https://datatracker.ietf.org/doc/html/rfc6335) and [IANA standard service names](http://www.iana.org/assignments/service-names)). Non-standard protocols should use prefixed names such as `mycompany.com/my-custom-protocol`. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/services-networking/service/#application-protocol)
173174
* `name` - (Optional) The name of this port within the service. All ports within the service must have unique names. Optional if only one ServicePort is defined on this service.
174175
* `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. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/services#type--nodeport)
175176
* `port` - (Required) The port that will be exposed by this service.

0 commit comments

Comments
 (0)