Skip to content

Commit 495b156

Browse files
authored
fix(cnivpc): When pod enable static ip, block ip assignment without ipamd (#34)
1 parent 85d8dde commit 495b156

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ all: cnivpc
3535

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

4141
.PHONY: cnivpc
4242
cnivpc: cnivpc-bin
@@ -46,14 +46,14 @@ cnivpc: cnivpc-bin
4646

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

5454
.PHONY: vip-controller
5555
vip-controller:
56-
go build ${LDFLAGS} -o ./bin/vip-controller ./cmd/vip-controller
56+
CGO_ENABLED=0 GOOS="linux" GOARCH="amd64" go build ${LDFLAGS} -o ./bin/vip-controller ./cmd/vip-controller
5757
$(DOCKER_CMD) build -t $(VIP_CONTROLLER_IMAGE) -f dockerfiles/vip-controller/Dockerfile .
5858
$(DOCKER_CMD) push $(VIP_CONTROLLER_IMAGE)
5959
@echo "Build done: $(VIP_CONTROLLER_IMAGE)"

cmd/cnivpc/rpc.go

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/ucloud/uk8s-cni-vpc/pkg/database"
2222
"github.com/ucloud/uk8s-cni-vpc/pkg/iputils"
23+
"github.com/ucloud/uk8s-cni-vpc/pkg/kubeclient"
2324
"github.com/ucloud/uk8s-cni-vpc/pkg/uapi"
2425

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

71+
kubeClient, err := kubeclient.GetNodeClient()
72+
if err != nil {
73+
return nil, false, fmt.Errorf("failed to get node kube client: %v", err)
74+
}
75+
enableStaticIP, _, err := ipamd.IsPodEnableStaticIP(kubeClient, podName, podNS)
76+
if err != nil {
77+
return nil, false, fmt.Errorf("failed to check pod static ip enable: %v", err)
78+
}
79+
if enableStaticIP {
80+
// If pod enable static ip, we donot allow it to allocate ip without ipamd
81+
return nil, false, fmt.Errorf("pod %s/%s enable static ip, but ipamd is not enabled", podNS, podName)
82+
}
83+
7084
uapi, err := uapi.NewClient()
7185
if err != nil {
7286
return nil, false, fmt.Errorf("failed to init uapi client: %v", err)

pkg/ipamd/k8s.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/ucloud/uk8s-cni-vpc/pkg/ulog"
2929
"k8s.io/apimachinery/pkg/fields"
30+
"k8s.io/client-go/kubernetes"
3031
"k8s.io/client-go/util/retry"
3132
)
3233

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

189190
func (s *ipamServer) podEnableStaticIP(podName, podNS string) (bool, *v1.Pod, error) {
191+
return IsPodEnableStaticIP(s.kubeClient, podName, podNS)
192+
}
193+
194+
func IsPodEnableStaticIP(client *kubernetes.Clientset, podName, podNS string) (bool, *v1.Pod, error) {
190195
statefulset := false
191-
pod, err := s.getPod(podName, podNS)
196+
pod, err := client.CoreV1().Pods(podNS).Get(context.Background(), podName, metav1.GetOptions{})
192197
if err != nil {
193-
ulog.Errorf("Get pod error: %v", err)
198+
ulog.Errorf("Get %s/%s pod error: %v", podNS, podName, err)
194199
return false, nil, err
195200
}
196201

0 commit comments

Comments
 (0)