pod template use rbd-dns config

This commit is contained in:
凡羊羊 2020-01-03 13:42:09 +08:00
parent ec58a7df39
commit fe030f615f
4 changed files with 87 additions and 5 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/goodrain/rainbond/util"
"github.com/goodrain/rainbond/worker/appm/store"
"github.com/goodrain/rainbond/worker/appm/types/v1"
v1 "github.com/goodrain/rainbond/worker/appm/types/v1"
"k8s.io/client-go/kubernetes"
)

View File

@ -23,6 +23,8 @@ import (
"sync"
"time"
workerutil "github.com/goodrain/rainbond/worker/util"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/util"
@ -109,13 +111,25 @@ func (s *startController) startOne(app v1.AppService) error {
}
//step 2: create statefulset or deployment
if statefulset := app.GetStatefulSet(); statefulset != nil {
_, err := s.manager.client.AppsV1().StatefulSets(app.TenantID).Create(statefulset)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace)
if err != nil {
return err
}
statefulset.Spec.Template.Spec.DNSConfig = podDNSConfig
statefulset.Spec.Template.Spec.DNSPolicy = "None"
_, err = s.manager.client.AppsV1().StatefulSets(app.TenantID).Create(statefulset)
if err != nil {
return fmt.Errorf("create statefulset failure:%s", err.Error())
}
}
if deployment := app.GetDeployment(); deployment != nil {
_, err := s.manager.client.AppsV1().Deployments(app.TenantID).Create(deployment)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace)
if err != nil {
return err
}
deployment.Spec.Template.Spec.DNSConfig = podDNSConfig
deployment.Spec.Template.Spec.DNSPolicy = "None"
_, err = s.manager.client.AppsV1().Deployments(app.TenantID).Create(deployment)
if err != nil {
return fmt.Errorf("create deployment failure:%s;", err.Error())
}

View File

@ -28,6 +28,7 @@ import (
"github.com/goodrain/rainbond/util"
"github.com/goodrain/rainbond/worker/appm/f"
v1 "github.com/goodrain/rainbond/worker/appm/types/v1"
workerutil "github.com/goodrain/rainbond/worker/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -187,14 +188,26 @@ func (s *upgradeController) upgradeOne(app v1.AppService) error {
}
s.upgradeConfigMap(app)
if deployment := app.GetDeployment(); deployment != nil {
_, err := s.manager.client.AppsV1().Deployments(deployment.Namespace).Patch(deployment.Name, types.MergePatchType, app.UpgradePatch["deployment"])
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace)
if err != nil {
return err
}
deployment.Spec.Template.Spec.DNSConfig = podDNSConfig
deployment.Spec.Template.Spec.DNSPolicy = "None"
_, err = s.manager.client.AppsV1().Deployments(deployment.Namespace).Patch(deployment.Name, types.MergePatchType, app.UpgradePatch["deployment"])
if err != nil {
app.Logger.Error(fmt.Sprintf("upgrade deployment %s failure %s", app.ServiceAlias, err.Error()), event.GetLoggerOption("failure"))
return fmt.Errorf("upgrade deployment %s failure %s", app.ServiceAlias, err.Error())
}
}
if statefulset := app.GetStatefulSet(); statefulset != nil {
_, err := s.manager.client.AppsV1().StatefulSets(statefulset.Namespace).Patch(statefulset.Name, types.MergePatchType, app.UpgradePatch["statefulset"])
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace)
if err != nil {
return err
}
statefulset.Spec.Template.Spec.DNSConfig = podDNSConfig
statefulset.Spec.Template.Spec.DNSPolicy = "None"
_, err = s.manager.client.AppsV1().StatefulSets(statefulset.Namespace).Patch(statefulset.Name, types.MergePatchType, app.UpgradePatch["statefulset"])
if err != nil {
logrus.Errorf("patch statefulset error : %s", err.Error())
app.Logger.Error(fmt.Sprintf("upgrade statefulset %s failure %s", app.ServiceAlias, err.Error()), event.GetLoggerOption("failure"))

55
worker/util/dns.go Normal file
View File

@ -0,0 +1,55 @@
package util
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
var (
defaultRBDSystem = "rbd-system"
defaultRBDDNS = "rbd-dns"
)
func dns2Config(endpoint *corev1.Endpoints, podNamespace string) (podDNSConfig *corev1.PodDNSConfig, err error) {
if endpoint == nil {
return nil, fmt.Errorf("rbd-dns endpoints is nil")
}
servers := make([]string, 0)
for _, sub := range endpoint.Subsets {
for _, addr := range sub.Addresses {
servers = append(servers, addr.IP)
}
}
searchRBDDNS := fmt.Sprintf("%s.svc.cluster.local", podNamespace)
ndotsValue := "5"
return &corev1.PodDNSConfig{
Nameservers: servers,
Options: []corev1.PodDNSConfigOption{corev1.PodDNSConfigOption{Name: "ndots", Value: &ndotsValue}},
Searches: []string{searchRBDDNS, "svc.cluster.local", "cluster.local"},
}, nil
}
// MakePodDNSConfig make pod dns config
func MakePodDNSConfig(clientset *kubernetes.Clientset, podNamespace string) (podDNSConfig *corev1.PodDNSConfig, err error) {
namespace := os.Getenv("RBD_SYSTEM")
rbdName := os.Getenv("RBD_DNS")
if namespace == "" {
namespace = defaultRBDSystem
}
if rbdName == "" {
rbdName = defaultRBDDNS
}
endpoints, err := clientset.CoreV1().Endpoints(namespace).Get(rbdName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("found rbd-dns error: %s", err.Error())
}
podDNSConfig, err = dns2Config(endpoints, podNamespace)
if err != nil {
return nil, fmt.Errorf("parse rbd-dns to dnsconfig error: %s", err.Error())
}
return podDNSConfig, nil
}