Skip to content

Commit

Permalink
Merge pull request kubernetes#49018 from luxas/kubeadm_cleanup_kubern…
Browse files Browse the repository at this point in the history
…etesdir

Automatic merge from submit-queue (batch tested with PRs 49019, 48919, 49040, 49018, 48874)

kubeadm: Remove the old KubernetesDir envparam

**What this PR does / why we need it**:

The last piece of kubernetes#48053 for moving the env params into the API, and the KubernetesDir into a constant.

After this pretty mechanical change, we might offer short-hand functions in constants like `GetStaticPodManifestDir()` etc easily in order to centralize the `filepath.Join` logic into one place.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
fixes kubernetes/kubeadm#326

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
cc @kubernetes/sig-cluster-lifecycle-pr-reviews @fabriziopandini
  • Loading branch information
Kubernetes Submit Queue authored Jul 18, 2017
2 parents 3548031 + 6632899 commit d3ff93e
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 90 deletions.
1 change: 0 additions & 1 deletion cmd/kubeadm/app/apis/kubeadm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ go_library(
name = "go_default_library",
srcs = [
"doc.go",
"env.go",
"register.go",
"types.go",
],
Expand Down
43 changes: 0 additions & 43 deletions cmd/kubeadm/app/apis/kubeadm/env.go

This file was deleted.

4 changes: 0 additions & 4 deletions cmd/kubeadm/app/apis/kubeadm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type EnvParams struct {
KubernetesDir string
}

type MasterConfiguration struct {
metav1.TypeMeta

Expand Down
6 changes: 3 additions & 3 deletions cmd/kubeadm/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (i *Init) Run(out io.Writer) error {
// PHASE 2: Generate kubeconfig files for the admin and the kubelet

masterEndpoint := fmt.Sprintf("https://%s:%d", i.cfg.API.AdvertiseAddress, i.cfg.API.BindPort)
err = kubeconfigphase.CreateInitKubeConfigFiles(masterEndpoint, i.cfg.CertificatesDir, kubeadmapi.GlobalEnvParams.KubernetesDir, i.cfg.NodeName)
err = kubeconfigphase.CreateInitKubeConfigFiles(masterEndpoint, i.cfg.CertificatesDir, kubeadmconstants.KubernetesDir, i.cfg.NodeName)
if err != nil {
return err
}
Expand All @@ -240,7 +240,7 @@ func (i *Init) Run(out io.Writer) error {
return err
}

adminKubeConfigPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName)
adminKubeConfigPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName)
client, err := kubeadmutil.CreateClientAndWaitForAPI(adminKubeConfigPath)
if err != nil {
return err
Expand Down Expand Up @@ -297,7 +297,7 @@ func (i *Init) Run(out io.Writer) error {
}

ctx := map[string]string{
"KubeConfigPath": filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName),
"KubeConfigPath": filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName),
"KubeConfigName": kubeadmconstants.AdminKubeConfigFileName,
"Token": i.cfg.Token,
"MasterIP": i.cfg.API.AdvertiseAddress,
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (j *Join) Run(out io.Writer) error {
return err
}

kubeconfigFile := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)
kubeconfigFile := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)
if err := kubeconfigutil.WriteToDisk(kubeconfigFile, cfg); err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kubeadm/app/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/spf13/cobra"

kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
Expand Down Expand Up @@ -118,7 +117,7 @@ func (r *Reset) Run(out io.Writer) error {

// Only clear etcd data when the etcd manifest is found. In case it is not found, we must assume that the user
// provided external etcd endpoints. In that case, it is his own responsibility to reset etcd
etcdManifestPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "etcd.yaml")
etcdManifestPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "etcd.yaml")
if _, err := os.Stat(etcdManifestPath); err == nil {
dirsToClean = append(dirsToClean, "/var/lib/etcd")
} else {
Expand All @@ -132,7 +131,7 @@ func (r *Reset) Run(out io.Writer) error {
}

// Remove contents from the config and pki directories
resetConfigDir(kubeadmapi.GlobalEnvParams.KubernetesDir, r.certsDir)
resetConfigDir(kubeadmconstants.KubernetesDir, r.certsDir)

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/kubeadm/app/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"k8s.io/kubernetes/pkg/util/version"
)

const (
// KubernetesDir is the directory kubernetes owns for storing various configuration files
KubernetesDir = "/etc/kubernetes"
// KubernetesDir is the directory kubernetes owns for storing various configuration files
// This semi-constant MUST NOT be modified during runtime. It's a variable solely for use in unit testing.
var KubernetesDir = "/etc/kubernetes"

const (
ManifestsSubDirName = "manifests"

CACertAndKeyBaseName = "ca"
Expand Down
10 changes: 5 additions & 5 deletions cmd/kubeadm/app/phases/controlplane/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
staticPodSpecs[etcd] = etcdPod
}

manifestsPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ManifestsSubDirName)
manifestsPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)
if err := os.MkdirAll(manifestsPath, 0700); err != nil {
return fmt.Errorf("failed to create directory %q [%v]", manifestsPath, err)
}
Expand Down Expand Up @@ -233,15 +233,15 @@ func k8sVolume() v1.Volume {
return v1.Volume{
Name: "k8s",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{Path: kubeadmapi.GlobalEnvParams.KubernetesDir},
HostPath: &v1.HostPathVolumeSource{Path: kubeadmconstants.KubernetesDir},
},
}
}

func k8sVolumeMount() v1.VolumeMount {
return v1.VolumeMount{
Name: "k8s",
MountPath: kubeadmapi.GlobalEnvParams.KubernetesDir,
MountPath: kubeadmconstants.KubernetesDir,
ReadOnly: true,
}
}
Expand Down Expand Up @@ -371,7 +371,7 @@ func getControllerManagerCommand(cfg *kubeadmapi.MasterConfiguration, selfHosted
defaultArguments := map[string]string{
"address": "127.0.0.1",
"leader-elect": "true",
"kubeconfig": filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ControllerManagerKubeConfigFileName),
"kubeconfig": filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ControllerManagerKubeConfigFileName),
"root-ca-file": filepath.Join(cfg.CertificatesDir, kubeadmconstants.CACertName),
"service-account-private-key-file": filepath.Join(cfg.CertificatesDir, kubeadmconstants.ServiceAccountPrivateKeyName),
"cluster-signing-cert-file": filepath.Join(cfg.CertificatesDir, kubeadmconstants.CACertName),
Expand Down Expand Up @@ -404,7 +404,7 @@ func getSchedulerCommand(cfg *kubeadmapi.MasterConfiguration, selfHosted bool) [
defaultArguments := map[string]string{
"address": "127.0.0.1",
"leader-elect": "true",
"kubeconfig": filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.SchedulerKubeConfigFileName),
"kubeconfig": filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.SchedulerKubeConfigFileName),
}

command := []string{"kube-scheduler"}
Expand Down
20 changes: 9 additions & 11 deletions cmd/kubeadm/app/phases/controlplane/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ func TestWriteStaticPodManifests(t *testing.T) {
}
defer os.RemoveAll(tmpdir)

// set up tmp GlobalEnvParams values for testing
oldEnv := kubeadmapi.GlobalEnvParams
kubeadmapi.GlobalEnvParams.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)
defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()
// set up tmp KubernetesDir for testing
kubeadmconstants.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)

var tests = []struct {
cfg *kubeadmapi.MasterConfiguration
Expand Down Expand Up @@ -86,7 +84,7 @@ func TestWriteStaticPodManifests(t *testing.T) {

// Below is dead code.
if rt.expectedAPIProbePort != 0 {
manifest, err := os.Open(filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "kube-apiserver.yaml"))
manifest, err := os.Open(filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "kube-apiserver.yaml"))
if err != nil {
t.Errorf("WriteStaticPodManifests: %v", err)
continue
Expand Down Expand Up @@ -340,7 +338,7 @@ func TestK8sVolume(t *testing.T) {
Name: "k8s",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: kubeadmapi.GlobalEnvParams.KubernetesDir},
Path: kubeadmconstants.KubernetesDir},
}},
},
}
Expand Down Expand Up @@ -371,7 +369,7 @@ func TestK8sVolumeMount(t *testing.T) {
{
expected: v1.VolumeMount{
Name: "k8s",
MountPath: kubeadmapi.GlobalEnvParams.KubernetesDir,
MountPath: kubeadmconstants.KubernetesDir,
ReadOnly: true,
},
},
Expand Down Expand Up @@ -688,7 +686,7 @@ func TestGetControllerManagerCommand(t *testing.T) {
"kube-controller-manager",
"--address=127.0.0.1",
"--leader-elect=true",
"--kubeconfig=" + kubeadmapi.GlobalEnvParams.KubernetesDir + "/controller-manager.conf",
"--kubeconfig=" + kubeadmconstants.KubernetesDir + "/controller-manager.conf",
"--root-ca-file=" + testCertsDir + "/ca.crt",
"--service-account-private-key-file=" + testCertsDir + "/sa.key",
"--cluster-signing-cert-file=" + testCertsDir + "/ca.crt",
Expand All @@ -707,7 +705,7 @@ func TestGetControllerManagerCommand(t *testing.T) {
"kube-controller-manager",
"--address=127.0.0.1",
"--leader-elect=true",
"--kubeconfig=" + kubeadmapi.GlobalEnvParams.KubernetesDir + "/controller-manager.conf",
"--kubeconfig=" + kubeadmconstants.KubernetesDir + "/controller-manager.conf",
"--root-ca-file=" + testCertsDir + "/ca.crt",
"--service-account-private-key-file=" + testCertsDir + "/sa.key",
"--cluster-signing-cert-file=" + testCertsDir + "/ca.crt",
Expand All @@ -727,7 +725,7 @@ func TestGetControllerManagerCommand(t *testing.T) {
"kube-controller-manager",
"--address=127.0.0.1",
"--leader-elect=true",
"--kubeconfig=" + kubeadmapi.GlobalEnvParams.KubernetesDir + "/controller-manager.conf",
"--kubeconfig=" + kubeadmconstants.KubernetesDir + "/controller-manager.conf",
"--root-ca-file=" + testCertsDir + "/ca.crt",
"--service-account-private-key-file=" + testCertsDir + "/sa.key",
"--cluster-signing-cert-file=" + testCertsDir + "/ca.crt",
Expand Down Expand Up @@ -817,7 +815,7 @@ func TestGetSchedulerCommand(t *testing.T) {
"kube-scheduler",
"--address=127.0.0.1",
"--leader-elect=true",
"--kubeconfig=" + kubeadmapi.GlobalEnvParams.KubernetesDir + "/scheduler.conf",
"--kubeconfig=" + kubeadmconstants.KubernetesDir + "/scheduler.conf",
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/selfhosting/selfhosting_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type tlsKeyPair struct {
func k8sSelfHostedVolumeMount() v1.VolumeMount {
return v1.VolumeMount{
Name: "k8s",
MountPath: kubeadmapi.GlobalEnvParams.KubernetesDir,
MountPath: kubeadmconstants.KubernetesDir,
ReadOnly: true,
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func createOpaqueSecrets(cfg *kubeadmapi.MasterConfiguration, client *clientset.
for _, file := range files {
secret, err := createOpaqueSecretFromFile(
file,
path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, file),
path.Join(kubeadmconstants.KubernetesDir, file),
)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubeadm/app/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func RunInitMasterChecks(cfg *kubeadmapi.MasterConfiguration) error {
PortOpenCheck{port: 10251},
PortOpenCheck{port: 10252},
HTTPProxyCheck{Proto: "https", Host: cfg.API.AdvertiseAddress, Port: int(cfg.API.BindPort)},
DirAvailableCheck{Path: filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ManifestsSubDirName)},
DirAvailableCheck{Path: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)},
DirAvailableCheck{Path: "/var/lib/kubelet"},
FileContentCheck{Path: bridgenf, Content: []byte{'1'}},
InPathCheck{executable: "ip", mandatory: true},
Expand Down Expand Up @@ -603,10 +603,10 @@ func RunJoinNodeChecks(cfg *kubeadmapi.NodeConfiguration) error {
ServiceCheck{Service: "kubelet", CheckIfActive: false},
ServiceCheck{Service: "docker", CheckIfActive: true},
PortOpenCheck{port: 10250},
DirAvailableCheck{Path: filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.ManifestsSubDirName)},
DirAvailableCheck{Path: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)},
DirAvailableCheck{Path: "/var/lib/kubelet"},
FileAvailableCheck{Path: cfg.CACertPath},
FileAvailableCheck{Path: filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)},
FileAvailableCheck{Path: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)},
FileContentCheck{Path: bridgenf, Content: []byte{'1'}},
InPathCheck{executable: "ip", mandatory: true},
InPathCheck{executable: "iptables", mandatory: true},
Expand Down
1 change: 0 additions & 1 deletion cmd/kubeadm/app/util/kubeconfig/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_test(
srcs = ["kubeconfig_test.go"],
library = ":go_default_library",
tags = ["automanaged"],
deps = ["//cmd/kubeadm/app/apis/kubeadm:go_default_library"],
)

go_library(
Expand Down
11 changes: 1 addition & 10 deletions cmd/kubeadm/app/util/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
)

const (
Expand Down Expand Up @@ -144,12 +141,6 @@ func TestWriteKubeconfigToDisk(t *testing.T) {
}
defer os.RemoveAll(tmpdir)

// set up tmp GlobalEnvParams values for testing
oldEnv := kubeadmapi.GlobalEnvParams
kubeadmapi.GlobalEnvParams = kubeadmapi.SetEnvParams()
kubeadmapi.GlobalEnvParams.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)
defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()

var writeConfig = []struct {
name string
cc configClient
Expand All @@ -168,7 +159,7 @@ func TestWriteKubeconfigToDisk(t *testing.T) {
rt.cc.caCert,
rt.ccWithToken.token,
)
configPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, fmt.Sprintf("%s.conf", rt.name))
configPath := fmt.Sprintf("%s/etc/kubernetes/%s.conf", tmpdir, rt.name)
err := WriteToDisk(configPath, c)
if err != rt.expected {
t.Errorf(
Expand Down

0 comments on commit d3ff93e

Please sign in to comment.