-
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
c943684
commit f8282a9
Showing
2 changed files
with
85 additions
and
10 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 { | ||
client *k8sapi.Client | ||
} | ||
|
||
func NewPodStateSetter() (*podStateSetter, error) { | ||
client, err := k8sapi.NewClient() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create k8s client: %w", err) | ||
} | ||
|
||
return &podStateSetter{client: client}, nil | ||
} | ||
|
||
func (p *podStateSetter) SetState(ctx context.Context, state string) error { | ||
return p.client.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 | ||
} |