Skip to content

Commit

Permalink
Add re-use fips for nodepools
Browse files Browse the repository at this point in the history
  • Loading branch information
jknipper committed Feb 8, 2023
1 parent 98db57c commit 97477f7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion kube-fip-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19.3-alpine3.16 as builder
FROM golang:1.19-alpine3.16 as builder
WORKDIR /go/src/github.com/sapcc/kubernetes-operators/kube-fip-controller
RUN apk add --no-cache make
COPY . .
Expand Down
5 changes: 3 additions & 2 deletions kube-fip-controller/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
DATE = $(shell date +%Y%m%d%H%M)
IMAGE ?= sapcc/kube-fip-controller
VERSION = v$(DATE)
VERSION = v$(DATE)-test
GOOS ?= $(shell go env | grep GOOS | cut -d'"' -f2)
BINARY := controller
OPTS ?=

SRCDIRS := cmd pkg
PACKAGES := $(shell find $(SRCDIRS) -type d)
Expand All @@ -20,7 +21,7 @@ bin/%/$(BINARY): $(GOFILES) Makefile
GOOS=$* GOARCH=amd64 go build -ldflags '-X github.com/sapcc/kubernetes-operators/kube-fip-controller/cmd.BuildCommit=$(GIT_COMMIT) -X github.com/sapcc/kubernetes-operators/kube-fip-controller/cmd.BuildDate=$(BUILD_DATE)' -mod vendor -v -o bin/$*/$(BINARY) ./cmd/main.go && chmod +x bin/$*/$(BINARY)

build:
docker build -t $(IMAGE):$(VERSION) .
docker build $(OPTS) -t $(IMAGE):$(VERSION) .

static-check:
@if s="$$(gofmt -s -l *.go pkg 2>/dev/null)" && test -n "$$s"; then printf ' => %s\n%s\n' gofmt "$$s"; false; fi
Expand Down
18 changes: 17 additions & 1 deletion kube-fip-controller/pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const (

//labelFloatingSubnetName controls which floating subnet is used for the FIP.
labelFloatingSubnetName = "kube-fip-controller.ccloud.sap.com/floating-subnet-name"

// labelNodepoolName label used to identify nodepools
labelNodepoolName = "ccloud.sap.com/nodepool"

// labelReuseFIPs indicates if FIPs should be re-used for a certain nodepool
labelReuseFIPs = "kube-fip-controller.ccloud.sap.com/reuse-fips"
)

// Controller ...
Expand Down Expand Up @@ -207,7 +213,17 @@ func (c *Controller) syncHandler(key string) error {
return err
}

fip, err := c.osFramework.GetOrCreateFloatingIP(floatingIP, floatingNetworkID, floatingSubnetID, server.TenantID)
nodepool := ""
if val, ok := getLabelValue(node, labelNodepoolName); ok {
nodepool = val
}

reuseFIPs := false
if val, ok := getLabelValue(node, labelReuseFIPs); ok {
reuseFIPs = (val == "true")
}

fip, err := c.osFramework.GetOrCreateFloatingIP(floatingIP, floatingNetworkID, floatingSubnetID, server.TenantID, nodepool, reuseFIPs)
if err != nil {
return err
}
Expand Down
28 changes: 22 additions & 6 deletions kube-fip-controller/pkg/frameworks/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ import (
"github.com/sapcc/kubernetes-operators/kube-fip-controller/pkg/metrics"
)

const statusActive = "ACTIVE"
const (
statusActive = "ACTIVE"
createFIPDescription = "Floating IP allocated by kube-fip-controller"
createFIPDescriptionNodepool = "Floating IP allocated by kube-fip-controller nodepool=%s"
)

var allProjectsHeader = map[string]string{"X-Auth-All-Projects": "true"}

Expand Down Expand Up @@ -193,14 +197,14 @@ func (o *OSFramework) GetSubnetIDByName(name string) (string, error) {
}

// GetOrCreateFloatingIP gets and existing or create a new neutron floating IP and returns it or an error.
func (o *OSFramework) GetOrCreateFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID string) (*neutronfip.FloatingIP, error) {
fip, err := o.getFloatingIP(floatingIP, projectID)
func (o *OSFramework) GetOrCreateFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID, nodepool string, reuse bool) (*neutronfip.FloatingIP, error) {
fip, err := o.getFloatingIP(floatingIP, projectID, nodepool, reuse)
if err == nil {
return fip, nil
}

if IsFIPNotFound(err) {
return o.createFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID)
return o.createFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID, nodepool)
}

return nil, err
Expand Down Expand Up @@ -246,12 +250,18 @@ func (o *OSFramework) getPortByID(id string) (*ports.Port, error) {
return ports.Get(o.neutronClient, id).Extract()
}

func (o *OSFramework) createFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID string) (*neutronfip.FloatingIP, error) {
func (o *OSFramework) createFloatingIP(floatingIP, floatingNetworkID, subnetID, projectID, nodepool string) (*neutronfip.FloatingIP, error) {
description := createFIPDescription
if nodepool != "" {
description = fmt.Sprintf(createFIPDescriptionNodepool, nodepool)
}

createOpts := neutronfip.CreateOpts{
FloatingNetworkID: floatingNetworkID,
SubnetID: subnetID,
FloatingIP: floatingIP,
ProjectID: projectID,
Description: description,
}
fip, err := neutronfip.Create(o.neutronClient, createOpts).Extract()
if err != nil {
Expand All @@ -263,11 +273,14 @@ func (o *OSFramework) createFloatingIP(floatingIP, floatingNetworkID, subnetID,
return fip, nil
}

func (o *OSFramework) getFloatingIP(floatingIP, projectID string) (*neutronfip.FloatingIP, error) {
func (o *OSFramework) getFloatingIP(floatingIP, projectID, nodepool string, reuse bool) (*neutronfip.FloatingIP, error) {
listOpts := neutronfip.ListOpts{
FloatingIP: floatingIP,
ProjectID: projectID,
}
if reuse && floatingIP == "" && nodepool != "" {
listOpts.Description = fmt.Sprintf(createFIPDescriptionNodepool, nodepool)
}
allPages, err := neutronfip.List(o.neutronClient, listOpts).AllPages()
if err != nil {
return nil, err
Expand All @@ -282,6 +295,9 @@ func (o *OSFramework) getFloatingIP(floatingIP, projectID string) (*neutronfip.F
if fip.FloatingIP == floatingIP {
return &fip, nil
}
if reuse && floatingIP == "" && nodepool != "" && fip.FixedIP == "" {
return &fip, nil
}
}

return nil, ErrFIPNotFound
Expand Down

0 comments on commit 97477f7

Please sign in to comment.