Skip to content

Commit

Permalink
add admission webhook for static ip
Browse files Browse the repository at this point in the history
  • Loading branch information
halfcrazy committed Jul 19, 2019
1 parent 97efc1f commit 766cec9
Show file tree
Hide file tree
Showing 231 changed files with 34,892 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist/images/kube-ovn
dist/images/kube-ovn-controller
dist/images/kube-ovn-daemon
dist/images/kube-ovn-gateway
dist/images/kube-ovn-webhook
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GOFILES_NOVENDOR=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
GO_VERSION=1.12

REGISTRY=index.alauda.cn/alaudak8s
ROLES=node controller cni db
ROLES=node controller cni db webhook
DEV_TAG=dev
RELEASE_TAG=$(shell cat VERSION)

Expand All @@ -18,6 +18,7 @@ build-go:
CGO_ENABLED=0 GOOS=linux go build -o $(PWD)/dist/images/kube-ovn -ldflags "-w -s" -v ./cmd/cni
CGO_ENABLED=0 GOOS=linux go build -o $(PWD)/dist/images/kube-ovn-controller -ldflags "-w -s" -v ./cmd/controller
CGO_ENABLED=0 GOOS=linux go build -o $(PWD)/dist/images/kube-ovn-daemon -ldflags "-w -s" -v ./cmd/daemon
CGO_ENABLED=0 GOOS=linux go build -o $(PWD)/dist/images/kube-ovn-webhook -ldflags "-w -s" -v ./cmd/webhook

release: build-go
@for role in ${ROLES} ; do \
Expand Down
24 changes: 3 additions & 21 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"strings"
"time"

"github.com/alauda/kube-ovn/pkg/controller"
"github.com/alauda/kube-ovn/pkg/ovs"

"k8s.io/klog"
"k8s.io/sample-controller/pkg/signals"
)
Expand Down Expand Up @@ -55,25 +55,7 @@ func loopOvnNbctlDaemon(config *controller.Configuration) {
time.Sleep(5 * time.Second)

if _, err := os.Stat(daemonSocket); os.IsNotExist(err) || daemonSocket == "" {
startOvnNbctlDaemon(config.OvnNbHost, config.OvnNbPort)
ovs.StartOvnNbctlDaemon(config.OvnNbHost, config.OvnNbPort)
}
}
}

func startOvnNbctlDaemon(nbHost string, nbPort int) (string, error) {
klog.Infof("start ovn-nbctl daemon")
output, err := exec.Command(
"ovn-nbctl",
fmt.Sprintf("--db=tcp:%s:%d", nbHost, nbPort),
"--pidfile",
"--detach",
).CombinedOutput()
if err != nil {
klog.Errorf("start ovn-nbctl daemon failed, %s", string(output))
return "", err
}

daemonSocket := strings.TrimSpace(string(output))
os.Setenv("OVN_NB_DAEMON", daemonSocket)
return daemonSocket, nil
}
101 changes: 101 additions & 0 deletions cmd/webhook/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"flag"
_ "net/http/pprof"
"os"
"time"

ovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/alauda/kube-ovn/pkg/ovs"
ovnwebhook "github.com/alauda/kube-ovn/pkg/webhook"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
)

const (
hookServerCertDir = "/tmp/k8s-webhook-server/serving-certs"
)

var (
scheme = runtime.NewScheme()
)

func init() {
corev1.AddToScheme(scheme)
appsv1.AddToScheme(scheme)
ovnv1.AddToScheme(scheme)
}

func main() {
var (
port int
ovnNbHost string
ovnNbPort int
defaultLS string
)
flag.IntVar(&port, "port", 8443, "The port webhook listen on.")
flag.IntVar(&ovnNbPort, "ovn-nb-port", 6641, "OVN nb port")
flag.StringVar(&ovnNbHost, "ovn-nb-host", "0.0.0.0", "OVN nb host")
flag.StringVar(&defaultLS, "default-ls", "ovn-default", "The default logical switch name, default: ovn-default")

klog.InitFlags(nil)
flag.Parse()

// Create a webhook server.
hookServer := &ctrlwebhook.Server{
Port: port,
CertDir: hookServerCertDir,
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
LeaderElection: true,
LeaderElectionNamespace: os.Getenv("KUBE_NAMESPACE"),
LeaderElectionID: os.Getenv("POD_NAME"),
// disable metrics to avoid port conflict
MetricsBindAddress: "0",
})
if err != nil {
panic(err)
}

opt := &ovnwebhook.WebhookOptions{
OvnNbHost: ovnNbHost,
OvnNbPort: ovnNbPort,
DefaultLS: defaultLS,
}
validatingHook, err := ovnwebhook.NewValidatingHook(mgr.GetCache(), opt)
if err != nil {
panic(err)
}
// Register the webhooks in the server.
hookServer.Register("/validate-ip", &ctrlwebhook.Admission{Handler: validatingHook})

if err := mgr.Add(hookServer); err != nil {
panic(err)
}

go loopOvnNbctlDaemon(ovnNbHost, ovnNbPort)

// Start the server by starting a previously-set-up manager
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
panic(err)
}
}

func loopOvnNbctlDaemon(ovnNbHost string, ovnNbPort int) {
for {
daemonSocket := os.Getenv("OVN_NB_DAEMON")
time.Sleep(5 * time.Second)

if _, err := os.Stat(daemonSocket); os.IsNotExist(err) || daemonSocket == "" {
ovs.StartOvnNbctlDaemon(ovnNbHost, ovnNbPort)
}
}
}
34 changes: 34 additions & 0 deletions dist/images/Dockerfile.webhook
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM centos:7

ENV PYTHONDONTWRITEBYTECODE yes

RUN yum install -y \
PyYAML bind-utils \
openssl \
numactl-libs \
firewalld-filesystem \
libpcap \
hostname \
iproute strace socat nc \
unbound unbound-devel python-openvswitch libreswan && \
yum clean all

ENV OVS_VERSION=2.11.1
ENV OVS_SUBVERSION=1

RUN rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/openvswitch-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/openvswitch-ipsec-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/openvswitch-devel-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-common-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-vtep-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-central-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm && \
rpm -ivh https://github.com/alauda/ovs/releases/download/v${OVS_VERSION}-${OVS_SUBVERSION}/ovn-host-${OVS_VERSION}-${OVS_SUBVERSION}.el7.x86_64.rpm

RUN mkdir -p /var/run/openvswitch
WORKDIR /kube-ovn

CMD ["sh", "start-webhook.sh"]

COPY start-webhook.sh /kube-ovn/start-webhook.sh
COPY kube-ovn-webhook /kube-ovn/kube-ovn-webhook
4 changes: 4 additions & 0 deletions dist/images/start-webhook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
export OVN_NB_DAEMON=$(ovn-nbctl --db=tcp:${OVN_NB_SERVICE_HOST}:${OVN_NB_SERVICE_PORT} --pidfile --detach)
exec ./kube-ovn-webhook --ovn-nb-host=${OVN_NB_SERVICE_HOST} --ovn-nb-port=${OVN_NB_SERVICE_PORT} $@
20 changes: 13 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ require (
github.com/coreos/go-iptables v0.4.0
github.com/elazarl/goproxy v0.0.0-20190630181448-f1e96bc0f4c5 // indirect
github.com/elazarl/goproxy/ext v0.0.0-20190630181448-f1e96bc0f4c5 // indirect
github.com/emicklei/go-restful v2.9.3+incompatible
github.com/evanphx/json-patch v4.5.0+incompatible // indirect
github.com/emicklei/go-restful v2.9.5+incompatible
github.com/go-ini/ini v1.42.0 // indirect
github.com/go-logr/zapr v0.1.1 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/hashicorp/go-version v1.2.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/imdario/mergo v0.3.7 // indirect
Expand All @@ -26,7 +25,6 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/parnurzeal/gorequest v0.2.15
github.com/pkg/errors v0.8.1 // indirect
github.com/projectcalico/felix v3.6.1+incompatible
github.com/projectcalico/go-json v0.0.0-20161128004156-6219dc7339ba // indirect
github.com/projectcalico/go-yaml v0.0.0-20161201183616-955bc3e451ef // indirect
Expand All @@ -36,20 +34,28 @@ require (
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.2.0 // indirect
github.com/prometheus/procfs v0.0.0-20190328153300-af7bedc223fb // indirect
github.com/sirupsen/logrus v1.4.1
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
github.com/spf13/pflag v1.0.3
github.com/vishvananda/netlink v1.0.0
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc // indirect
go.uber.org/zap v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.21.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.42.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
k8s.io/api v0.0.0-20190703205437-39734b2a72fe
k8s.io/apimachinery v0.0.0-20190703205208-4cfb76a8bf76
k8s.io/client-go v0.0.0-20190704045512-07281898b0f0
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
k8s.io/klog v0.3.1
k8s.io/kube-openapi v0.0.0-20190401085232-94e1e7b7574c // indirect
k8s.io/sample-controller v0.0.0-20190326030654-b8f621986e45
k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a // indirect
sigs.k8s.io/controller-runtime v0.2.0-alpha.0
)

replace (
k8s.io/client-go => k8s.io/client-go v0.0.0-20190620085101-78d2af792bab
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.2.0-beta.4
)

0 comments on commit 766cec9

Please sign in to comment.