Skip to content

Commit

Permalink
delete yurtctl-lock and yurt-tunnel-nodes when revert the whole clust…
Browse files Browse the repository at this point in the history
…er (#540)
  • Loading branch information
Congrool authored Nov 2, 2021
1 parent f74dd4a commit 3f48d43
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
19 changes: 15 additions & 4 deletions pkg/yurtctl/cmd/revert/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/yurtctl/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
)

const (
lockFinalizer = "kubernetes"
AnnotationAcquireTime = "openyurt.io/yurtctllock.acquire.time"
AnnotationIsLocked = "openyurt.io/yurtctllock.locked"

Expand All @@ -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",
Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions pkg/yurttunnel/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 9 additions & 8 deletions pkg/yurttunnel/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/yurttunnel/dns/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pkg/yurttunnel/dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"strings"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"

"github.com/openyurtio/openyurt/pkg/projectinfo"
)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3f48d43

Please sign in to comment.