From 3b002965dd2e0ad1b02753c2f041eb97b6188df7 Mon Sep 17 00:00:00 2001 From: Dhiraj Bokde Date: Thu, 19 Sep 2024 09:51:28 -0700 Subject: [PATCH] fix: fix Makefile and Dockerfile for idempotent images and to support multi-arch builds Fixes RHOAIENG-12078 (#322) * fix: remove .git from Dockerfile Signed-off-by: Dhiraj Bokde * fix: remove git check for model-registry.yaml in Makefile fix gen/openapi-server make target to generate go-server if model-registry.yaml file is newer Signed-off-by: Dhiraj Bokde * fix: commit updated type_asserts.go Signed-off-by: Dhiraj Bokde * fix: sort assert functions in gen_type_asserts.sh before writing them to type_asserts.go Signed-off-by: Dhiraj Bokde * feat: add 'image/buildx' target to create multi-platform docker images using docker buildx, split 'build' target into 'build/prepare' and 'build/compile' to optimize multi-platform builds Signed-off-by: Dhiraj Bokde * feat: add docker env variables to enable multi-platform cross compilation, optimize build steps Signed-off-by: Dhiraj Bokde * fix: add podman support for buildx Signed-off-by: Dhiraj Bokde * fix: remove docker builder if it already exists Signed-off-by: Dhiraj Bokde --------- Signed-off-by: Dhiraj Bokde --- Dockerfile | 15 +- Makefile | 56 +++- internal/server/openapi/type_asserts.go | 362 ++++++++++++------------ scripts/gen_type_asserts.sh | 31 +- 4 files changed, 267 insertions(+), 197 deletions(-) diff --git a/Dockerfile b/Dockerfile index 89a741c5..757eca6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ # Build the model-registry binary -FROM registry.access.redhat.com/ubi8/go-toolset:1.21 AS builder +FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi8/go-toolset:1.21 AS builder +ARG TARGETOS +ARG TARGETARCH WORKDIR /workspace # Copy the Go Modules manifests @@ -20,7 +22,6 @@ RUN yum install -y nodejs npm java-11 COPY ["Makefile", "main.go", ".openapi-generator-ignore", "openapitools.json", "./"] # Copy rest of the source -COPY .git/ .git/ COPY cmd/ cmd/ COPY api/ api/ COPY internal/ internal/ @@ -34,7 +35,15 @@ RUN make deps # Build USER root -RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 make clean model-registry + +# NOTE: The two instructions below are effectively equivalent to 'make clean build' +# DO NOT REMOVE THE 'build/prepare' TARGET!!! +# It ensures consitent repeatable Dockerfile builds + +# prepare the build in a separate layer +RUN make clean build/prepare +# compile separately to optimize multi-platform builds +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} make build/compile # Use distroless as minimal base image to package the model-registry binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index ed5503bd..3d5e1b39 100644 --- a/Makefile +++ b/Makefile @@ -76,14 +76,10 @@ openapi/validate: bin/openapi-generator-cli # generate the openapi server implementation .PHONY: gen/openapi-server -gen/openapi-server: bin/openapi-generator-cli openapi/validate - @if git diff --cached --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \ - git diff --exit-code --name-only | grep -q "api/openapi/model-registry.yaml" || \ - [ -n "${FORCE_SERVER_GENERATION}" ]; then \ - ./scripts/gen_openapi_server.sh; \ - else \ - echo "INFO api/openapi/model-registry.yaml is not staged or modified, will not re-generate server"; \ - fi +gen/openapi-server: bin/openapi-generator-cli openapi/validate internal/server/openapi/api_model_registry_service.go + +internal/server/openapi/api_model_registry_service.go: bin/openapi-generator-cli api/openapi/model-registry.yaml + ROOT_FOLDER=${PROJECT_PATH} ./scripts/gen_openapi_server.sh # generate the openapi schema model and client .PHONY: gen/openapi @@ -102,7 +98,7 @@ vet: .PHONY: clean clean: - rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go pkg/openapi + rm -Rf ./model-registry internal/ml_metadata/proto/*.go internal/converter/generated/*.go pkg/openapi internal/server/openapi/api_model_registry_service.go .PHONY: clean/odh clean/odh: @@ -157,10 +153,22 @@ deps: bin/protoc bin/go-enum bin/protoc-gen-go bin/protoc-gen-go-grpc bin/golang vendor: ${GO} mod vendor -.PHONY: build -build: gen vet lint +# WARNING: DO NOT DELETE THIS TARGET, USED BY Dockerfile!!! +.PHONY: build/prepare +build/prepare: gen vet lint + +# WARNING: DO NOT DELETE THIS TARGET, USED BY Dockerfile!!! +.PHONY: build/compile +build/compile: ${GO} build -buildvcs=false +# WARNING: DO NOT EDIT THIS TARGET DIRECTLY!!! +# Use build/prepare to add build prerequisites +# Use build/compile to add/edit go source compilation +# WARNING: Editing this target directly WILL affect the Dockerfile image build!!! +.PHONY: build +build: build/prepare build/compile + .PHONY: build/odh build/odh: vet ${GO} build -buildvcs=false @@ -210,6 +218,32 @@ endif image/build: ${DOCKER} build . -f ${DOCKERFILE} -t ${IMG}:$(IMG_VERSION) +# build docker image using buildx +# PLATFORMS defines the target platforms for the model registry image be built to provide support to multiple +# architectures. (i.e. make docker-buildx). To use this option you need to: +# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/ +# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/ +# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=> then the export will fail) +# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option. +PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le +.PHONY: image/buildx +image/buildx: +ifeq ($(DOCKER),docker) + # docker uses builder containers + - $(DOCKER) buildx rm model-registry-builder + $(DOCKER) buildx create --use --name model-registry-builder --platform=$(PLATFORMS) + $(DOCKER) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f ${DOCKERFILE} . + $(DOCKER) buildx rm model-registry-builder +else ifeq ($(DOCKER),podman) + # podman uses image manifests + $(DOCKER) manifest create -a ${IMG} + $(DOCKER) buildx build --platform=$(PLATFORMS) --manifest ${IMG} -f ${DOCKERFILE} . + $(DOCKER) manifest push ${IMG} + $(DOCKER) manifest rm ${IMG} +else + $(error Unsupported container tool $(DOCKER)) +endif + # push docker image .PHONY: image/push image/push: diff --git a/internal/server/openapi/type_asserts.go b/internal/server/openapi/type_asserts.go index 8defb4fc..fa7cde46 100644 --- a/internal/server/openapi/type_asserts.go +++ b/internal/server/openapi/type_asserts.go @@ -16,13 +16,13 @@ import ( model "github.com/kubeflow/model-registry/pkg/openapi" ) -// AssertArtifactRequired checks if the required fields are not zero-ed -func AssertArtifactRequired(obj model.Artifact) error { +// AssertArtifactConstraints checks if the values respects the defined constraints +func AssertArtifactConstraints(obj model.Artifact) error { return nil } -// AssertArtifactConstraints checks if the values respects the defined constraints -func AssertArtifactConstraints(obj model.Artifact) error { +// AssertArtifactListConstraints checks if the values respects the defined constraints +func AssertArtifactListConstraints(obj model.ArtifactList) error { return nil } @@ -47,13 +47,8 @@ func AssertArtifactListRequired(obj model.ArtifactList) error { return nil } -// AssertArtifactListConstraints checks if the values respects the defined constraints -func AssertArtifactListConstraints(obj model.ArtifactList) error { - return nil -} - -// AssertArtifactStateRequired checks if the required fields are not zero-ed -func AssertArtifactStateRequired(obj model.ArtifactState) error { +// AssertArtifactRequired checks if the required fields are not zero-ed +func AssertArtifactRequired(obj model.Artifact) error { return nil } @@ -62,8 +57,8 @@ func AssertArtifactStateConstraints(obj model.ArtifactState) error { return nil } -// AssertBaseArtifactRequired checks if the required fields are not zero-ed -func AssertBaseArtifactRequired(obj model.BaseArtifact) error { +// AssertArtifactStateRequired checks if the required fields are not zero-ed +func AssertArtifactStateRequired(obj model.ArtifactState) error { return nil } @@ -72,18 +67,18 @@ func AssertBaseArtifactConstraints(obj model.BaseArtifact) error { return nil } -// AssertBaseArtifactCreateRequired checks if the required fields are not zero-ed -func AssertBaseArtifactCreateRequired(obj model.BaseArtifactCreate) error { +// AssertBaseArtifactCreateConstraints checks if the values respects the defined constraints +func AssertBaseArtifactCreateConstraints(obj model.BaseArtifactCreate) error { return nil } -// AssertBaseArtifactCreateConstraints checks if the values respects the defined constraints -func AssertBaseArtifactCreateConstraints(obj model.BaseArtifactCreate) error { +// AssertBaseArtifactCreateRequired checks if the required fields are not zero-ed +func AssertBaseArtifactCreateRequired(obj model.BaseArtifactCreate) error { return nil } -// AssertBaseArtifactUpdateRequired checks if the required fields are not zero-ed -func AssertBaseArtifactUpdateRequired(obj model.BaseArtifactUpdate) error { +// AssertBaseArtifactRequired checks if the required fields are not zero-ed +func AssertBaseArtifactRequired(obj model.BaseArtifact) error { return nil } @@ -92,8 +87,8 @@ func AssertBaseArtifactUpdateConstraints(obj model.BaseArtifactUpdate) error { return nil } -// AssertBaseExecutionRequired checks if the required fields are not zero-ed -func AssertBaseExecutionRequired(obj model.BaseExecution) error { +// AssertBaseArtifactUpdateRequired checks if the required fields are not zero-ed +func AssertBaseArtifactUpdateRequired(obj model.BaseArtifactUpdate) error { return nil } @@ -102,18 +97,18 @@ func AssertBaseExecutionConstraints(obj model.BaseExecution) error { return nil } -// AssertBaseExecutionCreateRequired checks if the required fields are not zero-ed -func AssertBaseExecutionCreateRequired(obj model.BaseExecutionCreate) error { +// AssertBaseExecutionCreateConstraints checks if the values respects the defined constraints +func AssertBaseExecutionCreateConstraints(obj model.BaseExecutionCreate) error { return nil } -// AssertBaseExecutionCreateConstraints checks if the values respects the defined constraints -func AssertBaseExecutionCreateConstraints(obj model.BaseExecutionCreate) error { +// AssertBaseExecutionCreateRequired checks if the required fields are not zero-ed +func AssertBaseExecutionCreateRequired(obj model.BaseExecutionCreate) error { return nil } -// AssertBaseExecutionUpdateRequired checks if the required fields are not zero-ed -func AssertBaseExecutionUpdateRequired(obj model.BaseExecutionUpdate) error { +// AssertBaseExecutionRequired checks if the required fields are not zero-ed +func AssertBaseExecutionRequired(obj model.BaseExecution) error { return nil } @@ -122,8 +117,8 @@ func AssertBaseExecutionUpdateConstraints(obj model.BaseExecutionUpdate) error { return nil } -// AssertBaseResourceRequired checks if the required fields are not zero-ed -func AssertBaseResourceRequired(obj model.BaseResource) error { +// AssertBaseExecutionUpdateRequired checks if the required fields are not zero-ed +func AssertBaseExecutionUpdateRequired(obj model.BaseExecutionUpdate) error { return nil } @@ -132,13 +127,18 @@ func AssertBaseResourceConstraints(obj model.BaseResource) error { return nil } +// AssertBaseResourceCreateConstraints checks if the values respects the defined constraints +func AssertBaseResourceCreateConstraints(obj model.BaseResourceCreate) error { + return nil +} + // AssertBaseResourceCreateRequired checks if the required fields are not zero-ed func AssertBaseResourceCreateRequired(obj model.BaseResourceCreate) error { return nil } -// AssertBaseResourceCreateConstraints checks if the values respects the defined constraints -func AssertBaseResourceCreateConstraints(obj model.BaseResourceCreate) error { +// AssertBaseResourceListConstraints checks if the values respects the defined constraints +func AssertBaseResourceListConstraints(obj model.BaseResourceList) error { return nil } @@ -158,8 +158,13 @@ func AssertBaseResourceListRequired(obj model.BaseResourceList) error { return nil } -// AssertBaseResourceListConstraints checks if the values respects the defined constraints -func AssertBaseResourceListConstraints(obj model.BaseResourceList) error { +// AssertBaseResourceRequired checks if the required fields are not zero-ed +func AssertBaseResourceRequired(obj model.BaseResource) error { + return nil +} + +// AssertBaseResourceUpdateConstraints checks if the values respects the defined constraints +func AssertBaseResourceUpdateConstraints(obj model.BaseResourceUpdate) error { return nil } @@ -168,8 +173,8 @@ func AssertBaseResourceUpdateRequired(obj model.BaseResourceUpdate) error { return nil } -// AssertBaseResourceUpdateConstraints checks if the values respects the defined constraints -func AssertBaseResourceUpdateConstraints(obj model.BaseResourceUpdate) error { +// AssertDocArtifactConstraints checks if the values respects the defined constraints +func AssertDocArtifactConstraints(obj model.DocArtifact) error { return nil } @@ -187,8 +192,8 @@ func AssertDocArtifactRequired(obj model.DocArtifact) error { return nil } -// AssertDocArtifactConstraints checks if the values respects the defined constraints -func AssertDocArtifactConstraints(obj model.DocArtifact) error { +// AssertErrorConstraints checks if the values respects the defined constraints +func AssertErrorConstraints(obj model.Error) error { return nil } @@ -207,8 +212,8 @@ func AssertErrorRequired(obj model.Error) error { return nil } -// AssertErrorConstraints checks if the values respects the defined constraints -func AssertErrorConstraints(obj model.Error) error { +// AssertExecutionStateConstraints checks if the values respects the defined constraints +func AssertExecutionStateConstraints(obj model.ExecutionState) error { return nil } @@ -217,28 +222,13 @@ func AssertExecutionStateRequired(obj model.ExecutionState) error { return nil } -// AssertExecutionStateConstraints checks if the values respects the defined constraints -func AssertExecutionStateConstraints(obj model.ExecutionState) error { - return nil -} - -// AssertInferenceServiceRequired checks if the required fields are not zero-ed -func AssertInferenceServiceRequired(obj model.InferenceService) error { - elements := map[string]interface{}{ - "registeredModelId": obj.RegisteredModelId, - "servingEnvironmentId": obj.ServingEnvironmentId, - } - for name, el := range elements { - if isZero := IsZeroValue(el); isZero { - return &RequiredError{Field: name} - } - } - +// AssertInferenceServiceConstraints checks if the values respects the defined constraints +func AssertInferenceServiceConstraints(obj model.InferenceService) error { return nil } -// AssertInferenceServiceConstraints checks if the values respects the defined constraints -func AssertInferenceServiceConstraints(obj model.InferenceService) error { +// AssertInferenceServiceCreateConstraints checks if the values respects the defined constraints +func AssertInferenceServiceCreateConstraints(obj model.InferenceServiceCreate) error { return nil } @@ -257,8 +247,8 @@ func AssertInferenceServiceCreateRequired(obj model.InferenceServiceCreate) erro return nil } -// AssertInferenceServiceCreateConstraints checks if the values respects the defined constraints -func AssertInferenceServiceCreateConstraints(obj model.InferenceServiceCreate) error { +// AssertInferenceServiceListConstraints checks if the values respects the defined constraints +func AssertInferenceServiceListConstraints(obj model.InferenceServiceList) error { return nil } @@ -283,8 +273,23 @@ func AssertInferenceServiceListRequired(obj model.InferenceServiceList) error { return nil } -// AssertInferenceServiceListConstraints checks if the values respects the defined constraints -func AssertInferenceServiceListConstraints(obj model.InferenceServiceList) error { +// AssertInferenceServiceRequired checks if the required fields are not zero-ed +func AssertInferenceServiceRequired(obj model.InferenceService) error { + elements := map[string]interface{}{ + "registeredModelId": obj.RegisteredModelId, + "servingEnvironmentId": obj.ServingEnvironmentId, + } + for name, el := range elements { + if isZero := IsZeroValue(el); isZero { + return &RequiredError{Field: name} + } + } + + return nil +} + +// AssertInferenceServiceStateConstraints checks if the values respects the defined constraints +func AssertInferenceServiceStateConstraints(obj model.InferenceServiceState) error { return nil } @@ -293,8 +298,8 @@ func AssertInferenceServiceStateRequired(obj model.InferenceServiceState) error return nil } -// AssertInferenceServiceStateConstraints checks if the values respects the defined constraints -func AssertInferenceServiceStateConstraints(obj model.InferenceServiceState) error { +// AssertInferenceServiceUpdateConstraints checks if the values respects the defined constraints +func AssertInferenceServiceUpdateConstraints(obj model.InferenceServiceUpdate) error { return nil } @@ -303,8 +308,8 @@ func AssertInferenceServiceUpdateRequired(obj model.InferenceServiceUpdate) erro return nil } -// AssertInferenceServiceUpdateConstraints checks if the values respects the defined constraints -func AssertInferenceServiceUpdateConstraints(obj model.InferenceServiceUpdate) error { +// AssertMetadataBoolValueConstraints checks if the values respects the defined constraints +func AssertMetadataBoolValueConstraints(obj model.MetadataBoolValue) error { return nil } @@ -323,8 +328,8 @@ func AssertMetadataBoolValueRequired(obj model.MetadataBoolValue) error { return nil } -// AssertMetadataBoolValueConstraints checks if the values respects the defined constraints -func AssertMetadataBoolValueConstraints(obj model.MetadataBoolValue) error { +// AssertMetadataDoubleValueConstraints checks if the values respects the defined constraints +func AssertMetadataDoubleValueConstraints(obj model.MetadataDoubleValue) error { return nil } @@ -343,8 +348,8 @@ func AssertMetadataDoubleValueRequired(obj model.MetadataDoubleValue) error { return nil } -// AssertMetadataDoubleValueConstraints checks if the values respects the defined constraints -func AssertMetadataDoubleValueConstraints(obj model.MetadataDoubleValue) error { +// AssertMetadataIntValueConstraints checks if the values respects the defined constraints +func AssertMetadataIntValueConstraints(obj model.MetadataIntValue) error { return nil } @@ -363,8 +368,8 @@ func AssertMetadataIntValueRequired(obj model.MetadataIntValue) error { return nil } -// AssertMetadataIntValueConstraints checks if the values respects the defined constraints -func AssertMetadataIntValueConstraints(obj model.MetadataIntValue) error { +// AssertMetadataProtoValueConstraints checks if the values respects the defined constraints +func AssertMetadataProtoValueConstraints(obj model.MetadataProtoValue) error { return nil } @@ -384,8 +389,8 @@ func AssertMetadataProtoValueRequired(obj model.MetadataProtoValue) error { return nil } -// AssertMetadataProtoValueConstraints checks if the values respects the defined constraints -func AssertMetadataProtoValueConstraints(obj model.MetadataProtoValue) error { +// AssertMetadataStringValueConstraints checks if the values respects the defined constraints +func AssertMetadataStringValueConstraints(obj model.MetadataStringValue) error { return nil } @@ -404,8 +409,8 @@ func AssertMetadataStringValueRequired(obj model.MetadataStringValue) error { return nil } -// AssertMetadataStringValueConstraints checks if the values respects the defined constraints -func AssertMetadataStringValueConstraints(obj model.MetadataStringValue) error { +// AssertMetadataStructValueConstraints checks if the values respects the defined constraints +func AssertMetadataStructValueConstraints(obj model.MetadataStructValue) error { return nil } @@ -424,8 +429,8 @@ func AssertMetadataStructValueRequired(obj model.MetadataStructValue) error { return nil } -// AssertMetadataStructValueConstraints checks if the values respects the defined constraints -func AssertMetadataStructValueConstraints(obj model.MetadataStructValue) error { +// AssertMetadataValueConstraints checks if the values respects the defined constraints +func AssertMetadataValueConstraints(obj model.MetadataValue) error { return nil } @@ -451,27 +456,13 @@ func AssertMetadataValueRequired(obj model.MetadataValue) error { return nil } -// AssertMetadataValueConstraints checks if the values respects the defined constraints -func AssertMetadataValueConstraints(obj model.MetadataValue) error { - return nil -} - -// AssertModelArtifactRequired checks if the required fields are not zero-ed -func AssertModelArtifactRequired(obj model.ModelArtifact) error { - elements := map[string]interface{}{ - "artifactType": obj.ArtifactType, - } - for name, el := range elements { - if isZero := IsZeroValue(el); isZero { - return &RequiredError{Field: name} - } - } - +// AssertModelArtifactConstraints checks if the values respects the defined constraints +func AssertModelArtifactConstraints(obj model.ModelArtifact) error { return nil } -// AssertModelArtifactConstraints checks if the values respects the defined constraints -func AssertModelArtifactConstraints(obj model.ModelArtifact) error { +// AssertModelArtifactCreateConstraints checks if the values respects the defined constraints +func AssertModelArtifactCreateConstraints(obj model.ModelArtifactCreate) error { return nil } @@ -480,8 +471,8 @@ func AssertModelArtifactCreateRequired(obj model.ModelArtifactCreate) error { return nil } -// AssertModelArtifactCreateConstraints checks if the values respects the defined constraints -func AssertModelArtifactCreateConstraints(obj model.ModelArtifactCreate) error { +// AssertModelArtifactListConstraints checks if the values respects the defined constraints +func AssertModelArtifactListConstraints(obj model.ModelArtifactList) error { return nil } @@ -506,13 +497,17 @@ func AssertModelArtifactListRequired(obj model.ModelArtifactList) error { return nil } -// AssertModelArtifactListConstraints checks if the values respects the defined constraints -func AssertModelArtifactListConstraints(obj model.ModelArtifactList) error { - return nil -} +// AssertModelArtifactRequired checks if the required fields are not zero-ed +func AssertModelArtifactRequired(obj model.ModelArtifact) error { + elements := map[string]interface{}{ + "artifactType": obj.ArtifactType, + } + for name, el := range elements { + if isZero := IsZeroValue(el); isZero { + return &RequiredError{Field: name} + } + } -// AssertModelArtifactUpdateRequired checks if the required fields are not zero-ed -func AssertModelArtifactUpdateRequired(obj model.ModelArtifactUpdate) error { return nil } @@ -521,18 +516,8 @@ func AssertModelArtifactUpdateConstraints(obj model.ModelArtifactUpdate) error { return nil } -// AssertModelVersionRequired checks if the required fields are not zero-ed -func AssertModelVersionRequired(obj model.ModelVersion) error { - elements := map[string]interface{}{ - "name": obj.Name, - "registeredModelId": obj.RegisteredModelId, - } - for name, el := range elements { - if isZero := IsZeroValue(el); isZero { - return &RequiredError{Field: name} - } - } - +// AssertModelArtifactUpdateRequired checks if the required fields are not zero-ed +func AssertModelArtifactUpdateRequired(obj model.ModelArtifactUpdate) error { return nil } @@ -541,6 +526,11 @@ func AssertModelVersionConstraints(obj model.ModelVersion) error { return nil } +// AssertModelVersionCreateConstraints checks if the values respects the defined constraints +func AssertModelVersionCreateConstraints(obj model.ModelVersionCreate) error { + return nil +} + // AssertModelVersionCreateRequired checks if the required fields are not zero-ed func AssertModelVersionCreateRequired(obj model.ModelVersionCreate) error { elements := map[string]interface{}{ @@ -556,8 +546,8 @@ func AssertModelVersionCreateRequired(obj model.ModelVersionCreate) error { return nil } -// AssertModelVersionCreateConstraints checks if the values respects the defined constraints -func AssertModelVersionCreateConstraints(obj model.ModelVersionCreate) error { +// AssertModelVersionListConstraints checks if the values respects the defined constraints +func AssertModelVersionListConstraints(obj model.ModelVersionList) error { return nil } @@ -582,13 +572,18 @@ func AssertModelVersionListRequired(obj model.ModelVersionList) error { return nil } -// AssertModelVersionListConstraints checks if the values respects the defined constraints -func AssertModelVersionListConstraints(obj model.ModelVersionList) error { - return nil -} +// AssertModelVersionRequired checks if the required fields are not zero-ed +func AssertModelVersionRequired(obj model.ModelVersion) error { + elements := map[string]interface{}{ + "name": obj.Name, + "registeredModelId": obj.RegisteredModelId, + } + for name, el := range elements { + if isZero := IsZeroValue(el); isZero { + return &RequiredError{Field: name} + } + } -// AssertModelVersionStateRequired checks if the required fields are not zero-ed -func AssertModelVersionStateRequired(obj model.ModelVersionState) error { return nil } @@ -597,8 +592,8 @@ func AssertModelVersionStateConstraints(obj model.ModelVersionState) error { return nil } -// AssertModelVersionUpdateRequired checks if the required fields are not zero-ed -func AssertModelVersionUpdateRequired(obj model.ModelVersionUpdate) error { +// AssertModelVersionStateRequired checks if the required fields are not zero-ed +func AssertModelVersionStateRequired(obj model.ModelVersionState) error { return nil } @@ -607,8 +602,8 @@ func AssertModelVersionUpdateConstraints(obj model.ModelVersionUpdate) error { return nil } -// AssertOrderByFieldRequired checks if the required fields are not zero-ed -func AssertOrderByFieldRequired(obj model.OrderByField) error { +// AssertModelVersionUpdateRequired checks if the required fields are not zero-ed +func AssertModelVersionUpdateRequired(obj model.ModelVersionUpdate) error { return nil } @@ -617,17 +612,8 @@ func AssertOrderByFieldConstraints(obj model.OrderByField) error { return nil } -// AssertRegisteredModelRequired checks if the required fields are not zero-ed -func AssertRegisteredModelRequired(obj model.RegisteredModel) error { - elements := map[string]interface{}{ - "name": obj.Name, - } - for name, el := range elements { - if isZero := IsZeroValue(el); isZero { - return &RequiredError{Field: name} - } - } - +// AssertOrderByFieldRequired checks if the required fields are not zero-ed +func AssertOrderByFieldRequired(obj model.OrderByField) error { return nil } @@ -636,6 +622,11 @@ func AssertRegisteredModelConstraints(obj model.RegisteredModel) error { return nil } +// AssertRegisteredModelCreateConstraints checks if the values respects the defined constraints +func AssertRegisteredModelCreateConstraints(obj model.RegisteredModelCreate) error { + return nil +} + // AssertRegisteredModelCreateRequired checks if the required fields are not zero-ed func AssertRegisteredModelCreateRequired(obj model.RegisteredModelCreate) error { elements := map[string]interface{}{ @@ -650,8 +641,8 @@ func AssertRegisteredModelCreateRequired(obj model.RegisteredModelCreate) error return nil } -// AssertRegisteredModelCreateConstraints checks if the values respects the defined constraints -func AssertRegisteredModelCreateConstraints(obj model.RegisteredModelCreate) error { +// AssertRegisteredModelListConstraints checks if the values respects the defined constraints +func AssertRegisteredModelListConstraints(obj model.RegisteredModelList) error { return nil } @@ -676,13 +667,17 @@ func AssertRegisteredModelListRequired(obj model.RegisteredModelList) error { return nil } -// AssertRegisteredModelListConstraints checks if the values respects the defined constraints -func AssertRegisteredModelListConstraints(obj model.RegisteredModelList) error { - return nil -} +// AssertRegisteredModelRequired checks if the required fields are not zero-ed +func AssertRegisteredModelRequired(obj model.RegisteredModel) error { + elements := map[string]interface{}{ + "name": obj.Name, + } + for name, el := range elements { + if isZero := IsZeroValue(el); isZero { + return &RequiredError{Field: name} + } + } -// AssertRegisteredModelStateRequired checks if the required fields are not zero-ed -func AssertRegisteredModelStateRequired(obj model.RegisteredModelState) error { return nil } @@ -691,8 +686,8 @@ func AssertRegisteredModelStateConstraints(obj model.RegisteredModelState) error return nil } -// AssertRegisteredModelUpdateRequired checks if the required fields are not zero-ed -func AssertRegisteredModelUpdateRequired(obj model.RegisteredModelUpdate) error { +// AssertRegisteredModelStateRequired checks if the required fields are not zero-ed +func AssertRegisteredModelStateRequired(obj model.RegisteredModelState) error { return nil } @@ -701,17 +696,8 @@ func AssertRegisteredModelUpdateConstraints(obj model.RegisteredModelUpdate) err return nil } -// AssertServeModelRequired checks if the required fields are not zero-ed -func AssertServeModelRequired(obj model.ServeModel) error { - elements := map[string]interface{}{ - "modelVersionId": obj.ModelVersionId, - } - for name, el := range elements { - if isZero := IsZeroValue(el); isZero { - return &RequiredError{Field: name} - } - } - +// AssertRegisteredModelUpdateRequired checks if the required fields are not zero-ed +func AssertRegisteredModelUpdateRequired(obj model.RegisteredModelUpdate) error { return nil } @@ -720,6 +706,11 @@ func AssertServeModelConstraints(obj model.ServeModel) error { return nil } +// AssertServeModelCreateConstraints checks if the values respects the defined constraints +func AssertServeModelCreateConstraints(obj model.ServeModelCreate) error { + return nil +} + // AssertServeModelCreateRequired checks if the required fields are not zero-ed func AssertServeModelCreateRequired(obj model.ServeModelCreate) error { elements := map[string]interface{}{ @@ -734,8 +725,8 @@ func AssertServeModelCreateRequired(obj model.ServeModelCreate) error { return nil } -// AssertServeModelCreateConstraints checks if the values respects the defined constraints -func AssertServeModelCreateConstraints(obj model.ServeModelCreate) error { +// AssertServeModelListConstraints checks if the values respects the defined constraints +func AssertServeModelListConstraints(obj model.ServeModelList) error { return nil } @@ -760,13 +751,17 @@ func AssertServeModelListRequired(obj model.ServeModelList) error { return nil } -// AssertServeModelListConstraints checks if the values respects the defined constraints -func AssertServeModelListConstraints(obj model.ServeModelList) error { - return nil -} +// AssertServeModelRequired checks if the required fields are not zero-ed +func AssertServeModelRequired(obj model.ServeModel) error { + elements := map[string]interface{}{ + "modelVersionId": obj.ModelVersionId, + } + for name, el := range elements { + if isZero := IsZeroValue(el); isZero { + return &RequiredError{Field: name} + } + } -// AssertServeModelUpdateRequired checks if the required fields are not zero-ed -func AssertServeModelUpdateRequired(obj model.ServeModelUpdate) error { return nil } @@ -775,8 +770,8 @@ func AssertServeModelUpdateConstraints(obj model.ServeModelUpdate) error { return nil } -// AssertServingEnvironmentRequired checks if the required fields are not zero-ed -func AssertServingEnvironmentRequired(obj model.ServingEnvironment) error { +// AssertServeModelUpdateRequired checks if the required fields are not zero-ed +func AssertServeModelUpdateRequired(obj model.ServeModelUpdate) error { return nil } @@ -785,13 +780,18 @@ func AssertServingEnvironmentConstraints(obj model.ServingEnvironment) error { return nil } +// AssertServingEnvironmentCreateConstraints checks if the values respects the defined constraints +func AssertServingEnvironmentCreateConstraints(obj model.ServingEnvironmentCreate) error { + return nil +} + // AssertServingEnvironmentCreateRequired checks if the required fields are not zero-ed func AssertServingEnvironmentCreateRequired(obj model.ServingEnvironmentCreate) error { return nil } -// AssertServingEnvironmentCreateConstraints checks if the values respects the defined constraints -func AssertServingEnvironmentCreateConstraints(obj model.ServingEnvironmentCreate) error { +// AssertServingEnvironmentListConstraints checks if the values respects the defined constraints +func AssertServingEnvironmentListConstraints(obj model.ServingEnvironmentList) error { return nil } @@ -816,13 +816,8 @@ func AssertServingEnvironmentListRequired(obj model.ServingEnvironmentList) erro return nil } -// AssertServingEnvironmentListConstraints checks if the values respects the defined constraints -func AssertServingEnvironmentListConstraints(obj model.ServingEnvironmentList) error { - return nil -} - -// AssertServingEnvironmentUpdateRequired checks if the required fields are not zero-ed -func AssertServingEnvironmentUpdateRequired(obj model.ServingEnvironmentUpdate) error { +// AssertServingEnvironmentRequired checks if the required fields are not zero-ed +func AssertServingEnvironmentRequired(obj model.ServingEnvironment) error { return nil } @@ -831,8 +826,8 @@ func AssertServingEnvironmentUpdateConstraints(obj model.ServingEnvironmentUpdat return nil } -// AssertSortOrderRequired checks if the required fields are not zero-ed -func AssertSortOrderRequired(obj model.SortOrder) error { +// AssertServingEnvironmentUpdateRequired checks if the required fields are not zero-ed +func AssertServingEnvironmentUpdateRequired(obj model.ServingEnvironmentUpdate) error { return nil } @@ -840,3 +835,8 @@ func AssertSortOrderRequired(obj model.SortOrder) error { func AssertSortOrderConstraints(obj model.SortOrder) error { return nil } + +// AssertSortOrderRequired checks if the required fields are not zero-ed +func AssertSortOrderRequired(obj model.SortOrder) error { + return nil +} diff --git a/scripts/gen_type_asserts.sh b/scripts/gen_type_asserts.sh index d4b06c28..74a9a26f 100755 --- a/scripts/gen_type_asserts.sh +++ b/scripts/gen_type_asserts.sh @@ -42,17 +42,44 @@ EOF # Create the file and initialize it with the specified content echo -e "$INITIAL_CONTENT" >"$ASSERT_FILE_PATH" +declare -A assert_functions +pattern="\/\/ (\w+) checks(.|\n)+?\n\}\n" +shell_pattern="\/\/ (\w+) checks[^\/?]+?" + # Iterate over files starting with "model_" in the internal/server/openapi/ folder for file in "$PROJECT_ROOT"/internal/server/openapi/model_*; do # Check if the file is a regular file if [ -f "$file" ]; then - # Ignore first 15 lines containing license, package and imports - sed -n '13,$p' "$file" >>"$ASSERT_FILE_PATH" + + # grab all functions in the file + functions=$(grep -Pzo "$pattern" "${file}" | tr -d '\0') + while [[ $functions =~ $shell_pattern ]]; do + + name="${BASH_REMATCH[1]}" + body="${BASH_REMATCH[0]}" + + # add function to associative array + assert_functions["$name"]="$body" + + # Remove the matched function to process next function + functions=${functions//"$body"/} + done + # Remove the merged file rm "$file" fi done +# get sorted function names in another array +sorted_names=$(for name in "${!assert_functions[@]}"; do + echo "$name" +done | sort) + +# iterate over the sorted function names array and print bodies +for name in $sorted_names; do + echo "${assert_functions[$name]}" >>"$ASSERT_FILE_PATH" +done + gofmt -w "$ASSERT_FILE_PATH" git apply "$PATCH"