Skip to content

Commit

Permalink
Merge pull request #664 from shkuviak/feature/service-dhcp-hostname
Browse files Browse the repository at this point in the history
Add annotation to specify DHCP lease hostname
  • Loading branch information
thebsdbox committed Dec 2, 2023
2 parents b42b782 + e7b68cf commit 247cffb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Dockerfile_iptables
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ RUN --mount=type=cache,sharing=locked,id=gomod,target=/go/pkg/mod/cache \
CGO_ENABLED=0 GOOS=linux make build

FROM alpine:3.18.4
# Add Certificates into the image, for anything that does API calls
RUN apk add --no-cache iptables
# Update pkgs and add iptables
RUN apk upgrade && \
apk add --no-cache iptables

# Add kube-vip binary
COPY --from=dev /src/kube-vip /
ENTRYPOINT ["/kube-vip"]
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TARGET := kube-vip
.DEFAULT_GOAL := $(TARGET)

# These will be provided to the target
VERSION := v0.6.3
VERSION := v0.6.4

BUILD := `git rev-parse HEAD`

Expand Down Expand Up @@ -127,3 +127,14 @@ e2e-tests:

service-tests:
E2E_IMAGE_PATH=$(REPOSITORY)/$(TARGET):$(DOCKERTAG) go run ./testing/e2e/services -Services

trivy: dockerx86ActionIPTables
docker run -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:0.47.0 \
image \
--format table \
--exit-code 1 \
--ignore-unfixed \
--vuln-type 'os,library' \
--severity 'CRITICAL,HIGH' \
$(REPOSITORY)/$(TARGET):action

21 changes: 21 additions & 0 deletions docs/usage/on-prem/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17m
nginx-dhcp LoadBalancer 10.97.150.208 192.168.0.155 80:31184/TCP 3s
```

You can also specify a hostname used for the DHCP lease by adding an annotation to your service.

```
apiVersion: v1
kind: Service
metadata:
name: nginx-dhcp
annotations:
kube-vip.io/loadbalancerHostname: mydhcp-test
spec:
loadBalancerIP: 0.0.0.0
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: hello-world
type: LoadBalancer
```

### Using UPnP to expose a Service to the outside world

With `kube-vip` > 0.2.1, it is possible to expose a Service of type `LoadBalancer` on a specific port to the Internet by using UPnP (on a supported gateway).
Expand Down
8 changes: 8 additions & 0 deletions pkg/manager/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Instance struct {
dhcpInterface string
dhcpInterfaceHwaddr string
dhcpInterfaceIP string
dhcpHostname string
dhcpClient *vip.DHCPClient

// Kubernetes service mapping
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewInstance(svc *v1.Service, config *kubevip.Config) (*Instance, error) {
if svc.Annotations != nil {
instance.dhcpInterfaceHwaddr = svc.Annotations[hwAddrKey]
instance.dhcpInterfaceIP = svc.Annotations[requestedIP]
instance.dhcpHostname = svc.Annotations[loadbalancerHostname]
}

// Generate Load Balancer config
Expand Down Expand Up @@ -179,6 +181,12 @@ func (i *Instance) startDHCP() error {

client := vip.NewDHCPClient(iface, initRebootFlag, i.dhcpInterfaceIP)

// Add hostname to dhcp client if annotated
if i.dhcpHostname != "" {
log.Infof("Hostname specified for dhcp lease: [%s] - [%s]", interfaceName, i.dhcpHostname)
client.WithHostName(i.dhcpHostname)
}

go client.Start()

// Set that DHCP is enabled
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
endpoint = "kube-vip.io/active-endpoint"
flushContrack = "kube-vip.io/flush-conntrack"
loadbalancerIPAnnotation = "kube-vip.io/loadbalancerIPs"
loadbalancerHostname = "kube-vip.io/loadbalancerHostname"
)

func (sm *Manager) syncServices(_ context.Context, svc *v1.Service, wg *sync.WaitGroup) error {
Expand Down

0 comments on commit 247cffb

Please sign in to comment.