use config.option instead of env

This commit is contained in:
凡羊羊 2020-01-06 14:38:10 +08:00
parent fe030f615f
commit 7f13e6ff7d
6 changed files with 30 additions and 34 deletions

View File

@ -49,6 +49,8 @@ type Config struct {
KubeClient *kubernetes.Clientset
LeaderElectionNamespace string
LeaderElectionIdentity string
RBDNamespace string
RBDDNSName string
}
//Worker worker server
@ -84,6 +86,8 @@ func (a *Worker) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.NodeAPI, "node-api", "http://172.30.42.1:6100", "node discover api, node docker endpoints")
flag.StringVar(&a.LeaderElectionNamespace, "leader-election-namespace", "rainbond", "Namespace where this attacher runs.")
flag.StringVar(&a.LeaderElectionIdentity, "leader-election-identity", "", "Unique idenity of this attcher. Typically name of the pod where the attacher runs.")
flag.StringVar(&a.RBDNamespace, "rbd-system-namespace", "rbd-system", "rbd components kubernetes namespace")
flag.StringVar(&a.RBDDNSName, "rbd-dns", "rbd-dns", "rbd dns endpoint name")
}
//SetLog 设置log

View File

@ -25,9 +25,9 @@ import (
"github.com/Sirupsen/logrus"
"github.com/eapache/channels"
kubeaggregatorclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
kubeaggregatorclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"github.com/goodrain/rainbond/cmd/worker/option"
"github.com/goodrain/rainbond/db"
@ -101,7 +101,7 @@ func Run(s *option.Worker) error {
}
//step 4: create controller manager
controllerManager := controller.NewManager(cachestore, clientset)
controllerManager := controller.NewManager(cachestore, clientset, s.Config.RBDNamespace, s.Config.RBDDNSName)
defer controllerManager.Stop()
//step 5 : start runtime master

View File

@ -64,23 +64,27 @@ var TypeControllerRefreshHPA TypeController = "refreshhpa"
//Manager controller manager
type Manager struct {
ctx context.Context
cancel context.CancelFunc
client *kubernetes.Clientset
controllers map[string]Controller
store store.Storer
lock sync.Mutex
ctx context.Context
cancel context.CancelFunc
client *kubernetes.Clientset
rbdNamespace string
rbdDNSName string
controllers map[string]Controller
store store.Storer
lock sync.Mutex
}
//NewManager new manager
func NewManager(store store.Storer, client *kubernetes.Clientset) *Manager {
func NewManager(store store.Storer, client *kubernetes.Clientset, rbdNamespace, rbdDNSName string) *Manager {
ctx, cancel := context.WithCancel(context.Background())
return &Manager{
ctx: ctx,
cancel: cancel,
client: client,
controllers: make(map[string]Controller),
store: store,
ctx: ctx,
cancel: cancel,
client: client,
controllers: make(map[string]Controller),
store: store,
rbdNamespace: rbdNamespace,
rbdDNSName: rbdDNSName,
}
}

View File

@ -111,7 +111,7 @@ func (s *startController) startOne(app v1.AppService) error {
}
//step 2: create statefulset or deployment
if statefulset := app.GetStatefulSet(); statefulset != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
}
@ -123,7 +123,7 @@ func (s *startController) startOne(app v1.AppService) error {
}
}
if deployment := app.GetDeployment(); deployment != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
}

View File

@ -188,7 +188,8 @@ 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)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, deployment.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
}
@ -201,7 +202,7 @@ func (s *upgradeController) upgradeOne(app v1.AppService) error {
}
}
if statefulset := app.GetStatefulSet(); statefulset != nil {
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace)
podDNSConfig, err := workerutil.MakePodDNSConfig(s.manager.client, statefulset.Namespace, s.manager.rbdNamespace, s.manager.rbdDNSName)
if err != nil {
return err
}

View File

@ -2,18 +2,13 @@ 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")
@ -34,16 +29,8 @@ func dns2Config(endpoint *corev1.Endpoints, podNamespace string) (podDNSConfig *
}
// 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{})
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())
}