From 41bc5eeaedda5c6de5a30398e3870db6271d0c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E6=B5=81=E8=80=8C=E4=B8=8A?= <62740231+DokiDoki1103@users.noreply.github.com> Date: Thu, 21 Dec 2023 18:38:41 +0800 Subject: [PATCH] feat: support hostAliases (#1810) --- db/model/component.go | 44 ++++++++++++++++--------------- worker/appm/conversion/version.go | 30 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/db/model/component.go b/db/model/component.go index 5374c6efc..9b9bb5966 100644 --- a/db/model/component.go +++ b/db/model/component.go @@ -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 - diff --git a/worker/appm/conversion/version.go b/worker/appm/conversion/version.go index 88f5749d1..102fe8167 100644 --- a/worker/appm/conversion/version.go +++ b/worker/appm/conversion/version.go @@ -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)