Skip to content

Commit

Permalink
fix: fix the bug of sync endpoint in vc
Browse files Browse the repository at this point in the history
Signed-off-by: qiuwei <qiuwei_yewu@cmss.chinamobile.com>
  • Loading branch information
qiuwei68 committed Jul 12, 2024
1 parent d4135d6 commit 9f74640
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
14 changes: 9 additions & 5 deletions hack/k8s-in-k8s/kubelet_node_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ function revert() {


echo "exec(4/5): execute join cmd...."
if [ "$USE_KUBEADM" = "true" ]; then
if [ -z "$USE_KUBEADM" ]; then
# if "USE_KUBEADM is not set, default set to true"
export USE_KUBEADM=true
fi
if [ "$USE_KUBEADM" = true ]; then
echo "use kubeadm to join node to host"
kubeadm join --config "$PATH_FILE_TMP/kubeadm.cfg.current"
if [ $? -ne 0 ]; then
Expand All @@ -200,13 +204,13 @@ function revert() {
exit 1
fi

if [ "$USE_KUBEADM" = "false" ]; then
if [ "$USE_KUBEADM" = false ]; then
systemctl start kubelet
elapsed_time=0

while [ $elapsed_time -lt $KUBELET_CONF_TIMEOUT ]; do
if [ -f "/etc/kubernetes/kubelet.conf" ]; then
rm -f "/etc/kubernetes/bootstrap-kubelet.conf"
if [ -f "${PATH_KUBERNETES}/${KUBELET_KUBE_CONFIG_NAME}" ]; then
rm -f "${PATH_KUBERNETES}/bootstrap-kubelet.conf"
echo "Deleted bootstrap-kubelet.conf file as kubelet.conf exists."
break
fi
Expand Down Expand Up @@ -311,7 +315,7 @@ function check() {
echo "check(2/2): copy kubeadm-flags.env to create $PATH_FILE_TMP , remove args[cloud-provider] and taints"
# Since this function is used both to detach nodes, we need to make sure we haven't copied kubeadm-flags.env before
if [ ! -f "${PATH_FILE_TMP}/kubeadm-flags.env.origin" ]; then
cp "$PATH_KUBELET_LIB/kubeadm-flags.env" "${PATH_FILE_TMP}/kubeadm-flags.env.origin"
cp "${PATH_KUBELET_LIB}/kubeadm-flags.env" "${PATH_FILE_TMP}/kubeadm-flags.env.origin"
fi
sed -e "s| --cloud-provider=external | |g" -e "w ${PATH_FILE_TMP}/kubeadm-flags.env" "$PATH_KUBELET_LIB/kubeadm-flags.env"
sed -i "s| --register-with-taints=node.kosmos.io/unschedulable:NoSchedule||g" "${PATH_FILE_TMP}/kubeadm-flags.env"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (e *ApiServerExternalSyncController) SyncApiServerExternalEPS(ctx context.C
kubeEndpoints, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Get(ctx, "kubernetes", metav1.GetOptions{})
if err != nil {
klog.Errorf("Error getting endpoints: %v", err)
return fmt.Errorf("failed to get endpoints for kubernetes service: %v", err)
return err
} else {
klog.Infof("Endpoints for service 'kubernetes': %v", kubeEndpoints)
klog.V(4).Infof("Endpoints for service 'kubernetes': %v", kubeEndpoints)
for _, subset := range kubeEndpoints.Subsets {
for _, address := range subset.Addresses {
klog.Infof("IP: %s", address.IP)
klog.V(4).Infof("IP: %s", address.IP)
}
}
}
Expand All @@ -72,39 +72,42 @@ func (e *ApiServerExternalSyncController) SyncApiServerExternalEPS(ctx context.C
}

if kubeEndpoints.Subsets[0].Addresses == nil || len(kubeEndpoints.Subsets[0].Addresses) == 0 {
return fmt.Errorf("eps %s Addresses length is nil", "kubernetes")
klog.Errorf("eps %s Addresses length is nil", "kubernetes")
return err
}

apiServerExternalEndpoints, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Get(ctx, constants.ApiServerExternalService, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get endpoints for %s : %v", constants.ApiServerExternalService, err)
klog.Errorf("failed to get endpoints for %s : %v", constants.ApiServerExternalService, err)
return err
}

updateEPS := apiServerExternalEndpoints.DeepCopy()

if apiServerExternalEndpoints != nil {
klog.Infof("apiServerExternalEndpoints: %v", apiServerExternalEndpoints)
klog.V(4).Infof("apiServerExternalEndpoints: %v", apiServerExternalEndpoints)
} else {
klog.Info("apiServerExternalEndpoints is nil")
klog.V(4).Info("apiServerExternalEndpoints is nil")
}

if updateEPS != nil {
klog.Infof("updateEPS: %v", updateEPS)
klog.V(4).Infof("updateEPS: %v", updateEPS)
} else {
klog.Info("updateEPS is nil")
klog.V(4).Info("updateEPS is nil")
}

if len(updateEPS.Subsets) == 1 && len(updateEPS.Subsets[0].Addresses) == 1 {
ip := kubeEndpoints.Subsets[0].Addresses[0].IP
klog.Infof("IP address: %s", ip)
klog.V(4).Infof("IP address: %s", ip)
updateEPS.Subsets[0].Addresses[0].IP = ip

if _, err := k8sClient.CoreV1().Endpoints(constants.DefaultNs).Update(ctx, updateEPS, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to update endpoints for api-server-external-service: %v", err)
klog.Errorf("failed to update endpoints for api-server-external-service: %v", err)
return err
}
} else {
klog.ErrorS(err, "Unexpected format of endpoints for api-server-external-service", "endpoint_data", updateEPS)
return fmt.Errorf("unexpected format of endpoints for api-server-external-service")
return err
}

return nil
Expand Down Expand Up @@ -137,7 +140,7 @@ func (e *ApiServerExternalSyncController) Reconcile(ctx context.Context, request
return reconcile.Result{}, nil
}

if targetVirtualCluster.Status.Phase != v1alpha1.AllNodeReady && targetVirtualCluster.Status.Phase != v1alpha1.Completed {
if targetVirtualCluster.Status.Phase != v1alpha1.Initialized {
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/kubenest/controlplane/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func EnsureApiServerExternalEndPoint(dynamicClient dynamic.Interface) error {
}

func installApiServerExternalEndpointInVirtualCluster(dynamicClient dynamic.Interface) error {
klog.Info("begin to get kubernetes endpoint")
klog.V(4).Info("begin to get kubernetes endpoint")
kubeEndpointUnstructured, err := dynamicClient.Resource(schema.GroupVersionResource{
Group: "",
Version: "v1",
Expand Down Expand Up @@ -74,7 +74,7 @@ func installApiServerExternalEndpointInVirtualCluster(dynamicClient dynamic.Inte
klog.Error("create api-server-external-service endpoint failed", err)
return errors.Wrap(err, "failed to create api-server-external-service endpoint")
} else {
klog.Info("success create api-server-external-service endpoint:", createResult)
klog.V(4).Info("success create api-server-external-service endpoint:", createResult)
}
} else {
return errors.New("kubernetes endpoint does not exist")
Expand Down Expand Up @@ -110,7 +110,7 @@ func installApiServerExternalServiceInVirtualCluster(dynamicClient dynamic.Inter
}

func getEndPointPort(dynamicClient dynamic.Interface) (int32, error) {
klog.Info("begin to get Endpoints ports...")
klog.V(4).Info("begin to get Endpoints ports...")
endpointsRes := dynamicClient.Resource(schema.GroupVersionResource{
Group: "",
Version: "v1",
Expand Down Expand Up @@ -153,6 +153,6 @@ func getEndPointPort(dynamicClient dynamic.Interface) (int32, error) {
return 0, fmt.Errorf("port field not found or error parsing it")
}

klog.Infof("The port number was successfully obtained: %d", portNum)
klog.V(4).Infof("The port number was successfully obtained: %d", portNum)
return int32(portNum), nil
}

0 comments on commit 9f74640

Please sign in to comment.