# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: helm-chart-ci
on:
  push:
    branches:
      - main
    paths:
      - 'helm-chart/**'
      - '.github/workflows/helm-chart-ci.yaml'
  pull_request:
    paths:
      - 'helm-chart/**'
      - '.github/workflows/helm-chart-ci.yaml'
jobs:
  helm-chart-ci:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
      - name: helm lint
        run: |
          cd helm-chart/
          helm lint --strict
      - name: helm template default
        run: |
          cd helm-chart/
          helm template . > helm-template.yaml
          cat helm-template.yaml 
          kustomize create --resources helm-template.yaml
          kustomize build .
      - name: helm template grpc health probes
        run: |
          # Test related to https://medium.com/google-cloud/b5bd26253a4c
          cd helm-chart/
          SPANNER_CONNECTION_STRING=projects/PROJECT_ID/instances/SPANNER_INSTANCE_NAME/databases/SPANNER_DATABASE_NAME
          helm template . \
            --set nativeGrpcHealthCheck=true \
            -n onlineboutique \
            > helm-template.yaml
          cat helm-template.yaml
          kustomize build .
      - name: helm template spanner
        run: |
          # Test related to https://medium.com/google-cloud/f7248e077339
          cd helm-chart/
          SPANNER_CONNECTION_STRING=projects/PROJECT_ID/instances/SPANNER_INSTANCE_NAME/databases/SPANNER_DATABASE_NAME
          SPANNER_DB_USER_GSA_ID=spanner-db-user@my-project.iam.gserviceaccount.com
          helm template . \
            --set cartDatabase.inClusterRedis.create=false \
            --set cartDatabase.type=spanner \
            --set cartDatabase.connectionString=${SPANNER_CONNECTION_STRING} \
            --set serviceAccounts.create=true \
            --set serviceAccounts.annotationsOnlyForCartservice=true \
            --set "serviceAccounts.annotations.iam\.gke\.io/gcp-service-account=${SPANNER_DB_USER_GSA_ID}" \
            -n onlineboutique \
            > helm-template.yaml
          cat helm-template.yaml
          kustomize build .
      - name: helm template asm
        run: |
          # Test related to https://medium.com/google-cloud/246119e46d53
          cd helm-chart/
          helm template . \
            --set networkPolicies.create=true \
            --set sidecars.create=true \
            --set serviceAccounts.create=true \
            --set authorizationPolicies.create=true \
            --set frontend.externalService=false \
            --set frontend.virtualService.create=true \
            --set frontend.virtualService.gateway.name=asm-ingressgateway \
            --set frontend.virtualService.gateway.namespace=asm-ingress \
            --set frontend.virtualService.gateway.labelKey=asm \
            --set frontend.virtualService.gateway.labelValue=ingressgateway \
            -n onlineboutique \
            > helm-template.yaml
          cat helm-template.yaml
          kustomize build .
      - name: helm template memorystore istio tls origination
        run: |
          # Test related to https://medium.com/google-cloud/64b71969318d
          cd helm-chart/
          REDIS_IP=0.0.0.0
          REDIS_PORT=7378
          REDIS_CERT=dsjfgkldsjflkdsjflksdajfkldsjkfljsdaklfjaskjfakdsjfaklsdjflskadjfklasjfkls
          helm template . \
            --set cartDatabase.inClusterRedis.create=false \
            --set cartDatabase.connectionString=${REDIS_IP}:${REDIS_PORT} \
            --set cartDatabase.externalRedisTlsOrigination.enable=true \
            --set cartDatabase.externalRedisTlsOrigination.certificate="${REDIS_CERT}" \
            --set cartDatabase.externalRedisTlsOrigination.endpointAddress=${REDIS_IP} \
            --set cartDatabase.externalRedisTlsOrigination.endpointPort=${REDIS_PORT} \
            -n onlineboutique \
            > helm-template.yaml
          cat helm-template.yaml
          kustomize build .
