Skip to content

Commit

Permalink
Merge pull request #80 from Charliekenney23/update-k8s-deps
Browse files Browse the repository at this point in the history
update k8s dependencies
  • Loading branch information
0xch4z authored Jan 20, 2021
2 parents a0465bd + b21d579 commit 680fdd9
Show file tree
Hide file tree
Showing 25 changed files with 1,108 additions and 577 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
- GO111MODULE=on

go:
- 1.12.x
- 1.15.x
- tip

cache:
Expand All @@ -33,6 +33,6 @@ script:
- make
- make test
- make docker-build
- golangci-lint run
- golangci-lint run --timeout=2m
- travis_wait 20 roveralls -ignore e2e
- goveralls -coverprofile=roveralls.coverprofile -service=travis-ci
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fmt: vet $(GOPATH)/bin/goimports
.PHONY: test
# we say code is not worth testing unless it's formatted
test: $(GOPATH)/bin/ginkgo fmt
ginkgo -r --v --progress --trace --cover --skipPackage=test $(TEST_ARGS) -- --v=3
ginkgo -r --v --progress --trace --cover --skipPackage=test $(TEST_ARGS)

.PHONY: build-linux
build-linux:
Expand Down
15 changes: 7 additions & 8 deletions cloud/linode/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/linode/linodego"
"github.com/spf13/pflag"
"k8s.io/client-go/informers"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/controller"
cloudprovider "k8s.io/cloud-provider"
)

const (
Expand Down Expand Up @@ -70,17 +69,13 @@ func newCloud() (cloudprovider.Interface, error) {
}, nil
}

func (c *linodeCloud) Initialize(clientBuilder controller.ControllerClientBuilder) {
func (c *linodeCloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stopCh <-chan struct{}) {
kubeclient := clientBuilder.ClientOrDie("linode-shared-informers")
sharedInformer := informers.NewSharedInformerFactory(kubeclient, 0)
serviceInformer := sharedInformer.Core().V1().Services()

serviceController := newServiceController(c.loadbalancers.(*loadbalancers), serviceInformer)

// in future version of the cloudprovider package, we should use the stopCh provided to
// (cloudprovider.Interface).Initialize instead
forever := make(chan struct{})
go serviceController.Run(forever)
go serviceController.Run(stopCh)
}

func (c *linodeCloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
Expand All @@ -91,6 +86,10 @@ func (c *linodeCloud) Instances() (cloudprovider.Instances, bool) {
return c.instances, true
}

func (c *linodeCloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
return nil, false
}

func (c *linodeCloud) Zones() (cloudprovider.Zones, bool) {
return c.zones, true
}
Expand Down
2 changes: 1 addition & 1 deletion cloud/linode/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/cloudprovider"
cloudprovider "k8s.io/cloud-provider"
)

type instances struct {
Expand Down
41 changes: 24 additions & 17 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/cloudprovider"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"

"github.com/linode/linode-cloud-controller-manager/sentry"
"github.com/linode/linodego"
Expand Down Expand Up @@ -111,7 +111,7 @@ func (l *loadbalancers) getLatestServiceLoadBalancerStatus(ctx context.Context,
return v1.LoadBalancerStatus{}, err
}

service, err = l.kubeClient.CoreV1().Services(service.Namespace).Get(service.Name, metav1.GetOptions{})
service, err = l.kubeClient.CoreV1().Services(service.Namespace).Get(ctx, service.Name, metav1.GetOptions{})
if err != nil {
return v1.LoadBalancerStatus{}, err
}
Expand Down Expand Up @@ -164,6 +164,14 @@ func (l *loadbalancers) cleanupOldNodeBalancer(ctx context.Context, service *v1.
return nil
}

// GetLoadBalancerName returns the name of the load balancer.
//
// GetLoadBalancer will not modify service.
func (l *loadbalancers) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string {
unixNano := strconv.FormatInt(time.Now().UnixNano(), 16)
return fmt.Sprintf("ccm-%s", unixNano[len(unixNano)-12:])
}

// GetLoadBalancer returns the *v1.LoadBalancerStatus of service.
//
// GetLoadBalancer will not modify service.
Expand Down Expand Up @@ -203,7 +211,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
nb, err = l.getNodeBalancerForService(ctx, service)
switch err.(type) {
case lbNotFoundError:
if nb, err = l.buildLoadBalancerRequest(ctx, service, nodes); err != nil {
if nb, err = l.buildLoadBalancerRequest(ctx, clusterName, service, nodes); err != nil {
sentry.CaptureError(ctx, err)
return nil, err
}
Expand Down Expand Up @@ -269,7 +277,7 @@ func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Serv
}

// Construct a new config for this port
newNBCfg, err := l.buildNodeBalancerConfig(service, int(port.Port))
newNBCfg, err := l.buildNodeBalancerConfig(ctx, service, int(port.Port))
if err != nil {
sentry.CaptureError(ctx, err)
return err
Expand Down Expand Up @@ -453,11 +461,10 @@ func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Ser
return nb, nil
}

func (l *loadbalancers) createNodeBalancer(ctx context.Context, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
connThrottle := getConnectionThrottle(service)

unixNano := strconv.FormatInt(time.Now().UnixNano(), 16)
label := fmt.Sprintf("ccm-%s", unixNano[len(unixNano)-12:])
label := l.GetLoadBalancerName(ctx, clusterName, service)
createOpts := linodego.NodeBalancerCreateOptions{
Label: &label,
Region: l.zone,
Expand All @@ -468,7 +475,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, service *v1.Serv
}

//nolint:funlen
func (l *loadbalancers) buildNodeBalancerConfig(service *v1.Service, port int) (linodego.NodeBalancerConfig, error) {
func (l *loadbalancers) buildNodeBalancerConfig(ctx context.Context, service *v1.Service, port int) (linodego.NodeBalancerConfig, error) {
portConfig, err := getPortConfig(service, port)
if err != nil {
return linodego.NodeBalancerConfig{}, err
Expand Down Expand Up @@ -534,21 +541,21 @@ func (l *loadbalancers) buildNodeBalancerConfig(service *v1.Service, port int) (
config.CheckPassive = checkPassive

if portConfig.Protocol == linodego.ProtocolHTTPS {
if err = l.addTLSCert(service, &config, portConfig); err != nil {
if err = l.addTLSCert(ctx, service, &config, portConfig); err != nil {
return config, err
}
}

return config, nil
}

func (l *loadbalancers) addTLSCert(service *v1.Service, nbConfig *linodego.NodeBalancerConfig, config portConfig) error {
func (l *loadbalancers) addTLSCert(ctx context.Context, service *v1.Service, nbConfig *linodego.NodeBalancerConfig, config portConfig) error {
err := l.retrieveKubeClient()
if err != nil {
return err
}

nbConfig.SSLCert, nbConfig.SSLKey, err = getTLSCertInfo(l.kubeClient, service.Namespace, config)
nbConfig.SSLCert, nbConfig.SSLKey, err = getTLSCertInfo(ctx, l.kubeClient, service.Namespace, config)
if err != nil {
return err
}
Expand All @@ -557,7 +564,7 @@ func (l *loadbalancers) addTLSCert(service *v1.Service, nbConfig *linodego.NodeB

// 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) {
func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) (*linodego.NodeBalancer, error) {
ports := service.Spec.Ports
configs := make([]*linodego.NodeBalancerConfigCreateOptions, 0, len(ports))

Expand All @@ -566,7 +573,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v
return nil, fmt.Errorf("error creating NodeBalancer Config: ports with the UDP protocol are not supported")
}

config, err := l.buildNodeBalancerConfig(service, int(port.Port))
config, err := l.buildNodeBalancerConfig(ctx, service, int(port.Port))
if err != nil {
return nil, err
}
Expand All @@ -578,7 +585,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v

configs = append(configs, &createOpt)
}
return l.createNodeBalancer(ctx, service, configs)
return l.createNodeBalancer(ctx, clusterName, service, configs)
}

func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePort int32) linodego.NodeBalancerNodeCreateOptions {
Expand Down Expand Up @@ -705,12 +712,12 @@ func getNodeInternalIP(node *v1.Node) string {
return ""
}

func getTLSCertInfo(kubeClient kubernetes.Interface, namespace string, config portConfig) (string, string, error) {
func getTLSCertInfo(ctx context.Context, kubeClient kubernetes.Interface, namespace string, config portConfig) (string, string, error) {
if config.TLSSecretName == "" {
return "", "", fmt.Errorf("TLS secret name for port %v is not specified", config.Port)
}

secret, err := kubeClient.CoreV1().Secrets(namespace).Get(config.TLSSecretName, metav1.GetOptions{})
secret, err := kubeClient.CoreV1().Secrets(namespace).Get(ctx, config.TLSSecretName, metav1.GetOptions{})
if err != nil {
return "", "", err
}
Expand Down
14 changes: 7 additions & 7 deletions cloud/linode/loadbalancers_deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func testBuildLoadBalancerRequestDeprecated(t *testing.T, client *linodego.Clien
}

lb := &loadbalancers{client, "us-west", nil}
nb, err := lb.buildLoadBalancerRequest(context.TODO(), svc, nodes)
nb, err := lb.buildLoadBalancerRequest(context.TODO(), "linodelb", svc, nodes)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -187,11 +187,11 @@ func testEnsureLoadBalancerDeletedDeprecated(t *testing.T, client *linodego.Clie

lb := &loadbalancers{client, "us-west", nil}
configs := []*linodego.NodeBalancerConfigCreateOptions{}
_, err := lb.createNodeBalancer(context.TODO(), svc, configs)
_, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
if err != nil {
t.Fatal(err)
}
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "lnodelb", svc) }()
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc) }()

for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -235,13 +235,13 @@ func testEnsureLoadBalancerDeprecated(t *testing.T, client *linodego.Client) {
lb := &loadbalancers{client, "us-west", nil}

configs := []*linodego.NodeBalancerConfigCreateOptions{}
nb, err := lb.createNodeBalancer(context.TODO(), svc, configs)
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
if err != nil {
t.Fatal(err)
}

svc.Status.LoadBalancer = *makeLoadBalancerStatus(nb)
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "lnodelb", svc) }()
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc) }()
lbStatus, exists, err := lb.GetLoadBalancer(context.TODO(), "linodelb", svc)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -351,14 +351,14 @@ func testGetLoadBalancerDeprecated(t *testing.T, client *linodego.Client) {
}

configs := []*linodego.NodeBalancerConfigCreateOptions{}
nb, err := lb.createNodeBalancer(context.TODO(), svc, configs)
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
if err != nil {
t.Fatal(err)
}

lbStatus := makeLoadBalancerStatus(nb)
svc.Status.LoadBalancer = *lbStatus
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "lnodelb", svc) }()
defer func() { _ = lb.EnsureLoadBalancerDeleted(context.TODO(), "linodelb", svc) }()
testcases := []struct {
name string
service *v1.Service
Expand Down
Loading

0 comments on commit 680fdd9

Please sign in to comment.