Skip to content

Commit

Permalink
refactor pod controller
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Feb 24, 2020
1 parent 9759ccc commit 7d918f5
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 338 deletions.
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ REGISTRY=index.alauda.cn/alaudak8s
DEV_TAG=dev
RELEASE_TAG=$(shell cat VERSION)

.PHONY: build-dev-images build-go build-bin test lint up down halt suspend resume kind push-dev push-release
.PHONY: build-dev-images build-go build-bin lint up down halt suspend resume kind push-dev push-release

build-dev-images: build-bin
docker build -t ${REGISTRY}/kube-ovn:${DEV_TAG} -f dist/images/Dockerfile dist/images/
Expand All @@ -24,16 +24,13 @@ release: lint build-go
docker build -t ${REGISTRY}/kube-ovn:${RELEASE_TAG} -f dist/images/Dockerfile dist/images/

push-release:
docker push ${REGISTRY}/kube-ovn:${RELEASE_TAG}
docker push ${REGISTRY}/kube-ovn:${RELEASE_TAG}

lint:
@gofmt -d ${GOFILES_NOVENDOR}
@gofmt -l ${GOFILES_NOVENDOR} | read && echo "Code differs from gofmt's style" 1>&2 && exit 1 || true
@GOOS=linux go vet ./...

test:
GOOS=linux go test -cover -v ./...

build-bin:
docker run --rm -e GOOS=linux -e GOCACHE=/tmp \
-u $(shell id -u):$(shell id -g) \
Expand Down Expand Up @@ -84,4 +81,4 @@ kind-clean:
kind delete cluster --name=kube-ovn

e2e:
ginkgo -p --slowSpecThreshold=30 --flakeAttempts=3 test/e2e
ginkgo -p --slowSpecThreshold=60 test/e2e
2 changes: 1 addition & 1 deletion cmd/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Scheme: scheme,
// disable metrics to avoid port conflict
MetricsBindAddress: "0",
})
Expand Down
19 changes: 7 additions & 12 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ func ParseFlags() (*Configuration, error) {
config.NodeSwitchGateway = gw
}

err := config.initKubeClient()
if err != nil {
if err := config.initKubeClient(); err != nil {
return nil, err
}

klog.Infof("config is %v", config)

klog.Infof("config is %+v", config)
return config, nil
}

Expand All @@ -147,19 +145,16 @@ func (config *Configuration) initKubeClient() error {
if config.KubeConfigFile == "" {
klog.Infof("no --kubeconfig, use in-cluster kubernetes config")
cfg, err = rest.InClusterConfig()
if err != nil {
klog.Errorf("use in cluster config failed %v", err)
return err
}
} else {
cfg, err = clientcmd.BuildConfigFromFlags("", config.KubeConfigFile)
if err != nil {
klog.Errorf("use --kubeconfig %s failed %v", config.KubeConfigFile, err)
return err
}
}
if err != nil {
klog.Errorf("failed to build kubeconfig %v", err)
return err
}
cfg.QPS = 1000
cfg.Burst = 2000

kubeOvnClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubeovn client failed %v", err)
Expand Down
16 changes: 3 additions & 13 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -128,7 +127,7 @@ func (c *Controller) handleAddNamespace(key string) error {
}

op := "replace"
if namespace.Annotations == nil {
if namespace.Annotations == nil || len(namespace.Annotations) == 0 {
op = "add"
namespace.Annotations = map[string]string{}
} else {
Expand All @@ -142,17 +141,8 @@ func (c *Controller) handleAddNamespace(key string) error {
namespace.Annotations[util.LogicalSwitchAnnotation] = subnet.Name
namespace.Annotations[util.CidrAnnotation] = subnet.Spec.CIDRBlock
namespace.Annotations[util.ExcludeIpsAnnotation] = strings.Join(subnet.Spec.ExcludeIps, ",")
patchPayloadTemplate :=
`[{
"op": "%s",
"path": "/metadata/annotations",
"value": %s
}]`

raw, _ := json.Marshal(namespace.Annotations)
patchPayload := fmt.Sprintf(patchPayloadTemplate, op, raw)
_, err = c.config.KubeClient.CoreV1().Namespaces().Patch(key, types.JSONPatchType, []byte(patchPayload))
if err != nil {

if _, err = c.config.KubeClient.CoreV1().Namespaces().Patch(key, types.JSONPatchType, generatePatchPayload(namespace.Annotations, op)); err != nil {
klog.Errorf("patch namespace %s failed %v", key, err)
}
return err
Expand Down

0 comments on commit 7d918f5

Please sign in to comment.