Skip to content

Commit

Permalink
feature: bcs-kube-agent support to register proxy addresss to bcs-api…
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhe-bupt committed Feb 25, 2020
1 parent 15209d6 commit 4dd3bc1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion bcs-k8s/bcs-kube-agent/app/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ import (
"github.com/json-iterator/go"
"github.com/parnurzeal/gorequest"
"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

const (
defaultNamespace = "default"
systemNamespace = "kube-system"
clusterServiceName = "kubernetes"
// endpoints name for kube-apiserver proxy
apiserverProxyServiceName = "apiserver-proxy-for-bcs"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
Expand Down Expand Up @@ -106,8 +110,16 @@ func reportToBke(kubeClient *kubernetes.Clientset, cfg *rest.Config) {
func getApiserverAdresses(kubeClient *kubernetes.Clientset) (string, error) {
var apiserverPort int32
var endpointsList []string
var endpoints *corev1.Endpoints
var err error

isExternal := viper.GetBool("agent.isExternal")
if isExternal {
endpoints, err = kubeClient.CoreV1().Endpoints(systemNamespace).Get(apiserverProxyServiceName, metav1.GetOptions{})
} else {
endpoints, err = kubeClient.CoreV1().Endpoints(defaultNamespace).Get(clusterServiceName, metav1.GetOptions{})
}

endpoints, err := kubeClient.CoreV1().Endpoints(defaultNamespace).Get(clusterServiceName, metav1.GetOptions{})
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions bcs-k8s/bcs-kube-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
bkeAddress string
clusterId string
insecureSkipVerify bool
// 是否在外网跨云部署
isExternal bool
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -73,13 +75,15 @@ func init() {
rootCmd.PersistentFlags().StringVar(&bkeAddress, "bke-address", "", "the bke address")
rootCmd.PersistentFlags().StringVar(&clusterId, "cluster-id", "", "cluster which the agent run in")
rootCmd.PersistentFlags().BoolVar(&insecureSkipVerify, "insecureSkipVerify", false, "verifies the server's certificate chain and host name")
rootCmd.PersistentFlags().BoolVar(&isExternal, "isExternal", false, "whether the k8s cluster and kube-agent is in external network")
// these three flag support direct flag and viper config at the same time, the direct flag could cover the viper config.
viper.BindPFlag("agent.kubeconfig", rootCmd.PersistentFlags().Lookup("kubeconfig"))
viper.BindPFlag("agent.periodSync", rootCmd.PersistentFlags().Lookup("periodsync"))
viper.BindPFlag("agent.listenAddr", rootCmd.PersistentFlags().Lookup("listen-addr"))
viper.BindPFlag("bke.serverAddress", rootCmd.PersistentFlags().Lookup("bke-address"))
viper.BindPFlag("cluster.id", rootCmd.PersistentFlags().Lookup("cluster-id"))
viper.BindPFlag("agent.insecureSkipVerify", rootCmd.PersistentFlags().Lookup("insecureSkipVerify"))
viper.BindPFlag("agent.isExternal", rootCmd.PersistentFlags().Lookup("isExternal"))
}

func initConfig() {
Expand Down

0 comments on commit 4dd3bc1

Please sign in to comment.