Skip to content

Commit

Permalink
feat(agent): enable leader election
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Jan 10, 2023
1 parent e7eb6c2 commit 4e9c0f2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
22 changes: 22 additions & 0 deletions charts/ledger/templates/agent_role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- if .Values.ledgerAgent.enabled -}}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: {{ .Values.ledgerAgent.namespaced | ternary "Role" "ClusterRole" }}
metadata:
Expand All @@ -23,4 +24,25 @@ rules:
verbs:
- create
- patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "ledger.fullname" . }}-agent-le
labels:
{{- include "ledger.labels" . | nindent 4 }}
component: agent
rules:
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
{{- end }}
19 changes: 18 additions & 1 deletion charts/ledger/templates/agent_rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- if .Values.ledgerAgent.enabled -}}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: {{ .Values.ledgerAgent.namespaced | ternary "RoleBinding" "ClusterRoleBinding" }}
metadata:
Expand All @@ -14,4 +15,20 @@ subjects:
- kind: ServiceAccount
name: {{ include "ledger.serviceAccountName" . }}-agent
namespace: {{ .Release.Namespace }}
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "ledger.fullname" . }}-agent-le
labels:
{{- include "ledger.labels" . | nindent 4 }}
component: agent
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "ledger.fullname" . }}-agent-le
subjects:
- kind: ServiceAccount
name: {{ include "ledger.serviceAccountName" . }}-agent
namespace: {{ .Release.Namespace }}
{{- end }}
17 changes: 8 additions & 9 deletions cmd/ledger/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ import (
"github.com/stenic/ledger/internal/pkg/utils/env"
)

var opts agent.Options

func NewAgentCmd() *cobra.Command {
var (
endpoint string
namespace string
location string
)

cmd := &cobra.Command{
Use: "agent",
Short: "Ledger cluster agent",
Run: func(c *cobra.Command, args []string) {
agent.Run(endpoint, namespace, location)
agent.Run(opts)
},
}

cmd.Flags().StringVar(&endpoint, "endpoint", env.GetString("LEDGER_ENDPOINT", "http://127.0.0.1:8080"), "Ledger endpoint url")
cmd.Flags().StringVar(&namespace, "namespace", env.GetString("LEDGER_NAMESPACE", ""), "Ledger namespace, empty for all")
cmd.Flags().StringVar(&location, "location", env.GetString("LEDGER_LOCATION", ""), "Location")
cmd.Flags().StringVar(&opts.Endpoint, "endpoint", env.GetString("LEDGER_ENDPOINT", "http://127.0.0.1:8080"), "Ledger endpoint url")
cmd.Flags().StringVar(&opts.Namespaces, "namespace", env.GetString("LEDGER_NAMESPACE", ""), "Ledger namespace, empty for all")
cmd.Flags().StringVar(&opts.Location, "location", env.GetString("LEDGER_LOCATION", ""), "Location")
cmd.Flags().BoolVar(&opts.LeaderElection, "leader-election", env.GetBool("LEDGER_LEADER_ELECTION", false), "Leader election")
cmd.Flags().StringVar(&opts.LeaderElectionNamespace, "leader-election-namespace", env.GetString("LEDGER_LEADER_ELECTION_NAMESPACE", "ledger"), "Leader election namespace")

return cmd
}
Expand Down
36 changes: 23 additions & 13 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
}

func Run(endpoint, namespace, envLocation string) {
location = envLocation
type Options struct {
Endpoint string
Namespaces string
Location string

LeaderElection bool
LeaderElectionNamespace string
}

func Run(opts Options) {
location = opts.Location
ctrl.SetLogger(logrusr.New(logrus.StandardLogger()))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: ":8082",
Port: 9443,
HealthProbeBindAddress: ":8081",
Namespace: namespace,
// LeaderElection: true,
// LeaderElectionID: "ledger.stenic.io",
// LeaderElectionNamespace: "ledger",
})
mgrOpts := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: ":8082",
Port: 9443,
HealthProbeBindAddress: ":8081",
Namespace: opts.Namespaces,
LeaderElection: opts.LeaderElection,
LeaderElectionID: "ledger.stenic.io",
LeaderElectionNamespace: opts.LeaderElectionNamespace,
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOpts)
if err != nil {
logrus.Fatal(err)
}
Expand All @@ -47,7 +57,7 @@ func Run(endpoint, namespace, envLocation string) {
logrus.Fatal("Please provide a TOKEN environment variable")
}
lc = client.LedgerClient{
Endpoint: endpoint + "/query",
Endpoint: opts.Endpoint + "/query",
Token: tkn,
}

Expand Down

0 comments on commit 4e9c0f2

Please sign in to comment.