Skip to content

Commit

Permalink
feat: implement client to send FinalizeCleanup requests to spectro-cl…
Browse files Browse the repository at this point in the history
…eanup
  • Loading branch information
ahmad-ibra committed Dec 16, 2023
1 parent 1e340c9 commit 2c2e0f0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
32 changes: 27 additions & 5 deletions chart/validator/templates/cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,32 @@ metadata:
"helm.sh/hook": pre-delete
spec:
template:
metadata:
labels:
app: validator-cleanup-job
spec:
restartPolicy: Never
serviceAccountName: spectro-cleanup
containers:
- name: validator-cleanup
image: gcr.io/spectro-images-public/release/spectro-cleanup:1.1.0
image: gcr.io/spectro-images-public/release/spectro-cleanup:1.2.0
command: ["/cleanup"]
env:
- name: CLEANUP_DELAY_SECONDS
value: {{ mul 10 (max 1 (len (required ".Values.plugins is required!" .Values.plugins))) | quote }}
value: {{ mul 15 (max 1 (len (required ".Values.plugins is required!" .Values.plugins))) | quote }}
{{- if .Values.cleanup.grpcServerEnabled }}
- name: CLEANUP_GRPC_SERVER_ENABLED
value: "true"
- name: CLEANUP_GRPC_SERVER_PORT
value: {{ required ".Values.cleanup.port is required!" .Values.cleanup.port | toString | quote }}
{{- end }}
resources:
requests:
cpu: "10m"
memory: "25Mi"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
memory: "100Mi"
volumeMounts:
- name: validator-cleanup-config
mountPath: /tmp/spectro-cleanup
Expand All @@ -112,4 +121,17 @@ spec:
name: validator-cleanup-config
items:
- key: resource-config.json
path: resource-config.json
path: resource-config.json
---
{{- if .Values.cleanup.grpcServerEnabled }}
apiVersion: v1
kind: Service
metadata:
name: {{ required ".Values.cleanup.hostname is required!" .Values.cleanup.hostname }}
spec:
selector:
app: validator-cleanup-job
ports:
- protocol: TCP
port: {{ required ".Values.cleanup.port is required!" .Values.cleanup.port }}
{{- end }}
10 changes: 9 additions & 1 deletion chart/validator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ spec:
{{- end }}
- name: SINK_WEBHOOK_TIMEOUT_SECONDS
value: {{ quote .Values.controllerManager.manager.sinkWebhookTimeout }}
{{- if .Values.cleanup.grpcServerEnabled }}
- name: CLEANUP_GRPC_ENABLED
value: "true"
- name: CLEANUP_GRPC_SERVER_HOST
value: {{ required ".Values.cleanup.hostname is required!" .Values.cleanup.hostname | toString }}
- name: CLEANUP_GRPC_SERVER_PORT
value: {{ required ".Values.cleanup.port is required!" .Values.cleanup.port | toString | quote }}
{{- end }}
image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag | default .Chart.AppVersion }}
livenessProbe:
httpGet:
Expand Down Expand Up @@ -109,4 +117,4 @@ spec:
secretName: {{ required ".Values.proxy.secretName is required!" .Values.proxy.secretName }}
- name: certs
emptyDir: {}
{{- end}}
{{- end}}
9 changes: 7 additions & 2 deletions chart/validator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ controllerManager:
drop:
- ALL
image:
repository: quay.io/spectrocloud-labs/validator
tag: v0.0.28 # x-release-please-version
repository: ahmadibraspectrocloud/validator
tag: latest # x-release-please-version
resources:
limits:
cpu: 500m
Expand Down Expand Up @@ -97,6 +97,11 @@ sink: {}
# WARNING: the existing secret must match the format used in sink-secret.yaml
# createSecret: true

cleanup:
grpcServerEnabled: true
hostname: validator-cleanup-service
port: 3006

# Validation plugin charts
plugins:
- chart:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module github.com/spectrocloud-labs/validator
go 1.20

require (
buf.build/gen/go/spectrocloud/spectro-cleanup/connectrpc/go v1.13.0-20231213011348-5645e27c876a.1
buf.build/gen/go/spectrocloud/spectro-cleanup/protocolbuffers/go v1.31.0-20231213011348-5645e27c876a.2
connectrpc.com/connect v1.13.0
github.com/go-logr/logr v1.3.0
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
buf.build/gen/go/spectrocloud/spectro-cleanup/connectrpc/go v1.13.0-20231213011348-5645e27c876a.1 h1:flYv+oyV4sGFTc8UrA6bSxCkGE7mvSUyydrCiXk5s7A=
buf.build/gen/go/spectrocloud/spectro-cleanup/connectrpc/go v1.13.0-20231213011348-5645e27c876a.1/go.mod h1:pNAXVmeA3b2y1Hi/j2poNtPTT0Bvo2LgRK7FThfG0oc=
buf.build/gen/go/spectrocloud/spectro-cleanup/protocolbuffers/go v1.31.0-20231213011348-5645e27c876a.2 h1:ub8BpTL/wC0JVAjnfKzSdqu3xjJBFn4ndVPGu0u3KHU=
buf.build/gen/go/spectrocloud/spectro-cleanup/protocolbuffers/go v1.31.0-20231213011348-5645e27c876a.2/go.mod h1:629c8Zj/8OXoFZZfqhsjqXJ0MIIVuonsR0x8/Nngi+U=
connectrpc.com/connect v1.13.0 h1:lGs5maZZzWOOD+PFFiOt5OncKmMsk9ZdPwpy5jcmaYg=
connectrpc.com/connect v1.13.0/go.mod h1:uHAFHtYgeSZJxXrkN1IunDpKghnTXhYbVh0wW4StPW0=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
36 changes: 35 additions & 1 deletion internal/controller/validatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"

connect "connectrpc.com/connect"
"github.com/go-logr/logr"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
Expand All @@ -35,6 +38,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"buf.build/gen/go/spectrocloud/spectro-cleanup/connectrpc/go/cleanup/v1/cleanupv1connect"
cleanv1 "buf.build/gen/go/spectrocloud/spectro-cleanup/protocolbuffers/go/cleanup/v1"
v1alpha1 "github.com/spectrocloud-labs/validator/api/v1alpha1"
"github.com/spectrocloud-labs/validator/pkg/helm"
)
Expand Down Expand Up @@ -100,7 +105,11 @@ func (r *ValidatorConfigReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if err := r.deletePlugins(ctx, vc); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, removeFinalizer(ctx, r.Client, vc, CleanupFinalizer)

err = removeFinalizer(ctx, r.Client, vc, CleanupFinalizer)
r.emitFinalizeCleanup()

return ctrl.Result{}, err
}

// TODO: implement a proper patcher to avoid this hacky approach with global vars
Expand Down Expand Up @@ -373,3 +382,28 @@ func isConditionTrue(vc *v1alpha1.ValidatorConfig, chartName string, conditionTy
}
return vc.Status.Conditions[idx], vc.Status.Conditions[idx].Status == corev1.ConditionTrue
}

func (r *ValidatorConfigReconciler) emitFinalizeCleanup() {
grpcEnabled := os.Getenv("CLEANUP_GRPC_ENABLED")
if grpcEnabled != "true" {
r.Log.V(0).Info("Cleanup job gRPC server is not enabled")
return
}

host := os.Getenv("CLEANUP_GRPC_SERVER_HOST")
port := os.Getenv("CLEANUP_GRPC_SERVER_PORT")

url := fmt.Sprintf("https://%s:%s", host, port)

client := cleanupv1connect.NewCleanupServiceClient(
http.DefaultClient,
url,
)
_, err := client.FinalizeCleanup(
context.Background(),
connect.NewRequest(&cleanv1.FinalizeCleanupRequest{}),
)
if err != nil {
r.Log.Error(err, "FinalizeCleanup request failed", "url", url)
}
}

0 comments on commit 2c2e0f0

Please sign in to comment.