<commit_before>/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package phases

import (
	"fmt"

	"github.com/spf13/cobra"

	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
	kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3"
	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
	cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
	kubeletphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet"
	"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
	configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
	kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
	"k8s.io/kubernetes/pkg/util/normalizer"
	"k8s.io/kubernetes/pkg/util/version"
	utilsexec "k8s.io/utils/exec"
)

var (
	kubeletWriteEnvFileLongDesc = normalizer.LongDesc(`
		Writes an environment file with flags that should be passed to the kubelet executing on the master or node.
		This --config flag can either consume a InitConfiguration object or a JoinConfiguration one, as this
		function is used for both "kubeadm init" and "kubeadm join".
		` + cmdutil.AlphaDisclaimer)

	kubeletWriteEnvFileExample = normalizer.Examples(`
		# Writes a dynamic environment file with kubelet flags from a InitConfiguration file.
		kubeadm alpha phase kubelet write-env-file --config masterconfig.yaml

		# Writes a dynamic environment file with kubelet flags from a JoinConfiguration file.
		kubeadm alpha phase kubelet write-env-file --config nodeconfig.yaml
		`)

	kubeletConfigUploadLongDesc = normalizer.LongDesc(`
		Uploads kubelet configuration extracted from the kubeadm InitConfiguration object to a ConfigMap
		of the form kubelet-config-1.X in the cluster, where X is the minor version of the current (API Server) Kubernetes version.
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigUploadExample = normalizer.Examples(`
		# Uploads the kubelet configuration from the kubeadm Config file to a ConfigMap in the cluster.
		kubeadm alpha phase kubelet config upload --config kubeadm.yaml
		`)

	kubeletConfigDownloadLongDesc = normalizer.LongDesc(`
		Downloads the kubelet configuration from a ConfigMap of the form "kubelet-config-1.X" in the cluster,
		where X is the minor version of the kubelet. Either kubeadm autodetects the kubelet version by exec-ing
		"kubelet --version" or respects the --kubelet-version parameter.
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigDownloadExample = normalizer.Examples(`
		# Downloads the kubelet configuration from the ConfigMap in the cluster. Autodetects the kubelet version.
		kubeadm alpha phase kubelet config download

		# Downloads the kubelet configuration from the ConfigMap in the cluster. Uses a specific desired kubelet version.
		kubeadm alpha phase kubelet config download --kubelet-version v1.11.0
		`)

	kubeletConfigWriteToDiskLongDesc = normalizer.LongDesc(`
		Writes kubelet configuration to disk, based on the kubeadm configuration passed via "--config".
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigWriteToDiskExample = normalizer.Examples(`
		# Extracts the kubelet configuration from a kubeadm configuration file
		kubeadm alpha phase kubelet config write-to-disk --config kubeadm.yaml
		`)

	kubeletConfigEnableDynamicLongDesc = normalizer.LongDesc(`
		Enables or updates dynamic kubelet configuration for a Node, against the kubelet-config-1.X ConfigMap in the cluster,
		where X is the minor version of the desired kubelet version.

		WARNING: This feature is still experimental, and disabled by default. Enable only if you know what you are doing, as it
		may have surprising side-effects at this stage.

		` + cmdutil.AlphaDisclaimer)

	kubeletConfigEnableDynamicExample = normalizer.Examples(`
		# Enables dynamic kubelet configuration for a Node.
		kubeadm alpha phase kubelet enable-dynamic-config --node-name node-1 --kubelet-version v1.11.0

		WARNING: This feature is still experimental, and disabled by default. Enable only if you know what you are doing, as it
		may have surprising side-effects at this stage.
		`)
)

// NewCmdKubelet returns command for `kubeadm phase kubelet`
func NewCmdKubelet() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "kubelet",
		Short: "Commands related to handling the kubelet.",
		Long:  cmdutil.MacroCommandLongDescription,
	}

	cmd.AddCommand(NewCmdKubeletConfig())
	cmd.AddCommand(NewCmdKubeletWriteEnvFile())
	return cmd
}

// NewCmdKubeletWriteEnvFile calls cobra.Command for writing the dynamic kubelet env file based on a InitConfiguration or JoinConfiguration object
func NewCmdKubeletWriteEnvFile() *cobra.Command {
	var cfgPath string

	cmd := &cobra.Command{
		Use:     "write-env-file",
		Short:   "Writes an environment file with runtime flags for the kubelet.",
		Long:    kubeletWriteEnvFileLongDesc,
		Example: kubeletWriteEnvFileExample,
		Run: func(cmd *cobra.Command, args []string) {
			err := RunKubeletWriteEnvFile(cfgPath)
			kubeadmutil.CheckErr(err)
		},
	}

	options.AddConfigFlag(cmd.Flags(), &cfgPath)<commit_msg>Check config path for command "kubeadm alpha phase kubelet write-env-file"<commit_after>/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package phases

import (
	"fmt"

	"github.com/spf13/cobra"

	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
	kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3"
	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
	cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
	kubeletphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet"
	"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
	configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
	kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
	"k8s.io/kubernetes/pkg/util/normalizer"
	"k8s.io/kubernetes/pkg/util/version"
	utilsexec "k8s.io/utils/exec"
)

var (
	kubeletWriteEnvFileLongDesc = normalizer.LongDesc(`
		Writes an environment file with flags that should be passed to the kubelet executing on the master or node.
		This --config flag can either consume a InitConfiguration object or a JoinConfiguration one, as this
		function is used for both "kubeadm init" and "kubeadm join".
		` + cmdutil.AlphaDisclaimer)

	kubeletWriteEnvFileExample = normalizer.Examples(`
		# Writes a dynamic environment file with kubelet flags from a InitConfiguration file.
		kubeadm alpha phase kubelet write-env-file --config masterconfig.yaml

		# Writes a dynamic environment file with kubelet flags from a JoinConfiguration file.
		kubeadm alpha phase kubelet write-env-file --config nodeconfig.yaml
		`)

	kubeletConfigUploadLongDesc = normalizer.LongDesc(`
		Uploads kubelet configuration extracted from the kubeadm InitConfiguration object to a ConfigMap
		of the form kubelet-config-1.X in the cluster, where X is the minor version of the current (API Server) Kubernetes version.
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigUploadExample = normalizer.Examples(`
		# Uploads the kubelet configuration from the kubeadm Config file to a ConfigMap in the cluster.
		kubeadm alpha phase kubelet config upload --config kubeadm.yaml
		`)

	kubeletConfigDownloadLongDesc = normalizer.LongDesc(`
		Downloads the kubelet configuration from a ConfigMap of the form "kubelet-config-1.X" in the cluster,
		where X is the minor version of the kubelet. Either kubeadm autodetects the kubelet version by exec-ing
		"kubelet --version" or respects the --kubelet-version parameter.
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigDownloadExample = normalizer.Examples(`
		# Downloads the kubelet configuration from the ConfigMap in the cluster. Autodetects the kubelet version.
		kubeadm alpha phase kubelet config download

		# Downloads the kubelet configuration from the ConfigMap in the cluster. Uses a specific desired kubelet version.
		kubeadm alpha phase kubelet config download --kubelet-version v1.11.0
		`)

	kubeletConfigWriteToDiskLongDesc = normalizer.LongDesc(`
		Writes kubelet configuration to disk, based on the kubeadm configuration passed via "--config".
		` + cmdutil.AlphaDisclaimer)

	kubeletConfigWriteToDiskExample = normalizer.Examples(`
		# Extracts the kubelet configuration from a kubeadm configuration file
		kubeadm alpha phase kubelet config write-to-disk --config kubeadm.yaml
		`)

	kubeletConfigEnableDynamicLongDesc = normalizer.LongDesc(`
		Enables or updates dynamic kubelet configuration for a Node, against the kubelet-config-1.X ConfigMap in the cluster,
		where X is the minor version of the desired kubelet version.

		WARNING: This feature is still experimental, and disabled by default. Enable only if you know what you are doing, as it
		may have surprising side-effects at this stage.

		` + cmdutil.AlphaDisclaimer)

	kubeletConfigEnableDynamicExample = normalizer.Examples(`
		# Enables dynamic kubelet configuration for a Node.
		kubeadm alpha phase kubelet enable-dynamic-config --node-name node-1 --kubelet-version v1.11.0

		WARNING: This feature is still experimental, and disabled by default. Enable only if you know what you are doing, as it
		may have surprising side-effects at this stage.
		`)
)

// NewCmdKubelet returns command for `kubeadm phase kubelet`
func NewCmdKubelet() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "kubelet",
		Short: "Commands related to handling the kubelet.",
		Long:  cmdutil.MacroCommandLongDescription,
	}

	cmd.AddCommand(NewCmdKubeletConfig())
	cmd.AddCommand(NewCmdKubeletWriteEnvFile())
	return cmd
}

// NewCmdKubeletWriteEnvFile calls cobra.Command for writing the dynamic kubelet env file based on a InitConfiguration or JoinConfiguration object
func NewCmdKubeletWriteEnvFile() *cobra.Command {
	var cfgPath string

	cmd := &cobra.Command{
		Use:     "write-env-file",
		Short:   "Writes an environment file with runtime flags for the kubelet.",
		Long:    kubeletWriteEnvFileLongDesc,
		Example: kubeletWriteEnvFileExample,
		Run: func(cmd *cobra.Command, args []string) {
			if len(cfgPath) == 0 {
				kubeadmutil.CheckErr(fmt.Errorf("The --config flag is mandatory"))
			}

			err := RunKubeletWriteEnvFile(cfgPath)
			kubeadmutil.CheckErr(err)
		},
	}

	options.AddConfigFlag(cmd.Flags(), &cfgPath)
{'fraction_non_alphanumeric': 0.07557831538054036, 'fraction_numerical': 0.006975101536288187, 'mean_word_length': 4.5688298918387416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3613595', 'n_tokens_mistral': 3746, 'n_tokens_neox': 3509, 'n_words': 1142}