From dee405d9572a3079f854256f1bb6f7e2fb902c7f Mon Sep 17 00:00:00 2001 From: adamzhoul <770822772@qq.com> Date: Sun, 26 Sep 2021 10:39:30 +0800 Subject: [PATCH] add tunnelServerAddress --- pkg/yurtctl/cmd/convert/convert.go | 12 ++- pkg/yurtctl/cmd/yurtinit/init.go | 102 ++++++++++-------- .../yurtinit/phases/install_yurt_addons.go | 3 +- pkg/yurtctl/cmd/yurtinit/phases/types.go | 1 + .../constants/yurt-tunnel-agent-tmpl.go | 3 + .../constants/yurt-tunnel-server-tmpl.go | 1 + pkg/yurtctl/util/kubernetes/apply_addons.go | 7 +- 7 files changed, 78 insertions(+), 51 deletions(-) diff --git a/pkg/yurtctl/cmd/convert/convert.go b/pkg/yurtctl/cmd/convert/convert.go index c405d4da83d..690ecf3d22c 100644 --- a/pkg/yurtctl/cmd/convert/convert.go +++ b/pkg/yurtctl/cmd/convert/convert.go @@ -69,6 +69,7 @@ type ConvertOptions struct { YurtControllerManagerImage string YurctlServantImage string YurttunnelServerImage string + YurttunnelServerAddress string YurttunnelAgentImage string PodMainfestPath string KubeadmConfPath string @@ -129,6 +130,9 @@ func NewConvertCmd() *cobra.Command { cmd.Flags().String("yurt-tunnel-server-image", "openyurt/yurt-tunnel-server:latest", "The yurt-tunnel-server image.") + cmd.Flags().String("yurt-tunnel-server-address", + "", + "The yurt-tunnel-server address.") cmd.Flags().String("yurt-tunnel-agent-image", "openyurt/yurt-tunnel-agent:latest", "The yurt-tunnel-agent image.") @@ -206,6 +210,12 @@ func (co *ConvertOptions) Complete(flags *pflag.FlagSet) error { } co.YurttunnelServerImage = ytsi + ytsa, err := flags.GetString("yurt-tunnel-server-address") + if err != nil { + return err + } + co.YurttunnelServerAddress = ytsa + ytai, err := flags.GetString("yurt-tunnel-agent-image") if err != nil { return err @@ -351,7 +361,7 @@ func (co *ConvertOptions) RunConvert() (err error) { klog.Info("yurt-tunnel-server is deployed") // we will deploy yurt-tunnel-agent on every edge node if err = kubeutil.DeployYurttunnelAgent(co.clientSet, - edgeNodeNames, + co.YurttunnelServerAddress, co.YurttunnelAgentImage); err != nil { err = fmt.Errorf("fail to deploy the yurt-tunnel-agent: %s", err) return diff --git a/pkg/yurtctl/cmd/yurtinit/init.go b/pkg/yurtctl/cmd/yurtinit/init.go index de5105a97e5..edfd5d89676 100644 --- a/pkg/yurtctl/cmd/yurtinit/init.go +++ b/pkg/yurtctl/cmd/yurtinit/init.go @@ -85,19 +85,20 @@ var ( // Please note that this structure includes the public kubeadm config API, but only a subset of the options // supported by this api will be exposed as a flag. type initOptions struct { - cfgPath string - kubeconfigDir string - kubeconfigPath string - featureGatesString string - ignorePreflightErrors []string - bto *options.BootstrapTokenOptions - externalInitCfg *kubeadmapiv1beta2.InitConfiguration - externalClusterCfg *kubeadmapiv1beta2.ClusterConfiguration - kustomizeDir string - installCNIFile string - isConvertOpenYurtCluster bool - openyurtImageRegistry string - openyurtVersion string + cfgPath string + kubeconfigDir string + kubeconfigPath string + featureGatesString string + ignorePreflightErrors []string + bto *options.BootstrapTokenOptions + externalInitCfg *kubeadmapiv1beta2.InitConfiguration + externalClusterCfg *kubeadmapiv1beta2.ClusterConfiguration + kustomizeDir string + installCNIFile string + isConvertOpenYurtCluster bool + openyurtImageRegistry string + openyurtVersion string + openyurtTunnelServerAddress string } // compile-time assert that the local data object satisfies the phases data interface. @@ -106,24 +107,25 @@ var _ yurtphase.YurtInitData = &initData{} // initData defines all the runtime information used when running the kubeadm init workflow; // this data is shared across all the phases that are included in the workflow. type initData struct { - cfg *kubeadmapi.InitConfiguration - skipTokenPrint bool - dryRun bool - kubeconfigDir string - kubeconfigPath string - ignorePreflightErrors sets.String - certificatesDir string - dryRunDir string - externalCA bool - client clientset.Interface - outputWriter io.Writer - uploadCerts bool - skipCertificateKeyPrint bool - kustomizeDir string - cniFileName string - isConvertOpenYurtCluster bool - openyurtImageRegistry string - openyurtVersion string + cfg *kubeadmapi.InitConfiguration + skipTokenPrint bool + dryRun bool + kubeconfigDir string + kubeconfigPath string + ignorePreflightErrors sets.String + certificatesDir string + dryRunDir string + externalCA bool + client clientset.Interface + outputWriter io.Writer + uploadCerts bool + skipCertificateKeyPrint bool + kustomizeDir string + cniFileName string + isConvertOpenYurtCluster bool + openyurtImageRegistry string + openyurtVersion string + openyurtTunnelServerAddress string } // NewCmdInit returns "kubeadm init" command. @@ -253,6 +255,8 @@ func AddInitOtherFlags(flagSet *flag.FlagSet, initOptions *initOptions) { "Choose a container registry to pull OpenYurt component images from") flagSet.StringVar(&initOptions.openyurtVersion, "yurt-version", "", "Choose a specific OpenYurt version") + flagSet.StringVar(&initOptions.openyurtTunnelServerAddress, "yurt-tunnel-server-address", "", + "Choose an accessible address for tunnelAgent when deployed in an isolated network") flagSet.StringVar(&initOptions.installCNIFile, "install-cni-file", "", "Configure install cni yaml file.") flagSet.BoolVar( @@ -362,21 +366,22 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io } return &initData{ - cfg: cfg, - certificatesDir: cfg.CertificatesDir, - skipTokenPrint: false, - kubeconfigDir: options.kubeconfigDir, - kubeconfigPath: options.kubeconfigPath, - ignorePreflightErrors: ignorePreflightErrorsSet, - externalCA: externalCA, - outputWriter: out, - uploadCerts: false, - skipCertificateKeyPrint: false, - kustomizeDir: options.kustomizeDir, - isConvertOpenYurtCluster: options.isConvertOpenYurtCluster, - openyurtVersion: options.openyurtVersion, - cniFileName: options.installCNIFile, - openyurtImageRegistry: options.openyurtImageRegistry, + cfg: cfg, + certificatesDir: cfg.CertificatesDir, + skipTokenPrint: false, + kubeconfigDir: options.kubeconfigDir, + kubeconfigPath: options.kubeconfigPath, + ignorePreflightErrors: ignorePreflightErrorsSet, + externalCA: externalCA, + outputWriter: out, + uploadCerts: false, + skipCertificateKeyPrint: false, + kustomizeDir: options.kustomizeDir, + isConvertOpenYurtCluster: options.isConvertOpenYurtCluster, + openyurtVersion: options.openyurtVersion, + cniFileName: options.installCNIFile, + openyurtImageRegistry: options.openyurtImageRegistry, + openyurtTunnelServerAddress: options.openyurtTunnelServerAddress, }, nil } @@ -495,6 +500,11 @@ func (d *initData) CNIFileName() string { return d.cniFileName } +// YurtTunnelAddress return the openyurtTunnelServerAddress +func (d *initData) YurtTunnelAddress() string { + return d.openyurtTunnelServerAddress +} + // Client returns a Kubernetes client to be used by kubeadm. // This function is implemented as a singleton, thus avoiding to recreate the client when it is used by different phases. // Important. This function must be called after the admin.conf kubeconfig file is created. diff --git a/pkg/yurtctl/cmd/yurtinit/phases/install_yurt_addons.go b/pkg/yurtctl/cmd/yurtinit/phases/install_yurt_addons.go index b1cd3a47ef0..2f9d22aa4fe 100644 --- a/pkg/yurtctl/cmd/yurtinit/phases/install_yurt_addons.go +++ b/pkg/yurtctl/cmd/yurtinit/phases/install_yurt_addons.go @@ -61,6 +61,7 @@ func runInstallYurtAddons(c workflow.RunData) error { } imageRegistry := data.OpenYurtImageRegistry() version := data.OpenYurtVersion() + tunnelServerAddress := data.YurtTunnelAddress() if len(imageRegistry) == 0 { imageRegistry = YurtContants.DefaultOpenYurtImageRegistry @@ -77,7 +78,7 @@ func runInstallYurtAddons(c workflow.RunData) error { if err := kubeutil.DeployYurttunnelServer(client, nil, fmt.Sprintf("%s/%s:%s", imageRegistry, YurtContants.YurtTunnelServer, version), runtime.GOARCH); err != nil { return err } - if err := kubeutil.DeployYurttunnelAgent(client, nil, fmt.Sprintf("%s/%s:%s", imageRegistry, YurtContants.YurtTunnelAgent, version)); err != nil { + if err := kubeutil.DeployYurttunnelAgent(client, tunnelServerAddress, fmt.Sprintf("%s/%s:%s", imageRegistry, YurtContants.YurtTunnelAgent, version)); err != nil { return err } return nil diff --git a/pkg/yurtctl/cmd/yurtinit/phases/types.go b/pkg/yurtctl/cmd/yurtinit/phases/types.go index f45a919a923..2dcaa0debb7 100644 --- a/pkg/yurtctl/cmd/yurtinit/phases/types.go +++ b/pkg/yurtctl/cmd/yurtinit/phases/types.go @@ -25,5 +25,6 @@ type YurtInitData interface { IsConvertYurtCluster() bool OpenYurtVersion() string OpenYurtImageRegistry() string + YurtTunnelAddress() string CNIFileName() string } diff --git a/pkg/yurtctl/constants/yurt-tunnel-agent-tmpl.go b/pkg/yurtctl/constants/yurt-tunnel-agent-tmpl.go index 7c522b39add..2bc17da6a27 100644 --- a/pkg/yurtctl/constants/yurt-tunnel-agent-tmpl.go +++ b/pkg/yurtctl/constants/yurt-tunnel-agent-tmpl.go @@ -42,6 +42,9 @@ spec: - yurt-tunnel-agent args: - --node-name=$(NODE_NAME) + {{if .tunnelServerAddress }} + - --tunnelserver-addr={{.tunnelServerAddress}} + {{end}} image: {{.image}} imagePullPolicy: IfNotPresent name: yurt-tunnel-agent diff --git a/pkg/yurtctl/constants/yurt-tunnel-server-tmpl.go b/pkg/yurtctl/constants/yurt-tunnel-server-tmpl.go index 081f5006398..c7de0e4e249 100644 --- a/pkg/yurtctl/constants/yurt-tunnel-server-tmpl.go +++ b/pkg/yurtctl/constants/yurt-tunnel-server-tmpl.go @@ -125,6 +125,7 @@ spec: name: https - port: 10262 targetPort: 10262 + nodePort: 31008 name: tcp selector: k8s-app: yurt-tunnel-server diff --git a/pkg/yurtctl/util/kubernetes/apply_addons.go b/pkg/yurtctl/util/kubernetes/apply_addons.go index 69e0c2d3884..2921a5efbad 100644 --- a/pkg/yurtctl/util/kubernetes/apply_addons.go +++ b/pkg/yurtctl/util/kubernetes/apply_addons.go @@ -194,14 +194,15 @@ func DeployYurttunnelServer( func DeployYurttunnelAgent( client *kubernetes.Clientset, - tunnelAgentNodes []string, + tunnelServerAddress string, yurttunnelAgentImage string) error { // 1. Deploy the yurt-tunnel-agent DaemonSet if err := CreateDaemonSetFromYaml(client, constants.YurttunnelAgentDaemonSet, map[string]string{ - "image": yurttunnelAgentImage, - "edgeWorkerLabel": projectinfo.GetEdgeWorkerLabelKey()}); err != nil { + "image": yurttunnelAgentImage, + "edgeWorkerLabel": projectinfo.GetEdgeWorkerLabelKey(), + "tunnelServerAddress": tunnelServerAddress}); err != nil { return err } return nil