Skip to content

Commit

Permalink
IPPool and LoadBalancer
Browse files Browse the repository at this point in the history
More functionality, especially around the LoadBalancer resource

Signed-off-by: Moritz Röhrich <[email protected]>
  • Loading branch information
m-ildefons committed Aug 22, 2024
1 parent 941b5e1 commit 1a4686b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 16 deletions.
12 changes: 8 additions & 4 deletions internal/provider/ippool/resource_ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func resourceIPPoolRead(ctx context.Context, data *schema.ResourceData, meta int

ippool, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
IPPools().Get(ctx, name, metav1.GetOptions{})
IPPools().
Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -82,7 +83,8 @@ func resourceIPPoolUpdate(ctx context.Context, data *schema.ResourceData, meta i

obj, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
IPPools().Get(ctx, name, metav1.GetOptions{})
IPPools().
Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -94,7 +96,8 @@ func resourceIPPoolUpdate(ctx context.Context, data *schema.ResourceData, meta i

ippool, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
IPPools().Update(ctx, toUpdate.(*loadbalancerv1.IPPool), metav1.UpdateOptions{})
IPPools().
Update(ctx, toUpdate.(*loadbalancerv1.IPPool), metav1.UpdateOptions{})
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -110,7 +113,8 @@ func resourceIPPoolDelete(ctx context.Context, data *schema.ResourceData, meta i

err = c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
IPPools().Delete(ctx, name, metav1.DeleteOptions{})
IPPools().
Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/ippool/resource_ippool_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *Constructor) subresourceIPPoolRangeParser(data interface{}) error {
func (c *Constructor) subresourceIPPoolSelectorParser(data interface{}) error {
ippoolSelector := data.(map[string]interface{})

priority := uint32(ippoolSelector[constants.FieldSelectorPriority].(int))
priority := ippoolSelector[constants.FieldSelectorPriority].(uint32)
network := ippoolSelector[constants.FieldSelectorNetwork].(string)

scopesData := ippoolSelector[constants.SubresourceTypeIPPoolSelectorScope].([]interface{})
Expand Down
58 changes: 56 additions & 2 deletions internal/provider/loadbalancer/resource_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -57,14 +58,67 @@ func resourceLoadBalancerCreate(ctx context.Context, data *schema.ResourceData,
}

func resourceLoadBalancerRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(nil)
c := meta.(*client.Client)
namespace, name, err := helper.IDParts(data.Id())
if err != nil {
return diag.FromErr(err)
}

loadbalancer, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
LoadBalancers(namespace).
Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(err)
}

return diag.FromErr(resourceLoadBalancerImport(data, loadbalancer))
}

func resourceLoadBalancerUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(nil)
c := meta.(*client.Client)
namespace, name, err := helper.IDParts(data.Id())
if err != nil {
return diag.FromErr(err)
}

obj, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
LoadBalancers(namespace).
Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(err)
}

toUpdate, err := util.ResourceConstruct(data, Updater(obj))
if err != nil {
return diag.FromErr(err)
}

loadbalancer, err := c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
LoadBalancers(namespace).
Update(ctx, toUpdate.(*loadbalancerv1.LoadBalancer), metav1.UpdateOptions{})
if err != nil {
return diag.FromErr(err)
}
return diag.FromErr(resourceLoadBalancerImport(data, loadbalancer))
}

func resourceLoadBalancerDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*client.Client)
namespace, name, err := helper.IDParts(data.Id())
if err != nil {
return diag.FromErr(err)
}

err = c.HarvesterLoadbalancerClient.
LoadbalancerV1beta1().
LoadBalancers(namespace).
Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return diag.FromErr(err)
}
return diag.FromErr(nil)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func Creator(namespace, name string) util.Constructor {
}

func Updater(loadbalancer *loadbalancerv1.LoadBalancer) util.Constructor {
loadbalancer.Spec.Listeners = []loadbalancerv1.Listener{}
loadbalancer.Spec.HealthCheck = &loadbalancerv1.HealthCheck{}

return newLoadBalancerConstructor(loadbalancer)
}

Expand Down Expand Up @@ -102,9 +105,9 @@ func (c *Constructor) subresourceLoadBalancerListenerParser(data interface{}) er
listener := data.(map[string]interface{})

name := listener[constants.FieldListenerName].(string)
port := int32(listener[constants.FieldListenerPort].(int))
port := listener[constants.FieldListenerPort].(int32)
protocol := corev1.Protocol(listener[constants.FieldListenerProtocol].(string))
backendPort := int32(listener[constants.FieldListenerBackendPort].(int))
backendPort := listener[constants.FieldListenerBackendPort].(int32)

c.LoadBalancer.Spec.Listeners = append(c.LoadBalancer.Spec.Listeners, loadbalancerv1.Listener{
Name: name,
Expand All @@ -117,5 +120,21 @@ func (c *Constructor) subresourceLoadBalancerListenerParser(data interface{}) er
}

func (c *Constructor) subresourceLoadBalancerHealthCheckParser(data interface{}) error {
healthcheck := data.(map[string]interface{})

port := healthcheck[constants.FieldHealthCheckPort].(uint)
success := healthcheck[constants.FieldHealthCheckSuccessThreshold].(uint)
failure := healthcheck[constants.FieldHealthCheckFailureThreshold].(uint)
period := healthcheck[constants.FieldHealthCheckPeriodSeconds].(uint)
timeout := healthcheck[constants.FieldHealthCheckTimeoutSeconds].(uint)

c.LoadBalancer.Spec.HealthCheck = &loadbalancerv1.HealthCheck{
Port: port,
SuccessThreshold: success,
FailureThreshold: failure,
PeriodSeconds: period,
TimeoutSeconds: timeout,
}

return nil
}
3 changes: 1 addition & 2 deletions internal/tests/resource_ippool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

// loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1"
)

Expand All @@ -15,7 +14,7 @@ func TestIPPoolBasic(t *testing.T) {
// )

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Expand Down
38 changes: 38 additions & 0 deletions internal/tests/resource_loadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tests

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
// loadbalancerv1 "github.com/harvester/harvester-load-balancer/pkg/apis/loadbalancer.harvesterhci.io/v1beta1"
)

func TestLoadBalancerBasic(t *testing.T) {
// var (
// loadbalancer *loadbalancerv1.LoadBalancer
// ctx = context.Background()
// )

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: `
resource harvester_loadbalancer \"test_loadbalancer\" {
name = \"test_loadbalancer\"
listener {
port = 443
protocol = \"tcp\"
backend_port = 8080
}
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("harvester_loadbalancer.test_loadbalancer", "name", "test_loadbalancer"),
),
},
},
})
}
10 changes: 5 additions & 5 deletions pkg/constants/constants_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const (
const (
SubresourceTypeLoadBalancerHealthCheck = "healthcheck"

FieldHealthcheckPort = "port"
FieldHealthcheckSuccessThreshold = "success_threshold"
FieldHealthcheckFailureThreshold = "failure_threshold"
FieldHealthcheckPeriodSeconds = "period_seconds"
FieldHealthcheckTimeoutSeconds = "timeout_seconds"
FieldHealthCheckPort = "port"
FieldHealthCheckSuccessThreshold = "success_threshold"
FieldHealthCheckFailureThreshold = "failure_threshold"
FieldHealthCheckPeriodSeconds = "period_seconds"
FieldHealthCheckTimeoutSeconds = "timeout_seconds"
)

const (
Expand Down

0 comments on commit 1a4686b

Please sign in to comment.