From 8498f607c7053783f76282ea680de61cf0d799f6 Mon Sep 17 00:00:00 2001 From: Florian Bacher Date: Fri, 19 Feb 2021 10:49:54 +0100 Subject: [PATCH] #2983 added keptn spec version to events sent by shipyard-controller (#3294) * #2983 added keptn spec versino to events sent by shipyard-controller Signed-off-by: Florian Bacher * #2983 added a little unit test Signed-off-by: Florian Bacher * #2983 get tag name of specification Signed-off-by: Florian Bacher * #2983 remove gitmodules Signed-off-by: Florian Bacher * #2983 re-added specification submodule Signed-off-by: Florian Bacher * #2983 check out submodules Signed-off-by: Florian Bacher * #2983 removed --exact-match from describe tags command Signed-off-by: Florian Bacher * #2983 update models in mongodb-datastore Signed-off-by: Florian Bacher * #2983 update deployment yaml of mongodb-datastore Signed-off-by: Florian Bacher * #2983 removed duplications Signed-off-by: Florian Bacher * #2983 reverted changes to deployment.yaml Signed-off-by: Florian Bacher --- .github/workflows/CI.yml | 14 +++++++-- .gitmodules | 3 +- .../build_installer_helm_chart.sh | 4 +++ .../charts/control-plane/templates/core.yaml | 2 ++ .../keptn/charts/control-plane/values.yaml | 2 ++ .../deploy/mongodb-datastore.yaml | 6 ++-- .../models/keptn_context_extended_c_e.go | 16 ++++++++-- mongodb-datastore/restapi/embedded_spec.go | 6 ++++ mongodb-datastore/swagger.yaml | 2 ++ shipyard-controller/common/keptn_helpers.go | 29 +++++++++---------- .../common/keptn_helpers_test.go | 16 ++++++++++ shipyard-controller/deploy/service.yaml | 2 ++ specification | 1 + 13 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 shipyard-controller/common/keptn_helpers_test.go create mode 160000 specification diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 15bfc17912..8baafc29ce 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -46,6 +46,7 @@ jobs: BRANCH: ${{ steps.extract_branch.outputs.BRANCH }} BRANCH_SLUG: ${{ steps.extract_branch.outputs.BRANCH_SLUG }} VERSION: ${{ steps.get_version.outputs.VERSION }} + KEPTN_SPEC_VERSION: ${{ steps.get_version.outputs.KEPTN_SPEC_VERSION }} DATE: ${{ steps.get_datetime.outputs.DATE }} TIME: ${{ steps.get_datetime.outputs.TIME }} DATETIME: ${{ steps.get_datetime.outputs.DATETIME }} @@ -55,6 +56,7 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 # need to checkout "all commits" for certain features to work (e.g., get all changed files) + submodules: 'true' - name: Load CI Environemnt from .ci_env id: load_ci_env @@ -229,9 +231,16 @@ jobs: fi fi + # determine keptn spec version + git submodule update --init + cd specification + KEPTN_SPEC_VERSION=$(git describe --tags) + echo "VERSION=${VERSION}" + echo "KEPTN_SPEC_VERSION=${KEPTN_SPEC_VERSION}" echo "##[set-output name=VERSION;]$(echo ${VERSION})" + echo "##[set-output name=KEPTN_SPEC_VERSION;]$(echo ${KEPTN_SPEC_VERSION})" - name: Get current date and time id: get_datetime run: | @@ -548,6 +557,7 @@ jobs: env: BRANCH: ${{ needs.prepare_ci_run.outputs.BRANCH }} VERSION: ${{ needs.prepare_ci_run.outputs.VERSION }} + KEPTN_SPEC_VERSION: ${{ needs.prepare_ci_run.outputs.KEPTN_SPEC_VERSION }} DATETIME: ${{ needs.prepare_ci_run.outputs.DATE }}${{ needs.prepare_ci_run.outputs.TIME }} GIT_SHA: ${{ needs.prepare_ci_run.outputs.GIT_SHA }} steps: @@ -565,10 +575,10 @@ jobs: run: | if [[ "$BRANCH" == "master" ]]; then # use VERSION.DATETIME for the image tag (e.g., nightly build) - ./gh-actions-scripts/build_installer_helm_chart.sh "${VERSION}" "${VERSION}.${DATETIME}" + ./gh-actions-scripts/build_installer_helm_chart.sh "${VERSION}" "${VERSION}.${DATETIME}" "${KEPTN_SPEC_VERSION}" else # just use VERSION for the image tag - ./gh-actions-scripts/build_installer_helm_chart.sh "${VERSION}" "${VERSION}" + ./gh-actions-scripts/build_installer_helm_chart.sh "${VERSION}" "${VERSION}" "${KEPTN_SPEC_VERSION}" fi - name: Upload Helm Chart as an artifact diff --git a/.gitmodules b/.gitmodules index c274691ca5..4be1b116d0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ + [submodule "specification"] path = specification - url = https://github.com/keptn/spec.git + url = https://github.com/keptn/spec diff --git a/gh-actions-scripts/build_installer_helm_chart.sh b/gh-actions-scripts/build_installer_helm_chart.sh index 5e740b9721..ac003d1559 100755 --- a/gh-actions-scripts/build_installer_helm_chart.sh +++ b/gh-actions-scripts/build_installer_helm_chart.sh @@ -2,6 +2,7 @@ VERSION=$1 IMAGE_TAG=$2 +KEPTN_SPEC_VERSION=$3 if [ -z "$VERSION" ]; then echo "No Version set, exiting..." @@ -22,6 +23,9 @@ helm dependency build ${BASE_PATH}/keptn/charts/control-plane find -name Chart.yaml -exec sed -i -- "s/appVersion: latest/appVersion: ${IMAGE_TAG}/g" {} \; find -name Chart.yaml -exec sed -i -- "s/version: latest/version: ${VERSION}/g" {} \; +# replace "keptnSpecVersion: latest" with "keptnSpecVersion: $KEPTN_SPEC_VERSION" in all values.yaml files +find -name values.yaml -exec sed -i -- "s/keptnSpecVersion: latest/keptnSpecVersion: ${KEPTN_SPEC_VERSION}/g" {} \; + helm package ${BASE_PATH}/keptn --app-version $VERSION --version $VERSION if [ $? -ne 0 ]; then echo "Error packing installer, exiting..." diff --git a/installer/manifests/keptn/charts/control-plane/templates/core.yaml b/installer/manifests/keptn/charts/control-plane/templates/core.yaml index dd9c4fed1a..24163eac6b 100644 --- a/installer/manifests/keptn/charts/control-plane/templates/core.yaml +++ b/installer/manifests/keptn/charts/control-plane/templates/core.yaml @@ -294,6 +294,8 @@ spec: key: password - name: MONGO_DB_NAME value: 'keptn' + - name: KEPTN_SPEC_VERSION + value: {{ .Values.keptnSpecVersion }} ports: - containerPort: 8080 livenessProbe: diff --git a/installer/manifests/keptn/charts/control-plane/values.yaml b/installer/manifests/keptn/charts/control-plane/values.yaml index ab0ec1568e..bcf81b8c05 100644 --- a/installer/manifests/keptn/charts/control-plane/values.yaml +++ b/installer/manifests/keptn/charts/control-plane/values.yaml @@ -7,6 +7,8 @@ mongodb: prefixPath: "" +keptnSpecVersion: latest + nats: nameOverride: keptn-nats-cluster nats.cluster.replicas: 3 diff --git a/mongodb-datastore/deploy/mongodb-datastore.yaml b/mongodb-datastore/deploy/mongodb-datastore.yaml index c3a0038ef1..988337bb9d 100644 --- a/mongodb-datastore/deploy/mongodb-datastore.yaml +++ b/mongodb-datastore/deploy/mongodb-datastore.yaml @@ -35,11 +35,11 @@ spec: - containerPort: 8080 resources: requests: - memory: "128Mi" + memory: "256Mi" cpu: "100m" limits: - memory: "256Mi" - cpu: "1000m" + memory: "512Mi" + cpu: "5000m" env: - name: DT_CUSTOM_PROP value: 'keptn_service=mongodb-datastore' diff --git a/mongodb-datastore/models/keptn_context_extended_c_e.go b/mongodb-datastore/models/keptn_context_extended_c_e.go index 84069f6d67..043c6a520d 100644 --- a/mongodb-datastore/models/keptn_context_extended_c_e.go +++ b/mongodb-datastore/models/keptn_context_extended_c_e.go @@ -7,11 +7,12 @@ package models import ( "github.com/go-openapi/errors" - strfmt "github.com/go-openapi/strfmt" + "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" ) // KeptnContextExtendedCE keptn context extended c e +// // swagger:model KeptnContextExtendedCE type KeptnContextExtendedCE struct { Event @@ -19,6 +20,9 @@ type KeptnContextExtendedCE struct { // shkeptncontext Shkeptncontext string `json:"shkeptncontext,omitempty"` + // shkeptnspecversion + Shkeptnspecversion string `json:"shkeptnspecversion,omitempty"` + // triggeredid Triggeredid string `json:"triggeredid,omitempty"` } @@ -36,6 +40,8 @@ func (m *KeptnContextExtendedCE) UnmarshalJSON(raw []byte) error { var dataAO1 struct { Shkeptncontext string `json:"shkeptncontext,omitempty"` + Shkeptnspecversion string `json:"shkeptnspecversion,omitempty"` + Triggeredid string `json:"triggeredid,omitempty"` } if err := swag.ReadJSON(raw, &dataAO1); err != nil { @@ -44,6 +50,8 @@ func (m *KeptnContextExtendedCE) UnmarshalJSON(raw []byte) error { m.Shkeptncontext = dataAO1.Shkeptncontext + m.Shkeptnspecversion = dataAO1.Shkeptnspecversion + m.Triggeredid = dataAO1.Triggeredid return nil @@ -58,15 +66,18 @@ func (m KeptnContextExtendedCE) MarshalJSON() ([]byte, error) { return nil, err } _parts = append(_parts, aO0) - var dataAO1 struct { Shkeptncontext string `json:"shkeptncontext,omitempty"` + Shkeptnspecversion string `json:"shkeptnspecversion,omitempty"` + Triggeredid string `json:"triggeredid,omitempty"` } dataAO1.Shkeptncontext = m.Shkeptncontext + dataAO1.Shkeptnspecversion = m.Shkeptnspecversion + dataAO1.Triggeredid = m.Triggeredid jsonDataAO1, errAO1 := swag.WriteJSON(dataAO1) @@ -74,7 +85,6 @@ func (m KeptnContextExtendedCE) MarshalJSON() ([]byte, error) { return nil, errAO1 } _parts = append(_parts, jsonDataAO1) - return swag.ConcatJSON(_parts...), nil } diff --git a/mongodb-datastore/restapi/embedded_spec.go b/mongodb-datastore/restapi/embedded_spec.go index d8807bd67d..fc9de29ddf 100644 --- a/mongodb-datastore/restapi/embedded_spec.go +++ b/mongodb-datastore/restapi/embedded_spec.go @@ -239,6 +239,9 @@ func init() { "shkeptncontext": { "type": "string" }, + "shkeptnspecversion": { + "type": "string" + }, "triggeredid": { "type": "string" } @@ -532,6 +535,9 @@ func init() { "shkeptncontext": { "type": "string" }, + "shkeptnspecversion": { + "type": "string" + }, "triggeredid": { "type": "string" } diff --git a/mongodb-datastore/swagger.yaml b/mongodb-datastore/swagger.yaml index dffe9a65e7..c7783a0e89 100644 --- a/mongodb-datastore/swagger.yaml +++ b/mongodb-datastore/swagger.yaml @@ -179,6 +179,8 @@ definitions: type: string triggeredid: type: string + shkeptnspecversion: + type: string error: type: object required: diff --git a/shipyard-controller/common/keptn_helpers.go b/shipyard-controller/common/keptn_helpers.go index 255d1ba3d7..f183b3ce36 100644 --- a/shipyard-controller/common/keptn_helpers.go +++ b/shipyard-controller/common/keptn_helpers.go @@ -14,6 +14,8 @@ import ( const defaultKeptnNamespace = "keptn" +const keptnSpecVersionEnvVar = "KEPTN_SPEC_VERSION" + // GetKeptnNamespace godoc func GetKeptnNamespace() string { ns := os.Getenv("POD_NAMESPACE") @@ -78,6 +80,11 @@ func ValidateShipyardStages(shipyard *keptnv2.Shipyard) error { return nil } +// GetKeptnSpecVersion returns the Keptn Spec version the shipyard controller is based on +func GetKeptnSpecVersion() string { + return os.Getenv(keptnSpecVersionEnvVar) +} + // SendEvent godoc func SendEvent(event cloudevents.Event) error { ebEndpoint, err := keptncommon.GetServiceEndpoint("EVENTBROKER") @@ -90,7 +97,9 @@ func SendEvent(event cloudevents.Event) error { if err != nil { return errors.New("Could not initialize Keptn handler: " + err.Error()) } - + if specVersion := GetKeptnSpecVersion(); specVersion != "" { + event.SetExtension("shkeptnspecversion", specVersion) + } err = k.SendCloudEvent(event) if err != nil { return errors.New("Could not send CloudEvent: " + err.Error()) @@ -101,20 +110,7 @@ func SendEvent(event cloudevents.Event) error { // SendEventWithPayload godoc // Deprecated will be removed, use functionality from go-utils instead func SendEventWithPayload(keptnContext, triggeredID, eventType string, payload interface{}) error { - source, _ := url.Parse("shipyard-controller") - event := cloudevents.NewEvent() - event.SetType(eventType) - event.SetSource(source.String()) - event.SetDataContentType(cloudevents.ApplicationJSON) - if keptnContext == "" { - event.SetExtension("shkeptncontext", uuid.New().String()) - } else { - event.SetExtension("shkeptncontext", keptnContext) - } - if triggeredID != "" { - event.SetExtension("triggeredid", triggeredID) - } - event.SetData(cloudevents.ApplicationJSON, payload) + event := CreateEventWithPayload(keptnContext, triggeredID, eventType, payload) ebEndpoint, err := keptncommon.GetServiceEndpoint("EVENTBROKER") if err != nil { @@ -148,6 +144,9 @@ func CreateEventWithPayload(keptnContext, triggeredID, eventType string, payload if triggeredID != "" { event.SetExtension("triggeredid", triggeredID) } + if specVersion := GetKeptnSpecVersion(); specVersion != "" { + event.SetExtension("shkeptnspecversion", specVersion) + } event.SetData(cloudevents.ApplicationJSON, payload) return event } diff --git a/shipyard-controller/common/keptn_helpers_test.go b/shipyard-controller/common/keptn_helpers_test.go new file mode 100644 index 0000000000..9d5c8eabbf --- /dev/null +++ b/shipyard-controller/common/keptn_helpers_test.go @@ -0,0 +1,16 @@ +package common + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestGetKeptnSpecVersion(t *testing.T) { + specVersion := GetKeptnSpecVersion() + assert.Equal(t, "", specVersion) + + os.Setenv(keptnSpecVersionEnvVar, "0.2.0") + specVersion = GetKeptnSpecVersion() + assert.Equal(t, "0.2.0", specVersion) +} diff --git a/shipyard-controller/deploy/service.yaml b/shipyard-controller/deploy/service.yaml index d8ae80676d..9d4addc714 100644 --- a/shipyard-controller/deploy/service.yaml +++ b/shipyard-controller/deploy/service.yaml @@ -55,6 +55,8 @@ spec: key: password - name: MONGO_DB_NAME value: 'keptn' + - name: KEPTN_SPEC_VERSION + value: "0.2.0" ports: - containerPort: 8080 resources: diff --git a/specification b/specification new file mode 160000 index 0000000000..b14dc2bce9 --- /dev/null +++ b/specification @@ -0,0 +1 @@ +Subproject commit b14dc2bce96953fa14b5f3f0fcc853ffda515795