Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kargo Slack Posting messages not received #3166

Closed
creeram opened this issue Dec 20, 2024 Discussed in #3165 · 15 comments · Fixed by #3170
Closed

kargo Slack Posting messages not received #3166

creeram opened this issue Dec 20, 2024 Discussed in #3165 · 15 comments · Fixed by #3170

Comments

@creeram
Copy link

creeram commented Dec 20, 2024

Discussed in #3165

Originally posted by creeram December 20, 2024
I'm trying to post messages to Slack. The promotion is successful, but I don't receive any notifications or messages in the channel.

The Slack credentials are valid and tested with the curl command, and I can see the messages in the Slack channel.

Kargo Verision:

Client Version: v1.1.1
Server Version: v1.1.1

Screenshot of Stage promotion successful run.

Screenshot 2024-12-20 at 08 15 13

There are not error logs in Kargo API and controller pods.

apiVersion: kargo.akuity.io/v1alpha1
kind: Stage
metadata:
  name: demo
  namespace: kargo-demo
spec:
  requestedFreight:
  - origin:
      kind: Warehouse
      name: kargo-demo
    sources:
      direct: true
  promotionTemplate:
    spec:
      vars:
       - name: slackChannel
         value: xxxxxx 
      steps:
      - uses: git-clone
        config:
          repoURL: xxxxxxx
          checkout:
          - branch: main
            path: ./src
      - uses: helm-update-image
        as: update-chart-image
        config:
          path: ./src/apps/demo/e2e.yaml
          images:
          - image: xxxxxxxxxxxxx
            key: image.tag
            value: Tag
      - uses: git-commit
        as: commit
        config:
          path: ./src/apps
          messageFromSteps:
          - update-chart-image
      - uses: git-push
        config:
          path: ./src
          targetBranch: main

      - uses: http
        config:
          method: POST
          url: https://slack.com/api/chat.postMessage
          headers:
          - name: Authorization
            value: Bearer xoxb-xxxxxxx-xxxxxxx-xxxxxxx
          - name: Content-type
            value: application/json
          body: |
            ${{ quote({
              "channel": vars.slackChannel,
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
                  }
                }
                ]
            }) }}


```</div>
@krancour
Copy link
Member

@creeram is there some possibility of posting to netcat or socat instead so you can see what's coming through?

@creeram
Copy link
Author

creeram commented Dec 20, 2024

@krancour What do you mean by posting to Netcat or Socat? could you give me some info on how to do that?

@krancour
Copy link
Member

Depending on your OS and package manager, you should have at least one of the command line utilities netcat, nc, or socat available to you.

The syntax is slightly different for each and you'll have to look it up, but all three should allow you to easily listen for TCP connections on a port of your choosing and echo whatever they receive.

This means if you send it an HTTP request it will show you method, path, HTTP version, all headers, and the message body.

Using nc, for example:

nc -l 8080

Then alter your http step so the url begins with http://localhost:8080 (instead of https://slack.com).

Run your promotion and see what, if anything nc receives.

@krancour
Copy link
Member

It seems the message is coming through with extra quotes:

POST / HTTP/1.1
Host: 192.168.2.120:8080
User-Agent: Go-http-client/1.1
Content-Length: 148
Content-Type: application/json
Accept-Encoding: gzip
Connection: close

"{"blocks":[{"text":{"text":"Updated test to use image public.ecr.aws/nginx/nginx:1.27.3","type":"mrkdwn"},"type":"section"}],"channel":"C123456"}"

@krancour
Copy link
Member

There's some odd interplay between the | and the quote() function.

@krancour krancour added this to the v1.1.2 milestone Dec 21, 2024
@krancour
Copy link
Member

| introduces a trailing newline that interferes with some other logic for interpreting strings that are explicitly enclosed in quotes.

@creeram
Copy link
Author

creeram commented Dec 22, 2024

@krancour That means I will have to wait until the next version is released?

@krancour
Copy link
Member

I'm hoping for a patch release tomorrow and that's the milestone I've included this in.

@creeram
Copy link
Author

creeram commented Dec 24, 2024

@krancour With the new release the slack post message is working.

but for that i had to hardcode the slack token here

      - uses: http
        config:
          method: POST
          url: https://slack.com/api/chat.postMessage
          headers:
          - name: Authorization
            value: Bearer xoxb-xxxxxxx-xxxxxxx-xxxxxxx
          - name: Content-type
            value: application/json
          body: |
            ${{ quote({
              "channel": vars.slackChannel,
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
                  }
                }
                ]
            }) }}

I tried creating a k8s secret for that as well but it didn't work. How can i make it work with K8 Secret?

      - uses: http
        config:
          method: POST
          url: https://slack.com/api/chat.postMessage
          headers:
          - name: Authorization
            value: Bearer ${{ secrets.slack.token }}
          - name: Content-type
            value: application/json
          body: |
            ${{ quote({
              "channel": vars.slackChannel,
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
                  }
                }
                ]
            }) }}

the k8s secret used:

apiVersion: v1
kind: Secret
metadata:
  name: slack
  namespace: kargo-demo
type: Opaque
data:
   slack.token: xxxxxxxxxxxxxxxxx(base64 encoded)

and how can we use variables inside that text block?

like ${{ imageFrom("public.ecr.aws/nginx/nginx", warehouse("my-warehouse")).Tag }}

@krancour
Copy link
Member

krancour commented Dec 24, 2024

This doesn't match the expression used for accessing the secret (${{ secrets.slack.token }}):

apiVersion: v1
kind: Secret
metadata:
  name: slack
  namespace: kargo-demo
type: Opaque
data:
   slack.token: xxxxxxxxxxxxxxxxx(base64 encoded)

This would:

apiVersion: v1
kind: Secret
metadata:
  name: slack
  namespace: kargo-demo
type: Opaque
data:
   token: xxxxxxxxxxxxxxxxx(base64 encoded)

and how can we use variables inside that text block?

I'm afraid I don't understand the question, as I already see you successfully using vars.slackChannel.

@creeram
Copy link
Author

creeram commented Dec 24, 2024

@krancour I tried both secrets but it didn't work.

apiVersion: v1
kind: Secret
metadata:
  name: slack
  namespace: kargo-demo
type: Opaque
data:
   token: xxxxxxxxxxxxxxxxx(base64 encoded)

For variable how can I use it here

"blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "vars.variable."//it didn't work
                  }
                }
                ]

@krancour
Copy link
Member

@creeram here's a full working example:

apiVersion: kargo.akuity.io/v1alpha1
kind: Project
metadata:
  name: kargo-demo-15
spec:
  promotionPolicies:
  - stage: test
    autoPromotionEnabled: true
---
apiVersion: kargo.akuity.io/v1alpha1
kind: Warehouse
metadata:
  name: kargo-demo
  namespace: kargo-demo-15
spec:
  subscriptions:
  - image:
      repoURL: public.ecr.aws/nginx/nginx
      semverConstraint: ^1.24.0
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: slack
  namespace: kargo-demo-15
stringData:
  token: foo
---
apiVersion: kargo.akuity.io/v1alpha1
kind: Stage
metadata:
  name: test
  namespace: kargo-demo-15
spec:
  requestedFreight:
  - origin:
      kind: Warehouse
      name: kargo-demo
    sources:
      direct: true
  promotionTemplate:
    spec:
      vars:
      - name: repoURL
        value: public.ecr.aws/nginx/nginx
      - name: url
        value: <url here>
      - name: slackChannel
        value: C123456
      steps:
      - uses: http
        config:
          method: POST
          url: ${{ vars.url }}
          headers:
          - name: Content-Type
            value: application/json
          - name: Authorization
            value: Bearer ${{ secrets.slack.token }}
          body: |
            ${{ quote({
              "channel": vars.slackChannel,
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "Updated " + ctx.stage + " to use image " + vars.repoURL + ":" + imageFrom(vars.repoURL).Tag
                  }
                }
              ]
            }) }}

@creeram
Copy link
Author

creeram commented Dec 25, 2024

Could you please help me with one more detail?

how can I get the info from a variable for who did the promotion?

@krancour
Copy link
Member

That's not something that's currently exposed. It should be easy enough to add if you would please open an issue requesting it.

@creeram
Copy link
Author

creeram commented Dec 26, 2024

@krancour you can find it here.
#3194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants