diff --git a/go/apps/ctrl/services/deployment/create_deployment.go b/go/apps/ctrl/services/deployment/create_deployment.go index 72d499a743..f8a25c393a 100644 --- a/go/apps/ctrl/services/deployment/create_deployment.go +++ b/go/apps/ctrl/services/deployment/create_deployment.go @@ -142,6 +142,7 @@ func (s *Service) CreateDeployment( "memory": 2048 }`), OpenapiSpec: sql.NullString{String: "", Valid: false}, + GatewayConfig: env.GatewayConfig, Status: db.DeploymentsStatusPending, CreatedAt: now, UpdatedAt: sql.NullInt64{Int64: now, Valid: true}, diff --git a/go/apps/ctrl/workflows/deploy/deploy_handler.go b/go/apps/ctrl/workflows/deploy/deploy_handler.go index 9f4e8a5eac..ed6094198f 100644 --- a/go/apps/ctrl/workflows/deploy/deploy_handler.go +++ b/go/apps/ctrl/workflows/deploy/deploy_handler.go @@ -208,10 +208,9 @@ func (w *Workflow) Deploy(ctx restate.ObjectContext, req *hydrav1.DeployRequest) } - } - - if allReady { - return resp.Msg.GetInstances(), nil + if allReady { + return resp.Msg.GetInstances(), nil + } } // next loop diff --git a/go/apps/ctrl/workflows/project/create.go b/go/apps/ctrl/workflows/project/create.go index e24234bd6b..423e4ed684 100644 --- a/go/apps/ctrl/workflows/project/create.go +++ b/go/apps/ctrl/workflows/project/create.go @@ -83,13 +83,14 @@ func (s *Service) CreateProject(ctx restate.ObjectContext, req *hydrav1.CreatePr _, err = restate.Run(ctx, func(runCtx restate.RunContext) (restate.Void, error) { return restate.Void{}, db.Query.InsertEnvironment(runCtx, s.db.RW(), db.InsertEnvironmentParams{ - ID: environmentID, - WorkspaceID: workspace.ID, - ProjectID: projectID, - Slug: env.Slug, - Description: env.Description, - CreatedAt: time.Now().UnixMilli(), - UpdatedAt: sql.NullInt64{Valid: false, Int64: 0}, + ID: environmentID, + WorkspaceID: workspace.ID, + ProjectID: projectID, + Slug: env.Slug, + Description: env.Description, + CreatedAt: time.Now().UnixMilli(), + UpdatedAt: sql.NullInt64{Valid: false, Int64: 0}, + GatewayConfig: []byte(""), }) }, restate.WithName("insert environment")) diff --git a/go/apps/krane/backend/kubernetes/deployment_create.go b/go/apps/krane/backend/kubernetes/deployment_create.go index 1c27c70c6a..5b1421485a 100644 --- a/go/apps/krane/backend/kubernetes/deployment_create.go +++ b/go/apps/krane/backend/kubernetes/deployment_create.go @@ -68,13 +68,15 @@ import ( // and ready for traffic after creation. func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1.CreateDeploymentRequest]) (*connect.Response[kranev1.CreateDeploymentResponse], error) { k8sDeploymentID := safeIDForK8s(req.Msg.GetDeployment().GetDeploymentId()) + namespace := safeIDForK8s(req.Msg.GetDeployment().GetNamespace()) + k.logger.Info("creating deployment", - "namespace", req.Msg.GetDeployment().GetNamespace(), + "namespace", namespace, "deployment_id", k8sDeploymentID, ) service, err := k.clientset.CoreV1(). - Services(req.Msg.GetDeployment().GetNamespace()). + Services(namespace). Create(ctx, // This implementation of using stateful sets is very likely not what we want to // use in v1. @@ -87,7 +89,7 @@ func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1 &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: k8sDeploymentID, - Namespace: req.Msg.GetDeployment().GetNamespace(), + Namespace: namespace, Labels: map[string]string{ "unkey.deployment.id": k8sDeploymentID, "unkey.managed.by": "krane", @@ -124,12 +126,12 @@ func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1 return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to create service: %w", err)) } - sfs, err := k.clientset.AppsV1().StatefulSets(req.Msg.GetDeployment().GetNamespace()).Create(ctx, + sfs, err := k.clientset.AppsV1().StatefulSets(namespace).Create(ctx, //nolint: exhaustruct &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ Name: k8sDeploymentID, - Namespace: req.Msg.GetDeployment().GetNamespace(), + Namespace: namespace, Labels: map[string]string{ "unkey.deployment.id": k8sDeploymentID, "unkey.managed.by": "krane", @@ -154,6 +156,7 @@ func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1 Annotations: map[string]string{}, }, Spec: corev1.PodSpec{ + ImagePullSecrets: func() []corev1.LocalObjectReference { // Only add imagePullSecrets if using Depot registry if strings.HasPrefix(req.Msg.GetDeployment().GetImage(), "registry.depot.dev/") { @@ -203,7 +206,7 @@ func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1 k.logger.Info("Deleting service, because deployment creation failed") // Delete service // nolint: exhaustruct - if rollbackErr := k.clientset.CoreV1().Services(req.Msg.GetDeployment().GetNamespace()).Delete(ctx, service.Name, metav1.DeleteOptions{}); rollbackErr != nil { + if rollbackErr := k.clientset.CoreV1().Services(namespace).Delete(ctx, service.Name, metav1.DeleteOptions{}); rollbackErr != nil { k.logger.Error("Failed to delete service", "error", rollbackErr.Error()) } @@ -221,7 +224,7 @@ func (k *k8s) CreateDeployment(ctx context.Context, req *connect.Request[kranev1 }, } //nolint:exhaustruct - _, err = k.clientset.CoreV1().Services(req.Msg.GetDeployment().GetNamespace()).Update(ctx, service, metav1.UpdateOptions{}) + _, err = k.clientset.CoreV1().Services(namespace).Update(ctx, service, metav1.UpdateOptions{}) if err != nil { return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to update deployment: %w", err)) } diff --git a/go/apps/krane/backend/kubernetes/deployment_delete.go b/go/apps/krane/backend/kubernetes/deployment_delete.go index e6260dbb0d..f90c9f482f 100644 --- a/go/apps/krane/backend/kubernetes/deployment_delete.go +++ b/go/apps/krane/backend/kubernetes/deployment_delete.go @@ -20,14 +20,15 @@ import ( // ensure associated pods and other resources are properly terminated. func (k *k8s) DeleteDeployment(ctx context.Context, req *connect.Request[kranev1.DeleteDeploymentRequest]) (*connect.Response[kranev1.DeleteDeploymentResponse], error) { k8sDeploymentID := strings.ReplaceAll(req.Msg.GetDeploymentId(), "_", "-") + namespace := safeIDForK8s(req.Msg.GetNamespace()) k.logger.Info("deleting deployment", - "namespace", req.Msg.GetNamespace(), + "namespace", namespace, "deployment_id", k8sDeploymentID, ) //nolint: exhaustruct - err := k.clientset.CoreV1().Services(req.Msg.GetNamespace()).Delete(ctx, k8sDeploymentID, metav1.DeleteOptions{ + err := k.clientset.CoreV1().Services(namespace).Delete(ctx, k8sDeploymentID, metav1.DeleteOptions{ PropagationPolicy: ptr.P(metav1.DeletePropagationBackground), }) if err != nil && !apierrors.IsNotFound(err) { @@ -35,7 +36,7 @@ func (k *k8s) DeleteDeployment(ctx context.Context, req *connect.Request[kranev1 } //nolint: exhaustruct - err = k.clientset.AppsV1().StatefulSets(req.Msg.GetNamespace()).Delete(ctx, k8sDeploymentID, metav1.DeleteOptions{ + err = k.clientset.AppsV1().StatefulSets(namespace).Delete(ctx, k8sDeploymentID, metav1.DeleteOptions{ PropagationPolicy: ptr.P(metav1.DeletePropagationBackground), }) if err != nil && !apierrors.IsNotFound(err) { diff --git a/go/apps/krane/backend/kubernetes/deployment_get.go b/go/apps/krane/backend/kubernetes/deployment_get.go index 14be48e83a..53456303de 100644 --- a/go/apps/krane/backend/kubernetes/deployment_get.go +++ b/go/apps/krane/backend/kubernetes/deployment_get.go @@ -28,12 +28,13 @@ func (k *k8s) GetDeployment(ctx context.Context, req *connect.Request[kranev1.Ge } k8sDeploymentID := safeIDForK8s(req.Msg.GetDeploymentId()) + namespace := safeIDForK8s(req.Msg.GetNamespace()) k.logger.Info("getting deployment", "deployment_id", k8sDeploymentID) // Get the Job by name (deployment_id) // nolint: exhaustruct - sfs, err := k.clientset.AppsV1().StatefulSets(req.Msg.GetNamespace()).Get(ctx, k8sDeploymentID, metav1.GetOptions{}) + sfs, err := k.clientset.AppsV1().StatefulSets(namespace).Get(ctx, k8sDeploymentID, metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("deployment not found: %s", k8sDeploymentID)) @@ -57,7 +58,7 @@ func (k *k8s) GetDeployment(ctx context.Context, req *connect.Request[kranev1.Ge // Get the service to retrieve port info // nolint: exhaustruct - service, err := k.clientset.CoreV1().Services(req.Msg.GetNamespace()).Get(ctx, k8sDeploymentID, metav1.GetOptions{}) + service, err := k.clientset.CoreV1().Services(namespace).Get(ctx, k8sDeploymentID, metav1.GetOptions{}) if err != nil { return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("could not load service: %s", k8sDeploymentID)) } @@ -73,7 +74,7 @@ func (k *k8s) GetDeployment(ctx context.Context, req *connect.Request[kranev1.Ge }) //nolint: exhaustruct - pods, err := k.clientset.CoreV1().Pods(req.Msg.GetNamespace()).List(ctx, metav1.ListOptions{ + pods, err := k.clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ LabelSelector: labelSelector, }) if err != nil { diff --git a/go/apps/krane/backend/kubernetes/eviction.go b/go/apps/krane/backend/kubernetes/eviction.go deleted file mode 100644 index ad55d880c3..0000000000 --- a/go/apps/krane/backend/kubernetes/eviction.go +++ /dev/null @@ -1,57 +0,0 @@ -package kubernetes - -import ( - "context" - "fmt" - "time" - - "github.com/unkeyed/unkey/go/pkg/ptr" - "github.com/unkeyed/unkey/go/pkg/repeat" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// autoEvictDeployments removes deployments that are older than the specified TTL. -// -// This function implements automatic cleanup of old deployments to prevent -// resource accumulation in development and testing environments. It runs -// continuously in a background goroutine, scanning for deployments that -// exceed the configured time-to-live threshold. -func (k *k8s) autoEvictDeployments(ttl time.Duration) { - k.logger.Info(fmt.Sprintf("Krane setup to auto-evict deployments after %s", ttl.String())) - repeat.Every(time.Minute, func() { - ctx := context.Background() - k.logger.Info("evicting old deployments") - - //nolint: exhaustruct - deployments, err := k.clientset.AppsV1().StatefulSets("unkey").List(ctx, metav1.ListOptions{ - LabelSelector: "unkey.managed.by=krane", - }) - if err != nil { - k.logger.Error("failed to list deployments", - "error", err.Error(), - ) - - return - } - - for _, deployment := range deployments.Items { - if time.Since(deployment.GetCreationTimestamp().Time) > ttl { - k.logger.Info("deployment is old and will be deleted", - "name", deployment.Name, - ) - - //nolint: exhaustruct - err = k.clientset.AppsV1().StatefulSets("unkey").Delete(ctx, deployment.Name, metav1.DeleteOptions{ - PropagationPolicy: ptr.P(metav1.DeletePropagationBackground), - }) - if err != nil { - k.logger.Error("failed to delete deployment", - "error", err.Error(), - "uid", string(deployment.GetUID()), - "name", deployment.Name, - ) - } - } - } - }) -} diff --git a/go/apps/krane/backend/kubernetes/gateway_create.go b/go/apps/krane/backend/kubernetes/gateway_create.go index b8f99a7500..689ec43c09 100644 --- a/go/apps/krane/backend/kubernetes/gateway_create.go +++ b/go/apps/krane/backend/kubernetes/gateway_create.go @@ -9,6 +9,7 @@ import ( "github.com/unkeyed/unkey/go/pkg/ptr" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" @@ -16,18 +17,44 @@ import ( func (k *k8s) CreateGateway(ctx context.Context, req *connect.Request[kranev1.CreateGatewayRequest]) (*connect.Response[kranev1.CreateGatewayResponse], error) { k8sGatewayID := safeIDForK8s(req.Msg.GetGateway().GetGatewayId()) + namespace := safeIDForK8s(req.Msg.GetGateway().GetNamespace()) k.logger.Info("creating deployment", - "namespace", req.Msg.GetGateway().GetNamespace(), + "namespace", namespace, "deployment_id", k8sGatewayID, ) + // Ensure namespace exists + // It's not ideal to do it here, but it's the best I can do for now without rebuilding the entire workspace creation system. + _, err := k.clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) + if err != nil { + // If namespace doesn't exist, create it + if errors.IsNotFound(err) { + k.logger.Info("namespace not found, creating it", "namespace", namespace) + _, createErr := k.clientset.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: map[string]string{ + "unkey.managed.by": "krane", + }, + }, + }, metav1.CreateOptions{}) + if createErr != nil { + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to create namespace: %w", createErr)) + } + k.logger.Info("namespace created successfully", "namespace", namespace) + } else { + // Some other error occurred while getting the namespace + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to get namespace: %w", err)) + } + } + // Create Deployment - deployment, err := k.clientset.AppsV1().Deployments(req.Msg.GetGateway().GetNamespace()).Create(ctx, + deployment, err := k.clientset.AppsV1().Deployments(namespace).Create(ctx, //nolint: exhaustruct &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: k8sGatewayID, - Namespace: req.Msg.GetGateway().GetNamespace(), + Namespace: namespace, Labels: map[string]string{ "unkey.gateway.id": k8sGatewayID, "unkey.managed.by": "krane", @@ -82,12 +109,12 @@ func (k *k8s) CreateGateway(ctx context.Context, req *connect.Request[kranev1.Cr }, }, metav1.CreateOptions{}) if err != nil { - return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to create deployment: %w", err)) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to create gateway: %w", err)) } // Create Service with owner reference to the Deployment service, err := k.clientset.CoreV1(). - Services(req.Msg.GetGateway().GetNamespace()). + Services(namespace). Create(ctx, // This implementation uses Deployments with ClusterIP services // for better scalability while maintaining internal accessibility via service name. @@ -96,7 +123,7 @@ func (k *k8s) CreateGateway(ctx context.Context, req *connect.Request[kranev1.Cr &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: k8sGatewayID, - Namespace: req.Msg.GetGateway().GetNamespace(), + Namespace: namespace, Labels: map[string]string{ "unkey.gateway.id": k8sGatewayID, "unkey.managed.by": "krane", @@ -138,7 +165,7 @@ func (k *k8s) CreateGateway(ctx context.Context, req *connect.Request[kranev1.Cr k.logger.Info("Deleting deployment, because service creation failed") // Delete deployment // nolint: exhaustruct - if rollbackErr := k.clientset.AppsV1().Deployments(req.Msg.GetGateway().GetNamespace()).Delete(ctx, deployment.Name, metav1.DeleteOptions{}); rollbackErr != nil { + if rollbackErr := k.clientset.AppsV1().Deployments(namespace).Delete(ctx, deployment.Name, metav1.DeleteOptions{}); rollbackErr != nil { k.logger.Error("Failed to delete deployment", "error", rollbackErr.Error()) } diff --git a/go/apps/krane/backend/kubernetes/gateway_delete.go b/go/apps/krane/backend/kubernetes/gateway_delete.go index 3798977012..95907d09d3 100644 --- a/go/apps/krane/backend/kubernetes/gateway_delete.go +++ b/go/apps/krane/backend/kubernetes/gateway_delete.go @@ -15,8 +15,10 @@ import ( func (k *k8s) DeleteGateway(ctx context.Context, req *connect.Request[kranev1.DeleteGatewayRequest]) (*connect.Response[kranev1.DeleteGatewayResponse], error) { k8sGatewayID := strings.ReplaceAll(req.Msg.GetGatewayId(), "_", "-") + namespace := safeIDForK8s(req.Msg.GetGatewayId()) + k.logger.Info("deleting deployment", - "namespace", req.Msg.GetNamespace(), + "namespace", namespace, "gateway_id", k8sGatewayID, ) diff --git a/go/apps/krane/backend/kubernetes/gateway_get.go b/go/apps/krane/backend/kubernetes/gateway_get.go index 456770bed6..d73b6e0719 100644 --- a/go/apps/krane/backend/kubernetes/gateway_get.go +++ b/go/apps/krane/backend/kubernetes/gateway_get.go @@ -28,12 +28,13 @@ func (k *k8s) GetGateway(ctx context.Context, req *connect.Request[kranev1.GetGa } k8sgatewayID := safeIDForK8s(req.Msg.GetGatewayId()) + namespace := safeIDForK8s(req.Msg.GetNamespace()) k.logger.Info("getting gateway", "gateway_id", k8sgatewayID) // Get the deployment by name (gateway_id) // nolint: exhaustruct - deployment, err := k.clientset.AppsV1().Deployments(req.Msg.GetNamespace()).Get(ctx, k8sgatewayID, metav1.GetOptions{}) + deployment, err := k.clientset.AppsV1().Deployments(namespace).Get(ctx, k8sgatewayID, metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("deployment not found: %s", k8sgatewayID)) @@ -56,16 +57,16 @@ func (k *k8s) GetGateway(ctx context.Context, req *connect.Request[kranev1.GetGa } // Get the service to retrieve port info - service, err := k.clientset.CoreV1().Services(req.Msg.GetNamespace()).Get(ctx, k8sgatewayID, metav1.GetOptions{}) + service, err := k.clientset.CoreV1().Services(namespace).Get(ctx, k8sgatewayID, metav1.GetOptions{}) if err != nil { - return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("could not load service %s: %w", k8sgatewayID, err)) + return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("could not load service: %s", k8sgatewayID)) } var port int32 = 8080 // default if len(service.Spec.Ports) > 0 { port = service.Spec.Ports[0].Port } - pods, err := k.clientset.CoreV1().Pods(req.Msg.GetNamespace()).List(ctx, metav1.ListOptions{ + pods, err := k.clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{ MatchExpressions: nil, MatchLabels: deployment.Spec.Selector.MatchLabels, diff --git a/go/apps/krane/backend/kubernetes/service.go b/go/apps/krane/backend/kubernetes/service.go index 04915b582e..f1231e1336 100644 --- a/go/apps/krane/backend/kubernetes/service.go +++ b/go/apps/krane/backend/kubernetes/service.go @@ -54,9 +54,5 @@ func New(cfg Config) (*k8s, error) { clientset: clientset, } - if cfg.DeploymentEvictionTTL > 0 { - k.autoEvictDeployments(cfg.DeploymentEvictionTTL) - } - return k, nil } diff --git a/go/k8s/manifests/ctrl.yaml b/go/k8s/manifests/ctrl.yaml index d0cb293552..53fe2d9990 100644 --- a/go/k8s/manifests/ctrl.yaml +++ b/go/k8s/manifests/ctrl.yaml @@ -17,6 +17,11 @@ spec: app: ctrl spec: serviceAccountName: unkey-serviceaccount + volumes: + - name: docker-socket + hostPath: + path: /var/run/docker.sock + type: Socket containers: - name: ctrl image: unkey-ctrl:latest @@ -59,14 +64,14 @@ spec: - name: UNKEY_VAULT_S3_URL value: "http://s3:3902" - name: UNKEY_VAULT_S3_BUCKET - value: "vault" + value: "acme-vault" - name: UNKEY_VAULT_S3_ACCESS_KEY_ID value: "minio_root_user" - name: UNKEY_VAULT_S3_ACCESS_KEY_SECRET value: "minio_root_password" # Build Configuration - name: UNKEY_BUILD_BACKEND - value: "depot" + value: "depot" # Changed to docker as default for local k8s - name: UNKEY_BUILD_PLATFORM value: "linux/amd64" # Build S3 Storage (from depot-credentials secret) @@ -139,7 +144,22 @@ spec: - name: UNKEY_API_KEY value: "your-local-dev-key" + # ClickHouse Configuration + - name: UNKEY_CLICKHOUSE_URL + value: "clickhouse://default:password@clickhouse:9000?secure=false&skip_verify=true" + + # Docker Configuration (for Docker build backend) + - name: UNKEY_DOCKER_SOCKET + value: "/var/run/docker.sock" + + # Deployment Configuration + - name: UNKEY_DEPLOYMENT_EVICTION_TTL + value: "10m" + command: ["/unkey", "run", "ctrl"] + volumeMounts: + - name: docker-socket + mountPath: /var/run/docker.sock initContainers: - name: wait-for-dependencies image: busybox:1.36 diff --git a/go/k8s/manifests/rbac.yaml b/go/k8s/manifests/rbac.yaml index 4626aca689..1242ef51e0 100644 --- a/go/k8s/manifests/rbac.yaml +++ b/go/k8s/manifests/rbac.yaml @@ -17,6 +17,11 @@ metadata: app: unkey component: krane rules: + # Namespace management + - apiGroups: [""] + resources: ["namespaces"] + verbs: ["get", "list", "watch", "create", "update", "patch"] + # Pod management for VMs - apiGroups: [""] resources: ["pods"] @@ -27,9 +32,9 @@ rules: resources: ["services"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] - # Job management for VM workloads + # Job management for user workloads - apiGroups: ["apps"] - resources: ["statefulsets"] + resources: ["statefulsets", "deployments"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] # Events access for debugging diff --git a/go/pkg/db/bulk_deployment_insert.sql_generated.go b/go/pkg/db/bulk_deployment_insert.sql_generated.go index e22548ad03..771e97d7e5 100644 --- a/go/pkg/db/bulk_deployment_insert.sql_generated.go +++ b/go/pkg/db/bulk_deployment_insert.sql_generated.go @@ -9,7 +9,7 @@ import ( ) // bulkInsertDeployment is the base query for bulk insert -const bulkInsertDeployment = `INSERT INTO ` + "`" + `deployments` + "`" + ` ( id, workspace_id, project_id, environment_id, git_commit_sha, git_branch, runtime_config, gateway_config, git_commit_message, git_commit_author_handle, git_commit_author_avatar_url, git_commit_timestamp, openapi_spec, status, created_at, updated_at ) VALUES %s` +const bulkInsertDeployment = `INSERT INTO ` + "`" + `deployments` + "`" + ` ( id, workspace_id, project_id, environment_id, git_commit_sha, git_branch, runtime_config, gateway_config, git_commit_message, git_commit_author_handle, git_commit_author_avatar_url, git_commit_timestamp, openapi_spec, status, gateway_config, created_at, updated_at ) VALUES %s` // InsertDeployments performs bulk insert in a single query func (q *BulkQueries) InsertDeployments(ctx context.Context, db DBTX, args []InsertDeploymentParams) error { @@ -21,7 +21,7 @@ func (q *BulkQueries) InsertDeployments(ctx context.Context, db DBTX, args []Ins // Build the bulk insert query valueClauses := make([]string, len(args)) for i := range args { - valueClauses[i] = "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" + valueClauses[i] = "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" } bulkQuery := fmt.Sprintf(bulkInsertDeployment, strings.Join(valueClauses, ", ")) @@ -43,6 +43,7 @@ func (q *BulkQueries) InsertDeployments(ctx context.Context, db DBTX, args []Ins allArgs = append(allArgs, arg.GitCommitTimestamp) allArgs = append(allArgs, arg.OpenapiSpec) allArgs = append(allArgs, arg.Status) + allArgs = append(allArgs, arg.GatewayConfig) allArgs = append(allArgs, arg.CreatedAt) allArgs = append(allArgs, arg.UpdatedAt) } diff --git a/go/pkg/db/bulk_environment_insert.sql_generated.go b/go/pkg/db/bulk_environment_insert.sql_generated.go index bf683304d8..74a805e1d9 100644 --- a/go/pkg/db/bulk_environment_insert.sql_generated.go +++ b/go/pkg/db/bulk_environment_insert.sql_generated.go @@ -9,7 +9,7 @@ import ( ) // bulkInsertEnvironment is the base query for bulk insert -const bulkInsertEnvironment = `INSERT INTO environments ( id, workspace_id, project_id, slug, description, created_at, updated_at ) VALUES %s` +const bulkInsertEnvironment = `INSERT INTO environments ( id, workspace_id, project_id, slug, description, created_at, updated_at, gateway_config ) VALUES %s` // InsertEnvironments performs bulk insert in a single query func (q *BulkQueries) InsertEnvironments(ctx context.Context, db DBTX, args []InsertEnvironmentParams) error { @@ -21,7 +21,7 @@ func (q *BulkQueries) InsertEnvironments(ctx context.Context, db DBTX, args []In // Build the bulk insert query valueClauses := make([]string, len(args)) for i := range args { - valueClauses[i] = "( ?, ?, ?, ?, ?, ?, ? )" + valueClauses[i] = "( ?, ?, ?, ?, ?, ?, ?, ? )" } bulkQuery := fmt.Sprintf(bulkInsertEnvironment, strings.Join(valueClauses, ", ")) @@ -36,6 +36,7 @@ func (q *BulkQueries) InsertEnvironments(ctx context.Context, db DBTX, args []In allArgs = append(allArgs, arg.Description) allArgs = append(allArgs, arg.CreatedAt) allArgs = append(allArgs, arg.UpdatedAt) + allArgs = append(allArgs, arg.GatewayConfig) } // Execute the bulk insert diff --git a/go/pkg/db/deployment_insert.sql_generated.go b/go/pkg/db/deployment_insert.sql_generated.go index 4bcb158db2..95e368905a 100644 --- a/go/pkg/db/deployment_insert.sql_generated.go +++ b/go/pkg/db/deployment_insert.sql_generated.go @@ -27,6 +27,7 @@ INSERT INTO ` + "`" + `deployments` + "`" + ` ( git_commit_timestamp, -- Unix epoch milliseconds openapi_spec, status, + gateway_config, created_at, updated_at ) @@ -46,6 +47,7 @@ VALUES ( ?, ?, ?, + ?, ? ) ` @@ -86,6 +88,7 @@ type InsertDeploymentParams struct { // git_commit_timestamp, -- Unix epoch milliseconds // openapi_spec, // status, +// gateway_config, // created_at, // updated_at // ) @@ -105,6 +108,7 @@ type InsertDeploymentParams struct { // ?, // ?, // ?, +// ?, // ? // ) func (q *Queries) InsertDeployment(ctx context.Context, db DBTX, arg InsertDeploymentParams) error { @@ -123,6 +127,7 @@ func (q *Queries) InsertDeployment(ctx context.Context, db DBTX, arg InsertDeplo arg.GitCommitTimestamp, arg.OpenapiSpec, arg.Status, + arg.GatewayConfig, arg.CreatedAt, arg.UpdatedAt, ) diff --git a/go/pkg/db/environment_find_by_project_id_and_slug.sql_generated.go b/go/pkg/db/environment_find_by_project_id_and_slug.sql_generated.go index 337f5791e1..499198a73b 100644 --- a/go/pkg/db/environment_find_by_project_id_and_slug.sql_generated.go +++ b/go/pkg/db/environment_find_by_project_id_and_slug.sql_generated.go @@ -10,10 +10,10 @@ import ( ) const findEnvironmentByProjectIdAndSlug = `-- name: FindEnvironmentByProjectIdAndSlug :one -SELECT id, workspace_id, project_id, slug, description +SELECT id, workspace_id, project_id, slug, description, gateway_config, delete_protection, created_at, updated_at FROM environments -WHERE workspace_id = ? - AND project_id = ? +WHERE workspace_id = ? + AND project_id = ? AND slug = ? ` @@ -23,30 +23,26 @@ type FindEnvironmentByProjectIdAndSlugParams struct { Slug string `db:"slug"` } -type FindEnvironmentByProjectIdAndSlugRow struct { - ID string `db:"id"` - WorkspaceID string `db:"workspace_id"` - ProjectID string `db:"project_id"` - Slug string `db:"slug"` - Description string `db:"description"` -} - // FindEnvironmentByProjectIdAndSlug // -// SELECT id, workspace_id, project_id, slug, description +// SELECT id, workspace_id, project_id, slug, description, gateway_config, delete_protection, created_at, updated_at // FROM environments // WHERE workspace_id = ? // AND project_id = ? // AND slug = ? -func (q *Queries) FindEnvironmentByProjectIdAndSlug(ctx context.Context, db DBTX, arg FindEnvironmentByProjectIdAndSlugParams) (FindEnvironmentByProjectIdAndSlugRow, error) { +func (q *Queries) FindEnvironmentByProjectIdAndSlug(ctx context.Context, db DBTX, arg FindEnvironmentByProjectIdAndSlugParams) (Environment, error) { row := db.QueryRowContext(ctx, findEnvironmentByProjectIdAndSlug, arg.WorkspaceID, arg.ProjectID, arg.Slug) - var i FindEnvironmentByProjectIdAndSlugRow + var i Environment err := row.Scan( &i.ID, &i.WorkspaceID, &i.ProjectID, &i.Slug, &i.Description, + &i.GatewayConfig, + &i.DeleteProtection, + &i.CreatedAt, + &i.UpdatedAt, ) return i, err } diff --git a/go/pkg/db/environment_insert.sql_generated.go b/go/pkg/db/environment_insert.sql_generated.go index 924e3f8fe0..4078cbe4ef 100644 --- a/go/pkg/db/environment_insert.sql_generated.go +++ b/go/pkg/db/environment_insert.sql_generated.go @@ -18,20 +18,22 @@ INSERT INTO environments ( slug, description, created_at, - updated_at + updated_at, + gateway_config ) VALUES ( - ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ? ) ` type InsertEnvironmentParams struct { - ID string `db:"id"` - WorkspaceID string `db:"workspace_id"` - ProjectID string `db:"project_id"` - Slug string `db:"slug"` - Description string `db:"description"` - CreatedAt int64 `db:"created_at"` - UpdatedAt sql.NullInt64 `db:"updated_at"` + ID string `db:"id"` + WorkspaceID string `db:"workspace_id"` + ProjectID string `db:"project_id"` + Slug string `db:"slug"` + Description string `db:"description"` + CreatedAt int64 `db:"created_at"` + UpdatedAt sql.NullInt64 `db:"updated_at"` + GatewayConfig []byte `db:"gateway_config"` } // InsertEnvironment @@ -43,9 +45,10 @@ type InsertEnvironmentParams struct { // slug, // description, // created_at, -// updated_at +// updated_at, +// gateway_config // ) VALUES ( -// ?, ?, ?, ?, ?, ?, ? +// ?, ?, ?, ?, ?, ?, ?, ? // ) func (q *Queries) InsertEnvironment(ctx context.Context, db DBTX, arg InsertEnvironmentParams) error { _, err := db.ExecContext(ctx, insertEnvironment, @@ -56,6 +59,7 @@ func (q *Queries) InsertEnvironment(ctx context.Context, db DBTX, arg InsertEnvi arg.Description, arg.CreatedAt, arg.UpdatedAt, + arg.GatewayConfig, ) return err } diff --git a/go/pkg/db/querier_generated.go b/go/pkg/db/querier_generated.go index 83176d1b73..5ea4b27d97 100644 --- a/go/pkg/db/querier_generated.go +++ b/go/pkg/db/querier_generated.go @@ -207,12 +207,12 @@ type Querier interface { FindEnvironmentById(ctx context.Context, db DBTX, id string) (FindEnvironmentByIdRow, error) //FindEnvironmentByProjectIdAndSlug // - // SELECT id, workspace_id, project_id, slug, description + // SELECT id, workspace_id, project_id, slug, description, gateway_config, delete_protection, created_at, updated_at // FROM environments // WHERE workspace_id = ? // AND project_id = ? // AND slug = ? - FindEnvironmentByProjectIdAndSlug(ctx context.Context, db DBTX, arg FindEnvironmentByProjectIdAndSlugParams) (FindEnvironmentByProjectIdAndSlugRow, error) + FindEnvironmentByProjectIdAndSlug(ctx context.Context, db DBTX, arg FindEnvironmentByProjectIdAndSlugParams) (Environment, error) //FindGatewaysByEnvironmentID // // SELECT id, workspace_id, environment_id, k8s_service_name, region, image, health, replicas FROM gateways WHERE environment_id = ? @@ -1101,6 +1101,7 @@ type Querier interface { // git_commit_timestamp, -- Unix epoch milliseconds // openapi_spec, // status, + // gateway_config, // created_at, // updated_at // ) @@ -1120,6 +1121,7 @@ type Querier interface { // ?, // ?, // ?, + // ?, // ? // ) InsertDeployment(ctx context.Context, db DBTX, arg InsertDeploymentParams) error @@ -1153,9 +1155,10 @@ type Querier interface { // slug, // description, // created_at, - // updated_at + // updated_at, + // gateway_config // ) VALUES ( - // ?, ?, ?, ?, ?, ?, ? + // ?, ?, ?, ?, ?, ?, ?, ? // ) InsertEnvironment(ctx context.Context, db DBTX, arg InsertEnvironmentParams) error //InsertGateway diff --git a/go/pkg/db/queries/deployment_insert.sql b/go/pkg/db/queries/deployment_insert.sql index 51ea5051f0..4473559101 100644 --- a/go/pkg/db/queries/deployment_insert.sql +++ b/go/pkg/db/queries/deployment_insert.sql @@ -14,6 +14,7 @@ INSERT INTO `deployments` ( git_commit_timestamp, -- Unix epoch milliseconds openapi_spec, status, + gateway_config, created_at, updated_at ) @@ -32,6 +33,7 @@ VALUES ( sqlc.arg(git_commit_timestamp), sqlc.arg(openapi_spec), sqlc.arg(status), + sqlc.arg(gateway_config), sqlc.arg(created_at), sqlc.arg(updated_at) ); diff --git a/go/pkg/db/queries/environment_find_by_project_id_and_slug.sql b/go/pkg/db/queries/environment_find_by_project_id_and_slug.sql index cf86c7f6db..2788c3a5f1 100644 --- a/go/pkg/db/queries/environment_find_by_project_id_and_slug.sql +++ b/go/pkg/db/queries/environment_find_by_project_id_and_slug.sql @@ -1,6 +1,6 @@ -- name: FindEnvironmentByProjectIdAndSlug :one -SELECT id, workspace_id, project_id, slug, description +SELECT * FROM environments -WHERE workspace_id = sqlc.arg(workspace_id) - AND project_id = sqlc.arg(project_id) +WHERE workspace_id = sqlc.arg(workspace_id) + AND project_id = sqlc.arg(project_id) AND slug = sqlc.arg(slug); diff --git a/go/pkg/db/queries/environment_insert.sql b/go/pkg/db/queries/environment_insert.sql index 564a15ccdb..941ce83441 100644 --- a/go/pkg/db/queries/environment_insert.sql +++ b/go/pkg/db/queries/environment_insert.sql @@ -6,7 +6,8 @@ INSERT INTO environments ( slug, description, created_at, - updated_at + updated_at, + gateway_config ) VALUES ( - ?, ?, ?, ?, ?, ?, ? -); \ No newline at end of file + ?, ?, ?, ?, ?, ?, ?, ? +);