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

fix(cnivpc): When pod enable static ip, block ip assignment without ipamd #34

Merged
merged 2 commits into from
Nov 23, 2023
Merged
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
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