Skip to content

Commit

Permalink
feat: support config embedded and external files (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid authored Apr 16, 2024
1 parent e9babb7 commit 9eaa6c4
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func NewCmdClusterCreate() *cobra.Command {
l.Log().Fatalf("error processing/sanitizing simple config: %v", err)
}

clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg)
clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg, configFile)
if err != nil {
l.Log().Fatalln(err)
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *conf
})
}

/*
* Step 4: Files
*/

for id, node := range clusterConfig.Nodes {
for _, nodefile := range node.Files {
clusterConfig.Nodes[id].HookActions = append(clusterConfig.Nodes[id].HookActions, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: nodefile.Content,
Dest: nodefile.Destination,
Mode: 0644,
Description: nodefile.Description,
},
})
}
}

return nil
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ func TestReadSimpleConfig(t *testing.T) {
NodeFilters: []string{"all"},
},
},
Files: []conf.FileWithNodeFilters{
{
Source: "apiVersion: v1\n" +
"kind: Namespace\n" +
"metadata:\n" +
" name: foo\n",
Destination: "/var/lib/rancher/k3s/server/manifests/foo.yaml",
},
{
Source: "apiVersion: v1\n" +
"kind: Namespace\n" +
"metadata:\n" +
" name: bar\n",
Destination: "k3s-manifests/bar.yaml",
Description: "Source: Embedded content in k3d config file, Destination: Magic shortcut path, Description: Defined",
},
{
Source: "baz-ns.yaml",
Destination: "k3s-manifests-custom/baz.yaml",
Description: "Source: Relative path to k3d config file, Destination: Magic shortcut path, Description: Defined",
},
{
Source: "baz-ns.yaml",
Destination: "k3s-manifests-custom/baz-server.yaml",
NodeFilters: []string{"server:*"},
Description: "Source: Relative path to k3d config file, Destination: Magic shortcut path, Node: Defined, Description: Defined",
},
},
Options: conf.SimpleConfigOptions{
K3dOptions: conf.SimpleConfigOptionsK3d{
Wait: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestProcessClusterConfig(t *testing.T) {

t.Logf("\n========== Read Config and transform to cluster ==========\n%+v\n=================================\n", cfg)

clusterCfg, err := TransformSimpleToClusterConfig(context.Background(), runtimes.Docker, cfg.(conf.SimpleConfig))
clusterCfg, err := TransformSimpleToClusterConfig(context.Background(), runtimes.Docker, cfg.(conf.SimpleConfig), cfgFile)
if err != nil {
t.Error(err)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/test_assets/baz-ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: baz
23 changes: 23 additions & 0 deletions pkg/config/test_assets/config_test_simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ env:
- envVar: bar=baz
nodeFilters:
- all
files:
- source: |
apiVersion: v1
kind: Namespace
metadata:
name: foo
destination: /var/lib/rancher/k3s/server/manifests/foo.yaml
# destination: "Source: Embedded content in k3d config file, Destination: Absolute path, Description: Not defined"
- source: |
apiVersion: v1
kind: Namespace
metadata:
name: bar
destination: k3s-manifests/bar.yaml # Resolved to /var/lib/rancher/k3s/server/manifests/bar.yaml
description: 'Source: Embedded content in k3d config file, Destination: Magic shortcut path, Description: Defined'
- source: baz-ns.yaml
destination: k3s-manifests-custom/baz.yaml # Resolved to /var/lib/rancher/k3s/server/manifests/custom/bar.yaml
description: 'Source: Relative path to k3d config file, Destination: Magic shortcut path, Description: Defined'
- source: baz-ns.yaml
destination: k3s-manifests-custom/baz-server.yaml # Resolved to /var/lib/rancher/k3s/server/manifests/custom/bar-server.yaml
nodeFilters:
- "server:*"
description: 'Source: Relative path to k3d config file, Destination: Magic shortcut path, Node: Defined, Description: Defined'

options:
k3d:
Expand Down
31 changes: 30 additions & 1 deletion pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
)

// TransformSimpleToClusterConfig transforms a simple configuration to a full-fledged cluster configuration
func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtime, simpleConfig conf.SimpleConfig) (*conf.ClusterConfig, error) {
func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtime, simpleConfig conf.SimpleConfig, configFileName string) (*conf.ClusterConfig, error) {
// set default cluster name
if simpleConfig.Name == "" {
simpleConfig.Name = k3d.DefaultClusterName
Expand Down Expand Up @@ -383,6 +383,35 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
clusterCreateOpts.Registries.Config = k3sRegistry
}

/*
* Files
*/

for _, fileWithNodeFilters := range simpleConfig.Files {
nodes, err := util.FilterNodes(nodeList, fileWithNodeFilters.NodeFilters)
if err != nil {
return nil, fmt.Errorf("failed to filter nodes for file copying '%s': %w", fileWithNodeFilters, err)
}

content, err := util.ReadFileSource(configFileName, fileWithNodeFilters.Source)
if err != nil {
return nil, fmt.Errorf("failed to read source content: %w", err)
}

destination, err := util.ResolveFileDestination(fileWithNodeFilters.Destination)
if err != nil {
return nil, fmt.Errorf("destination path is not correct: %w", err)
}

for _, node := range nodes {
node.Files = append(node.Files, k3d.File{
Content: content,
Destination: destination,
Description: fileWithNodeFilters.Description,
})
}
}

/**********************
* Kubeconfig Options *
**********************/
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestTransformSimpleConfigToClusterConfig(t *testing.T) {

t.Logf("\n========== Read Config ==========\n%+v\n=================================\n", cfg)

clusterCfg, err := TransformSimpleToClusterConfig(context.Background(), runtimes.Docker, cfg.(conf.SimpleConfig))
clusterCfg, err := TransformSimpleToClusterConfig(context.Background(), runtimes.Docker, cfg.(conf.SimpleConfig), cfgFile)
if err != nil {
t.Error(err)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/config/v1alpha5/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@
"additionalProperties": false
}
},
"files": {
"type": "array",
"items": {
"type": "object",
"required": ["source", "destination"],
"properties": {
"source": {
"type": "string"
},
"destination": {
"type": "string"
},
"description": {
"type": "string"
},
"nodeFilters": {
"$ref": "#/definitions/nodeFilters"
}
},
"additionalProperties": false
}
},
"options": {
"type": "object",
"properties": {
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/v1alpha5/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ type K3sArgWithNodeFilters struct {
NodeFilters []string `mapstructure:"nodeFilters" json:"nodeFilters,omitempty"`
}

type FileWithNodeFilters struct {
Source string `mapstructure:"source" json:"source,omitempty"`
Destination string `mapstructure:"destination" json:"destination,omitempty"`
Description string `mapstructure:"description" json:"description,omitempty"`
NodeFilters []string `mapstructure:"nodeFilters" json:"nodeFilters,omitempty"`
}

type SimpleConfigRegistryCreateConfig struct {
Name string `mapstructure:"name" json:"name,omitempty"`
Host string `mapstructure:"host" json:"host,omitempty"`
Expand Down Expand Up @@ -162,6 +169,7 @@ type SimpleConfig struct {
Env []EnvVarWithNodeFilters `mapstructure:"env" json:"env,omitempty"`
Registries SimpleConfigRegistries `mapstructure:"registries" json:"registries,omitempty"`
HostAliases []k3d.HostAlias `mapstructure:"hostAliases" json:"hostAliases,omitempty"`
Files []FileWithNodeFilters `mapstructure:"files" json:"files,omitempty"`
}

// SimpleExposureOpts provides a simplified syntax compared to the original k3d.ExposureOpts
Expand Down
28 changes: 28 additions & 0 deletions pkg/types/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright © 2020-2024 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package types

type File struct {
Content []byte `mapstructure:"content" json:"content,omitempty"`
Destination string `mapstructure:"destination" json:"destination,omitempty"`
Description string `mapstructure:"description" json:"description,omitempty"`
}
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ type Node struct {
Env []string `json:"env,omitempty"`
Cmd []string // filled automatically based on role
Args []string `json:"extraArgs,omitempty"`
Files []File `json:"files,omitempty"`
Ports nat.PortMap `json:"portMappings,omitempty"`
Restart bool `json:"restart,omitempty"`
Created string `json:"created,omitempty"`
Expand Down
51 changes: 51 additions & 0 deletions pkg/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

homedir "github.com/mitchellh/go-homedir"

l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
yaml "gopkg.in/yaml.v3"
)

// GetConfigDirOrCreate will return the base path of the k3d config directory or create it if it doesn't exist yet
Expand Down Expand Up @@ -60,3 +65,49 @@ func createDirIfNotExists(path string) error {
}
return nil
}

// ReadFileSource reads the file source which is either embedded in the k3d config file or relative to it.
func ReadFileSource(configFile, source string) ([]byte, error) {
sourceContent := &yaml.Node{}
sourceContent.SetString(source)

// If the source input is embedded in the config file, use it as it is.
if sourceContent.Style == yaml.LiteralStyle || sourceContent.Style == yaml.FoldedStyle {
l.Log().Debugf("read source from embedded file with content '%s'", sourceContent.Value)
return []byte(sourceContent.Value), nil
}

// If the source input is referenced as an external file, read its content.
sourceFilePath := filepath.Join(filepath.Dir(configFile), sourceContent.Value)
fileInfo, err := os.Stat(sourceFilePath)
if err == nil && !fileInfo.IsDir() {
fileContent, err := os.ReadFile(sourceFilePath)
if err != nil {
return nil, fmt.Errorf("cannot read file: %s", sourceFilePath)
}
l.Log().Debugf("read source from external file '%s'", sourceFilePath)
return fileContent, nil
}

return nil, fmt.Errorf("could resolve source file path: %s", sourceFilePath)
}

// ResolveFileDestination determines the file destination and resolves it if it has a magic shortcut.
func ResolveFileDestination(destPath string) (string, error) {
// If the destination path is absolute, then use it as it is.
if filepath.IsAbs(destPath) {
l.Log().Debugf("resolved destination with absolute path '%s'", destPath)
return destPath, nil
}

// If the destination path has a magic shortcut, then resolve it and use it in the path.
destPathTree := strings.Split(destPath, string(os.PathSeparator))
if shortcutPath, found := k3s.K3sPathShortcuts[destPathTree[0]]; found {
destPathTree[0] = shortcutPath
destPathResolved := filepath.Join(destPathTree...)
l.Log().Debugf("resolved destination with magic shortcut path: '%s'", destPathResolved)
return filepath.Join(destPathResolved), nil
}

return "", fmt.Errorf("destination can be only absolute path or starts with predefined shortcut path. Could not resolve destination file path: %s", destPath)
}

0 comments on commit 9eaa6c4

Please sign in to comment.