feat: support hostAliases (#1810)

This commit is contained in:
逆流而上 2023-12-21 18:38:41 +08:00 committed by GitHub
parent 723ac6161a
commit 41bc5eeaed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 22 deletions

View File

@ -19,48 +19,50 @@
package model
const (
//K8sAttributeNameNodeSelector -
// K8sAttributeNameNodeSelector -
K8sAttributeNameNodeSelector = "nodeSelector"
//K8sAttributeNameLabels -
// K8sAttributeNameLabels -
K8sAttributeNameLabels = "labels"
//K8sAttributeNameAnnotations -
// K8sAttributeNameAnnotations -
K8sAttributeNameAnnotations = "annotations"
//K8sAttributeNameTolerations -
// K8sAttributeNameTolerations -
K8sAttributeNameTolerations = "tolerations"
//K8sAttributeNameVolumes -
// K8sAttributeNameVolumes -
K8sAttributeNameVolumes = "volumes"
//K8sAttributeNameServiceAccountName -
// K8sAttributeNameServiceAccountName -
K8sAttributeNameServiceAccountName = "serviceAccountName"
//K8sAttributeNamePrivileged -
// K8sAttributeNamePrivileged -
K8sAttributeNamePrivileged = "privileged"
//K8sAttributeNameAffinity -
// K8sAttributeNameAffinity -
K8sAttributeNameAffinity = "affinity"
//K8sAttributeNameVolumeMounts -
// K8sAttributeNameVolumeMounts -
K8sAttributeNameVolumeMounts = "volumeMounts"
//K8sAttributeNameENV -
// K8sAttributeNameENV -
K8sAttributeNameENV = "env"
//K8sAttributeNameShareProcessNamespace -
// K8sAttributeNameShareProcessNamespace -
K8sAttributeNameShareProcessNamespace = "shareProcessNamespace"
//K8sAttributeNameDNSPolicy -
// K8sAttributeNameDNSPolicy -
K8sAttributeNameDNSPolicy = "dnsPolicy"
//K8sAttributeNameDNSConfig -
// K8sAttributeNameDNSConfig -
K8sAttributeNameDNSConfig = "dnsConfig"
//K8sAttributeNameResources -
// K8sAttributeNameResources -
K8sAttributeNameResources = "resources"
//K8sAttributeNameHostIPC -
// K8sAttributeNameHostIPC -
K8sAttributeNameHostIPC = "hostIPC"
//K8sAttributeNameLifecycle -
// K8sAttributeNameLifecycle -
K8sAttributeNameLifecycle = "lifecycle"
//K8sAttributeNameVolumeClaimTemplate -
// K8sAttributeNameVolumeClaimTemplate -
K8sAttributeNameVolumeClaimTemplate = "volumeClaimTemplate"
//K8sAttributeNameENVFromSource -
// K8sAttributeNameENVFromSource -
K8sAttributeNameENVFromSource = "envFromSource"
//K8sAttributeNameSecurityContext -
// K8sAttributeNameSecurityContext -
K8sAttributeNameSecurityContext = "securityContext"
//K8sAttributeNameReadinessProbe -
// K8sAttributeNameReadinessProbe -
K8sAttributeNameReadinessProbe = "readinessProbe"
//K8sAttributeNameLiveNessProbe -
// K8sAttributeNameLiveNessProbe -
K8sAttributeNameLiveNessProbe = "livenessProbe"
// K8sAttributeNameHostAliases -
K8sAttributeNameHostAliases = "hostAliases"
)
// ComponentK8sAttributes -

View File

@ -205,6 +205,14 @@ func TenantServiceVersion(as *v1.AppService, dbmanager db.Manager) error {
if err != nil {
return fmt.Errorf("conv service main container failure %s", err.Error())
}
aliases, err := getHostAliases(as, dbmanager)
if err != nil {
return fmt.Errorf("hostAliases service main container failure %s", err.Error())
}
if len(aliases) == 0 {
aliases = append(aliases, createHostAliases(as)...)
}
podtmpSpec = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
@ -218,7 +226,7 @@ func TenantServiceVersion(as *v1.AppService, dbmanager db.Manager) error {
NodeSelector: nodeSelector,
Tolerations: tolerations,
Affinity: affinity,
HostAliases: createHostAliases(as),
HostAliases: aliases,
Hostname: func() string {
if nodeID, ok := as.ExtensionSet["hostname"]; ok {
return nodeID
@ -728,6 +736,26 @@ func getENVFromSource(as *v1.AppService, dbmanager db.Manager) ([]corev1.EnvFrom
return envFromSource, nil
}
func getHostAliases(as *v1.AppService, dbmanager db.Manager) ([]corev1.HostAlias, error) {
hostAliasesAttribute, err := dbmanager.ComponentK8sAttributeDao().GetByComponentIDAndName(as.ServiceID, model.K8sAttributeNameHostAliases)
if err != nil {
return nil, err
}
var ha []corev1.HostAlias
if hostAliasesAttribute != nil {
VolumeAttributeJSON, err := yaml.YAMLToJSON([]byte(hostAliasesAttribute.AttributeValue))
if err != nil {
return nil, err
}
err = json.Unmarshal(VolumeAttributeJSON, &ha)
if err != nil {
logrus.Debug("hostAliasesAttribute json unmarshal error", err)
return nil, err
}
}
return ha, nil
}
func getVolumes(dv *volume.Define, as *v1.AppService, dbmanager db.Manager) ([]corev1.Volume, error) {
volumes := dv.GetVolumes()
volumeAttribute, err := dbmanager.ComponentK8sAttributeDao().GetByComponentIDAndName(as.ServiceID, model.K8sAttributeNameVolumes)