Skip to content

Commit

Permalink
fix(cnivpc): When pod enable static ip, block ip assignment without i…
Browse files Browse the repository at this point in the history
…pamd (#34)
  • Loading branch information
fioncat authored Nov 23, 2023
1 parent 85d8dde commit 495b156
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ all: cnivpc

.PHONY: cnivpc-bin
cnivpc-bin:
go build ${LDFLAGS} -o ./bin/cnivpc ./cmd/cnivpc
go build ${LDFLAGS} -o ./bin/cnivpctl ./cmd/cnivpctl
CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build ${LDFLAGS} -o ./bin/cnivpc ./cmd/cnivpc
CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build ${LDFLAGS} -o ./bin/cnivpctl ./cmd/cnivpctl

.PHONY: cnivpc
cnivpc: cnivpc-bin
Expand All @@ -46,14 +46,14 @@ cnivpc: cnivpc-bin

.PHONY: ipamd
ipamd:
go build ${LDFLAGS} -o ./bin/cnivpc-ipamd ./cmd/cnivpc-ipamd
CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build ${LDFLAGS} -o ./bin/cnivpc-ipamd ./cmd/cnivpc-ipamd
$(DOCKER_CMD) build -t $(IPAMD_IMAGE) -f dockerfiles/ipamd/Dockerfile .
$(DOCKER_CMD) push $(IPAMD_IMAGE)
@echo "Build done: $(IPAMD_IMAGE)"

.PHONY: vip-controller
vip-controller:
go build ${LDFLAGS} -o ./bin/vip-controller ./cmd/vip-controller
CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build ${LDFLAGS} -o ./bin/vip-controller ./cmd/vip-controller
$(DOCKER_CMD) build -t $(VIP_CONTROLLER_IMAGE) -f dockerfiles/vip-controller/Dockerfile .
$(DOCKER_CMD) push $(VIP_CONTROLLER_IMAGE)
@echo "Build done: $(VIP_CONTROLLER_IMAGE)"
Expand Down
14 changes: 14 additions & 0 deletions cmd/cnivpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/ucloud/uk8s-cni-vpc/pkg/database"
"github.com/ucloud/uk8s-cni-vpc/pkg/iputils"
"github.com/ucloud/uk8s-cni-vpc/pkg/kubeclient"
"github.com/ucloud/uk8s-cni-vpc/pkg/uapi"

"github.com/ucloud/ucloud-sdk-go/ucloud"
Expand Down Expand Up @@ -67,6 +68,19 @@ func assignPodIp(podName, podNS, netNS, sandboxId string) (*rpc.PodNetwork, bool
}
}

kubeClient, err := kubeclient.GetNodeClient()
if err != nil {
return nil, false, fmt.Errorf("failed to get node kube client: %v", err)
}
enableStaticIP, _, err := ipamd.IsPodEnableStaticIP(kubeClient, podName, podNS)
if err != nil {
return nil, false, fmt.Errorf("failed to check pod static ip enable: %v", err)
}
if enableStaticIP {
// If pod enable static ip, we donot allow it to allocate ip without ipamd
return nil, false, fmt.Errorf("pod %s/%s enable static ip, but ipamd is not enabled", podNS, podName)
}

uapi, err := uapi.NewClient()
if err != nil {
return nil, false, fmt.Errorf("failed to init uapi client: %v", err)
Expand Down
9 changes: 7 additions & 2 deletions pkg/ipamd/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ucloud/uk8s-cni-vpc/pkg/ulog"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)

Expand Down Expand Up @@ -187,10 +188,14 @@ func (s *ipamServer) setAnnotationForCalicoPolicy(pod *v1.Pod, network *rpc.PodN
}

func (s *ipamServer) podEnableStaticIP(podName, podNS string) (bool, *v1.Pod, error) {
return IsPodEnableStaticIP(s.kubeClient, podName, podNS)
}

func IsPodEnableStaticIP(client *kubernetes.Clientset, podName, podNS string) (bool, *v1.Pod, error) {
statefulset := false
pod, err := s.getPod(podName, podNS)
pod, err := client.CoreV1().Pods(podNS).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
ulog.Errorf("Get pod error: %v", err)
ulog.Errorf("Get %s/%s pod error: %v", podNS, podName, err)
return false, nil, err
}

Expand Down

0 comments on commit 495b156

Please sign in to comment.