Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea01771

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #51171 from andrewrynhard/proxy-dns-phase
Automatic merge from submit-queue kubeadm: add `kubeadm phase addons` command **What this PR does / why we need it**: Adds the `addons` phase command to `kubeadm` fixes: kubernetes/kubeadm#418 /cc @luxas
2 parents a51eb2a + d55cea6 commit ea01771

7 files changed

Lines changed: 236 additions & 3 deletions

File tree

cmd/kubeadm/app/cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (i *Init) Run(out io.Writer) error {
401401
return err
402402
}
403403

404-
if err := dnsaddonphase.EnsureDNSAddon(i.cfg, client, k8sVersion); err != nil {
404+
if err := dnsaddonphase.EnsureDNSAddon(i.cfg, client); err != nil {
405405
return err
406406
}
407407

cmd/kubeadm/app/cmd/phases/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load(
99
go_library(
1010
name = "go_default_library",
1111
srcs = [
12+
"addons.go",
1213
"bootstraptoken.go",
1314
"certs.go",
1415
"controlplane.go",
@@ -28,6 +29,8 @@ go_library(
2829
"//cmd/kubeadm/app/cmd/util:go_default_library",
2930
"//cmd/kubeadm/app/constants:go_default_library",
3031
"//cmd/kubeadm/app/features:go_default_library",
32+
"//cmd/kubeadm/app/phases/addons/dns:go_default_library",
33+
"//cmd/kubeadm/app/phases/addons/proxy:go_default_library",
3134
"//cmd/kubeadm/app/phases/bootstraptoken/clusterinfo:go_default_library",
3235
"//cmd/kubeadm/app/phases/bootstraptoken/node:go_default_library",
3336
"//cmd/kubeadm/app/phases/certs:go_default_library",
@@ -52,6 +55,7 @@ go_library(
5255
go_test(
5356
name = "go_default_test",
5457
srcs = [
58+
"addons_test.go",
5559
"certs_test.go",
5660
"controlplane_test.go",
5761
"etcd_test.go",
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package phases
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
clientset "k8s.io/client-go/kubernetes"
23+
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
24+
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
25+
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
26+
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
27+
dnsaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
28+
proxyaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy"
29+
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
30+
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
31+
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
32+
"k8s.io/kubernetes/pkg/api"
33+
)
34+
35+
// NewCmdAddon returns the addon Cobra command
36+
func NewCmdAddon() *cobra.Command {
37+
cmd := &cobra.Command{
38+
Use: "addon <addon-name>",
39+
Aliases: []string{"addons"},
40+
Short: "Install an addon to a Kubernetes cluster.",
41+
RunE: cmdutil.SubCmdRunE("addon"),
42+
}
43+
44+
cmd.AddCommand(getAddonsSubCommands()...)
45+
return cmd
46+
}
47+
48+
// EnsureAllAddons install all addons to a Kubernetes cluster.
49+
func EnsureAllAddons(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface) error {
50+
51+
addonActions := []func(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface) error{
52+
dnsaddon.EnsureDNSAddon,
53+
proxyaddon.EnsureProxyAddon,
54+
}
55+
56+
for _, action := range addonActions {
57+
err := action(cfg, client)
58+
if err != nil {
59+
return err
60+
}
61+
}
62+
63+
return nil
64+
}
65+
66+
// getAddonsSubCommands returns sub commands for addons phase
67+
func getAddonsSubCommands() []*cobra.Command {
68+
cfg := &kubeadmapiext.MasterConfiguration{}
69+
// Default values for the cobra help text
70+
api.Scheme.Default(cfg)
71+
72+
var cfgPath, kubeConfigFile string
73+
var subCmds []*cobra.Command
74+
75+
subCmdProperties := []struct {
76+
use string
77+
short string
78+
cmdFunc func(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface) error
79+
}{
80+
{
81+
use: "all",
82+
short: "Install all addons to a Kubernetes cluster",
83+
cmdFunc: EnsureAllAddons,
84+
},
85+
{
86+
use: "kube-dns",
87+
short: "Install the kube-dns addon to a Kubernetes cluster.",
88+
cmdFunc: dnsaddon.EnsureDNSAddon,
89+
},
90+
{
91+
use: "kube-proxy",
92+
short: "Install the kube-proxy addon to a Kubernetes cluster.",
93+
cmdFunc: proxyaddon.EnsureProxyAddon,
94+
},
95+
}
96+
97+
for _, properties := range subCmdProperties {
98+
// Creates the UX Command
99+
cmd := &cobra.Command{
100+
Use: properties.use,
101+
Short: properties.short,
102+
Run: runAddonsCmdFunc(properties.cmdFunc, cfg, cfgPath),
103+
}
104+
105+
// Add flags to the command
106+
cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", "/etc/kubernetes/admin.conf", "The KubeConfig file to use for talking to the cluster")
107+
cmd.Flags().StringVar(&cfgPath, "config", cfgPath, "Path to kubeadm config file (WARNING: Usage of a configuration file is experimental)")
108+
cmd.Flags().StringVar(&cfg.KubernetesVersion, "kubernetes-version", cfg.KubernetesVersion, `Choose a specific Kubernetes version for the control plane.`)
109+
cmd.Flags().StringVar(&cfg.ImageRepository, "image-repository", cfg.ImageRepository, `Choose a container registry to pull control plane images from.`)
110+
111+
if properties.use == "all" || properties.use == "kube-proxy" {
112+
cmd.Flags().StringVar(&cfg.API.AdvertiseAddress, "apiserver-advertise-address", cfg.API.AdvertiseAddress, `The IP address the API Server will advertise it's listening on. 0.0.0.0 means the default network interface's address.`)
113+
cmd.Flags().Int32Var(&cfg.API.BindPort, "apiserver-bind-port", cfg.API.BindPort, `Port for the API Server to bind to.`)
114+
cmd.Flags().StringVar(&cfg.Networking.PodSubnet, "pod-network-cidr", cfg.Networking.PodSubnet, `Specify range of IP addresses for the pod network; if set, the control plane will automatically allocate CIDRs for every node.`)
115+
}
116+
117+
if properties.use == "all" || properties.use == "kube-dns" {
118+
cmd.Flags().StringVar(&cfg.Networking.DNSDomain, "service-dns-domain", cfg.Networking.DNSDomain, `Use alternative domain for services, e.g. "myorg.internal.`)
119+
}
120+
subCmds = append(subCmds, cmd)
121+
}
122+
123+
return subCmds
124+
}
125+
126+
// runAddonsCmdFunc creates a cobra.Command Run function, by composing the call to the given cmdFunc with necessary additional steps (e.g preparation of input parameters)
127+
func runAddonsCmdFunc(cmdFunc func(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface) error, cfg *kubeadmapiext.MasterConfiguration, cfgPath string) func(cmd *cobra.Command, args []string) {
128+
129+
// the following statement build a clousure that wraps a call to a cmdFunc, binding
130+
// the function itself with the specific parameters of each sub command.
131+
// Please note that specific parameter should be passed as value, while other parameters - passed as reference -
132+
// are shared between sub commands and gets access to current value e.g. flags value.
133+
134+
return func(cmd *cobra.Command, args []string) {
135+
if err := validation.ValidateMixedArguments(cmd.Flags()); err != nil {
136+
kubeadmutil.CheckErr(err)
137+
}
138+
139+
var kubeConfigFile string
140+
internalcfg := &kubeadmapi.MasterConfiguration{}
141+
api.Scheme.Convert(cfg, internalcfg, nil)
142+
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
143+
kubeadmutil.CheckErr(err)
144+
internalcfg, err = configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
145+
kubeadmutil.CheckErr(err)
146+
147+
// Execute the cmdFunc
148+
err = cmdFunc(internalcfg, client)
149+
kubeadmutil.CheckErr(err)
150+
}
151+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package phases
18+
19+
import (
20+
"testing"
21+
22+
// required for triggering api machinery startup when running unit tests
23+
_ "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/install"
24+
25+
cmdtestutil "k8s.io/kubernetes/cmd/kubeadm/test/cmd"
26+
)
27+
28+
func TestAddonsSubCommandsHasFlags(t *testing.T) {
29+
30+
subCmds := getAddonsSubCommands()
31+
32+
commonFlags := []string{
33+
"kubeconfig",
34+
"config",
35+
"kubernetes-version",
36+
"image-repository",
37+
}
38+
39+
var tests = []struct {
40+
command string
41+
additionalFlags []string
42+
}{
43+
{
44+
command: "all",
45+
additionalFlags: []string{
46+
"apiserver-advertise-address",
47+
"apiserver-bind-port",
48+
"pod-network-cidr",
49+
"service-dns-domain",
50+
},
51+
},
52+
{
53+
command: "kube-proxy",
54+
additionalFlags: []string{
55+
"apiserver-advertise-address",
56+
"apiserver-bind-port",
57+
"pod-network-cidr",
58+
},
59+
},
60+
{
61+
command: "kube-dns",
62+
additionalFlags: []string{
63+
"service-dns-domain",
64+
},
65+
},
66+
}
67+
68+
for _, test := range tests {
69+
expectedFlags := append(commonFlags, test.additionalFlags...)
70+
cmdtestutil.AssertSubCommandHasFlags(t, subCmds, test.command, expectedFlags...)
71+
}
72+
}

cmd/kubeadm/app/cmd/phases/phase.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCmdPhase(out io.Writer) *cobra.Command {
3131
RunE: cmdutil.SubCmdRunE("phase"),
3232
}
3333

34+
cmd.AddCommand(NewCmdAddon())
3435
cmd.AddCommand(NewCmdBootstrapToken())
3536
cmd.AddCommand(NewCmdCerts())
3637
cmd.AddCommand(NewCmdControlplane())

cmd/kubeadm/app/phases/addons/dns/dns.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ const (
4141
)
4242

4343
// EnsureDNSAddon creates the kube-dns addon
44-
func EnsureDNSAddon(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface, k8sVersion *version.Version) error {
44+
func EnsureDNSAddon(cfg *kubeadmapi.MasterConfiguration, client clientset.Interface) error {
45+
k8sVersion, err := version.ParseSemantic(cfg.KubernetesVersion)
46+
if err != nil {
47+
return fmt.Errorf("couldn't parse kubernetes version %q: %v", cfg.KubernetesVersion, err)
48+
}
49+
4550
if err := CreateServiceAccount(client); err != nil {
4651
return err
4752
}

cmd/kubeadm/app/phases/upgrade/postupgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.MasterC
8383
}
8484

8585
// Upgrade kube-dns and kube-proxy
86-
if err := dns.EnsureDNSAddon(cfg, client, k8sVersion); err != nil {
86+
if err := dns.EnsureDNSAddon(cfg, client); err != nil {
8787
errs = append(errs, err)
8888
}
8989
if err := proxy.EnsureProxyAddon(cfg, client); err != nil {

0 commit comments

Comments
 (0)