Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17fa87c

Browse files
committedFeb 16, 2023
fix(kube): Make a full acceptance test for customization attributes
1 parent 4d2c9a4 commit 17fa87c

File tree

4 files changed

+315
-71
lines changed

4 files changed

+315
-71
lines changed
 

‎ovh/data_cloud_project_kube.go

+45-35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
910
)
1011

1112
func dataSourceCloudProjectKube() *schema.Resource {
@@ -30,9 +31,10 @@ func dataSourceCloudProjectKube() *schema.Resource {
3031
Optional: true,
3132
},
3233
kubeClusterProxyModeKey: {
33-
Type: schema.TypeString,
34-
Optional: true,
35-
ForceNew: true,
34+
Type: schema.TypeString,
35+
Optional: true,
36+
ForceNew: true,
37+
ValidateFunc: helpers.ValidateEnum([]string{"iptables", "ipvs"}),
3638
},
3739
kubeClusterCustomizationApiServerKey: {
3840
Type: schema.TypeSet,
@@ -143,16 +145,18 @@ func dataSourceCloudProjectKube() *schema.Resource {
143145
Elem: &schema.Resource{
144146
Schema: map[string]*schema.Schema{
145147
"min_sync_period": {
146-
Type: schema.TypeString,
147-
Computed: false,
148-
Optional: true,
149-
ForceNew: false,
148+
Type: schema.TypeString,
149+
Computed: false,
150+
Optional: true,
151+
ForceNew: false,
152+
ValidateFunc: helpers.ValidateRFC3339Duration,
150153
},
151154
"sync_period": {
152-
Type: schema.TypeString,
153-
Computed: false,
154-
Optional: true,
155-
ForceNew: false,
155+
Type: schema.TypeString,
156+
Computed: false,
157+
Optional: true,
158+
ForceNew: false,
159+
ValidateFunc: helpers.ValidateRFC3339Duration,
156160
},
157161
},
158162
},
@@ -167,40 +171,46 @@ func dataSourceCloudProjectKube() *schema.Resource {
167171
Elem: &schema.Resource{
168172
Schema: map[string]*schema.Schema{
169173
"min_sync_period": {
170-
Type: schema.TypeString,
171-
Computed: false,
172-
Optional: true,
173-
ForceNew: false,
174+
Type: schema.TypeString,
175+
Computed: false,
176+
Optional: true,
177+
ForceNew: false,
178+
ValidateFunc: helpers.ValidateRFC3339Duration,
174179
},
175180
"sync_period": {
176-
Type: schema.TypeString,
177-
Computed: false,
178-
Optional: true,
179-
ForceNew: false,
181+
Type: schema.TypeString,
182+
Computed: false,
183+
Optional: true,
184+
ForceNew: false,
185+
ValidateFunc: helpers.ValidateRFC3339Duration,
180186
},
181187
"scheduler": {
182-
Type: schema.TypeString,
183-
Computed: false,
184-
Optional: true,
185-
ForceNew: false,
188+
Type: schema.TypeString,
189+
Computed: false,
190+
Optional: true,
191+
ForceNew: false,
192+
ValidateFunc: helpers.ValidateEnum([]string{"rr", "lc", "dh", "sh", "sed", "nq"}),
186193
},
187194
"tcp_fin_timeout": {
188-
Type: schema.TypeString,
189-
Computed: false,
190-
Optional: true,
191-
ForceNew: false,
195+
Type: schema.TypeString,
196+
Computed: false,
197+
Optional: true,
198+
ForceNew: false,
199+
ValidateFunc: helpers.ValidateRFC3339Duration,
192200
},
193201
"tcp_timeout": {
194-
Type: schema.TypeString,
195-
Computed: false,
196-
Optional: true,
197-
ForceNew: false,
202+
Type: schema.TypeString,
203+
Computed: false,
204+
Optional: true,
205+
ForceNew: false,
206+
ValidateFunc: helpers.ValidateRFC3339Duration,
198207
},
199208
"udp_timeout": {
200-
Type: schema.TypeString,
201-
Computed: false,
202-
Optional: true,
203-
ForceNew: false,
209+
Type: schema.TypeString,
210+
Computed: false,
211+
Optional: true,
212+
ForceNew: false,
213+
ValidateFunc: helpers.ValidateRFC3339Duration,
204214
},
205215
},
206216
},

‎ovh/helpers/helpers.go

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/ovh/go-ovh/ovh"
13+
"github.com/ybriffa/rfc3339"
1314
)
1415

1516
func ValidateIpBlock(value string) error {
@@ -186,6 +187,14 @@ func ValidateDedicatedCephStatus(value string) error {
186187
})
187188
}
188189

190+
// ValidateRFC3339Duration implements schema.SchemaValidateFunc for RFC3339 durations.
191+
func ValidateRFC3339Duration(i interface{}, _ string) (_ []string, errors []error) {
192+
if _, err := rfc3339.ParseDuration(i.(string)); err != nil {
193+
errors = append(errors, err)
194+
}
195+
return
196+
}
197+
189198
func ValidateDedicatedCephACLFamily(value string) error {
190199
return ValidateStringEnum(value, []string{
191200
"IPv4",

‎ovh/resource_cloud_project_kube.go

+45-36
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,18 @@ func resourceCloudProjectKube() *schema.Resource {
170170
Elem: &schema.Resource{
171171
Schema: map[string]*schema.Schema{
172172
"min_sync_period": {
173-
Type: schema.TypeString,
174-
Computed: false,
175-
Optional: true,
176-
ForceNew: false,
173+
Type: schema.TypeString,
174+
Computed: false,
175+
Optional: true,
176+
ForceNew: false,
177+
ValidateFunc: helpers.ValidateRFC3339Duration,
177178
},
178179
"sync_period": {
179-
Type: schema.TypeString,
180-
Computed: false,
181-
Optional: true,
182-
ForceNew: false,
180+
Type: schema.TypeString,
181+
Computed: false,
182+
Optional: true,
183+
ForceNew: false,
184+
ValidateFunc: helpers.ValidateRFC3339Duration,
183185
},
184186
},
185187
},
@@ -194,40 +196,46 @@ func resourceCloudProjectKube() *schema.Resource {
194196
Elem: &schema.Resource{
195197
Schema: map[string]*schema.Schema{
196198
"min_sync_period": {
197-
Type: schema.TypeString,
198-
Computed: false,
199-
Optional: true,
200-
ForceNew: false,
199+
Type: schema.TypeString,
200+
Computed: false,
201+
Optional: true,
202+
ForceNew: false,
203+
ValidateFunc: helpers.ValidateRFC3339Duration,
201204
},
202205
"sync_period": {
203-
Type: schema.TypeString,
204-
Computed: false,
205-
Optional: true,
206-
ForceNew: false,
206+
Type: schema.TypeString,
207+
Computed: false,
208+
Optional: true,
209+
ForceNew: false,
210+
ValidateFunc: helpers.ValidateRFC3339Duration,
207211
},
208212
"scheduler": {
209-
Type: schema.TypeString,
210-
Computed: false,
211-
Optional: true,
212-
ForceNew: false,
213+
Type: schema.TypeString,
214+
Computed: false,
215+
Optional: true,
216+
ForceNew: false,
217+
ValidateFunc: helpers.ValidateEnum([]string{"rr", "lc", "dh", "sh", "sed", "nq"}),
213218
},
214219
"tcp_fin_timeout": {
215-
Type: schema.TypeString,
216-
Computed: false,
217-
Optional: true,
218-
ForceNew: false,
220+
Type: schema.TypeString,
221+
Computed: false,
222+
Optional: true,
223+
ForceNew: false,
224+
ValidateFunc: helpers.ValidateRFC3339Duration,
219225
},
220226
"tcp_timeout": {
221-
Type: schema.TypeString,
222-
Computed: false,
223-
Optional: true,
224-
ForceNew: false,
227+
Type: schema.TypeString,
228+
Computed: false,
229+
Optional: true,
230+
ForceNew: false,
231+
ValidateFunc: helpers.ValidateRFC3339Duration,
225232
},
226233
"udp_timeout": {
227-
Type: schema.TypeString,
228-
Computed: false,
229-
Optional: true,
230-
ForceNew: false,
234+
Type: schema.TypeString,
235+
Computed: false,
236+
Optional: true,
237+
ForceNew: false,
238+
ValidateFunc: helpers.ValidateRFC3339Duration,
231239
},
232240
},
233241
},
@@ -242,10 +250,11 @@ func resourceCloudProjectKube() *schema.Resource {
242250
ForceNew: true,
243251
},
244252
kubeClusterProxyModeKey: {
245-
Type: schema.TypeString,
246-
Optional: true,
247-
ForceNew: true,
248-
Computed: true,
253+
Type: schema.TypeString,
254+
Optional: true,
255+
ForceNew: true,
256+
Computed: true,
257+
ValidateFunc: helpers.ValidateEnum([]string{"iptables", "ipvs"}),
249258
},
250259
kubeClusterPrivateNetworkConfigurationKey: {
251260
Type: schema.TypeSet,

‎ovh/resource_cloud_project_kube_test.go

+216
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"regexp"
89
"strconv"
910
"strings"
1011
"testing"
@@ -576,6 +577,221 @@ resource "ovh_cloud_project_kube" "cluster" {
576577
})
577578
}
578579

580+
func TestAccCloudProjectKube_customization_full_deprecated(t *testing.T) {
581+
region := os.Getenv("OVH_CLOUD_PROJECT_KUBE_REGION_TEST")
582+
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
583+
name := acctest.RandomWithPrefix(test_prefix)
584+
585+
erroredConfigKubeProxyMode := fmt.Sprintf(`
586+
resource "ovh_cloud_project_kube" "cluster" {
587+
service_name = "%s"
588+
name = "%s"
589+
region = "%s"
590+
kube_proxy_mode = "foo"
591+
}
592+
`,
593+
serviceName,
594+
name,
595+
region,
596+
)
597+
598+
erroredConfigInvalidRFC3339Duration := fmt.Sprintf(`
599+
resource "ovh_cloud_project_kube" "cluster" {
600+
service_name = "%s"
601+
name = "%s"
602+
region = "%s"
603+
604+
customization_kube_proxy {
605+
iptables {
606+
min_sync_period = "foo"
607+
sync_period = "foo"
608+
}
609+
ipvs {
610+
min_sync_period = "foo"
611+
scheduler = "rr"
612+
sync_period = "foo"
613+
tcp_fin_timeout = "foo"
614+
tcp_timeout = "foo"
615+
udp_timeout = "foo"
616+
}
617+
}
618+
}
619+
`,
620+
serviceName,
621+
name,
622+
region,
623+
)
624+
625+
erroredConfigInvalidScheduler := fmt.Sprintf(`
626+
resource "ovh_cloud_project_kube" "cluster" {
627+
service_name = "%s"
628+
name = "%s"
629+
region = "%s"
630+
631+
customization_kube_proxy {
632+
ipvs {
633+
scheduler = "foo"
634+
}
635+
}
636+
}
637+
`,
638+
serviceName,
639+
name,
640+
region,
641+
)
642+
643+
config := fmt.Sprintf(`
644+
resource "ovh_cloud_project_kube" "cluster" {
645+
service_name = "%s"
646+
name = "%s"
647+
region = "%s"
648+
kube_proxy_mode = "iptables"
649+
650+
customization {
651+
apiserver {
652+
admissionplugins {
653+
enabled = ["NodeRestriction"]
654+
disabled = ["AlwaysPullImages"]
655+
}
656+
}
657+
}
658+
659+
customization_kube_proxy {
660+
iptables {
661+
min_sync_period = "PT0S"
662+
sync_period = "PT0S"
663+
}
664+
ipvs {
665+
min_sync_period = "PT0S"
666+
scheduler = "rr"
667+
sync_period = "PT0S"
668+
tcp_fin_timeout = "PT0S"
669+
tcp_timeout = "PT0S"
670+
udp_timeout = "PT0S"
671+
}
672+
}
673+
}
674+
`,
675+
serviceName,
676+
name,
677+
region,
678+
)
679+
680+
updatedConfig := fmt.Sprintf(`
681+
resource "ovh_cloud_project_kube" "cluster" {
682+
service_name = "%s"
683+
name = "%s"
684+
region = "%s"
685+
kube_proxy_mode = "iptables"
686+
687+
customization {
688+
apiserver {
689+
admissionplugins {
690+
enabled = ["AlwaysPullImages", "NodeRestriction"]
691+
disabled = []
692+
}
693+
}
694+
}
695+
696+
customization_kube_proxy {
697+
iptables {
698+
min_sync_period = "PT30S"
699+
sync_period = "PT30S"
700+
}
701+
ipvs {
702+
min_sync_period = "PT30S"
703+
scheduler = "rr"
704+
sync_period = "PT30S"
705+
tcp_fin_timeout = "PT30S"
706+
tcp_timeout = "PT30S"
707+
udp_timeout = "PT30S"
708+
}
709+
}
710+
}
711+
`,
712+
serviceName,
713+
name,
714+
region,
715+
)
716+
717+
resource.Test(t, resource.TestCase{
718+
PreCheck: func() {
719+
testAccPreCheckCloud(t)
720+
testAccCheckCloudProjectExists(t)
721+
testAccPreCheckKubernetes(t)
722+
},
723+
Providers: testAccProviders,
724+
Steps: []resource.TestStep{
725+
{
726+
Config: erroredConfigKubeProxyMode,
727+
ExpectError: regexp.MustCompile(`is not among valid values`),
728+
},
729+
{
730+
Config: erroredConfigInvalidRFC3339Duration,
731+
ExpectError: regexp.MustCompile(`does not match RFC3339 duration`),
732+
},
733+
{
734+
Config: erroredConfigInvalidScheduler,
735+
ExpectError: regexp.MustCompile(`is not among valid values`),
736+
},
737+
{
738+
Config: config,
739+
Check: resource.ComposeTestCheckFunc(
740+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", kubeClusterNameKey, name),
741+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "region", region),
742+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "service_name", serviceName),
743+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "kube_proxy_mode", "iptables"),
744+
745+
// customization_kube_proxy - ipvs
746+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.min_sync_period", "PT0S"),
747+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.scheduler", "rr"),
748+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.sync_period", "PT0S"),
749+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.tcp_fin_timeout", "PT0S"),
750+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.tcp_timeout", "PT0S"),
751+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.udp_timeout", "PT0S"),
752+
753+
// customization_kube_proxy - iptables
754+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.iptables.0.min_sync_period", "PT0S"),
755+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.iptables.0.sync_period", "PT0S"),
756+
757+
// customization - apiserver
758+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.enabled.#", "1"),
759+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.enabled.0", "NodeRestriction"),
760+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.disabled.#", "1"),
761+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.disabled.0", "AlwaysPullImages"),
762+
),
763+
},
764+
{
765+
Config: updatedConfig,
766+
Check: resource.ComposeTestCheckFunc(
767+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", kubeClusterNameKey, name),
768+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "region", region),
769+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "service_name", serviceName),
770+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "kube_proxy_mode", "iptables"),
771+
772+
// customization_kube_proxy - ipvs
773+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.min_sync_period", "PT30S"),
774+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.scheduler", "rr"),
775+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.sync_period", "PT30S"),
776+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.tcp_fin_timeout", "PT30S"),
777+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.tcp_timeout", "PT30S"),
778+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.ipvs.0.udp_timeout", "PT30S"),
779+
780+
// customization_kube_proxy - iptables
781+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.iptables.0.min_sync_period", "PT30S"),
782+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization_kube_proxy.0.iptables.0.sync_period", "PT30S"),
783+
784+
// customization - apiserver
785+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.disabled.#", "0"),
786+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.enabled.#", "2"),
787+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.enabled.0", "AlwaysPullImages"),
788+
resource.TestCheckResourceAttr("ovh_cloud_project_kube.cluster", "customization.0.apiserver.0.admissionplugins.0.enabled.1", "NodeRestriction"),
789+
),
790+
},
791+
},
792+
})
793+
}
794+
579795
func TestAccCloudProjectKubeVRack(t *testing.T) {
580796
serviceName := os.Getenv("OVH_CLOUD_PROJECT_SERVICE_TEST")
581797
vrackID := os.Getenv("OVH_VRACK_SERVICE_TEST")

0 commit comments

Comments
 (0)
Please sign in to comment.