Skip to content

Commit

Permalink
Use dynamic system agent applied dir
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti committed Jun 19, 2024
1 parent 2c3f177 commit b09a3e1
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions cmd/support/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"sigs.k8s.io/yaml" // See: https://github.com/rancher/system-agent/blob/main/pkg/config/config.go

systemagent "github.com/rancher/elemental-operator/internal/system-agent"
"github.com/rancher/elemental-operator/pkg/log"
"github.com/rancher/elemental-operator/pkg/version"
)

const (
k3sKubeConfig = "/etc/rancher/k3s/k3s.yaml"
k3sKubectl = "/usr/local/bin/kubectl"
rkeKubeConfig = "/etc/rancher/rke2/rke2.yaml"
rkeKubectl = "/var/lib/rancher/rke2/bin/kubectl"
elementalAgentPlanDir = "/var/lib/elemental/agent/applied/"
rancherAgentPlanDir = "/var/lib/rancher/agent/applied/"
rancherAgentConf = "/etc/rancher/agent/config.yaml"
elementalAgentConf = "/etc/rancher/agent/config.yaml"
osRelease = "/etc/os-release"
hostnameFile = "/etc/hostname"
resolvConf = "/etc/resolv.conf"
oemDir = "/oem/"
systemOEMDir = "/system/oem"
k3sKubeConfig = "/etc/rancher/k3s/k3s.yaml"
k3sKubectl = "/usr/local/bin/kubectl"
rkeKubeConfig = "/etc/rancher/rke2/rke2.yaml"
rkeKubectl = "/var/lib/rancher/rke2/bin/kubectl"
elementalAgentPlanDir = "/var/lib/elemental/agent/applied/"
rancherAgentPlanDirDefault = "/var/lib/rancher/agent/applied/"
rancherAgentConf = "/etc/rancher/agent/config.yaml"
elementalAgentConf = "/etc/rancher/agent/config.yaml"
osRelease = "/etc/os-release"
hostnameFile = "/etc/hostname"
resolvConf = "/etc/resolv.conf"
oemDir = "/oem/"
systemOEMDir = "/system/oem"
)

func getServices() []string {
Expand Down Expand Up @@ -143,6 +145,8 @@ func run() (err error) {
log.Infof("Copying %s", rancherAgentConf)
copyFileWithAltName(rancherAgentConf, tempDir, "rancher-agent-config.yaml")

rancherAgentPlanDir := getSystemAgentAppliedDir(rancherAgentConf)

// TODO: Flag to skip certain files? They could have passwords set in them so maybe we need to search and replace
// any sensitive fields?
for _, f := range []string{elementalAgentPlanDir, rancherAgentPlanDir, systemOEMDir, oemDir} {
Expand Down Expand Up @@ -556,3 +560,23 @@ func getKubectl() (string, error) {

return "", errors.New("Cant find kubectl")
}

func getSystemAgentAppliedDir(systemAgentConfigPath string) string {
if existsNoWarn(systemAgentConfigPath) {
log.Infof("Found Rancher System Agent config: %s", systemAgentConfigPath)
configBytes, err := os.ReadFile(systemAgentConfigPath)
if err != nil {
log.Errorf("Could not read file '%s': %s", systemAgentConfigPath, err.Error())
return rancherAgentPlanDirDefault
}
agentConfig := systemagent.AgentConfig{}
err = yaml.Unmarshal(configBytes, &agentConfig)
if err != nil {
log.Errorf("Could not unmarshal file '%s': %s", systemAgentConfigPath, err.Error())
return rancherAgentPlanDirDefault
}
log.Infof("Determined Rancher System Agent Applied Dir to be: %s", agentConfig.AppliedPlanDir)
return agentConfig.AppliedPlanDir
}
return rancherAgentPlanDirDefault
}

0 comments on commit b09a3e1

Please sign in to comment.