|
| 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 | +} |
0 commit comments