-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial LinodeCluster controller logic
- Loading branch information
1 parent
ad28192
commit e10305c
Showing
8 changed files
with
454 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/go-logr/logr" | ||
"github.com/linode/cluster-api-provider-linode/cloud/scope" | ||
"github.com/linode/cluster-api-provider-linode/util" | ||
"github.com/linode/linodego" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ( | ||
defaultLBPort = 6443 | ||
) | ||
|
||
// CreateNodeBalancer creates a new NodeBalancer if one doesn't exist | ||
func CreateNodeBalancer(ctx context.Context, clusterScope *scope.ClusterScope, logger logr.Logger) (*linodego.NodeBalancer, error) { | ||
var linodeNBs []linodego.NodeBalancer | ||
var linodeNB *linodego.NodeBalancer | ||
|
||
tags := []string{string(clusterScope.LinodeCluster.UID)} | ||
filter := map[string]string{ | ||
"tags": strings.Join(tags, ","), | ||
} | ||
|
||
rawFilter, err := json.Marshal(filter) | ||
if err != nil { | ||
|
||
return nil, err | ||
} | ||
logger.Info("Creating NodeBalancer") | ||
if linodeNBs, err = clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, string(rawFilter))); err != nil { | ||
logger.Info("Failed to list NodeBalancers", "error", err.Error()) | ||
|
||
return nil, err | ||
} | ||
|
||
switch len(linodeNBs) { | ||
case 1: | ||
logger.Info(fmt.Sprintf("NodeBalancer %s already exists", *linodeNBs[0].Label)) | ||
|
||
linodeNB = &linodeNBs[0] | ||
case 0: | ||
logger.Info(fmt.Sprintf("Creating NodeBalancer %s-api-server", clusterScope.LinodeCluster.Name)) | ||
createConfig := linodego.NodeBalancerCreateOptions{ | ||
Label: util.Pointer(fmt.Sprintf("%s-api-server", clusterScope.LinodeCluster.Name)), | ||
Region: clusterScope.LinodeCluster.Spec.Region, | ||
ClientConnThrottle: nil, | ||
Tags: tags, | ||
} | ||
|
||
if linodeNB, err = clusterScope.LinodeClient.CreateNodeBalancer(ctx, createConfig); err != nil { | ||
logger.Info("Failed to create Linode NodeBalancer", "error", err.Error()) | ||
|
||
// Already exists is not an error | ||
apiErr := linodego.Error{} | ||
if errors.As(err, &apiErr) && apiErr.Code != http.StatusFound { | ||
return nil, err | ||
} | ||
|
||
err = nil | ||
|
||
if linodeNB != nil { | ||
logger.Info("Linode NodeBalancer already exists", "existing", linodeNB.Label) | ||
} | ||
} | ||
|
||
default: | ||
err = errors.New("multiple NodeBalancers") | ||
|
||
logger.Error(err, "Panic! Multiple NodeBalancers found. This might be a concurrency issue in the controller!!!", "filters", string(rawFilter)) | ||
|
||
return nil, err | ||
} | ||
|
||
if linodeNB == nil { | ||
err = errors.New("missing NodeBalancer") | ||
|
||
logger.Error(err, "Panic! Failed to create NodeBalancer") | ||
|
||
return nil, err | ||
} | ||
|
||
return linodeNB, nil | ||
} | ||
|
||
// CreateNodeBalancerConfig creates NodeBalancer config if it does not exist | ||
func CreateNodeBalancerConfig(ctx context.Context, clusterScope *scope.ClusterScope, logger logr.Logger) (*linodego.NodeBalancerConfig, error) { | ||
|
||
var linodeNBConfigs []linodego.NodeBalancerConfig | ||
var linodeNBConfig *linodego.NodeBalancerConfig | ||
var err error | ||
|
||
if linodeNBConfigs, err = clusterScope.LinodeClient.ListNodeBalancerConfigs(ctx, clusterScope.LinodeCluster.Spec.Network.NodeBalancerID, linodego.NewListOptions(1, "")); err != nil { | ||
logger.Info("Failed to list NodeBalancer Configs", "error", err.Error()) | ||
|
||
return nil, err | ||
} | ||
lbPort := defaultLBPort | ||
if clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort != 0 { | ||
lbPort = clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort | ||
} | ||
switch len(linodeNBConfigs) { | ||
case 1: | ||
logger.Info("NodeBalancer ", strconv.Itoa(linodeNBConfigs[0].ID), " already exists") | ||
linodeNBConfig = &linodeNBConfigs[0] | ||
|
||
case 0: | ||
createConfig := linodego.NodeBalancerConfigCreateOptions{ | ||
Port: lbPort, | ||
Protocol: linodego.ProtocolTCP, | ||
Algorithm: linodego.AlgorithmRoundRobin, | ||
Check: linodego.CheckConnection, | ||
} | ||
|
||
if linodeNBConfig, err = clusterScope.LinodeClient.CreateNodeBalancerConfig(ctx, clusterScope.LinodeCluster.Spec.Network.NodeBalancerID, createConfig); err != nil { | ||
logger.Info("Failed to create Linode NodeBalancer config", "error", err.Error()) | ||
|
||
return nil, err | ||
|
||
} | ||
|
||
default: | ||
err = errors.New("multiple NodeBalancer Configs") | ||
|
||
logger.Error(err, "Panic! Multiple NodeBalancer Configs found. This might be a concurrency issue in the controller!!!") | ||
|
||
return nil, err | ||
} | ||
|
||
return linodeNBConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.