forked from TencentBlueKing/bk-bcs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: bcs-kube-agent register to bcs-api with websocket, to implem…
…ent service register and discovery accross clouds. issue TencentBlueKing#414
- Loading branch information
1 parent
3a0e460
commit b07166a
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Blueking Container Service available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* 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 app | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
"bk-bcs/bcs-common/common/blog" | ||
"bk-bcs/bcs-common/common/websocketDialer" | ||
"github.com/spf13/viper" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const ( | ||
kubernetesServiceHost = "KUBERNETES_SERVICE_HOST" | ||
kubernetesServicePort = "KUBERNETES_SERVICE_PORT" | ||
|
||
Module = "BCS-API-Tunnel-Module" | ||
RegisterToken = "BCS-API-Tunnel-Token" | ||
Params = "BCS-API-Tunnel-Params" | ||
Cluster = "BCS-API-Tunnel-ClusterId" | ||
|
||
ModuleName = "kube-agent" | ||
) | ||
|
||
func getenv(env string) (string, error) { | ||
value := os.Getenv(env) | ||
if value == "" { | ||
return "", fmt.Errorf("%s is empty", env) | ||
} | ||
return value, nil | ||
} | ||
|
||
func buildWebsocketToBke(cfg *rest.Config) error { | ||
bkeServerAddress := viper.GetString("bke.serverAddress") | ||
clusterId := viper.GetString("cluster.id") | ||
registerToken := os.Getenv("REGISTER_TOKEN") | ||
|
||
bkeServerUrl, err := url.Parse(bkeServerAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := populateCAData(cfg); err != nil { | ||
return fmt.Errorf("error populating ca data: %s", err.Error()) | ||
} | ||
|
||
kubernetesServiceHost, err := getenv(kubernetesServiceHost) | ||
if err != nil { | ||
return err | ||
} | ||
kubernetesServicePort, err := getenv(kubernetesServicePort) | ||
if err != nil { | ||
return err | ||
} | ||
params := map[string]interface{}{ | ||
"address": fmt.Sprintf("https://%s:%s", kubernetesServiceHost, kubernetesServicePort), | ||
"userToken": cfg.BearerToken, | ||
"caCert": base64.StdEncoding.EncodeToString(cfg.CAData), | ||
} | ||
bytes, err := json.Marshal(params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
headers := map[string][]string{ | ||
Module: {ModuleName}, | ||
Cluster: {clusterId}, | ||
RegisterToken: {registerToken}, | ||
Params: {base64.StdEncoding.EncodeToString(bytes)}, | ||
} | ||
|
||
var tlsConfig *tls.Config | ||
insecureSkipVerify := viper.GetBool("agent.insecureSkipVerify") | ||
if insecureSkipVerify { | ||
tlsConfig = &tls.Config{InsecureSkipVerify: insecureSkipVerify} | ||
} else { | ||
pool := x509.NewCertPool() | ||
caCrtStr := os.Getenv("SERVER_CERT") | ||
caCrt := []byte(caCrtStr) | ||
pool.AppendCertsFromPEM(caCrt) | ||
tlsConfig = &tls.Config{RootCAs: pool} | ||
} | ||
|
||
go func() { | ||
for { | ||
wsURL := fmt.Sprintf("wss://%s/bcsapi/v1/websocket/connect", bkeServerUrl.Host) | ||
blog.Infof("Connecting to %s with token %s", wsURL, registerToken) | ||
|
||
websocketDialer.ClientConnect(context.Background(), wsURL, headers, tlsConfig, nil, func(proto, address string) bool { | ||
switch proto { | ||
case "tcp": | ||
return true | ||
case "unix": | ||
return address == "/var/run/docker.sock" | ||
} | ||
return false | ||
}) | ||
time.Sleep(5 * time.Second) | ||
} | ||
}() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters