mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #35265 from redhatlinux10/master-patch-optimise-kubeadm-join-args-generation
Automatic merge from submit-queue enhance join arguments generation logic using template **What this PR does / why we need it**: this PR enhances kubeadm join arguments generation logic using template, this makes code more readable and adding arguments more easier. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: <!-- Steps to write your release note: 1. Use the release-note-* labels to set the release note state (if you have access) 2. Enter your extended release note in the below block; leaving it blank means using the PR title as the release note. If no release note is required, just write `NONE`. --> ```release-note ``` Signed-off-by: 欧阳钦华10079130 <ouyang.qinhua@zte.com.cn>
This commit is contained in:
commit
1bd46e5a4e
@ -17,10 +17,11 @@ limitations under the License.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/renstrom/dedent"
|
"github.com/renstrom/dedent"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -36,6 +37,18 @@ import (
|
|||||||
netutil "k8s.io/kubernetes/pkg/util/net"
|
netutil "k8s.io/kubernetes/pkg/util/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
joinArgsTemplateLiteral = `--token={{.Cfg.Secrets.GivenToken -}}
|
||||||
|
{{if ne .Cfg.API.BindPort .DefaultAPIBindPort -}}
|
||||||
|
{{" --api-port="}}{{.Cfg.API.BindPort -}}
|
||||||
|
{{end -}}
|
||||||
|
{{if ne .Cfg.Discovery.BindPort .DefaultDiscoveryBindPort -}}
|
||||||
|
{{" --discovery-port="}}{{.Cfg.Discovery.BindPort -}}
|
||||||
|
{{end -}}
|
||||||
|
{{" "}}{{index .Cfg.API.AdvertiseAddresses 0 -}}
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
initDoneMsgf = dedent.Dedent(`
|
initDoneMsgf = dedent.Dedent(`
|
||||||
Kubernetes master initialised successfully!
|
Kubernetes master initialised successfully!
|
||||||
@ -186,6 +199,13 @@ func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight
|
|||||||
return &Init{cfg: cfg}, nil
|
return &Init{cfg: cfg}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// joinArgsData denotes a data object which is needed by function generateJoinArgs to generate kubeadm join arguments.
|
||||||
|
type joinArgsData struct {
|
||||||
|
Cfg *kubeadmapi.MasterConfiguration
|
||||||
|
DefaultAPIBindPort uint
|
||||||
|
DefaultDiscoveryBindPort uint
|
||||||
|
}
|
||||||
|
|
||||||
// Run executes master node provisioning, including certificates, needed static pod manifests, etc.
|
// Run executes master node provisioning, including certificates, needed static pod manifests, etc.
|
||||||
func (i *Init) Run(out io.Writer) error {
|
func (i *Init) Run(out io.Writer) error {
|
||||||
if err := kubemaster.CreateTokenAuthFile(&i.cfg.Secrets); err != nil {
|
if err := kubemaster.CreateTokenAuthFile(&i.cfg.Secrets); err != nil {
|
||||||
@ -239,16 +259,21 @@ func (i *Init) Run(out io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(phase1+) we could probably use templates for this logic, and reference struct fields directly etc
|
data := joinArgsData{i.cfg, kubeadmapi.DefaultAPIBindPort, kubeadmapi.DefaultDiscoveryBindPort}
|
||||||
joinArgs := []string{fmt.Sprintf("--token=%s", i.cfg.Secrets.GivenToken)}
|
if joinArgs, err := generateJoinArgs(data); err != nil {
|
||||||
if i.cfg.API.BindPort != kubeadmapi.DefaultAPIBindPort {
|
return err
|
||||||
joinArgs = append(joinArgs, fmt.Sprintf("--api-port=%d", i.cfg.API.BindPort))
|
} else {
|
||||||
|
fmt.Fprintf(out, initDoneMsgf, joinArgs)
|
||||||
}
|
}
|
||||||
if i.cfg.Discovery.BindPort != kubeadmapi.DefaultDiscoveryBindPort {
|
|
||||||
joinArgs = append(joinArgs, fmt.Sprintf("--discovery-port=%d", i.cfg.Discovery.BindPort))
|
|
||||||
}
|
|
||||||
joinArgs = append(joinArgs, i.cfg.API.AdvertiseAddresses[0])
|
|
||||||
fmt.Fprintf(out, initDoneMsgf, strings.Join(joinArgs, " "))
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generateJoinArgs generates kubeadm join arguments
|
||||||
|
func generateJoinArgs(data joinArgsData) (string, error) {
|
||||||
|
joinArgsTemplate := template.Must(template.New("joinArgsTemplate").Parse(joinArgsTemplateLiteral))
|
||||||
|
var b bytes.Buffer
|
||||||
|
if err := joinArgsTemplate.Execute(&b, data); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return b.String(), nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user