Skip to content

Commit

Permalink
support default HostIp
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Oct 21, 2022
1 parent 826be97 commit 797cc3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
23 changes: 18 additions & 5 deletions pkg/infradriver/ssh_infradriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils/shellcommand"
"github.com/sealerio/sealer/utils/ssh"

"golang.org/x/sync/errgroup"
)

Expand All @@ -34,7 +35,7 @@ type SSHInfraDriver struct {
hosts []net.IP
roleHostsMap map[string][]net.IP
hostEnvMap map[string]map[string]interface{}
clusterRenderData map[string]interface{}
clusterEnv map[string]interface{}
clusterName string
clusterImageName string
clusterLaunchCmds []string
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewInfraDriver(cluster *v2.Cluster) (InfraDriver, error) {
roleHostsMap: map[string][]net.IP{},
// todo need to separate env into app render data and sys render data
hostEnvMap: map[string]map[string]interface{}{},
clusterRenderData: ConvertEnv(cluster.Spec.Env),
clusterEnv: ConvertEnv(cluster.Spec.Env),
clusterHostAliases: cluster.Spec.HostAliases,
}

Expand Down Expand Up @@ -125,7 +126,7 @@ func NewInfraDriver(cluster *v2.Cluster) (InfraDriver, error) {
// merge the host ENV and global env, the host env will overwrite cluster.Spec.Env
for _, host := range cluster.Spec.Hosts {
for _, ip := range host.IPS {
ret.hostEnvMap[ip.String()] = mergeList(ConvertEnv(host.Env), ret.clusterRenderData)
ret.hostEnvMap[ip.String()] = mergeList(ConvertEnv(host.Env), ret.clusterEnv)
}
}

Expand All @@ -141,11 +142,23 @@ func (d *SSHInfraDriver) GetHostIPListByRole(role string) []net.IP {
}

func (d *SSHInfraDriver) GetHostEnv(host net.IP) map[string]interface{} {
return d.hostEnvMap[host.String()]
hostEnv := d.hostEnvMap[host.String()]
if hostEnv["HostIP"] == nil {
hostEnv["HostIP"] = host.String()
}

return hostEnv
}

func (d *SSHInfraDriver) GetClusterEnv() map[string]interface{} {
return d.clusterRenderData
if d.clusterEnv["RegistryDomain"] == nil {
d.clusterEnv["RegistryDomain"] = "sea.hub"
}
if d.clusterEnv["RegistryPort"] == nil {
d.clusterEnv["RegistryPort"] = "5000"
}

return d.clusterEnv
}

func (d *SSHInfraDriver) Copy(host net.IP, localFilePath, remoteFilePath string) error {
Expand Down
7 changes: 5 additions & 2 deletions pkg/infradriver/ssh_infradriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,20 @@ func TestSSHInfraDriver_GetClusterInfo(t *testing.T) {
})

assert.Equal(t, driver.GetClusterEnv(), map[string]interface{}{
"key1": "value1",
"key2": []string{"value2", "value3"},
"HostIP": []string{"192.168.0.2", "192.168.0.3"},
"key1": "value1",
"key2": []string{"value2", "value3"},
})

assert.Equal(t, driver.GetHostEnv(net.IPv4(192, 168, 0, 2)), map[string]interface{}{
"HostIP": []string{"192.168.0.2", "192.168.0.3"},
"key1": "value1",
"key2": []string{"value2", "value3"},
"etcd-dir": "/data/etcd",
})

assert.Equal(t, driver.GetHostEnv(net.IPv4(192, 168, 0, 3)), map[string]interface{}{
"HostIP": []string{"192.168.0.2", "192.168.0.3"},
"key1": "value1",
"key2": []string{"value2", "value3"},
"test_node_env_key": "test_node_env_value",
Expand Down

0 comments on commit 797cc3e

Please sign in to comment.