mirror of https://github.com/fluxcd/cli-utils.git
implement RawConfigFileProvider
This commit is contained in:
parent
26b23e9eec
commit
c98b06a9ff
|
|
@ -18,21 +18,20 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sigs.k8s.io/kustomize/pkg/inventory"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
|
||||||
"sigs.k8s.io/yaml"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"sigs.k8s.io/cli-experimental/internal/pkg/clik8s"
|
"sigs.k8s.io/cli-experimental/internal/pkg/clik8s"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
|
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
|
||||||
|
"sigs.k8s.io/kustomize/pkg/inventory"
|
||||||
"sigs.k8s.io/kustomize/pkg/loader"
|
"sigs.k8s.io/kustomize/pkg/loader"
|
||||||
"sigs.k8s.io/kustomize/pkg/plugins"
|
"sigs.k8s.io/kustomize/pkg/plugins"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/target"
|
"sigs.k8s.io/kustomize/pkg/target"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigProvider provides runtime.Objects for a path
|
// ConfigProvider provides runtime.Objects for a path
|
||||||
|
|
@ -135,29 +134,51 @@ func (p *RawConfigFileProvider) IsSupported(path string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfig returns the resource configs
|
// GetConfig returns the resource configs
|
||||||
func (p *RawConfigFileProvider) GetConfig(path string) ([]*unstructured.Unstructured, error) {
|
// from a directory or a file containing raw Kubernetes resource configurations
|
||||||
|
func (p *RawConfigFileProvider) GetConfig(root string) ([]*unstructured.Unstructured, error) {
|
||||||
var values clik8s.ResourceConfigs
|
var values clik8s.ResourceConfigs
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(path)
|
err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := ioutil.ReadFile(path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
objs := strings.Split(string(b), "---")
|
||||||
|
for _, o := range objs {
|
||||||
|
body := map[string]interface{}{}
|
||||||
|
|
||||||
|
if err := yaml.Unmarshal([]byte(o), &body); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
values = append(values, &unstructured.Unstructured{Object: body})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
objs := strings.Split(string(b), "---")
|
|
||||||
for _, o := range objs {
|
|
||||||
body := map[string]interface{}{}
|
|
||||||
|
|
||||||
if err := yaml.Unmarshal([]byte(o), &body); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
values = append(values, &unstructured.Unstructured{Object: body})
|
|
||||||
}
|
|
||||||
|
|
||||||
return values, nil
|
return values, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPruneConfig returns the resource configs
|
// GetPruneConfig returns the resource configs
|
||||||
|
// from a directory or a file containing raw Kubernetes resource configurations
|
||||||
|
// The resource used for prune is get by checking the presence of the inventory annotation
|
||||||
func (p *RawConfigFileProvider) GetPruneConfig(path string) (*unstructured.Unstructured, error) {
|
func (p *RawConfigFileProvider) GetPruneConfig(path string) (*unstructured.Unstructured, error) {
|
||||||
return nil, nil
|
resources, err := p.GetConfig(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return GetPruneResources(resources)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RawConfigHTTPProvider provides configs from HTTP urls
|
// RawConfigHTTPProvider provides configs from HTTP urls
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,115 @@ func TestKustomizeProvider2(t *testing.T) {
|
||||||
assert.Equal(t, len(inv.Current), 1)
|
assert.Equal(t, len(inv.Current), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
setupRawConfigFiles provides a directory with Kubernetes resources
|
||||||
|
that can be used as input to test RawConfigFileProvider.
|
||||||
|
The directory created is
|
||||||
|
|
||||||
|
f
|
||||||
|
├── service.yaml
|
||||||
|
├── subdir1
|
||||||
|
│ └── service.yaml
|
||||||
|
└── subdir2
|
||||||
|
└── service.yaml
|
||||||
|
|
||||||
|
*/
|
||||||
|
func setupRawConfigFiles(t *testing.T) (string, string) {
|
||||||
|
f, err := ioutil.TempDir("/tmp", "TestConfigProvider")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = ioutil.WriteFile(filepath.Join(f, "service.yaml"), []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-a
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: MyApp
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
port: 80
|
||||||
|
targetPort: 9376
|
||||||
|
`), 0644)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
subdir1, err := ioutil.TempDir(f, "subdir")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
subdir2, err := ioutil.TempDir(f, "subdir")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = ioutil.WriteFile(filepath.Join(subdir1, "service.yaml"), []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-b
|
||||||
|
annotations:
|
||||||
|
kustomize.config.k8s.io/Inventory: ""
|
||||||
|
kustomize.config.k8s.io/InventoryHash: 8mk644dhch
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: MyApp
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
port: 80
|
||||||
|
targetPort: 9376
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: cm
|
||||||
|
`), 0644)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = ioutil.WriteFile(filepath.Join(subdir2, "service.yaml"), []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: service-c
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: MyApp
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
port: 80
|
||||||
|
targetPort: 9376
|
||||||
|
`), 0644)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
return f, subdir1
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRawConfigFileProvider(t *testing.T) {
|
||||||
|
f, subdir := setupRawConfigFiles(t)
|
||||||
|
defer os.RemoveAll(f)
|
||||||
|
cp := wiretest.InitializeRawConfigProvider()
|
||||||
|
b := cp.IsSupported(f)
|
||||||
|
assert.Equal(t, b, true)
|
||||||
|
resources, err := cp.GetConfig(f)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, len(resources), 4)
|
||||||
|
resources, err = cp.GetConfig(filepath.Join(f, "service.yaml"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, len(resources), 1)
|
||||||
|
|
||||||
|
resource, err := cp.GetPruneConfig(f)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resource)
|
||||||
|
resource, err = cp.GetPruneConfig(filepath.Join(f, "service.yaml"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Nil(t, resource)
|
||||||
|
|
||||||
|
b = cp.IsSupported(subdir)
|
||||||
|
resources, err = cp.GetConfig(subdir)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, len(resources), 2)
|
||||||
|
resources, err = cp.GetConfig(filepath.Join(subdir, "service.yaml"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, len(resources), 2)
|
||||||
|
|
||||||
|
resource, err = cp.GetPruneConfig(subdir)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resource)
|
||||||
|
resource, err = cp.GetPruneConfig(filepath.Join(subdir, "service.yaml"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resource)
|
||||||
|
}
|
||||||
|
|
||||||
func setupKustomizeWithoutInventory(t *testing.T) string {
|
func setupKustomizeWithoutInventory(t *testing.T) string {
|
||||||
f, err := ioutil.TempDir("/tmp", "TestApply")
|
f, err := ioutil.TempDir("/tmp", "TestApply")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,12 @@ var ConfigProviderSet = wire.NewSet(
|
||||||
NewResourcePruneConfig,
|
NewResourcePruneConfig,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RawConfigProviderSet defines dependencies for initializing a RawConfigFileProvider
|
||||||
|
var RawConfigProviderSet = wire.NewSet(
|
||||||
|
wire.Struct(new(resourceconfig.RawConfigFileProvider), "*"),
|
||||||
|
wire.Bind(new(resourceconfig.ConfigProvider), new(*resourceconfig.RawConfigFileProvider)),
|
||||||
|
)
|
||||||
|
|
||||||
// NewPluginConfig returns a new PluginConfig
|
// NewPluginConfig returns a new PluginConfig
|
||||||
func NewPluginConfig() *types.PluginConfig {
|
func NewPluginConfig() *types.PluginConfig {
|
||||||
pc := plugin.DefaultPluginConfig()
|
pc := plugin.DefaultPluginConfig()
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,10 @@ func InitializConfigProvider() resourceconfig.ConfigProvider {
|
||||||
panic(wire.Build(wireconfig.ConfigProviderSet))
|
panic(wire.Build(wireconfig.ConfigProviderSet))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InitializeRawConfigProvider() resourceconfig.ConfigProvider {
|
||||||
|
panic(wire.Build(wireconfig.RawConfigProviderSet))
|
||||||
|
}
|
||||||
|
|
||||||
func InitializeKustomization() ([]string, func(), error) {
|
func InitializeKustomization() ([]string, func(), error) {
|
||||||
f1, err := ioutil.TempDir("/tmp", "TestApply")
|
f1, err := ioutil.TempDir("/tmp", "TestApply")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -248,6 +248,11 @@ func InitializConfigProvider() resourceconfig.ConfigProvider {
|
||||||
return kustomizeProvider
|
return kustomizeProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InitializeRawConfigProvider() resourceconfig.ConfigProvider {
|
||||||
|
rawConfigFileProvider := &resourceconfig.RawConfigFileProvider{}
|
||||||
|
return rawConfigFileProvider
|
||||||
|
}
|
||||||
|
|
||||||
// wire.go:
|
// wire.go:
|
||||||
|
|
||||||
func InitializeKustomization() ([]string, func(), error) {
|
func InitializeKustomization() ([]string, func(), error) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue