diff --git a/charts/ledger/templates/agent_role.yaml b/charts/ledger/templates/agent_role.yaml index 6c44b77..f8f0d25 100644 --- a/charts/ledger/templates/agent_role.yaml +++ b/charts/ledger/templates/agent_role.yaml @@ -1,4 +1,5 @@ {{- if .Values.ledgerAgent.enabled -}} +--- apiVersion: rbac.authorization.k8s.io/v1 kind: {{ .Values.ledgerAgent.namespaced | ternary "Role" "ClusterRole" }} metadata: @@ -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 }} \ No newline at end of file diff --git a/charts/ledger/templates/agent_rolebinding.yaml b/charts/ledger/templates/agent_rolebinding.yaml index 1a1d942..258f74c 100644 --- a/charts/ledger/templates/agent_rolebinding.yaml +++ b/charts/ledger/templates/agent_rolebinding.yaml @@ -1,4 +1,5 @@ {{- if .Values.ledgerAgent.enabled -}} +--- apiVersion: rbac.authorization.k8s.io/v1 kind: {{ .Values.ledgerAgent.namespaced | ternary "RoleBinding" "ClusterRoleBinding" }} metadata: @@ -14,4 +15,20 @@ subjects: - kind: ServiceAccount name: {{ include "ledger.serviceAccountName" . }}-agent namespace: {{ .Release.Namespace }} -{{- end }} \ No newline at end of file +--- +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 }} diff --git a/cmd/ledger/agent.go b/cmd/ledger/agent.go index d6b375c..6b4a4fb 100644 --- a/cmd/ledger/agent.go +++ b/cmd/ledger/agent.go @@ -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 } diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 3495a6f..9675489 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -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) } @@ -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, }