Skip to content

Commit

Permalink
Revise target variable for dynamic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Feb 9, 2024
1 parent 014127c commit 818ed80
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions build/charts/yorkie-cluster/templates/plumber/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ spec:
args: [
"--service-discovery-mode",
"k8s",
"--target-filter",
"yorkie"
]
ports:
- containerPort: 80
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}

targetBackendImage, err := cmd.Flags().GetString("target-backend-image")
targetFilter, err := cmd.Flags().GetString("target-filter")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

agent, err := internal.NewAgent(serviceDiscoveryMode, targetBackendImage)
agent, err := internal.NewAgent(serviceDiscoveryMode, targetFilter)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -84,7 +84,7 @@ func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

rootCmd.Flags().String("service-discovery-mode", "docker", "Service discovery mode")
rootCmd.Flags().String("target-backend-image", "traefik/whoami", "Actor backend image for service discovery")
rootCmd.Flags().String("target-filter", "traefik/whoami", "Backend target filter for service discovery")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
4 changes: 2 additions & 2 deletions internal/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type Agent struct {
loadBalancer loadbalancer.LoadBalancer
}

func NewAgent(serviceDiscoveryMode, targetBackendImage string) (*Agent, error) {
func NewAgent(serviceDiscoveryMode, targetFilter string) (*Agent, error) {
// TODO(krapie): we fix LB configuration maglev for now, but we can make it configurable
loadBalancer, err := maglev.NewLB(serviceDiscoveryMode, targetBackendImage)
loadBalancer, err := maglev.NewLB(serviceDiscoveryMode, targetFilter)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions internal/backend/register/docker/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Register struct {
ServiceRegistry *registry.BackendRegistry
EventChannel chan register.BackendEvent

Target string
TargetFilter string
}

func NewRegister() (*Register, error) {
Expand All @@ -40,8 +40,8 @@ func (r *Register) GetEventChannel() chan register.BackendEvent {
return r.EventChannel
}

func (r *Register) SetTarget(target string) {
r.Target = target
func (r *Register) SetTargetFilter(targetFilter string) {
r.TargetFilter = targetFilter
}

func (r *Register) SetRegistry(registry *registry.BackendRegistry) {
Expand All @@ -56,7 +56,7 @@ func (r *Register) Initialize() error {
containerList, err := r.DockerClient.ContainerList(context.Background(), container.ListOptions{
Filters: filters.NewArgs(
filters.Arg("status", "running"),
filters.Arg("ancestor", r.Target),
filters.Arg("ancestor", r.TargetFilter),
),
})
if err != nil {
Expand Down Expand Up @@ -91,7 +91,7 @@ func (r *Register) observe() {
msgCh, errCh := r.DockerClient.Events(context.Background(), types.EventsOptions{
Filters: filters.NewArgs(
filters.Arg("type", "container"),
filters.Arg("image", r.Target),
filters.Arg("image", r.TargetFilter),
filters.Arg("event", "start"),
filters.Arg("event", "kill"),
),
Expand Down
10 changes: 5 additions & 5 deletions internal/backend/register/k8s/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Register struct {
ServiceRegistry *registry.BackendRegistry
EventChannel chan register.BackendEvent

Target string
TargetFilter string
}

func NewRegister() (*Register, error) {
Expand All @@ -46,8 +46,8 @@ func (r *Register) GetEventChannel() chan register.BackendEvent {
return r.EventChannel
}

func (r *Register) SetTarget(target string) {
r.Target = target
func (r *Register) SetTargetFilter(targetFilter string) {
r.TargetFilter = targetFilter
}

func (r *Register) SetRegistry(registry *registry.BackendRegistry) {
Expand Down Expand Up @@ -93,8 +93,8 @@ func (r *Register) Observe() {
}

func (r *Register) observe() {
podWatcher, err := r.client.CoreV1().Pods("yorkie").Watch(context.TODO(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{"app.kubernetes.io/instance": "yorkie"}).AsSelector().String(),
podWatcher, err := r.client.CoreV1().Pods(r.TargetFilter).Watch(context.TODO(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{"app.kubernetes.io/instance": r.TargetFilter}).AsSelector().String(),
})
if err != nil {
log.Println("[Register] Error:", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type BackendEvent struct {
}

type Register interface {
SetTarget(target string)
SetTargetFilter(targetFilter string)
SetRegistry(registry *registry.BackendRegistry)
GetEventChannel() chan BackendEvent
Initialize() error
Expand Down
4 changes: 2 additions & 2 deletions internal/loadbalancer/maglev/maglev_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MaglevLB struct {
streamConnections []*Connection
}

func NewLB(serviceDiscoveryMode, targetBackendImage string) (*MaglevLB, error) {
func NewLB(serviceDiscoveryMode, targetFilter string) (*MaglevLB, error) {
lookupTable, err := NewMaglev([]string{}, MinVirtualNodes)
if err != nil {
return nil, err
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewLB(serviceDiscoveryMode, targetBackendImage string) (*MaglevLB, error) {
}
lb.RunWatchEventLoop()

backendRegister.SetTarget(targetBackendImage)
backendRegister.SetTargetFilter(targetFilter)
backendRegister.SetRegistry(backendRegistry)
err = backendRegister.Initialize()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/loadbalancer/round_robin/round_robin_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RoundRobinLB struct {
index int64
}

func NewLB(serviceDiscoveryMode, targetBackendImage string) (*RoundRobinLB, error) {
func NewLB(serviceDiscoveryMode, targetFilter string) (*RoundRobinLB, error) {
backendRegistry := registry.NewRegistry()

var backendRegister register.Register
Expand All @@ -40,7 +40,7 @@ func NewLB(serviceDiscoveryMode, targetBackendImage string) (*RoundRobinLB, erro
}
}

backendRegister.SetTarget(targetBackendImage)
backendRegister.SetTargetFilter(targetFilter)
backendRegister.SetRegistry(backendRegistry)
err = backendRegister.Initialize()
if err != nil {
Expand Down

0 comments on commit 818ed80

Please sign in to comment.