Skip to content

Commit

Permalink
feat: use real kubernetes server address to yurthub when yurtadm join (
Browse files Browse the repository at this point in the history
…openyurtio#1517)

Signed-off-by: Lancelot <1984737645@qq.com>
  • Loading branch information
Lan-ce-lot authored and JameKeal committed Jun 9, 2023
1 parent fc93de5 commit aa15d76
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/yurtadm/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
// ReuseCNIBin flag sets whether to reuse local CNI binaries or not.
ReuseCNIBin = "reuse-cni-bin"

DefaultServerAddr = "https://127.0.0.1:6443"
ServerHealthzServer = "127.0.0.1:10267"
ServerHealthzURLPath = "/v1/healthz"
ServerReadyzURLPath = "/v1/readyz"
Expand Down
30 changes: 30 additions & 0 deletions pkg/yurtadm/util/yurthub/yurthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package yurthub

import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -73,6 +75,12 @@ func AddYurthubStaticYaml(data joindata.YurtJoinData, podManifestPath string) er
if err != nil {
return err
}

yurthubTemplate, err = useRealServerAddr(yurthubTemplate, kubernetesServerAddrs)
if err != nil {
return err
}

yurthubManifestFile := filepath.Join(podManifestPath, util.WithYamlSuffix(data.YurtHubManifest()))
klog.Infof("yurthub template: %s\n%s", yurthubManifestFile, yurthubTemplate)

Expand Down Expand Up @@ -170,3 +178,25 @@ func CleanHubBootstrapConfig() error {
}
return nil
}

// useRealServerAddr check if the server-addr from yurthubTemplate is default value: 127.0.0.1:6443
// if yes, we should use the real server addr
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)

for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, target) {
line = strings.Replace(line, constants.DefaultServerAddr, kubernetesServerAddrs, -1)
}
buffer.WriteString(line + "\n")
}

if err := scanner.Err(); err != nil {
klog.Infof("Error scanning file: %v\n", err)
return "", err
}
return buffer.String(), nil
}
227 changes: 227 additions & 0 deletions pkg/yurtadm/util/yurthub/yurthub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
Copyright 2023 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package yurthub

import (
"testing"

"github.com/stretchr/testify/assert"
)

var (
defaultAddr = `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://127.0.0.1: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: {}
`

setAddr = `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.1: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"
serverAddrsB = "https://192.0.0.2:6443"
)

func Test_useRealServerAddr(t *testing.T) {
type args struct {
yurthubTemplate string
kubernetesServerAddrs string
}
tests := []struct {
name string
args args
want string
}{
{
name: "change default server addr",
args: args{
yurthubTemplate: defaultAddr,
kubernetesServerAddrs: serverAddrsA,
},
want: setAddr,
},
{
name: " already set server addr",
args: args{
yurthubTemplate: setAddr,
kubernetesServerAddrs: serverAddrsB,
},
want: setAddr,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
actualYaml, err := useRealServerAddr(test.args.yurthubTemplate, test.args.kubernetesServerAddrs)
if err != nil {
t.Errorf("unexpected error: %s", err)
}

assert.Equal(t, actualYaml, test.want)
})
}
}

0 comments on commit aa15d76

Please sign in to comment.