-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79b8e04
commit 4dd2044
Showing
2 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package failover | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pace/bricks/backend/k8sapi" | ||
) | ||
|
||
const Label = "github.com.pace.bricks.activepassive" | ||
|
||
type StateSetter interface { | ||
SetState(ctx context.Context, state string) error | ||
} | ||
|
||
type podStateSetter struct { | ||
k8sClient *k8sapi.Client | ||
} | ||
|
||
func NewPodStateSetter() (*podStateSetter, error) { | ||
k8sClient, err := k8sapi.NewClient() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create k8s client: %w", err) | ||
} | ||
|
||
return &podStateSetter{k8sClient: k8sClient}, nil | ||
} | ||
|
||
func (p *podStateSetter) SetState(ctx context.Context, state string) error { | ||
return p.k8sClient.SetCurrentPodLabel(ctx, Label, state) | ||
} | ||
|
||
type CustomStateSetter struct { | ||
fn func(ctx context.Context, state string) error | ||
} | ||
|
||
func NewCustomStateSetter(fn func(ctx context.Context, state string) error) (*CustomStateSetter, error) { | ||
if fn == nil { | ||
return nil, fmt.Errorf("fn must not be nil") | ||
} | ||
|
||
return &CustomStateSetter{fn: fn}, nil | ||
} | ||
|
||
func (c *CustomStateSetter) SetState(ctx context.Context, state string) error { | ||
return c.fn(ctx, state) | ||
} | ||
|
||
type NoopStateSetter struct{} | ||
|
||
func (n *NoopStateSetter) SetState(ctx context.Context, state string) error { | ||
return nil | ||
} |