Skip to content

Commit 1bd46e5

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#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 <[email protected]>
2 parents 8b85b9e + 8bbeae0 commit 1bd46e5

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

cmd/kubeadm/app/cmd/init.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"bytes"
2021
"fmt"
22+
"html/template"
2123
"io"
2224
"io/ioutil"
23-
"strings"
2425

2526
"github.com/renstrom/dedent"
2627
"github.com/spf13/cobra"
@@ -36,6 +37,18 @@ import (
3637
netutil "k8s.io/kubernetes/pkg/util/net"
3738
)
3839

40+
const (
41+
joinArgsTemplateLiteral = `--token={{.Cfg.Secrets.GivenToken -}}
42+
{{if ne .Cfg.API.BindPort .DefaultAPIBindPort -}}
43+
{{" --api-port="}}{{.Cfg.API.BindPort -}}
44+
{{end -}}
45+
{{if ne .Cfg.Discovery.BindPort .DefaultDiscoveryBindPort -}}
46+
{{" --discovery-port="}}{{.Cfg.Discovery.BindPort -}}
47+
{{end -}}
48+
{{" "}}{{index .Cfg.API.AdvertiseAddresses 0 -}}
49+
`
50+
)
51+
3952
var (
4053
initDoneMsgf = dedent.Dedent(`
4154
Kubernetes master initialised successfully!
@@ -186,6 +199,13 @@ func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight
186199
return &Init{cfg: cfg}, nil
187200
}
188201

202+
// joinArgsData denotes a data object which is needed by function generateJoinArgs to generate kubeadm join arguments.
203+
type joinArgsData struct {
204+
Cfg *kubeadmapi.MasterConfiguration
205+
DefaultAPIBindPort uint
206+
DefaultDiscoveryBindPort uint
207+
}
208+
189209
// Run executes master node provisioning, including certificates, needed static pod manifests, etc.
190210
func (i *Init) Run(out io.Writer) error {
191211
if err := kubemaster.CreateTokenAuthFile(&i.cfg.Secrets); err != nil {
@@ -239,16 +259,21 @@ func (i *Init) Run(out io.Writer) error {
239259
return err
240260
}
241261

242-
// TODO(phase1+) we could probably use templates for this logic, and reference struct fields directly etc
243-
joinArgs := []string{fmt.Sprintf("--token=%s", i.cfg.Secrets.GivenToken)}
244-
if i.cfg.API.BindPort != kubeadmapi.DefaultAPIBindPort {
245-
joinArgs = append(joinArgs, fmt.Sprintf("--api-port=%d", i.cfg.API.BindPort))
246-
}
247-
if i.cfg.Discovery.BindPort != kubeadmapi.DefaultDiscoveryBindPort {
248-
joinArgs = append(joinArgs, fmt.Sprintf("--discovery-port=%d", i.cfg.Discovery.BindPort))
262+
data := joinArgsData{i.cfg, kubeadmapi.DefaultAPIBindPort, kubeadmapi.DefaultDiscoveryBindPort}
263+
if joinArgs, err := generateJoinArgs(data); err != nil {
264+
return err
265+
} else {
266+
fmt.Fprintf(out, initDoneMsgf, joinArgs)
249267
}
250-
joinArgs = append(joinArgs, i.cfg.API.AdvertiseAddresses[0])
251-
fmt.Fprintf(out, initDoneMsgf, strings.Join(joinArgs, " "))
252-
253268
return nil
254269
}
270+
271+
// generateJoinArgs generates kubeadm join arguments
272+
func generateJoinArgs(data joinArgsData) (string, error) {
273+
joinArgsTemplate := template.Must(template.New("joinArgsTemplate").Parse(joinArgsTemplateLiteral))
274+
var b bytes.Buffer
275+
if err := joinArgsTemplate.Execute(&b, data); err != nil {
276+
return "", err
277+
}
278+
return b.String(), nil
279+
}

0 commit comments

Comments
 (0)