Skip to content

Commit

Permalink
feat: Add agent for collecting information
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 13, 2022
1 parent a73817f commit d25fd2a
Show file tree
Hide file tree
Showing 20 changed files with 755 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ USER 65532:65532
ENV STATIC_ASSET_PATH=/app/static
ENV LOG_FORMAT=json

ENTRYPOINT ["/app/dumb-init", "--", "/app/ledger", "server"]
ENTRYPOINT ["/app/dumb-init", "--", "/app/ledger"]
CMD [ "server" ]
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## Collecting versions

### Sending by installing the included agent in your cluster

It will collect changes to Deployments and Statefulsets

### Sending using the included client

```bash
curl -o ledger https://[ledger-installation]/download
chmod +x ledger
./ledger client new-version app env version
```

### Sending using the GraphQL API

```bash
export TOKEN=ey...

Expand Down
23 changes: 23 additions & 0 deletions charts/ledger-agent/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
15 changes: 15 additions & 0 deletions charts/ledger-agent/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v2
name: ledger-agent
description: A Helm chart for Kubernetes
type: application
version: 0.3.0
appVersion: 1.0.0
maintainers:
- email: [email protected]
name: Stenic
icon: https://raw.githubusercontent.com/stenic/ledger/gh-pages/images/logo.png
home: https://github.com/stenic/ledger/tree/main/charts/ledger
sources:
- https://github.com/stenic/ledger
keywords:
- deployment
62 changes: 62 additions & 0 deletions charts/ledger-agent/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "ledger-agent.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "ledger-agent.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "ledger-agent.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "ledger-agent.labels" -}}
helm.sh/chart: {{ include "ledger-agent.chart" . }}
{{ include "ledger-agent.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "ledger-agent.selectorLabels" -}}
app.kubernetes.io/name: {{ include "ledger-agent.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "ledger-agent.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "ledger-agent.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
59 changes: 59 additions & 0 deletions charts/ledger-agent/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "ledger-agent.fullname" . }}
labels:
{{- include "ledger-agent.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "ledger-agent.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "ledger-agent.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "ledger-agent.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args: [ "agent" ]
resources:
{{- toYaml .Values.resources | nindent 12 }}
env:
- name: LEDGER_ENDPOINT
valueFrom:
secretKeyRef:
key: endpoint
name: {{ include "ledger-agent.fullname" . }}
- name: TOKEN
valueFrom:
secretKeyRef:
key: token
name: {{ include "ledger-agent.fullname" . }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
14 changes: 14 additions & 0 deletions charts/ledger-agent/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "ledger-agent.fullname" . }}
labels:
{{- include "ledger-agent.labels" . | nindent 4 }}
rules:
- apiGroups:
- "apps"
resources:
- deployments
- statefulsets
verbs:
- list
14 changes: 14 additions & 0 deletions charts/ledger-agent/templates/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "ledger-agent.fullname" . }}
labels:
{{- include "ledger-agent.labels" . | nindent 4 }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "ledger-agent.fullname" . }}
subjects:
- kind: ServiceAccount
name: {{ include "ledger-agent.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
11 changes: 11 additions & 0 deletions charts/ledger-agent/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: {{ include "ledger-agent.fullname" . }}
labels:
{{- include "ledger-agent.labels" . | nindent 4 }}
data:
endpoint: {{ .Values.ledgerAgent.endpoint | b64enc }}
token: {{ .Values.ledgerAgent.token | b64enc }}
log-level: {{ .Values.ledgerAgent.log.level | b64enc }}
12 changes: 12 additions & 0 deletions charts/ledger-agent/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "ledger-agent.serviceAccountName" . }}
labels:
{{- include "ledger-agent.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
73 changes: 73 additions & 0 deletions charts/ledger-agent/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Default values for ledger.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

# replicaCount -- Desired number of pods
replicaCount: 2

image:
# image.repository -- ledger image repository
repository: ghcr.io/stenic/ledger
# image.pullPolicy -- ledger image pullPolicy
pullPolicy: Always
# image.tag -- ledger image tag (immutable tags are recommended)
# Overrides the image tag whose default is the chart appVersion.
tag: ""

# imagePullSecrets -- Docker registry secret names as an array
imagePullSecrets: []
# nameOverride -- String to partially override fullname
nameOverride: ""
# fullnameOverride -- String to fully override fullname
fullnameOverride: ""

serviceAccount:
# serviceAccount.create -- Specifies whether a ServiceAccount should be created
create: true
# Annotations to add to the service account
annotations: {}
# serviceAccount.name -- The name of the ServiceAccount to use.
# If not set and create is true, a name is generated using the fullname template
name: ""

# podAnnotations -- Additional annotations for the pods.
podAnnotations: {}

# podSecurityContext -- Enable Controller pods' Security Context
podSecurityContext: {}
# fsGroup: 2000

# securityContext -- Enable Controller containers' Security Context
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
runAsUser: 100012
runAsGroup: 100012

# resources -- Resource requests and limits for the controller
resources:
limits:
cpu: 200m
memory: 64Mi
requests:
cpu: 50m
memory: 64Mi

# nodeSelector -- Node labels for controller pod assignment
nodeSelector: {}

# affinity -- Affinity and anti-affinity
affinity: {}

# tolerations -- Node tolerations for server scheduling to nodes with taints
tolerations: []

ledgerAgent:
endpoint: ledger
token: test
log:
level: info
29 changes: 29 additions & 0 deletions cmd/ledger/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/spf13/cobra"
"github.com/stenic/ledger/internal/agent"
"github.com/stenic/ledger/internal/pkg/utils/env"
)

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

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

cmd.Flags().StringVar(&endpoint, "endpoint", env.GetString("LEDGER_ENDPOINT", "http://127.0.0.1:8080"), "Ledger endpoint url")

return cmd
}

func init() {
rootCommand.AddCommand(NewAgentCmd())
}
Loading

0 comments on commit d25fd2a

Please sign in to comment.