Skip to content

Commit

Permalink
feat(PL-2701): update joy to unify schema values
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed May 9, 2024
1 parent fa34178 commit 124bf39
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 43 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ COPY --chown=golang:root internal ./internal

RUN go build -v -o joy-generator ./cmd/server

FROM scratch AS prod
FROM alpine AS prod

COPY --from=build /etc/passwd /etc/group /etc/
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build --chown=golang:root /app/joy-generator /app/

RUN apk add helm

USER golang:root
EXPOSE 8080

Expand Down
13 changes: 13 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ spec:
secret:
secretName: {{ include "joy-generator.fullname" . }}-github-app-key
{{- end }}
{{- if .Values.googleCredentials }}
- name: google-credentials
secret:
secretName: {{ include "joy-generator.fullname" .}}-google-credentials
{{- end }}
containers:
- name: {{ .Chart.Name }}
securityContext:
Expand All @@ -47,6 +52,10 @@ spec:
- mountPath: /etc/joy/config
name: github-app-private-key
{{- end }}
{{- if .Values.googleCredentials }}
- mountPath: /etc/joy/secrets
name: google-credentials
{{- end }}
env:
{{- range $key, $value := .Values.env }}
{{- if $value }}
Expand All @@ -58,6 +67,10 @@ spec:
- name: GH_APP_PRIVATE_KEY_PATH
value: /etc/joy/config/githubApp.pem
{{- end }}
{{- if .Values.googleCredentials }}
- name: GOOGLE_CREDENTIALS_FILE
value: /etc/joy/secrets/credentials.json
{{- end }}
envFrom:
- secretRef:
name: {{ include "joy-generator.fullname" . }}-config
Expand Down
15 changes: 13 additions & 2 deletions chart/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ stringData:
{{ toYaml .Values.secretEnv.values | indent 2 }}
{{- end }}

---

{{- if .Values.env.GH_APP_ID }}
---
{{- and (required ".Values.env.GH_APP_INSTALLATION_ID is required" .Values.env.GH_APP_INSTALLATION_ID) "" | -}}
{{- and (required ".Values.githubAppPrivateKey is required" .Values.githubAppPrivateKey) "" -}}
{{- if $sealedSecret }}
Expand All @@ -54,3 +53,15 @@ stringData:
{{ .Values.githubAppPrivateKey | indent 4 }}
{{- end }}
{{- end }}


{{- if .Values.googleCredentials }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "joy-generator.fullname" . }}-google-credentials
stringData:
credentials.json: |
{{ .Values.googleCredentials | indent 4 }}
{{- end }}
124 changes: 124 additions & 0 deletions chart/tests/with-google-credentials/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
# Source: joy-generator/templates/secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: my-release-joy-generator-config
stringData:
GH_TOKEN: "12312312312312"
PLUGIN_TOKEN: '@very!l0ngands3curet0ken'
---
# Source: joy-generator/templates/secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: my-release-joy-generator-google-credentials
stringData:
credentials.json: |
{
"credentials": "from google!"
}
---
# Source: joy-generator/templates/argocd-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-release-joy-generator-config
data:
token: "$my-release-joy-generator-config:PLUGIN_TOKEN"
baseUrl: "http://my-release-joy-generator"
---
# Source: joy-generator/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-release-joy-generator
labels:
helm.sh/chart: joy-generator-1.0.0
app.kubernetes.io/name: joy-generator
app.kubernetes.io/instance: my-release
app.kubernetes.io/version: "1.0.0"
app.kubernetes.io/managed-by: Helm
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: joy-generator
app.kubernetes.io/instance: my-release
---
# Source: joy-generator/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-release-joy-generator
labels:
helm.sh/chart: joy-generator-1.0.0
app.kubernetes.io/name: joy-generator
app.kubernetes.io/instance: my-release
app.kubernetes.io/version: "1.0.0"
app.kubernetes.io/managed-by: Helm
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: joy-generator
app.kubernetes.io/instance: my-release
template:
metadata:
labels:
app.kubernetes.io/name: joy-generator
app.kubernetes.io/instance: my-release
spec:
securityContext:
{}
volumes:
- name: catalog-dir
emptyDir: {}
- name: google-credentials
secret:
secretName: my-release-joy-generator-google-credentials
containers:
- name: joy-generator
securityContext:
{}
image: "ghcr.io/nestoca/joy-generator:0.1.2"
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /tmp/catalog
name: catalog-dir
- mountPath: /etc/joy/secrets
name: google-credentials
env:
- name: CATALOG_DIR
value: "/tmp/catalog"
- name: CATALOG_URL
value: "https://github.com/example/foobar.git"
- name: GH_USER
value: "username"
- name: GRACE_PERIOD
value: "10s"
- name: PORT
value: ":8080"
- name: GOOGLE_CREDENTIALS_FILE
value: /etc/joy/secrets/credentials.json
envFrom:
- secretRef:
name: my-release-joy-generator-config
ports:
- name: http
containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /api/v1/health
port: http
readinessProbe:
httpGet:
path: /api/v1/readiness
port: http
resources:
{}
20 changes: 20 additions & 0 deletions chart/tests/with-google-credentials/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Default values for joy-generator.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

env:
CATALOG_URL: https://github.com/example/foobar.git
GH_USER: username

secretEnv:
values:
PLUGIN_TOKEN: "@very!l0ngands3curet0ken"
GH_TOKEN: "12312312312312"

image:
tag: 0.1.2

googleCredentials: |
{
"credentials": "from google!"
}
25 changes: 21 additions & 4 deletions cmd/server/api_get_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -27,10 +28,14 @@ func TestGetParamsE2E(t *testing.T) {
}

var (
user github.User
catalog github.RepoMetadata
user github.User
catalog github.RepoMetadata
registry string
credentials []byte
)

conf.Var(conf.Environ, &registry, "REGISTRY", conf.Required[string](true))
conf.Var(conf.Environ, &credentials, "CREDENTIALS", conf.Required[[]byte](true))
conf.Var(conf.Environ, &catalog.Path, "CATALOG_PATH", conf.Default(filepath.Join(os.TempDir(), "catalog")))
conf.Var(conf.Environ, &catalog.URL, "CATALOG_URL", conf.Required[string](true))
conf.Var(conf.Environ, &catalog.TargetRevision, "CATALOG_REVISION", conf.Default("master"))
Expand All @@ -39,6 +44,8 @@ func TestGetParamsE2E(t *testing.T) {

require.NoError(t, conf.Environ.Parse())

require.NoError(t, AuthenticateHelm(context.Background(), registry, credentials))

require.NoError(t, os.RemoveAll(catalog.Path))

repo, err := user.NewRepo(catalog)
Expand All @@ -49,13 +56,20 @@ func TestGetParamsE2E(t *testing.T) {

repo = repo.WithLogger(logger)

cacheDir, err := os.MkdirTemp("", "joy-cache-*")
require.NoError(t, err)

t.Logf("cache dir: %s", cacheDir)

handler := Handler(HandlerParams{
pluginToken: "test-token",
logger: logger,
repo: repo,
generator: &generator.Generator{
CacheRoot: cacheDir,
LoadJoyContext: generator.RepoLoader(repo),
Logger: logger,
ChartPuller: generator.ChartPuller{},
},
})

Expand All @@ -79,7 +93,6 @@ func TestGetParamsE2E(t *testing.T) {

var response generator.GetParamsResponse
require.NoError(t, json.Unmarshal(body.Bytes(), &response))

require.Greater(t, len(response.Output.Parameters), 0)

for _, result := range response.Output.Parameters {
Expand All @@ -92,8 +105,12 @@ func TestGetParamsE2E(t *testing.T) {
require.Greater(t, len(logs.Records), 0)
for _, record := range logs.Records {
require.NotEmpty(t, record["level"])
require.NotEqual(t, "error", record["level"])
require.NotEqualf(t, "error", record["level"], "unexpected error log: %+v", record)
}

entries, err := os.ReadDir(cacheDir)
require.NoError(t, err)
require.Greater(t, len(entries), 0)
}

type TestLogOutputs struct {
Expand Down
26 changes: 26 additions & 0 deletions cmd/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ type Config struct {

Catalog github.RepoMetadata

CacheRoot string

Google struct {
Repository string
CredentialsFilePath string
RawCredentials []byte
}

Github struct {
User github.User
App github.App
Expand All @@ -27,19 +35,37 @@ type Config struct {
func GetConfig() Config {
var cfg Config

home, err := os.UserHomeDir()
if err != nil {
panic(err)
}

conf.Var(conf.Environ, &cfg.CacheRoot, "CACHE_ROOT", conf.Default(filepath.Join(home, ".cache", "joy")))
conf.Var(conf.Environ, &cfg.Port, "PORT", conf.Default(":8080"))
conf.Var(conf.Environ, &cfg.GracePeriod, "GRACE_PERIOD", conf.Default(10*time.Second))
conf.Var(conf.Environ, &cfg.PluginToken, "PLUGIN_TOKEN")

conf.Var(conf.Environ, &cfg.Catalog.URL, "CATALOG_URL")
conf.Var(conf.Environ, &cfg.Catalog.Path, "CATALOG_DIR", conf.Default(filepath.Join(os.TempDir(), "catalog")))
conf.Var(conf.Environ, &cfg.Catalog.TargetRevision, "CATALOG_REVISION")

conf.Var(conf.Environ, &cfg.Github.User.Token, "GH_TOKEN")
conf.Var(conf.Environ, &cfg.Github.User.Name, "GH_USER")
conf.Var(conf.Environ, &cfg.Github.App.ID, "GH_APP_ID")
conf.Var(conf.Environ, &cfg.Github.App.InstallationID, "GH_APP_INSTALLATION_ID")
conf.Var(conf.Environ, &cfg.Github.App.PrivateKeyPath, "GH_APP_PRIVATE_KEY_PATH")

conf.Var(conf.Environ, &cfg.Google.CredentialsFilePath, "GOOGLE_CREDENTIALS_FILE")
conf.Var(conf.Environ, &cfg.Google.Repository, "GOOGLE_ARTIFACT_REPOSITORY")

conf.Environ.MustParse()

if path := cfg.Google.CredentialsFilePath; path != "" {
fs := conf.MakeParser(conf.FileSystem(conf.FileSystemOptions{}))
defer fs.MustParse()

conf.Var(fs, &cfg.Google.RawCredentials, path, conf.Required[[]byte](true))
}

return cfg
}
Loading

0 comments on commit 124bf39

Please sign in to comment.