Skip to content

Commit

Permalink
support default HostIp
Browse files Browse the repository at this point in the history
reference and set constants;

add comments
  • Loading branch information
kakaZhou719 authored and Stevent-fei committed Oct 24, 2022
1 parent af43199 commit d1db1f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const (
RenderManifestsDir = "manifests"
APIVersion = "sealer.cloud/v2"
Kind = "Cluster"
DefaultRegistryDomain = "sea.hub"
DefaultRegistryPort = "5000"
HostIP = "HostIP"
)

// image module
Expand Down
24 changes: 19 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,24 @@ func (d *SSHInfraDriver) GetHostIPListByRole(role string) []net.IP {
}

func (d *SSHInfraDriver) GetHostEnv(host net.IP) map[string]interface{} {
return d.hostEnvMap[host.String()]
// Set env for each host
hostEnv := d.hostEnvMap[host.String()]
if _, ok := hostEnv[common.HostIP]; !ok {
hostEnv[common.HostIP] = host.String()
}
return hostEnv
}

func (d *SSHInfraDriver) GetClusterEnv() map[string]interface{} {
return d.clusterRenderData
// Set the default RegistryDomain and RegistryPort env,
// and override the env if the user specifies RegistryDomain and RegistryPort env
if _, ok := d.clusterEnv["RegistryDomain"]; !ok {
d.clusterEnv["RegistryDomain"] = common.DefaultRegistryDomain
}
if _, ok := d.clusterEnv["RegistryPort"]; !ok {
d.clusterEnv["RegistryPort"] = common.DefaultRegistryPort
}
return d.clusterEnv
}

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

assert.Equal(t, driver.GetClusterEnv(), map[string]interface{}{
"key1": "value1",
"key2": []string{"value2", "value3"},
"RegistryDomain": "sea.hub",
"RegistryPort": "5000",
"key1": "value1",
"key2": []string{"value2", "value3"},
})

assert.Equal(t, driver.GetHostEnv(net.IPv4(192, 168, 0, 2)), map[string]interface{}{
"HostIP": "192.168.0.2",
"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": "192.168.0.3",
"key1": "value1",
"key2": []string{"value2", "value3"},
"test_node_env_key": "test_node_env_value",
Expand Down

0 comments on commit d1db1f3

Please sign in to comment.