Skip to content

Commit

Permalink
feat: support config for embedded and relative files
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed AbouZaid <[email protected]>
  • Loading branch information
aabouzaid committed Apr 6, 2024
1 parent fcb3b06 commit adc077c
Show file tree
Hide file tree
Showing 13 changed files with 190 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
17 changes: 17 additions & 0 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ func ClusterPrep(ctx context.Context, runtime k3drt.Runtime, clusterConfig *conf
})
}

/*
* Step 4: Files
*/

for _, ccfile := range clusterConfig.ClusterCreateOpts.Files {
clusterConfig.ClusterCreateOpts.NodeHooks = append(clusterConfig.ClusterCreateOpts.NodeHooks, k3d.NodeHook{
Stage: k3d.LifecycleStagePreStart,
Action: actions.WriteFileAction{
Runtime: runtime,
Content: ccfile.Content,
Dest: ccfile.Destination,
Mode: 0644,
Description: ccfile.Description,
},
})
}

return nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ func TestReadSimpleConfig(t *testing.T) {
NodeFilters: []string{"all"},
},
},
Files: []conf.SimpleConfigFile{
{
Content: "apiVersion: v1\n" +
"kind: Namespace\n" +
"metadata:\n" +
" name: foo\n",
Destination: "/var/lib/rancher/k3s/server/manifests/foo.yaml",
},
{
Content: "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",
},
{
Path: "baz-ns.yaml",
Destination: "k3s-manifests-custom/baz.yaml",
Description: "Source: Relative path to k3d config file, Destination: Magic shortcut path, 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
18 changes: 18 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,24 @@ env:
- envVar: bar=baz
nodeFilters:
- all
files:
- content: |
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"
- content: |
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'
- path: 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'

options:
k3d:
Expand Down
14 changes: 13 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,18 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim
clusterCreateOpts.Registries.Config = k3sRegistry
}

/*
* Files
*/
for _, simpleConfigFile := range simpleConfig.Files {
clusterFile := k3d.File{
Content: util.ReadFileSource(configFileName, simpleConfigFile.Path, simpleConfigFile.Content),
Destination: util.ResolveFileDestination(simpleConfigFile.Destination),
Description: simpleConfigFile.Description,
}
clusterCreateOpts.Files = append(clusterCreateOpts.Files, clusterFile)
}

/**********************
* 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
30 changes: 30 additions & 0 deletions pkg/config/v1alpha5/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@
"additionalProperties": false
}
},
"files": {
"type": "array",
"items": {
"type": "object",
"required": ["destination"],
"properties": {
"path": {
"type": "string"
},
"content": {
"type": "string"
},
"destination": {
"type": "string"
}
},
"oneOf": [
{
"required": [
"path"
]
},
{
"required": [
"content"
]
}
]
}
},
"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 @@ -145,6 +145,13 @@ type SimpleConfigRegistries struct {
Config string `mapstructure:"config" json:"config,omitempty"` // registries.yaml (k3s config for containerd registry override)
}

type SimpleConfigFile struct {
Path string `mapstructure:"path" json:"path,omitempty"`
Content string `mapstructure:"content" json:"content,omitempty"`
Destination string `mapstructure:"destination" json:"destination,omitempty"`
Description string `mapstructure:"description" json:"description,omitempty"`
}

// SimpleConfig describes the toplevel k3d configuration file.
type SimpleConfig struct {
config.TypeMeta `mapstructure:",squash"`
Expand All @@ -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 []SimpleConfigFile `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 @@ -127,6 +127,7 @@ type ClusterCreateOpts struct {
Use []*Registry `json:"use,omitempty"`
Config *wharfie.Registry `json:"config,omitempty"` // registries.yaml (k3s config for containerd registry override)
} `json:"registries,omitempty"`
Files []File `json:"files,omitempty"`
}

// NodeHook is an action that is bound to a specifc stage of a node lifecycle
Expand Down
45 changes: 45 additions & 0 deletions pkg/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ 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"
)

// 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 +64,44 @@ func createDirIfNotExists(path string) error {
}
return nil
}

// ReadFileSource reads the file source which is either relative to the k3d config file or embedded in it.
func ReadFileSource(configFile, sourcePath, sourceContent string) []byte {
if sourcePath != "" {
sourceFilePath := filepath.Join(filepath.Dir(configFile), sourcePath)
fileInfo, err := os.Stat(sourceFilePath)
if err == nil && !fileInfo.IsDir() {
content, err := os.ReadFile(sourceFilePath)
if err != nil {
l.Log().Warnf("Cannot read file: %s", sourceFilePath)
return nil
}
return content
}
l.Log().Warnf("Could resolve source file path: %s", sourceFilePath)
return nil
}

return []byte(sourceContent)
}

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

// Check if the destination 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)
}

l.Log().Warnf("Destination can be only absolute path or starts with predefined shortcut path. Could resolve destination file path: %s", destPath)
return ""
}

0 comments on commit adc077c

Please sign in to comment.