Skip to content

Commit

Permalink
fix: always overwrite server-addr in yurt-static-set-yurt-hub configm…
Browse files Browse the repository at this point in the history
…ap by yurtadm command (#2271)

Co-authored-by: rayneLi <[email protected]>
  • Loading branch information
rayne-Li and rayneLi authored Feb 3, 2025
1 parent 887a02e commit 33bdc7e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
10 changes: 7 additions & 3 deletions pkg/yurtadm/util/yurthub/yurthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -188,12 +189,15 @@ func CleanHubBootstrapConfig() error {
func useRealServerAddr(yurthubTemplate string, kubernetesServerAddrs string) (string, error) {
scanner := bufio.NewScanner(bytes.NewReader([]byte(yurthubTemplate)))
var buffer bytes.Buffer
target := fmt.Sprintf("%v=%v", constants.ServerAddr, constants.DefaultServerAddr)
// compile ipv4 regex
ipRegex := regexp.MustCompile(`https?://(?:[0-9]{1,3}\.){3}[0-9]{1,3}:\d+`)

// scan template and replace setAddr
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, target) {
line = strings.Replace(line, constants.DefaultServerAddr, kubernetesServerAddrs, -1)
if strings.Contains(line, fmt.Sprintf("- --%s=", constants.ServerAddr)) {
// replace kubernetesServerAddrs by new addr
line = ipRegex.ReplaceAllString(line, kubernetesServerAddrs)
}
buffer.WriteString(line + "\n")
}
Expand Down
80 changes: 79 additions & 1 deletion pkg/yurtadm/util/yurthub/yurthub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,84 @@ spec:
type: Directory
name: kubernetes
status: {}
`
setAddr2 = `apiVersion: v1
kind: Pod
metadata:
annotations:
openyurt.io/static-pod-hash: 76f4f955b6
creationTimestamp: null
labels:
k8s-app: yurt-hub
name: yurt-hub
namespace: kube-system
spec:
containers:
- command:
- yurthub
- --v=2
- --bind-address=127.0.0.1
- --server-addr=https://192.0.0.2:6443
- --node-name=$(NODE_NAME)
- --bootstrap-file=/var/lib/yurthub/bootstrap-hub.conf
- --working-mode=edge
- --namespace=kube-system
env:
- name: NODE_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: spec.nodeName
image: openyurt/yurthub:v1.3.0
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
host: 127.0.0.1
path: /v1/healthz
port: 10267
scheme: HTTP
initialDelaySeconds: 300
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 1
name: yurt-hub
resources:
limits:
memory: 300Mi
requests:
cpu: 150m
memory: 150Mi
securityContext:
capabilities:
add:
- NET_ADMIN
- NET_RAW
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/lib/yurthub
name: hub-dir
- mountPath: /etc/kubernetes
name: kubernetes
dnsPolicy: ClusterFirst
hostNetwork: true
priority: 2000001000
priorityClassName: system-node-critical
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /var/lib/yurthub
type: DirectoryOrCreate
name: hub-dir
- hostPath:
path: /etc/kubernetes
type: Directory
name: kubernetes
status: {}
`

serverAddrsA = "https://192.0.0.1:6443"
Expand Down Expand Up @@ -214,7 +292,7 @@ func Test_useRealServerAddr(t *testing.T) {
yurthubTemplate: setAddr,
kubernetesServerAddrs: serverAddrsB,
},
want: setAddr,
want: setAddr2,
},
}

Expand Down

0 comments on commit 33bdc7e

Please sign in to comment.