diff --git a/pkg/yurtctl/cmd/revert/revert.go b/pkg/yurtctl/cmd/revert/revert.go index 57a4cba6175..ee87a61d008 100644 --- a/pkg/yurtctl/cmd/revert/revert.go +++ b/pkg/yurtctl/cmd/revert/revert.go @@ -33,6 +33,7 @@ import ( enutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/edgenode" kubeutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/kubernetes" strutil "github.com/openyurtio/openyurt/pkg/yurtctl/util/strings" + tunneldns "github.com/openyurtio/openyurt/pkg/yurttunnel/dns" ) // RevertOptions has the information required by the revert operation @@ -106,8 +107,8 @@ func (ro *RevertOptions) RunRevert() (err error) { return } defer func() { - if releaseLockErr := lock.ReleaseLock(ro.clientSet); releaseLockErr != nil { - klog.Error(releaseLockErr) + if deleteLockErr := lock.DeleteLock(ro.clientSet); deleteLockErr != nil { + klog.Error(deleteLockErr) } }() klog.V(4).Info("successfully acquire the lock") @@ -304,15 +305,25 @@ func removeYurtTunnelServer(client *kubernetes.Clientset) error { return fmt.Errorf("fail to delete the clusterrole/%s: %s", constants.YurttunnelServerComponentName, err) } + klog.V(4).Infof("clusterrole/%s is deleted", constants.YurttunnelServerComponentName) - // 6. remove the ConfigMap + // 6. remove the yurt-tunnel-server-cfg if err := client.CoreV1().ConfigMaps(constants.YurttunnelNamespace). Delete(context.Background(), constants.YurttunnelServerCmName, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("fail to delete the configmap/%s: %s", constants.YurttunnelServerCmName, err) } - klog.V(4).Infof("clusterrole/%s is deleted", constants.YurttunnelServerComponentName) + + // 7. remove the dns record configmap + yurttunnelDnsRecordConfigMapName := tunneldns.GetYurtTunnelDNSRecordConfigMapName() + if err := client.CoreV1().ConfigMaps(constants.YurttunnelNamespace). + Delete(context.Background(), yurttunnelDnsRecordConfigMapName, + metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("fail to delete configmap/%s: %s", + yurttunnelDnsRecordConfigMapName, err) + } + return nil } diff --git a/pkg/yurtctl/lock/lock.go b/pkg/yurtctl/lock/lock.go index 4f1ebce3b56..fc0c5e3b857 100644 --- a/pkg/yurtctl/lock/lock.go +++ b/pkg/yurtctl/lock/lock.go @@ -31,7 +31,6 @@ import ( ) const ( - lockFinalizer = "kubernetes" AnnotationAcquireTime = "openyurt.io/yurtctllock.acquire.time" AnnotationIsLocked = "openyurt.io/yurtctllock.locked" @@ -52,9 +51,8 @@ func AcquireLock(cli *kubernetes.Clientset) error { // the lock is not exist, create one cm := &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: constants.YurtctlLockConfigMapName, - Namespace: "kube-system", - Finalizers: []string{lockFinalizer}, + Name: constants.YurtctlLockConfigMapName, + Namespace: "kube-system", Annotations: map[string]string{ AnnotationAcquireTime: strconv.FormatInt(time.Now().Unix(), 10), AnnotationIsLocked: "true", @@ -167,3 +165,14 @@ func ReleaseLock(cli *kubernetes.Clientset) error { return nil } + +// DeleteLock should only be called when you've achieved the lock. +// It will delete the yurtctl-lock configmap. +func DeleteLock(cli *kubernetes.Clientset) error { + if err := cli.CoreV1().ConfigMaps("kube-system"). + Delete(context.Background(), constants.YurtctlLockConfigMapName, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + klog.Error("fail to delete the yurtctl lock", err) + return err + } + return nil +} diff --git a/pkg/yurttunnel/constants/constants.go b/pkg/yurttunnel/constants/constants.go index f441e54f5fa..75cd97d1dc9 100644 --- a/pkg/yurttunnel/constants/constants.go +++ b/pkg/yurttunnel/constants/constants.go @@ -29,6 +29,9 @@ const ( YurttunnelServerExternalAddrKey = "x-tunnel-server-external-addr" YurttunnelEndpointsNs = "kube-system" YurttunnelEndpointsName = "x-tunnel-server-svc" + YurttunnelDNSRecordConfigMapNs = "kube-system" + YurttunnelDNSRecordConfigMapName = "%s-tunnel-nodes" + YurttunnelDNSRecordNodeDataKey = "tunnel-nodes" // yurttunnel PKI related constants YurttunnelCSROrg = "openyurt:yurttunnel" diff --git a/pkg/yurttunnel/dns/dns.go b/pkg/yurttunnel/dns/dns.go index 58511f4b2bb..68018aa30fd 100644 --- a/pkg/yurttunnel/dns/dns.go +++ b/pkg/yurttunnel/dns/dns.go @@ -54,17 +54,18 @@ const ( maxRetries = 15 minSyncPeriod = 30 - yurttunnelDNSRecordConfigMapNs = "kube-system" - yurttunnelDNSRecordNodeDataKey = "tunnel-nodes" - dnatPortPrefix = "dnat-" ) var ( - yurttunnelDNSRecordConfigMapName = fmt.Sprintf("%s-tunnel-nodes", - strings.TrimRightFunc(projectinfo.GetProjectPrefix(), func(c rune) bool { return c == '-' })) + yurttunnelDNSRecordConfigMapName = GetYurtTunnelDNSRecordConfigMapName() ) +func GetYurtTunnelDNSRecordConfigMapName() string { + return fmt.Sprintf(constants.YurttunnelDNSRecordConfigMapName, + strings.TrimRightFunc(projectinfo.GetProjectPrefix(), func(c rune) bool { return c == '-' })) +} + // DNSRecordController interface defines the method for synchronizing // the node dns records with k8s DNS component(such as CoreDNS) type DNSRecordController interface { @@ -202,7 +203,7 @@ func (dnsctl *coreDNSRecordController) run(stopCh <-chan struct{}) { if err := dnsctl.ensureCoreDNSRecordConfigMap(); err != nil { klog.Errorf("failed to ensure dns record ConfigMap %v/%v, %v", - yurttunnelDNSRecordConfigMapNs, yurttunnelDNSRecordConfigMapName, err) + constants.YurttunnelDNSRecordConfigMapNs, yurttunnelDNSRecordConfigMapName, err) return } @@ -299,7 +300,7 @@ func (dnsctl *coreDNSRecordController) ensureCoreDNSRecordConfigMap() error { Namespace: constants.YurttunnelServerServiceNs, }, Data: map[string]string{ - yurttunnelDNSRecordNodeDataKey: "", + constants.YurttunnelDNSRecordNodeDataKey: "", }, } _, err = dnsctl.kubeClient.CoreV1().ConfigMaps(constants.YurttunnelServerServiceNs).Create(context.Background(), cm, metav1.CreateOptions{}) @@ -387,7 +388,7 @@ func (dnsctl *coreDNSRecordController) updateDNSRecords(records []string) error if err != nil { return err } - cm.Data[yurttunnelDNSRecordNodeDataKey] = strings.Join(records, "\n") + cm.Data[constants.YurttunnelDNSRecordNodeDataKey] = strings.Join(records, "\n") if _, err := dnsctl.kubeClient.CoreV1().ConfigMaps(constants.YurttunnelServerServiceNs).Update(context.Background(), cm, metav1.UpdateOptions{}); err != nil { return fmt.Errorf("failed to update configmap %v/%v, %v", constants.YurttunnelServerServiceNs, yurttunnelDNSRecordConfigMapName, err) diff --git a/pkg/yurttunnel/dns/handler.go b/pkg/yurttunnel/dns/handler.go index d5d0d7c47af..58c0f7a4605 100644 --- a/pkg/yurttunnel/dns/handler.go +++ b/pkg/yurttunnel/dns/handler.go @@ -224,10 +224,10 @@ func (dnsctl *coreDNSRecordController) getCurrentDNSRecords() ([]string, error) return nil, err } - data, ok := cm.Data[yurttunnelDNSRecordNodeDataKey] + data, ok := cm.Data[constants.YurttunnelDNSRecordNodeDataKey] if !ok { return nil, fmt.Errorf("key %q not found in %s/%s ConfigMap, %v", - yurttunnelDNSRecordNodeDataKey, constants.YurttunnelServerServiceNs, yurttunnelDNSRecordConfigMapName, err) + constants.YurttunnelDNSRecordNodeDataKey, constants.YurttunnelServerServiceNs, yurttunnelDNSRecordConfigMapName, err) } return strings.Split(data, "\n"), nil diff --git a/pkg/yurttunnel/dns/util.go b/pkg/yurttunnel/dns/util.go index d87d8142a20..bd1a1a31517 100644 --- a/pkg/yurttunnel/dns/util.go +++ b/pkg/yurttunnel/dns/util.go @@ -22,7 +22,6 @@ import ( "strings" corev1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" "github.com/openyurtio/openyurt/pkg/projectinfo" ) @@ -40,11 +39,11 @@ func formatDNSRecord(ip, host string) string { } // getNodeHostIP returns the provided node's "primary" IP -func getNodeHostIP(node *v1.Node) (string, error) { +func getNodeHostIP(node *corev1.Node) (string, error) { // re-sort the addresses with InternalIPs first and then ExternalIPs allIPs := make([]net.IP, 0, len(node.Status.Addresses)) for _, addr := range node.Status.Addresses { - if addr.Type == v1.NodeInternalIP { + if addr.Type == corev1.NodeInternalIP { ip := net.ParseIP(addr.Address) if ip != nil { allIPs = append(allIPs, ip) @@ -53,7 +52,7 @@ func getNodeHostIP(node *v1.Node) (string, error) { } } for _, addr := range node.Status.Addresses { - if addr.Type == v1.NodeExternalIP { + if addr.Type == corev1.NodeExternalIP { ip := net.ParseIP(addr.Address) if ip != nil { allIPs = append(allIPs, ip)