From 8ac59c99fd9cc61df5de48f561b59cc2f256a7e3 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Thu, 29 Aug 2024 15:39:51 +1000 Subject: [PATCH] feat: new basic-single service type --- internal/generator/services.go | 5 +- internal/servicetypes/basic.go | 20 ++++ internal/servicetypes/types.go | 1 + .../services/templates_deployment_test.go | 33 ++++++- .../templating/services/templates_pvc_test.go | 25 +++++ .../services/templates_service_test.go | 24 +++++ .../deployment/result-basic-4.yaml | 91 +++++++++++++++++++ .../test-resources/pvc/result-basic-4.yaml | 29 ++++++ .../service/result-basic-3.yaml | 31 +++++++ 9 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 internal/templating/services/test-resources/deployment/result-basic-4.yaml create mode 100644 internal/templating/services/test-resources/pvc/result-basic-4.yaml create mode 100644 internal/templating/services/test-resources/service/result-basic-3.yaml diff --git a/internal/generator/services.go b/internal/generator/services.go index 33d0ef0f..bd71bb18 100644 --- a/internal/generator/services.go +++ b/internal/generator/services.go @@ -4,13 +4,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/distribution/reference" "os" "reflect" "regexp" "strconv" "strings" + "github.com/distribution/reference" + composetypes "github.com/compose-spec/compose-go/types" "github.com/drone/envsubst" "github.com/uselagoon/build-deploy-tool/internal/helpers" @@ -32,6 +33,7 @@ var supportedAutogeneratedTypes = []string{ // "kibana", //@TODO: don't even need this anymore? "basic", "basic-persistent", + "basic-single", "node", "node-persistent", "nginx", @@ -63,6 +65,7 @@ var supportedDBTypes = []string{ // these are lagoon types that come with resources requiring backups var typesWithBackups = []string{ "basic-persistent", + "basic-single", "node-persistent", "nginx-php-persistent", "python-persistent", diff --git a/internal/servicetypes/basic.go b/internal/servicetypes/basic.go index f072ce6b..3f82cc7a 100644 --- a/internal/servicetypes/basic.go +++ b/internal/servicetypes/basic.go @@ -1,6 +1,7 @@ package servicetypes import ( + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/util/intstr" @@ -85,3 +86,22 @@ var basicPersistent = ServiceType{ Backup: true, }, } + +// contains all the single type overrides that the basic service doesn't have +// basicSingle is like basic persistent except that the volume is not bulk, and the pod can only ever have 1 replica because of this +var basicSingle = ServiceType{ + Name: "basic-single", + Ports: basic.Ports, + PrimaryContainer: ServiceContainer{ + Name: basic.PrimaryContainer.Name, + Container: basic.PrimaryContainer.Container, + }, + Strategy: appsv1.DeploymentStrategy{ + Type: appsv1.RecreateDeploymentStrategyType, + }, + Volumes: ServiceVolume{ + PersistentVolumeSize: "5Gi", + PersistentVolumeType: corev1.ReadWriteOnce, + Backup: true, + }, +} diff --git a/internal/servicetypes/types.go b/internal/servicetypes/types.go index 27c43d84..ac33cd53 100644 --- a/internal/servicetypes/types.go +++ b/internal/servicetypes/types.go @@ -58,6 +58,7 @@ type ServicePorts struct { var ServiceTypes = map[string]ServiceType{ "basic": basic, "basic-persistent": basicPersistent, + "basic-single": basicSingle, "cli": cli, "cli-persistent": cliPersistent, "elasticsearch": elasticsearch, diff --git a/internal/templating/services/templates_deployment_test.go b/internal/templating/services/templates_deployment_test.go index 824a46f5..5590b440 100644 --- a/internal/templating/services/templates_deployment_test.go +++ b/internal/templating/services/templates_deployment_test.go @@ -856,6 +856,37 @@ func TestGenerateDeploymentTemplate(t *testing.T) { }, want: "test-resources/deployment/result-postgres-1.yaml", }, + { + name: "test20 - basic-single", + args: args{ + buildValues: generator.BuildValues{ + Project: "example-project", + Environment: "environment-name", + EnvironmentType: "production", + Namespace: "myexample-project-environment-name", + BuildType: "branch", + LagoonVersion: "v2.x.x", + Kubernetes: "generator.local", + Branch: "environment-name", + GitSHA: "0", + ConfigMapSha: "32bf1359ac92178c8909f0ef938257b477708aa0d78a5a15ad7c2d7919adf273", + ImageReferences: map[string]string{ + "myservice": "harbor.example.com/example-project/environment-name/myservice@latest", + }, + Services: []generator.ServiceValues{ + { + Name: "myservice", + OverrideName: "myservice", + Type: "basic-single", + DBaaSEnvironment: "production", + ServicePort: 8080, + Replicas: 1, + }, + }, + }, + }, + want: "test-resources/deployment/result-basic-4.yaml", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1036,7 +1067,7 @@ func TestLinkedServiceCalculator(t *testing.T) { lValues, _ := json.Marshal(got) wValues, _ := json.Marshal(tt.want) if !reflect.DeepEqual(string(lValues), string(wValues)) { - t.Errorf("LinkedServiceCalculator() = %v, want %v", string(lValues), string(wValues)) + t.Errorf("LinkedServiceCalculator() = \n%v", diff.LineDiff(string(lValues), string(wValues))) } }) } diff --git a/internal/templating/services/templates_pvc_test.go b/internal/templating/services/templates_pvc_test.go index e92731e4..6848b313 100644 --- a/internal/templating/services/templates_pvc_test.go +++ b/internal/templating/services/templates_pvc_test.go @@ -213,6 +213,31 @@ func TestGeneratePVCTemplate(t *testing.T) { }, want: "test-resources/pvc/result-basic-3.yaml", }, + { + name: "test8 - basic-single", + args: args{ + buildValues: generator.BuildValues{ + Project: "example-project", + Environment: "environment-name", + EnvironmentType: "production", + Namespace: "myexample-project-environment-name", + BuildType: "branch", + LagoonVersion: "v2.x.x", + Kubernetes: "generator.local", + Branch: "environment-name", + RWX2RWO: true, + Services: []generator.ServiceValues{ + { + Name: "myservice", + OverrideName: "myservice", + Type: "basic-single", + DBaaSEnvironment: "development", + }, + }, + }, + }, + want: "test-resources/pvc/result-basic-4.yaml", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/templating/services/templates_service_test.go b/internal/templating/services/templates_service_test.go index 1dad3d26..6aa050ea 100644 --- a/internal/templating/services/templates_service_test.go +++ b/internal/templating/services/templates_service_test.go @@ -244,6 +244,30 @@ func TestGenerateServiceTemplate(t *testing.T) { }, want: "test-resources/service/result-nginx-php-1.yaml", }, + { + name: "test6 - basic-single", + args: args{ + buildValues: generator.BuildValues{ + Project: "example-project", + Environment: "environment-name", + EnvironmentType: "production", + Namespace: "myexample-project-environment-name", + BuildType: "branch", + LagoonVersion: "v2.x.x", + Kubernetes: "generator.local", + Branch: "environment-name", + Services: []generator.ServiceValues{ + { + Name: "myservice", + OverrideName: "myservice", + Type: "basic-single", + DBaaSEnvironment: "development", + }, + }, + }, + }, + want: "test-resources/service/result-basic-3.yaml", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/templating/services/test-resources/deployment/result-basic-4.yaml b/internal/templating/services/test-resources/deployment/result-basic-4.yaml new file mode 100644 index 00000000..ac282848 --- /dev/null +++ b/internal/templating/services/test-resources/deployment/result-basic-4.yaml @@ -0,0 +1,91 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + lagoon.sh/branch: environment-name + lagoon.sh/version: v2.x.x + creationTimestamp: null + labels: + app.kubernetes.io/instance: myservice + app.kubernetes.io/managed-by: build-deploy-tool + app.kubernetes.io/name: basic-single + lagoon.sh/buildType: branch + lagoon.sh/environment: environment-name + lagoon.sh/environmentType: production + lagoon.sh/project: example-project + lagoon.sh/service: myservice + lagoon.sh/service-type: basic-single + lagoon.sh/template: basic-single-0.1.0 + name: myservice +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/instance: myservice + app.kubernetes.io/name: basic-single + strategy: + type: Recreate + template: + metadata: + annotations: + lagoon.sh/branch: environment-name + lagoon.sh/configMapSha: 32bf1359ac92178c8909f0ef938257b477708aa0d78a5a15ad7c2d7919adf273 + lagoon.sh/version: v2.x.x + creationTimestamp: null + labels: + app.kubernetes.io/instance: myservice + app.kubernetes.io/managed-by: build-deploy-tool + app.kubernetes.io/name: basic-single + lagoon.sh/buildType: branch + lagoon.sh/environment: environment-name + lagoon.sh/environmentType: production + lagoon.sh/project: example-project + lagoon.sh/service: myservice + lagoon.sh/service-type: basic-single + lagoon.sh/template: basic-single-0.1.0 + spec: + containers: + - env: + - name: LAGOON_GIT_SHA + value: "0" + - name: CRONJOBS + - name: SERVICE_NAME + value: myservice + envFrom: + - configMapRef: + name: lagoon-env + image: harbor.example.com/example-project/environment-name/myservice@latest + imagePullPolicy: Always + livenessProbe: + initialDelaySeconds: 60 + tcpSocket: + port: 8080 + timeoutSeconds: 10 + name: basic + ports: + - containerPort: 8080 + name: http + protocol: TCP + readinessProbe: + initialDelaySeconds: 1 + tcpSocket: + port: 8080 + timeoutSeconds: 1 + resources: + requests: + cpu: 10m + memory: 10Mi + securityContext: {} + volumeMounts: + - mountPath: "" + name: myservice + enableServiceLinks: false + imagePullSecrets: + - name: lagoon-internal-registry-secret + priorityClassName: lagoon-priority-production + volumes: + - name: myservice + persistentVolumeClaim: + claimName: myservice +status: {} diff --git a/internal/templating/services/test-resources/pvc/result-basic-4.yaml b/internal/templating/services/test-resources/pvc/result-basic-4.yaml new file mode 100644 index 00000000..ce90e68c --- /dev/null +++ b/internal/templating/services/test-resources/pvc/result-basic-4.yaml @@ -0,0 +1,29 @@ +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + annotations: + k8up.io/backup: "true" + k8up.syn.tools/backup: "true" + lagoon.sh/branch: environment-name + lagoon.sh/version: v2.x.x + creationTimestamp: null + labels: + app.kubernetes.io/instance: myservice + app.kubernetes.io/managed-by: build-deploy-tool + app.kubernetes.io/name: basic-single + lagoon.sh/buildType: branch + lagoon.sh/environment: environment-name + lagoon.sh/environmentType: production + lagoon.sh/project: example-project + lagoon.sh/service: myservice + lagoon.sh/service-type: basic-single + lagoon.sh/template: basic-single-0.1.0 + name: myservice +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 5Gi +status: {} diff --git a/internal/templating/services/test-resources/service/result-basic-3.yaml b/internal/templating/services/test-resources/service/result-basic-3.yaml new file mode 100644 index 00000000..5a18549a --- /dev/null +++ b/internal/templating/services/test-resources/service/result-basic-3.yaml @@ -0,0 +1,31 @@ +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + lagoon.sh/branch: environment-name + lagoon.sh/version: v2.x.x + creationTimestamp: null + labels: + app.kubernetes.io/instance: myservice + app.kubernetes.io/managed-by: build-deploy-tool + app.kubernetes.io/name: basic-single + lagoon.sh/buildType: branch + lagoon.sh/environment: environment-name + lagoon.sh/environmentType: production + lagoon.sh/project: example-project + lagoon.sh/service: myservice + lagoon.sh/service-type: basic-single + lagoon.sh/template: basic-single-0.1.0 + name: myservice +spec: + ports: + - name: http + port: 3000 + protocol: TCP + targetPort: http + selector: + app.kubernetes.io/instance: myservice + app.kubernetes.io/name: basic-single +status: + loadBalancer: {}