Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Aug 31, 2017
1 parent c99082b commit eea2a27
Show file tree
Hide file tree
Showing 146 changed files with 8,870 additions and 5,488 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
kube-keepalived-vip
rootfs/kube-keepalived-vip
rootfs/keepalived.tar.gz
7 changes: 7 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
## 0.17
- Cleanup
- Configmap update detection

## 0.16
- Fix DR mode issue

## 0.15
- Update keepalived to 1.3.6
- Update go dependencies
- Migrate to client-go
Expand Down
26 changes: 13 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
name = "k8s.io/apimachinery"

[[constraint]]
name = "k8s.io/client-go"
branch = "master"
name = "k8s.io/client-go"

[[constraint]]
name = "k8s.io/kubernetes"
version = "1.7.4"
version = "1.7.5"

[[constraint]]
branch = "master"
Expand Down
15 changes: 9 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
all: push

# 0.0 shouldn't clobber any release builds, current "latest" is 0.16
TAG = 0.16
# 0.0 shouldn't clobber any release builds, current "latest" is 0.17
TAG = 0.17
PREFIX = aledbf/kube-keepalived-vip
BUILD_IMAGE = build-keepalived
PKG = github.com/aledbf/kube-keepalived-vip

controller: clean
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w' -o kube-keepalived-vip
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w' \
-o rootfs/kube-keepalived-vip \
${PKG}/pkg/cmd

container: controller keepalived
docker build -t $(PREFIX):$(TAG) .
docker build -t $(PREFIX):$(TAG) rootfs

keepalived:
docker build -t $(BUILD_IMAGE):$(TAG) build
docker create --name $(BUILD_IMAGE) $(BUILD_IMAGE):$(TAG) true
# docker cp semantics changed between 1.7 and 1.8, so we cp the file to cwd and rename it.
docker cp $(BUILD_IMAGE):/keepalived.tar.gz .
docker cp $(BUILD_IMAGE):/keepalived.tar.gz rootfs
docker rm -f $(BUILD_IMAGE)

push: container
docker -- push $(PREFIX):$(TAG)
docker push $(PREFIX):$(TAG)

clean:
rm -f kube-keepalived-vip
104 changes: 65 additions & 39 deletions main.go → pkg/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"

"github.com/golang/glog"
"github.com/spf13/pflag"

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/kubernetes/pkg/util/sysctl"
k8sexec "k8s.io/utils/exec"

"github.com/aledbf/kube-keepalived-vip/pkg/controller"
)

var (
flags = pflag.NewFlagSet("", pflag.ContinueOnError)
flags = pflag.NewFlagSet("", pflag.ExitOnError)

apiserverHost = flags.String("apiserver-host", "", "The address of the Kubernetes Apiserver "+
"to connect to in the format of protocol://address:port, e.g., "+
Expand Down Expand Up @@ -94,12 +96,11 @@ func main() {
glog.Info("keepalived will use unicast to sync the nodes")
}

kubeClient, err := createApiserverClient(*apiserverHost, *kubeConfigFile)
if err != nil {
handleFatalInitError(err)
if *vrid < 0 || *vrid > 255 {
glog.Fatalf("Error using VRID %d, only values between 0 and 255 are allowed.", vrid)
}

err = loadIPVModule()
err := loadIPVModule()
if err != nil {
glog.Fatalf("unexpected error: %v", err)
}
Expand All @@ -109,10 +110,6 @@ func main() {
glog.Fatalf("unexpected error: %v", err)
}

if *vrid < 0 || *vrid > 255 {
glog.Fatalf("Error using VRID %d, only values between 0 and 255 are allowed.", vrid)
}

err = resetIPVS()
if err != nil {
glog.Fatalf("unexpected error: %v", err)
Expand All @@ -122,34 +119,15 @@ func main() {
copyHaproxyCfg()
}

glog.Info("starting LVS configuration")
ipvsc := newIPVSController(kubeClient, *watchNamespace, *useUnicast, *configMapName, *vrid, *proxyMode)

go ipvsc.epController.Run(wait.NeverStop)
go ipvsc.svcController.Run(wait.NeverStop)

go ipvsc.syncQueue.run(time.Second, ipvsc.stopCh)

go handleSigterm(ipvsc)

glog.Info("starting keepalived to announce VIPs")
ipvsc.keepalived.Start()
}

func handleSigterm(ipvsc *ipvsControllerController) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM)
<-signalChan
glog.Infof("Received SIGTERM, shutting down")

exitCode := 0
if err := ipvsc.Stop(); err != nil {
glog.Infof("Error during shutdown %v", err)
exitCode = 1
kubeClient, err := createApiserverClient(*apiserverHost, *kubeConfigFile)
if err != nil {
handleFatalInitError(err)
}

glog.Infof("Exiting with %v", exitCode)
os.Exit(exitCode)
glog.Info("starting LVS configuration")
ipvsc := controller.NewIPVSController(kubeClient, *watchNamespace, *useUnicast, *configMapName, *vrid, *proxyMode)

ipvsc.Start()
}

const (
Expand Down Expand Up @@ -217,3 +195,51 @@ func handleFatalInitError(err error) {
"This most likely means that the cluster is misconfigured (e.g., it has "+
"invalid apiserver certificates or service accounts configuration). Reason: %s\n", err)
}

// loadIPVModule load module require to use keepalived
func loadIPVModule() error {
out, err := k8sexec.New().Command("modprobe", "ip_vs").CombinedOutput()
if err != nil {
glog.V(2).Infof("Error loading ip_vip: %s, %v", string(out), err)
return err
}

_, err = os.Stat("/proc/net/ip_vs")
return err
}

// changeSysctl changes the required network setting in /proc to get
// keepalived working in the local system.
func changeSysctl() error {
sys := sysctl.New()
for k, v := range sysctlAdjustments {
if err := sys.SetSysctl(k, v); err != nil {
return err
}
}

return nil
}

func resetIPVS() error {
glog.Info("cleaning ipvs configuration")
_, err := k8sexec.New().Command("ipvsadm", "-C").CombinedOutput()
if err != nil {
return fmt.Errorf("error removing ipvs configuration: %v", err)
}

return nil
}

// copyHaproxyCfg copies the default haproxy configuration file
// to the mounted directory (the mount overwrites the default file)
func copyHaproxyCfg() {
data, err := ioutil.ReadFile("/haproxy.cfg")
if err != nil {
glog.Fatalf("unexpected error reading haproxy.cfg: %v", err)
}
err = ioutil.WriteFile("/etc/haproxy/haproxy.cfg", data, 0644)
if err != nil {
glog.Fatalf("unexpected error writing haproxy.cfg: %v", err)
}
}
12 changes: 1 addition & 11 deletions keepalived.go → pkg/controller/keepalived.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package controller

import (
"encoding/json"
Expand Down Expand Up @@ -183,16 +183,6 @@ func (k *keepalived) Stop() {
}
}

func resetIPVS() error {
glog.Info("cleaning ipvs configuration")
_, err := k8sexec.New().Command("ipvsadm", "-C").CombinedOutput()
if err != nil {
return fmt.Errorf("error removing ipvs configuration: %v", err)
}

return nil
}

func (k *keepalived) removeVIP(vip string) error {
glog.Infof("removing configured VIP %v", vip)
out, err := k8sexec.New().Command("ip", "addr", "del", vip+"/32", "dev", k.iface).CombinedOutput()
Expand Down
Loading

0 comments on commit eea2a27

Please sign in to comment.