Skip to content

Commit

Permalink
feat(cluster): support customize config file for K3s server and agent
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao committed Jul 17, 2024
1 parent bcde548 commit 3dbc190
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/cluster/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ func (p *ProviderBase) GetClusterOptions() []types.Flag {
V: p.InstallEnv,
Usage: "The install environment variables config for K3s with install script(only support env starts with INSTALL_), e.g. --install-env INSTALL_K3S_SKIP_SELINUX_RPM=true, see: https://docs.k3s.io/installation/configuration#configuration-with-install-script",
},
{
Name: "server-config-file",
P: &p.ServerConfigFile,
V: p.ServerConfigFile,
Usage: "Config K3s server with configuration file which can do more complex configuration than environment variables and CLI arguments, see: https://docs.k3s.io/installation/configuration#configuration-file",
},
{
Name: "agent-config-file",
P: &p.AgentConfigFile,
V: p.AgentConfigFile,
Usage: "Config K3s agent with configuration file which can do more complex configuration than environment variables and CLI arguments, see: https://docs.k3s.io/installation/configuration#configuration-file",
},
}

fs = append(fs, p.GetSSHOptions()...)
Expand Down
47 changes: 47 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ func (p *ProviderBase) initNode(isFirstMaster bool, fixedIP string, cluster *typ
}
}

// handle configuration file
if err := p.handleConfiguration(&node, cluster); err != nil {
return err
}

cmd := getCommand(isFirstMaster, fixedIP, cluster, node, []string{extraArgs})
nodeRole := "master"
if !node.Master {
Expand Down Expand Up @@ -573,6 +578,48 @@ func (p *ProviderBase) handleRegistry(n *types.Node, c *types.Cluster) (err erro
return err
}

func (p *ProviderBase) handleConfiguration(n *types.Node, c *types.Cluster) (err error) {
if c.ServerConfigFileContent == "" && c.ServerConfigFile == "" && c.AgentConfigFileContent == "" && c.AgentConfigFile == "" {
return nil
}
var configFileContent []byte
if n.Master {
if c.ServerConfigFileContent != "" {
configFileContent, err = base64.StdEncoding.DecodeString(c.ServerConfigFileContent)
if err != nil {
return err
}
}
if c.ServerConfigFile != "" && c.ServerConfigFileContent == "" {
configFileContent, err = ioutil.ReadFile(c.ServerConfigFile)
if err != nil {
return err
}
}
} else {
if c.AgentConfigFileContent != "" {
configFileContent, err = base64.StdEncoding.DecodeString(c.AgentConfigFileContent)
if err != nil {
return err
}
}
if c.AgentConfigFile != "" && c.AgentConfigFileContent == "" {
configFileContent, err = ioutil.ReadFile(c.AgentConfigFile)
if err != nil {
return err
}
}
}
if len(configFileContent) == 0 {
return errors.New("configuration file is empty")
}
cmd := []string{"mkdir -p /etc/rancher/k3s"}
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"/etc/rancher/k3s/config.yaml\"",
base64.StdEncoding.EncodeToString(configFileContent)))
_, err = p.execute(n, cmd...)
return err
}

func registryTLSMap(registry *registries.Registry) (m map[string]map[string][]byte, err error) {
m = make(map[string]map[string][]byte)
if registry == nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/server/store/provider/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func toProviderObject(provider providers.Provider, schema *types.APISchema, id s
Required: false,
Default: "",
}
config["server-config-file-content"] = schemas.Field{
Type: "string",
Description: "Config K3s server with configuration file which can do more complex configuration than environment variables and CLI arguments, see: https://docs.k3s.io/installation/configuration#configuration-file",
Required: false,
Default: "",
}
config["agent-config-file-content"] = schemas.Field{
Type: "string",
Description: "Config K3s agent with configuration file which can do more complex configuration than environment variables and CLI arguments, see: https://docs.k3s.io/installation/configuration#configuration-file",
Required: false,
Default: "",
}
obj := types.APIObject{
Type: schema.ID,
ID: id,
Expand Down
4 changes: 4 additions & 0 deletions pkg/types/autok3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Metadata struct {
Rollback bool `json:"rollback" yaml:"rollback" gorm:"type:bool"`
Values StringMap `json:"values,omitempty" yaml:"values,omitempty" gorm:"type:stringMap"`
InstallEnv StringMap `json:"install-env,omitempty" yaml:"install-env,omitempty" gorm:"type:stringMap"`
ServerConfigFileContent string `json:"server-config-file-content,omitempty" yaml:"server-config-file-content,omitempty"`
ServerConfigFile string `json:"server-config-file,omitempty" yaml:"server-config-file,omitempty"`
AgentConfigFileContent string `json:"agent-config-file-content,omitempty" yaml:"agent-config-file-content,omitempty"`
AgentConfigFile string `json:"agent-config-file,omitempty" yaml:"agent-config-file,omitempty"`
}

// Status struct for status.
Expand Down

0 comments on commit 3dbc190

Please sign in to comment.