Skip to content

Commit

Permalink
review: incorporated some review comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ramantehlan committed Jun 5, 2024
1 parent 7c2b89e commit 51970f1
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 21 deletions.
3 changes: 3 additions & 0 deletions operator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func main() {
consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
zapLogger := uberZap.New(core)

controller.INITDirectory(zapLogger)

if err = (&controller.ElastiServiceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down
2 changes: 1 addition & 1 deletion operator/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resources:
- ../crd
- ../rbac
- ../manager
- ../resolver
#- ../resolver
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
Expand Down
4 changes: 2 additions & 2 deletions operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: controller
newTag: latest
newName: localhost:5001/elasti-operator
newTag: v1alpha1
4 changes: 2 additions & 2 deletions operator/config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ resources:
# runtime. Be sure to update RoleBinding and ClusterRoleBinding
# subjects if changing service account names.
- service_account.yaml
- role_EDS.yaml
- role_EDS_binding.yaml
- role_additional.yaml
- role_additional_binding.yaml
- role_apiserver.yaml
- role_apiserver_binding.yaml
- role.yaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: EDS-access
name: additional-access
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch"]
verbs: ["get", "list", "watch", "update", "patch"]
- apiGroups: ["discovery.k8s.io"]
resources: ["endpointslices"]
verbs: ["get", "list", "watch", "update", "patch", "delete", "create"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ metadata:
labels:
app.kubernetes.io/name: elasti-operator
app.kubernetes.io/managed-by: kustomize
name: EDS-access-binding
name: additional-access-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: EDS-access
name: additional-access
subjects:
- kind: ServiceAccount
name: elasti-operator-controller-manager
Expand Down
15 changes: 12 additions & 3 deletions operator/internal/controller/directory.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package controller

import "sync"
import (
"sync"

"go.uber.org/zap"
)

type Directory struct {
Services sync.Map
Logger *zap.Logger
}

type CRDDetails struct {
Expand All @@ -15,9 +20,11 @@ var ServiceDirectory *Directory

var serviceDirectoryOnce sync.Once

func NewDirectory() {
func INITDirectory(logger *zap.Logger) {
serviceDirectoryOnce.Do(func() {
ServiceDirectory = &Directory{}
ServiceDirectory = &Directory{
Logger: logger,
}
})
}

Expand All @@ -28,7 +35,9 @@ func (d *Directory) AddService(serviceName string, crdDetails *CRDDetails) {
func (d *Directory) GetService(serviceName string) (*CRDDetails, bool) {
value, ok := d.Services.Load(serviceName)
if !ok {
d.Logger.Error("Service not found in directory", zap.String("service", serviceName))
return nil, false
}
d.Logger.Info("Service found in directory", zap.String("service", serviceName))
return value.(*CRDDetails), true
}
7 changes: 5 additions & 2 deletions operator/internal/controller/elastiServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ func (r *ElastiServiceReconciler) resolverReqHandler(w http.ResponseWriter, req
return
}
w.Write(jsonResponse)
// TODOs: The deployment name should be dynamic
depl, found := ServiceDirectory.GetService(body.Svc)
if !found {
r.Logger.Error("Failed to get CRD details from directory", zap.String("component", "elastiServer"))
}
namespace := types.NamespacedName{
Name: "target",
Name: depl.DeploymentName,
Namespace: body.Namespace,
}
r.compareAndScaleDeployment(ctx, namespace)
Expand Down
6 changes: 6 additions & 0 deletions operator/internal/controller/elastiservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (r *ElastiServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
r.Logger.Error("Failed to get ElastiService in Reconcile", zap.Error(esErr))
return res, esErr
}

ServiceDirectory.AddService(es.Spec.Service, &CRDDetails{
CRDName: es.Name,
DeploymentName: es.Spec.DeploymentName,
})

if !es.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(es, v1alpha1.ElastiServiceFinalizer) {
r.Logger.Info("ElastiService is being deleted", zap.String("name", es.Name), zap.Any("deletionTimestamp", es.ObjectMeta.DeletionTimestamp))
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func GetPrivateSerivceName(publicSVCName string) string {
hash := sha256.New()
hash.Write([]byte(publicSVCName))
hashed := hex.EncodeToString(hash.Sum(nil))
return publicSVCName + "-" + string(hashed)[:8] + "-pvt"
return publicSVCName + "-pvt" + "-" + string(hashed)[:10] + "-" + string(hashed)[11:16]
}
2 changes: 1 addition & 1 deletion resolver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ run-resolver: ## Run resolver locally

.PHONY: docker-build-resolver
docker-build-resolver: ## Build docker image for the resolver
docker build -t ${IMG} -f ./build/resolver/Dockerfile .
docker build -t ${IMG} -f ./build/resolver/Dockerfile ../

.PHONY: docker-publish-resolver
docker-publish-resolver: ## Publish docker image for the resolver
Expand Down
9 changes: 5 additions & 4 deletions resolver/build/resolver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM golang:1.22.2-alpine3.19 AS builder
FROM golang:1.22 AS builder
WORKDIR /app
COPY go.sum .
COPY go.mod .
COPY ../pkg ../pkg
COPY ./resolver/go.sum .
COPY ./resolver/go.mod .
RUN go mod tidy
COPY ../../ .
RUN go build -o resolver ./cmd/resolver
RUN go build -o resolver ./resolver/cmd/resolver
# Break
FROM alpine:latest
WORKDIR /root/
Expand Down
3 changes: 2 additions & 1 deletion resolver/cmd/resolver/hostManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/truefoundry/elasti/pkg/utils"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -50,7 +51,7 @@ func (hm *hostManager) GetHost(req *http.Request) (*Host, error) {
}
host, ok := hm.hosts.Load(sourceService)
if !ok {
targetService = sourceService + "-pvt"
targetService = utils.GetPrivateSerivceName(sourceService)
sourceHost = hm.removeTrailingWildcardIfNeeded(sourceHost)
sourceHost = hm.addHTTPIfNeeded(sourceHost)
targetHost = hm.replaceServiceName(sourceHost, targetService)
Expand Down
3 changes: 3 additions & 0 deletions resolver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ require (
golang.org/x/net v0.24.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
github.com/truefoundry/elasti/pkg v0.0.0
)

replace github.com/truefoundry/elasti/pkg v0.0.0 => ../pkg

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand Down

0 comments on commit 51970f1

Please sign in to comment.