Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lease registry in kubernetes support #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gobuild
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
my-config.yaml
.gobuild
my-config.yaml
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.gopath": "${workspaceRoot}/.gobuild"
}
8 changes: 3 additions & 5 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
FROM golang:1.10.0-alpine AS build-env

ARG GOARCH=amd64
ENV GOPATH /usr/code
ENV GOPATH /usr/code/.gobuild
ENV CGO_ENABLED 0

WORKDIR /usr/code
COPY vendor/github.com /usr/code/src/github.com/
COPY vendor/golang.org /usr/code/src/golang.org/
COPY vendor/gopkg.in /usr/code/src/gopkg.in/
ADD . /usr/code
RUN GOARCH=${GOARCH} go build -installsuffix cgo -o kube-dhcp-${GOARCH}
RUN mkdir -p /usr/code/.gobuild/src/github.com/pulcy && ln -s ../../../../ /usr/code/.gobuild/src/github.com/pulcy/kube-dhcp
RUN GOARCH=${GOARCH} go build -installsuffix cgo -o kube-dhcp-${GOARCH} github.com/pulcy/kube-dhcp
RUN ls -al

# final stage
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SCRIPTDIR := $(shell pwd)
GOBUILDDIR := $(SCRIPTDIR)/.gobuild

ifndef DOCKERIMAGE
DOCKERIMAGE := kube-dhcp:dev
endif
Expand All @@ -11,16 +14,21 @@ build:

# Build all images for all archs.
.PHONY: all
all:
all: pkg/registry/kube_lease.pb.go
@${MAKE} -B DOCKERIMAGE=$(DOCKERIMAGE) GOARCH=amd64 build-arch
@${MAKE} -B DOCKERIMAGE=$(DOCKERIMAGE) GOARCH=arm build-arch
@${MAKE} -B DOCKERIMAGE=$(DOCKERIMAGE) GOARCH=arm64 build-arch

.PHONY: build-arch
.PHONY: build-arch
build-arch:
docker build -f Dockerfile.build --build-arg=GOARCH=$(GOARCH) -t $(DOCKERIMAGE)-$(GOARCH) .
docker push $(DOCKERIMAGE)-$(GOARCH)

.PHONY: development-env
development-env:
pulsar go path
GOPATH=$(GOBUILDDIR) pulsar go flatten -V vendor

$(MANIFESTTOOL):
go get github.com/estesp/manifest-tool

Expand Down
31 changes: 28 additions & 3 deletions deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ metadata:

---

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: leases.dhcp.pulcy.com
spec:
group: dhcp.pulcy.com
version: v1
# either Namespaced or Cluster
scope: Cluster
names:
plural: leases
singular: lease
kind: Lease
shortNames:
- dl

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
Expand All @@ -14,9 +32,16 @@ rules:
- ""
resources:
- configmaps
- namespaces
verbs:
- "*"

- apiGroups:
- dhcp.pulcy.com
resources:
- leases
verbs:
- "*"

---

apiVersion: rbac.authorization.k8s.io/v1beta1
Expand All @@ -40,7 +65,7 @@ metadata:
name: kube-dhcp
namespace: dhcp-system
spec:
replicas: 1
replicas: 2
template:
metadata:
labels:
Expand All @@ -50,7 +75,7 @@ spec:
containers:
- name: server
imagePullPolicy: IfNotPresent
image: pulcy/kube-dhcp@sha256:adc24ec063a43e51d25d77975ff999066db8720708bc33ea66311afdaa1ca2ef
image: pulcy/kube-dhcp-arm64@sha256:e5581b1998892c36c3295e9dbf8eecc92187f5549f1a94b3b6b7b231dd66a8fc
env:
- name: METADATA_NAMESPACE
valueFrom:
Expand Down
50 changes: 0 additions & 50 deletions lease.go

This file was deleted.

41 changes: 32 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/ericchiang/k8s"
"github.com/pkg/errors"
"github.com/spf13/pflag"

"github.com/pulcy/kube-dhcp/pkg/registry"
"github.com/pulcy/kube-dhcp/pkg/service"
)

var (
Expand All @@ -27,6 +30,10 @@ func main() {
if namespace == "" {
log.Fatal("METADATA_NAMESPACE not set\n")
}
name := os.Getenv("METADATA_NAME")
if name == "" {
log.Fatal("METADATA_NAME not set\n")
}
nodeIP := os.Getenv("METADATA_NODE_IP")
if nodeIP == "" {
log.Fatal("METADATA_NODE_IP not set\n")
Expand All @@ -36,25 +43,41 @@ func main() {
if err != nil {
log.Fatal(err)
}
registry := registry.NewKubeLeaseRegistry(client)

// Watch for config changes, relaunch handler on a valid change.
ctx := context.Background()
configChan := make(chan DHCPConfig)
go watchForConfigChanges(ctx, client, options.configMapName, namespace, nodeIP, configChan)
configChan := make(chan service.DHCPConfig)
go service.WatchForConfigChanges(ctx, client, options.configMapName, namespace, nodeIP, configChan)
leaderChan := make(chan bool, 32)
go service.PerformLeaderElection(ctx, client, namespace, name, leaderChan)

log.Printf("Starting kube-dhcp on %s\n", nodeIP)

var stopFunc context.CancelFunc
isLeader := false
var config *service.DHCPConfig
for {
select {
case config := <-configChan:
// Create handler
handler, err := NewHandler(config)
case l := <-leaderChan:
if isLeader == l {
continue
}
isLeader = l
case cfg := <-configChan:
config = &cfg
}
// Stop current handler (if any)
if stopFunc != nil {
stopFunc()
stopFunc = nil
}
// Create handler if leader & have config
if isLeader && config != nil {
handler, err := service.NewHandler(*config, registry)
if err != nil {
log.Fatalf("Creating handler failed: %s\n", err)
}
// Stop current handler
if stopFunc != nil {
stopFunc()
}
// Prepare context for new handler
handlerCtx, cancel := context.WithCancel(ctx)
go func() {
Expand Down
76 changes: 0 additions & 76 deletions memory_lease_registry.go

This file was deleted.

9 changes: 9 additions & 0 deletions pkg/registry/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package registry

import (
"github.com/pkg/errors"
)

var (
maskAny = errors.WithStack
)
Loading