Skip to content

Commit

Permalink
Merge 84cdede into 9e36b0c
Browse files Browse the repository at this point in the history
  • Loading branch information
phillc committed Oct 9, 2019
2 parents 9e36b0c + 84cdede commit 22f0c34
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
43 changes: 43 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
run:
tests: false

linters-settings:
errcheck:
check-type-assertions: true
check-blank: true

govet:
check-shadowing: true

enable:
- atomicalign
enable-all: false
disable:
- shadow
disable-all: false
golint:
min-confidence: 0.8
gocyclo:
min-complexity: 10
gocognit:
min-complexity: 10
maligned:
suggest-new: true
dupl:
threshold: 100

linters:
enable-all: true
disable:
- vetshadow
- gocyclo
- unparam
- nakedret
- lll
- dupl
- gosec
- gochecknoinits
- gochecknoglobals
fast: false


6 changes: 3 additions & 3 deletions cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (i *instances) nodeAddresses(ctx context.Context, linode *linodego.Instance
}

if (len(ips.IPv4.Public) == 0) && (len(ips.IPv4.Private) == 0) {
return nil, fmt.Errorf("Linode has no IP addresses")
return nil, fmt.Errorf("instance has no IP addresses")
}

if len(ips.IPv4.Public) > 0 {
Expand Down Expand Up @@ -172,11 +172,11 @@ func linodeIDFromProviderID(providerID string) (string, error) {

split := strings.Split(providerID, "://")
if len(split) != 2 {
return "", fmt.Errorf("Unexpected providerID format: %s, format should be: linode://12345", providerID)
return "", fmt.Errorf("unexpected providerID format: %s, format should be: linode://12345", providerID)
}

if split[0] != ProviderName {
return "", fmt.Errorf("Provider scheme from providerID should be 'linode://', %s", providerID)
return "", fmt.Errorf("provider scheme from providerID should be 'linode://', %s", providerID)
}

return split[1], nil
Expand Down
29 changes: 16 additions & 13 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
annLinodeThrottle = "service.beta.kubernetes.io/linode-loadbalancer-throttle"
)

var lbNotFound = errors.New("loadbalancer not found")
var errLbNotFound = errors.New("loadbalancer not found")

type loadbalancers struct {
client *linodego.Client
Expand All @@ -49,7 +49,7 @@ type loadbalancers struct {
}

type tlsAnnotation struct {
TlsSecretName string `json:"tls-secret-name"`
TLSSecretName string `json:"tls-secret-name"`
Port int `json:"port"`
}

Expand All @@ -65,7 +65,7 @@ func (l *loadbalancers) GetLoadBalancer(ctx context.Context, clusterName string,
lbName := cloudprovider.GetLoadBalancerName(service)
lb, err := l.lbByName(ctx, l.client, lbName)
if err != nil {
if err == lbNotFound {
if err == errLbNotFound {
return nil, false, nil
}

Expand Down Expand Up @@ -122,6 +122,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
}

// UpdateLoadBalancer updates the NodeBalancer to have configs that match the Service's ports
//nolint:funlen
func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) error {
// Get the NodeBalancer
lbName := cloudprovider.GetLoadBalancerName(service)
Expand Down Expand Up @@ -155,7 +156,7 @@ func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName stri
// Add or overwrite configs for each of the Service's ports
for _, port := range service.Spec.Ports {
if port.Protocol == v1.ProtocolUDP {
return fmt.Errorf("Error updating NodeBalancer Config: ports with the UDP protocol are not supported")
return fmt.Errorf("error updating NodeBalancer Config: ports with the UDP protocol are not supported")
}

// Construct a new config for this port
Expand All @@ -173,6 +174,7 @@ func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName stri
// Look for an existing config for this port
var existingNBCfg *linodego.NodeBalancerConfig
for _, nbc := range nbCfgs {
nbc := nbc
if nbc.Port == int(port.Port) {
existingNBCfg = &nbc
break
Expand All @@ -185,7 +187,7 @@ func (l *loadbalancers) UpdateLoadBalancer(ctx context.Context, clusterName stri
rebuildOpts.Nodes = newNBNodes

if _, err = l.client.RebuildNodeBalancerConfig(ctx, lb.ID, existingNBCfg.ID, rebuildOpts); err != nil {
return fmt.Errorf("Error rebuilding NodeBalancer config: %v", err)
return fmt.Errorf("error rebuilding NodeBalancer config: %v", err)
}
} else {
createOpts := newNBCfg.GetCreateOptions()
Expand Down Expand Up @@ -242,7 +244,7 @@ func (l *loadbalancers) EnsureLoadBalancerDeleted(ctx context.Context, clusterNa
return l.client.DeleteNodeBalancer(ctx, lb.ID)
}

// The returned error will be lbNotFound if the load balancer does not exist.
// The returned error will be errLbNotFound if the load balancer does not exist.
func (l *loadbalancers) lbByName(ctx context.Context, client *linodego.Client, name string) (*linodego.NodeBalancer, error) {
jsonFilter, err := json.Marshal(map[string]string{"label": name})
if err != nil {
Expand All @@ -257,7 +259,7 @@ func (l *loadbalancers) lbByName(ctx context.Context, client *linodego.Client, n
return &lbs[0], nil
}

return nil, lbNotFound
return nil, errLbNotFound
}

func (l *loadbalancers) createNodeBalancer(ctx context.Context, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (*linodego.NodeBalancer, error) {
Expand All @@ -273,6 +275,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, service *v1.Serv
return l.client.CreateNodeBalancer(ctx, createOpts)
}

//nolint:funlen
func (l *loadbalancers) buildNodeBalancerConfig(service *v1.Service, port int) (linodego.NodeBalancerConfig, error) {
protocol, err := getProtocol(service)
if err != nil {
Expand Down Expand Up @@ -368,12 +371,12 @@ func (l *loadbalancers) processHTTPS(service *v1.Service, nbConfig *linodego.Nod
// buildLoadBalancerRequest returns a linodego.NodeBalancer
// requests for service across nodes.
func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v1.Service, nodes []*v1.Node) (*linodego.NodeBalancer, error) {
var configs []*linodego.NodeBalancerConfigCreateOptions

ports := service.Spec.Ports
configs := make([]*linodego.NodeBalancerConfigCreateOptions, 0, len(ports))

for _, port := range ports {
if port.Protocol == v1.ProtocolUDP {
return nil, fmt.Errorf("Error creating NodeBalancer Config: ports with the UDP protocol are not supported")
return nil, fmt.Errorf("error creating NodeBalancer Config: ports with the UDP protocol are not supported")
}

config, err := l.buildNodeBalancerConfig(service, int(port.Port))
Expand All @@ -393,7 +396,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v

func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePort int32) linodego.NodeBalancerNodeCreateOptions {
return linodego.NodeBalancerNodeCreateOptions{
Address: fmt.Sprintf("%v:%v", getNodeInternalIp(node), nodePort),
Address: fmt.Sprintf("%v:%v", getNodeInternalIP(node), nodePort),
Label: node.Name,
Mode: "accept",
Weight: 100,
Expand Down Expand Up @@ -473,7 +476,7 @@ func getTLSAnnotations(service *v1.Service) ([]*tlsAnnotation, error) {
return tlsAnnotations, nil
}

func getNodeInternalIp(node *v1.Node) string {
func getNodeInternalIP(node *v1.Node) string {
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
return addr.Address
Expand All @@ -485,7 +488,7 @@ func getNodeInternalIp(node *v1.Node) string {
func getTLSCertInfo(kubeClient kubernetes.Interface, tlsAnnotations []*tlsAnnotation, namespace string, port int) (string, string, error) {
for _, tlsAnnotation := range tlsAnnotations {
if tlsAnnotation.Port == port {
secret, err := kubeClient.CoreV1().Secrets(namespace).Get(tlsAnnotation.TlsSecretName, metav1.GetOptions{})
secret, err := kubeClient.CoreV1().Secrets(namespace).Get(tlsAnnotation.TLSSecretName, metav1.GetOptions{})
if err != nil {
return "", "", err
}
Expand Down
20 changes: 10 additions & 10 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func Test_getHealthCheckType(t *testing.T) {
}
}

func Test_getNodeInternalIp(t *testing.T) {
func Test_getNodeInternalIP(t *testing.T) {
testcases := []struct {
name string
node *v1.Node
Expand Down Expand Up @@ -440,7 +440,7 @@ func Test_getNodeInternalIp(t *testing.T) {

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
ip := getNodeInternalIp(test.node)
ip := getNodeInternalIP(test.node)
if ip != test.address {
t.Error("unexpected certificate")
t.Logf("expected: %q", test.address)
Expand Down Expand Up @@ -824,11 +824,11 @@ func Test_getTLSPorts(t *testing.T) {
name: "Test get TLS ports",
ann: []*tlsAnnotation{
{
TlsSecretName: "secret-1",
TLSSecretName: "secret-1",
Port: 8080,
},
{
TlsSecretName: "secret-2",
TLSSecretName: "secret-2",
Port: 8081,
},
},
Expand Down Expand Up @@ -875,7 +875,7 @@ func Test_getTLSAnnotations(t *testing.T) {
ann: map[string]string{annLinodeLoadBalancerTLS: `[ { "tls-secret-name": "prod-app-tls", "port": 443} ]`},
annTLS: []*tlsAnnotation{
{
TlsSecretName: "prod-app-tls",
TLSSecretName: "prod-app-tls",
Port: 443,
},
},
Expand All @@ -886,11 +886,11 @@ func Test_getTLSAnnotations(t *testing.T) {
ann: map[string]string{annLinodeLoadBalancerTLS: `[ { "tls-secret-name": "prod-app-tls", "port": 443}, {"tls-secret-name": "dev-app-tls", "port": 8443} ]`},
annTLS: []*tlsAnnotation{
{
TlsSecretName: "prod-app-tls",
TLSSecretName: "prod-app-tls",
Port: 443,
},
{
TlsSecretName: "dev-app-tls",
TLSSecretName: "dev-app-tls",
Port: 8443,
},
},
Expand Down Expand Up @@ -1038,7 +1038,7 @@ bSiPJQsGIKtQvyCaZY2szyOoeUGgOId+He7ITlezxKrjdj+1pLMESvAxKeo=
name: "Test valid Cert info",
annTLS: []*tlsAnnotation{
{
TlsSecretName: "tls-secret",
TLSSecretName: "tls-secret",
Port: 8080,
},
},
Expand All @@ -1052,7 +1052,7 @@ bSiPJQsGIKtQvyCaZY2szyOoeUGgOId+He7ITlezxKrjdj+1pLMESvAxKeo=
name: "Test invalid Cert info",
annTLS: []*tlsAnnotation{
{
TlsSecretName: "tls-secret",
TLSSecretName: "tls-secret",
Port: 8080,
},
},
Expand All @@ -1066,7 +1066,7 @@ bSiPJQsGIKtQvyCaZY2szyOoeUGgOId+He7ITlezxKrjdj+1pLMESvAxKeo=
name: "Test no secret found",
annTLS: []*tlsAnnotation{
{
TlsSecretName: "secret",
TLSSecretName: "secret",
Port: 8080,
},
},
Expand Down

0 comments on commit 22f0c34

Please sign in to comment.