Reduce size of pgmconfig package

This commit is contained in:
Jeffrey Regan
2019-10-02 16:27:13 -07:00
committed by jregan
parent 14b0a65091
commit baa0296a12
53 changed files with 498 additions and 285 deletions

View File

@@ -1,55 +0,0 @@
/*
Copyright 2019 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 commands holds the CLI glue mapping textual commands/args to method calls.
package pgmconfig
import (
"os"
"path/filepath"
"runtime"
)
//noinspection GoSnakeCaseUsage
const (
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
defaultConfigSubdir = ".config"
)
// Use https://github.com/kirsle/configdir instead?
func ConfigRoot() string {
dir := os.Getenv(XDG_CONFIG_HOME)
if len(dir) == 0 {
dir = filepath.Join(
HomeDir(), defaultConfigSubdir)
}
return filepath.Join(dir, ProgramName)
}
func HomeDir() string {
home := os.Getenv(homeEnv())
if len(home) > 0 {
return home
}
return "~"
}
func homeEnv() string {
if runtime.GOOS == "windows" {
return "USERPROFILE"
}
return "HOME"
}

View File

@@ -1,56 +0,0 @@
/*
Copyright 2019 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 pgmconfig
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestConfigDirNoXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(XDG_CONFIG_HOME)
if isSet {
os.Unsetenv(XDG_CONFIG_HOME)
}
s := ConfigRoot()
if isSet {
os.Setenv(XDG_CONFIG_HOME, xdg)
}
if !strings.HasSuffix(
s,
rootedPath(defaultConfigSubdir, ProgramName)) {
t.Fatalf("unexpected config dir: %s", s)
}
}
func rootedPath(elem ...string) string {
return string(filepath.Separator) + filepath.Join(elem...)
}
func TestConfigDirWithXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(XDG_CONFIG_HOME)
os.Setenv(XDG_CONFIG_HOME, rootedPath("blah"))
s := ConfigRoot()
if isSet {
os.Setenv(XDG_CONFIG_HOME, xdg)
}
if s != rootedPath("blah", ProgramName) {
t.Fatalf("unexpected config dir: %s", s)
}
}

View File

@@ -19,10 +19,26 @@ const (
KustomizationFileName0 = "kustomization.yaml"
KustomizationFileName1 = "kustomization.yml"
KustomizationFileName2 = "Kustomization"
// An environment variable to consult for kustomization
// configuration data. See:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
XdgConfigHome = "XDG_CONFIG_HOME"
// Use this when XdgConfigHome not defined.
DefaultConfigSubdir = ".config"
// Program name, for help, finding the XDG_CONFIG_DIR, etc.
ProgramName = "kustomize"
// Domain from which kustomize code is imported, for locating
// plugin source code under $GOPATH.
// TODO: move to pgk/plugin/config.go or equivalent
// as part of v4 release. Cannot move till then
// because of pluginator dependence at v3.
DomainName = "sigs.k8s.io"
// Name of directory housing all plugins.
// TODO: move to pgk/plugin/config.go or equivalent
PluginRoot = "plugin"
)

View File

@@ -63,7 +63,7 @@ func DefaultSrcRoot() (string, error) {
nope = append(nope, root)
root = filepath.Join(
pgmconfig.HomeDir(),
homeDir(),
pgmconfig.ProgramName, pgmconfig.PluginRoot)
if FileExists(root) {
return root, nil

View File

@@ -5,18 +5,27 @@ package plugins
import (
"fmt"
"github.com/spf13/pflag"
"os"
"path/filepath"
"runtime"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
"sigs.k8s.io/kustomize/v3/pkg/types"
)
const (
PluginSymbol = "KustomizePlugin"
BuiltinPluginPackage = "builtin"
// Used with Go plugins.
PluginSymbol = "KustomizePlugin"
// Location of builtins.
BuiltinPluginPackage = "builtin"
// ApiVersion of builtins.
BuiltinPluginApiVersion = BuiltinPluginPackage
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/README.md
`
flagErrorFmt = `
@@ -36,11 +45,11 @@ func DefaultPluginConfig() *types.PluginConfig {
return &types.PluginConfig{
Enabled: false,
DirectoryPath: filepath.Join(
pgmconfig.ConfigRoot(), pgmconfig.PluginRoot),
configRoot(), pgmconfig.PluginRoot),
}
}
func NotEnabledErr(name string) error {
func notEnabledErr(name string) error {
return fmt.Errorf(
flagErrorFmt,
name,
@@ -53,3 +62,28 @@ func AddFlagEnablePlugins(set *pflag.FlagSet, v *bool) {
v, flagEnablePluginsName,
false, flagEnablePluginsHelp)
}
// Use https://github.com/kirsle/configdir instead?
func configRoot() string {
dir := os.Getenv(pgmconfig.XdgConfigHome)
if len(dir) == 0 {
dir = filepath.Join(
homeDir(), pgmconfig.DefaultConfigSubdir)
}
return filepath.Join(dir, pgmconfig.ProgramName)
}
func homeDir() string {
home := os.Getenv(homeEnv())
if len(home) > 0 {
return home
}
return "~"
}
func homeEnv() string {
if runtime.GOOS == "windows" {
return "USERPROFILE"
}
return "HOME"
}

View File

@@ -0,0 +1,45 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package plugins
import (
"os"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
)
func TestConfigDirNoXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(pgmconfig.XdgConfigHome)
if isSet {
os.Unsetenv(pgmconfig.XdgConfigHome)
}
s := configRoot()
if isSet {
os.Setenv(pgmconfig.XdgConfigHome, xdg)
}
if !strings.HasSuffix(
s,
rootedPath(pgmconfig.DefaultConfigSubdir, pgmconfig.ProgramName)) {
t.Fatalf("unexpected config dir: %s", s)
}
}
func rootedPath(elem ...string) string {
return string(filepath.Separator) + filepath.Join(elem...)
}
func TestConfigDirWithXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(pgmconfig.XdgConfigHome)
os.Setenv(pgmconfig.XdgConfigHome, rootedPath("blah"))
s := configRoot()
if isSet {
os.Setenv(pgmconfig.XdgConfigHome, xdg)
}
if s != rootedPath("blah", pgmconfig.ProgramName) {
t.Fatalf("unexpected config dir: %s", s)
}
}

View File

@@ -114,7 +114,7 @@ func (l *Loader) loadAndConfigurePlugin(
} else if l.pc.Enabled {
c, err = l.loadPlugin(res.OrgId())
} else {
err = NotEnabledErr(res.OrgId().Kind)
err = notEnabledErr(res.OrgId().Kind)
}
if err != nil {
return nil, err

View File

@@ -9,7 +9,7 @@ import (
"sigs.k8s.io/kustomize/v3/internal/loadertest"
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
. "sigs.k8s.io/kustomize/v3/pkg/plugins"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource"
)
@@ -42,7 +42,7 @@ port: "12345"
)
func TestLoader(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(

View File

@@ -1,7 +1,7 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package test
package testenv
import (
"io/ioutil"
@@ -98,14 +98,14 @@ func (x *EnvForTest) removeWorkDir() {
}
func (x *EnvForTest) setEnv() {
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME)
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.workDir)
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XdgConfigHome)
os.Setenv(pgmconfig.XdgConfigHome, x.workDir)
}
func (x *EnvForTest) resetEnv() {
if x.wasSet {
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg)
os.Setenv(pgmconfig.XdgConfigHome, x.oldXdg)
} else {
os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
os.Unsetenv(pgmconfig.XdgConfigHome)
}
}

View File

@@ -11,7 +11,7 @@ import (
"testing"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
)
// This is an example of using a helm chart as a base,
@@ -28,7 +28,7 @@ import (
// TODO: Download and inflate the chart, and check that
// in for the test.
func TestChartInflatorPlugin(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin(

View File

@@ -6,15 +6,15 @@ package target_test
import (
"testing"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
)
// Demo custom configuration of a builtin transformation.
// This is a NamePrefixer that only touches Deployments
// and Services.
func TestCustomNamePrefixer(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
@@ -103,7 +103,7 @@ metadata:
// Demo custom configuration as a base.
func TestReusableCustomNamePrefixer(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(

View File

@@ -9,8 +9,8 @@ import (
"strings"
"testing"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
)
const patchAddProbe = `
@@ -340,7 +340,7 @@ patchesStrategicMerge:
}
func TestIssue1251_Plugins_ProdVsDev(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
@@ -380,7 +380,7 @@ transformers:
}
func TestIssue1251_Plugins_Local(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
@@ -430,7 +430,7 @@ jsonOp: '%s'
// Remote in the sense that they are bundled in a different kustomization.
func TestIssue1251_Plugins_Bundled(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(

View File

@@ -4,8 +4,9 @@
package target_test
import (
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
"testing"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
)
// Here's a structure of two kustomizations,

View File

@@ -20,7 +20,7 @@ import (
"strings"
"testing"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
)
func makeCommonFileForMultiplePatchTest(th *kusttest_test.KustTestHarness) {

View File

@@ -12,10 +12,10 @@ import (
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/v3/k8sdeps/transformer"
"sigs.k8s.io/kustomize/v3/pkg/fs"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/loader"
"sigs.k8s.io/kustomize/v3/pkg/plugins"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/target"
@@ -23,7 +23,7 @@ import (
)
func TestPluginDir(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin(

View File

@@ -7,8 +7,8 @@ import (
"strings"
"testing"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
)
func writeDeployment(th *kusttest_test.KustTestHarness, path string) {
@@ -50,7 +50,7 @@ metadata:
}
func TestOrderedTransformers(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
@@ -96,7 +96,7 @@ spec:
}
func TestPluginsNotEnabled(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
@@ -119,7 +119,7 @@ transformers:
}
func TestSedTransformer(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildExecPlugin(
@@ -187,7 +187,7 @@ metadata:
}
func TestTransformedTransformers(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(