Skip to content

Commit

Permalink
operator: Added lookup of additional hostnames within the operator by…
Browse files Browse the repository at this point in the history
… status.HostIP environment variable

- Change format for passing hostname map to an actual map object
- Update playbook to parse hostnames from ansible hosts file "additional_hosts" var

Signed-off-by: Aaron Wilson <[email protected]>
  • Loading branch information
aaronnw committed Jan 25, 2024
1 parent de4b5ce commit c6d766b
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 40 deletions.
5 changes: 2 additions & 3 deletions operator/api/v1beta1/aistore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ type AIStoreSpec struct {
InitImage string `json:"initImage"` // init image for nodes
HostpathPrefix string `json:"hostpathPrefix"`
ConfigToUpdate *ConfigToUpdate `json:"configToUpdate,omitempty"`
// Map of primary hosts to additional hosts for multi-home
// Syntax: node_1_host=node_1_host,node_1_secondary node_2_host=node_2_host,node_2_secondary
// Map of primary host to comma-separated string of all hosts for multi-home
// +optional
HostnameMap *string `json:"hostnameMap,omitempty"`
HostnameMap map[string]string `json:"hostnameMap,omitempty"`
// Commma-separated list of names of additional network attachment definitions to attach to each pod
// +optional
NetAttachment *string `json:"networkAttachment,omitempty"`
Expand Down
6 changes: 4 additions & 2 deletions operator/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions operator/pkg/resources/cmn/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ func NewGlobalCM(ais *aisv1.AIStore, toUpdate *aiscmn.ConfigToSet) (*corev1.Conf
Namespace: ais.Namespace,
},
Data: map[string]string{
"ais.json": conf,
"ais_liveness.sh": livenessSh,
"ais_readiness.sh": readinessSh,
"hostname_lookup.sh": hostnameMapSh,
"ais.json": conf,
"ais_liveness.sh": livenessSh,
"ais_readiness.sh": readinessSh,
},
}
if ais.Spec.HostnameMap != nil {
cm.Data["hostname_map"] = *ais.Spec.HostnameMap
}
return cm, nil
}
20 changes: 0 additions & 20 deletions operator/pkg/resources/cmn/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,4 @@ const (
[[ "${stat}" == "200" ]] && exit 0
exit 1
`
hostnameMapSh = `
#!/bin/bash
# Lookup the hostnames in the hostname config map (allows for multiple host ips)
hostname_map="/var/global_config/hostname_map"
if [ -f "$hostname_map" ]; then
read -ra pairs <<< "$(cat "$hostname_map")"
for pair in "${pairs[@]}"; do
IFS='=' read -ra parts <<< "$pair"
key="${parts[0]}"
value="${parts[1]}"
if [ "$key" = "$AIS_PUBLIC_HOSTNAME" ]; then
echo "Setting AIS_PUBLIC_HOSTNAME to value from configMap: ${value}"
export AIS_PUBLIC_HOSTNAME="$value"
break
fi
done
fi
`
)
13 changes: 13 additions & 0 deletions operator/pkg/resources/cmn/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ package cmn
import (
aisv1 "github.com/ais-operator/api/v1beta1"
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
corev1 "k8s.io/api/core/v1"
)

func GetPublicHostname(ais *aisv1.AIStore) corev1.EnvVar {
hostIP := EnvFromFieldPath(EnvPublicHostname, "status.hostIP")
// Do not attach additional host IPs if we don't have any additional networks
if ais.Spec.NetAttachment != nil {
return hostIP
}
if val, ok := ais.Spec.HostnameMap[hostIP.Value]; ok {
hostIP.Value = val
}
return hostIP
}

func ParseAnnotations(ais *aisv1.AIStore) map[string]string {
if ais.Spec.NetAttachment != nil {
return map[string]string{
Expand Down
3 changes: 0 additions & 3 deletions operator/pkg/resources/proxy/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pod_dns="${MY_POD}.${MY_SERVICE}.${K8S_NS}.svc.${cluster_domain}"
export AIS_INTRA_HOSTNAME=${pod_dns}
export AIS_DATA_HOSTNAME=${pod_dns}
# Run script to replace AIS_PUBLIC_HOSTNAME with its entry in the hostname config map if provided
source "/var/global_config/hostname_lookup.sh"
local_conf_template="/var/ais_config_template/ais_local.json"
local_conf_file="/var/ais_config/ais_local.json"
envsubst < ${local_conf_template} > ${local_conf_file}
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/resources/proxy/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func proxyPodSpec(ais *aisv1.AIStore) corev1.PodSpec {
var optionals []corev1.EnvVar
if ais.Spec.ProxySpec.HostPort != nil {
optionals = []corev1.EnvVar{
cmn.EnvFromFieldPath(cmn.EnvPublicHostname, "status.hostIP"),
cmn.GetPublicHostname(ais),
}
}
if ais.Spec.GCPSecretName != nil {
Expand Down
3 changes: 0 additions & 3 deletions operator/pkg/resources/target/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ pod_dns="${MY_POD}.${MY_SERVICE}.${K8S_NS}.svc.${cluster_domain}"
export AIS_INTRA_HOSTNAME=${pod_dns}
export AIS_DATA_HOSTNAME=${pod_dns}
# Run script to replace AIS_PUBLIC_HOSTNAME with its entry in the hostname config map if provided
source "/var/global_config/hostname_lookup.sh"
local_conf_template="/var/ais_config_template/ais_local.json"
local_conf_file="/var/ais_config/ais_local.json"
envsubst < ${local_conf_template} > ${local_conf_file}
Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/resources/target/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewTargetSS(ais *aisv1.AIStore) *apiv1.StatefulSet {
var optionals []corev1.EnvVar
if ais.Spec.TargetSpec.HostPort != nil {
optionals = []corev1.EnvVar{
cmn.EnvFromFieldPath(cmn.EnvPublicHostname, "status.hostIP"),
cmn.GetPublicHostname(ais),
}
}
if ais.Spec.TLSSecretName != nil {
Expand Down
10 changes: 10 additions & 0 deletions playbooks/roles/ais_deploy_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
changed_when: "'configured' in createpvsout.stdout or 'created' in createpvsout.stdout"
when: redeploy is not defined

- name: Create hostname map if additional hosts are defined
set_fact:
hostname_map: "{{ hostname_map | default({}) | combine({ primary_host_key: all_hosts }) }}"
vars:
primary_host_key: "{{ hostvars[host_entry]['ansible_host'] | default(host_entry) }}"
all_hosts: "{{ hostvars[host_entry]['ansible_host'] | default(host_entry) }},{{ hostvars[host_entry]['additional_hosts'] }}"
loop: "{{ groups[cluster] }}"
loop_control:
loop_var: host_entry

- name: Copy cluster yaml
template:
src: "ais.yaml.j2"
Expand Down

0 comments on commit c6d766b

Please sign in to comment.