update code, use default dns if rbd-dns not found

This commit is contained in:
凡羊羊 2020-01-14 14:24:42 +08:00
parent 1712d04213
commit 09726a5d37
4 changed files with 30 additions and 35 deletions

View File

@ -66,7 +66,7 @@ var TypeControllerRefreshHPA TypeController = "refreshhpa"
type Manager struct {
ctx context.Context
cancel context.CancelFunc
client *kubernetes.Clientset
client kubernetes.Interface
rbdNamespace string
rbdDNSName string
controllers map[string]Controller

View File

@ -109,26 +109,26 @@ func (s *startController) startOne(app v1.AppService) error {
}
}
}
// before create app, prepare poddnsconfig
podDNSConfig := workerutil.MakePodDNSConfig(s.manager.client, app.TenantID, s.manager.rbdNamespace, s.manager.rbdDNSName)
//step 2: create statefulset or deployment
if statefulset := app.GetStatefulSet(); statefulset != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
if podDNSConfig != nil {
statefulset.Spec.Template.Spec.DNSConfig = podDNSConfig
statefulset.Spec.Template.Spec.DNSPolicy = "None"
}
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 {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
if podDNSConfig != nil {
deployment.Spec.Template.Spec.DNSConfig = podDNSConfig
deployment.Spec.Template.Spec.DNSPolicy = "None"
}
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

@ -187,14 +187,15 @@ func (s *upgradeController) upgradeOne(app v1.AppService) error {
}
}
s.upgradeConfigMap(app)
if deployment := app.GetDeployment(); deployment != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
podDNSConfig := workerutil.MakePodDNSConfig(s.manager.client, app.TenantID, s.manager.rbdNamespace, s.manager.rbdDNSName)
if deployment := app.GetDeployment(); deployment != nil {
if podDNSConfig != nil {
deployment.Spec.Template.Spec.DNSConfig = podDNSConfig
deployment.Spec.Template.Spec.DNSPolicy = "None"
}
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"))
@ -202,12 +203,11 @@ func (s *upgradeController) upgradeOne(app v1.AppService) error {
}
}
if statefulset := app.GetStatefulSet(); statefulset != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
if podDNSConfig != nil {
statefulset.Spec.Template.Spec.DNSConfig = podDNSConfig
statefulset.Spec.Template.Spec.DNSPolicy = "None"
}
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())

View File

@ -2,6 +2,7 @@ package util
import (
"fmt"
"github.com/Sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -9,9 +10,10 @@ import (
"k8s.io/client-go/kubernetes"
)
func dns2Config(endpoint *corev1.Endpoints, podNamespace string) (podDNSConfig *corev1.PodDNSConfig, err error) {
func dns2Config(endpoint *corev1.Endpoints, podNamespace string) (podDNSConfig *corev1.PodDNSConfig) {
if endpoint == nil {
return nil, fmt.Errorf("rbd-dns endpoints is nil")
logrus.Debug("rbd-dns endpoint is nil")
return nil
}
servers := make([]string, 0)
for _, sub := range endpoint.Subsets {
@ -25,18 +27,11 @@ func dns2Config(endpoint *corev1.Endpoints, podNamespace string) (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, rbdNamespace, rbdEndpointDNSName string) (podDNSConfig *corev1.PodDNSConfig, err error) {
endpoints, err := clientset.CoreV1().Endpoints(rbdNamespace).Get(rbdEndpointDNSName, 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
func MakePodDNSConfig(clientset kubernetes.Interface, podNamespace, rbdNamespace, rbdEndpointDNSName string) (podDNSConfig *corev1.PodDNSConfig) {
endpoints, _ := clientset.CoreV1().Endpoints(rbdNamespace).Get(rbdEndpointDNSName, metav1.GetOptions{})
return dns2Config(endpoints, podNamespace)
}