Skip to content

Commit

Permalink
moved slackOrder function into a k8s native job, added ingress and se…
Browse files Browse the repository at this point in the history
…rvice linked to slack-collector deployment
  • Loading branch information
ca7alindev committed Sep 27, 2024
1 parent 2f54fbb commit 645ae8b
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: Build and push slack-notify
run: docker build -f internal/slack-notify/Dockerfile . -t ${REGISTRY}/${{ github.repository }}/slack-notify:${SHORT_GITHUB_SHA} && docker push ${REGISTRY}/${{ github.repository }}/slack-notify:${SHORT_GITHUB_SHA}

- name: Build and push slack-order
run: docker build -f internal/slack-order/Dockerfile . -t ${REGISTRY}/${{ github.repository }}/slack-order:${SHORT_GITHUB_SHA} && docker push ${REGISTRY}/${{ github.repository }}/slack-order:${SHORT_GITHUB_SHA}

- name: Build function image
run: docker build . -t ${REGISTRY}/${{ github.repository }}:${SHORT_GITHUB_SHA}

Expand Down
1 change: 1 addition & 0 deletions example/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ spec:
providerConfigRef: "kndp-kubernetes-provider-config"
deploymentImage: "ghcr.io/kndpio/function-poll/slack-collector:d7a4b"
cronJobImage: "ghcr.io/kndpio/function-poll/slack-notify:d7a4b"
jobImage: "ghcr.io/kndpio/function-poll/slack-order:d7a4b"
deploymentName: "slack-collector"
serviceAccountName: "slack-collector"
10 changes: 6 additions & 4 deletions example/xr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ metadata:
name: meal
spec:
deliveryTime: 0
dueOrderTime: 15
dueOrderTime: 20
dueTakeTime: 0
voters: []
title: "meal"
schedule: "0 * * * *"
messages:
schedule: "49 14 * * *"
messages:
question: "how are you?"
response: "thank you for response."
result: "here are the voting results:"
color: "#cfd1d0"
status:
lastNotificationTime: 1
98 changes: 90 additions & 8 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"encoding/json"
"os"
"strconv"
"time"

"github.com/slack-go/slack"
Expand Down Expand Up @@ -59,7 +61,6 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
desired, _ := request.GetDesiredComposedResources(req)

rsp := response.To(req, response.DefaultTTL)

xr, _ := request.GetObservedCompositeResource(req)
users, err := slackchannel.ProcessSlackMembers(api, channelID, f.log)
if err != nil {
Expand All @@ -69,17 +70,88 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
pollName, _ := xr.Resource.GetString("metadata.name")
schedule, _ := xr.Resource.GetString("spec.schedule")
question, _ := xr.Resource.GetString("spec.messages.question")
d, _ := xr.Resource.GetBool("status.done")
resultText, _ := xr.Resource.GetString("spec.messages.result")
color, _ := xr.Resource.GetString("spec.messages.color")
votersDetails, _ := xr.Resource.GetValue("status.voters")
votersJSON, _ := json.Marshal(votersDetails)
votersString := string(votersJSON)

if checkDueOrderTimeAndVoteCount(xr, currentTimestamp, users) {
if !d {
xr.Resource.SetBool("status.done", true)
slackchannel.SlackOrder(input, api, xr, f.log, resultText)
_, results := slackchannel.PatchVoters(xr, f.log)
job := composed.Unstructured{
Unstructured: unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "kubernetes.crossplane.io/v1alpha2",
"kind": "Object",
"metadata": map[string]interface{}{
"name": "slack-notify-job",
},
"spec": map[string]interface{}{
"forProvider": map[string]interface{}{
"manifest": map[string]interface{}{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": map[string]interface{}{
"name": "slack-notify-job",
"namespace": "default",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"restartPolicy": "OnFailure",
"serviceAccountName": input.ServiceAccountName,
"containers": []interface{}{
map[string]interface{}{
"name": "poll-container",
"image": input.JobImage,
"env": []interface{}{
map[string]interface{}{
"name": "RESULT_TEXT",
"value": resultText,
},
map[string]interface{}{
"name": "RESULTS",
"value": strconv.Itoa(results),
},
map[string]interface{}{
"name": "POLL_NAME",
"value": pollName,
},
map[string]interface{}{
"name": "POLL_TITLE",
"value": pollTitle,
},
map[string]interface{}{
"name": "POLL_VOTERS_DETAILS",
"value": votersString,
},
map[string]interface{}{
"name": "COLOR",
"value": color,
},
},
"envFrom": []interface{}{
map[string]interface{}{
"secretRef": map[string]interface{}{
"name": secretName,
},
},
},
},
},
},
},
},
},
},
"providerConfigRef": map[string]interface{}{"name": input.ProviderConfigRef},
},
},
},
}
desired[resource.Name(job.GetName())] = &resource.DesiredComposed{Resource: &job}
xr.Resource.SetManagedFields(nil)
response.SetDesiredCompositeResource(rsp, xr)

} else {
xr.Resource.SetManagedFields(nil)
response.SetDesiredCompositeResource(rsp, xr)
Expand Down Expand Up @@ -120,10 +192,16 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
map[string]interface{}{
"name": "poll-container",
"image": input.DeploymentImage,
"env": []interface{}{
map[string]interface{}{
"name": "COLOR",
"value": color,
},
},
"envFrom": []interface{}{
map[string]interface{}{
"secretRef": map[string]interface{}{
"name": secretName + "creds",
"name": secretName,
},
},
},
Expand Down Expand Up @@ -280,11 +358,15 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
"name": "POLL_TITLE",
"value": pollTitle,
},
map[string]interface{}{
"name": "COLOR",
"value": color,
},
},
"envFrom": []interface{}{
map[string]interface{}{
"secretRef": map[string]interface{}{
"name": secretName + "creds",
"name": secretName,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ type Input struct {
DeploymentImage string `json:"deploymentImage"`
ServiceAccountName string `json:"serviceAccountName"`
CronJobImage string `json:"cronJobImage"`
JobImage string `json:"jobImage"`
}
37 changes: 26 additions & 11 deletions internal/slack-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Message struct {
Question string `json:"question"`
Response string `json:"response"`
Result string `json:"result"`
Color string `json:"color"`
}

// Poll represents the structure of a poll.
Expand All @@ -60,20 +61,20 @@ type Poll struct {
DueOrderTime int64 `json:"dueOrderTime"`
DueTakeTime int64 `json:"dueTakeTime"`
Schedule string `json:"schedule"`
Voters []Voter `json:"voters"`
Title string `json:"title"`
Messages Message `json:"messages"`
} `json:"spec"`
Status struct {
Done bool `json:"done"`
LastNotificationTime int64 `json:"lastNotificationTime"`
Voters []Voter `json:"voters"`
LastNotificationTime int64 `json:"lastNotificationTime"`
} `json:"status"`
}

var (
api = slack.New(os.Getenv("SLACK_API_TOKEN"))
path = os.Getenv("SLACK_COLLECTOR_PATH")
port = os.Getenv("SLACK_COLLECTOR_PORT")
color = os.Getenv("COLOR")
response string
)

Expand Down Expand Up @@ -117,9 +118,9 @@ func patchVoterStatus(user, pollSlackName, selectedOption string, dynamicClient
}
response = pollResource.Spec.Messages.Response
foundUser := false
for i := range pollResource.Spec.Voters {
if pollResource.Spec.Voters[i].Name == user {
pollResource.Spec.Voters[i].Status = selectedOption
for i := range pollResource.Status.Voters {
if pollResource.Status.Voters[i].Name == user {
pollResource.Status.Voters[i].Status = selectedOption
foundUser = true
break
}
Expand All @@ -130,15 +131,29 @@ func patchVoterStatus(user, pollSlackName, selectedOption string, dynamicClient
Name: user,
Status: selectedOption,
}
pollResource.Spec.Voters = append(pollResource.Spec.Voters, newVoter)
pollResource.Status.Voters = append(pollResource.Status.Voters, newVoter)
}

pollResource.GetObjectMeta().SetManagedFields(nil)
pollBytes, _ := json.Marshal(pollResource)
_, err = dynamicClient.Resource(resourceId).Namespace("").Patch(ctx, pollResource.GetObjectMeta().GetName(), types.MergePatchType, pollBytes, metav1.PatchOptions{FieldManager: "slack-collector"})

statusBytes, _ := json.Marshal(map[string]interface{}{
"status": map[string]interface{}{
"voters": pollResource.Status.Voters,
},
})

_, err = dynamicClient.Resource(resourceId).Namespace("").Patch(
context.Background(),
pollResource.GetObjectMeta().GetName(),
types.MergePatchType,
statusBytes,
metav1.PatchOptions{FieldManager: "slack-collector"},
"/status",
)
if err != nil {
fmt.Println("Error patching poll resource", err)
fmt.Println("Error patching poll status", err)
}

return nil
}

Expand Down Expand Up @@ -169,7 +184,7 @@ func getK8sResource(dynamicClient dynamic.Interface, ctx context.Context, pollSl
func respondMsg(userID string, userName string, selectedOption string, pollName string) {

attachment := slack.Attachment{
Color: "#f9a41b",
Color: color,
CallbackID: pollName,
Text: response + "\n Selected: " + selectedOption,
Fields: []slack.AttachmentField{},
Expand Down
13 changes: 6 additions & 7 deletions internal/slack-notify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
pollName = os.Getenv("POLL_NAME")
pollTitle = os.Getenv("POLL_TITLE")
slackNotifyMessage = os.Getenv("SLACK_NOTIFY_MESSAGE")
color = os.Getenv("COLOR")
)

// Voter represents the structure of an Voter reference.
Expand All @@ -35,6 +36,7 @@ type Message struct {
Question string `json:"question"`
Response string `json:"response"`
Result string `json:"result"`
Color string `json:"color"`
}

// Poll represents the structure of a poll.
Expand All @@ -46,13 +48,12 @@ type Poll struct {
DueOrderTime int64 `json:"dueOrderTime"`
DueTakeTime int64 `json:"dueTakeTime"`
Schedule string `json:"schedule"`
Voters []Voter `json:"voters"`
Title string `json:"title"`
Messages Message `json:"messages"`
} `json:"spec"`
Status struct {
Done bool `json:"done"`
LastNotificationTime int64 `json:"lastNotificationTime"`
Voters []Voter `json:"voters"`
LastNotificationTime int64 `json:"lastNotificationTime"`
} `json:"status"`
}

Expand Down Expand Up @@ -101,18 +102,16 @@ func main() {

statusBytes, _ := json.Marshal(map[string]interface{}{
"status": map[string]interface{}{
"done": false,
"lastNotificationTime": time.Now().Unix(),
},
})

// Use the "/status" subresource to update just the status
_, err = client.Resource(resourceId).Namespace("").Patch(
context.Background(),
pollResource.GetObjectMeta().GetName(),
types.MergePatchType,
statusBytes,
metav1.PatchOptions{FieldManager: "slack-collector"},
metav1.PatchOptions{FieldManager: "slack-notify"},
"/status",
)
if err != nil {
Expand All @@ -136,7 +135,7 @@ func main() {
}

attachment := slack.Attachment{
Color: "#f9a41b",
Color: color,
CallbackID: pollName,
Title: pollTitle,
TitleLink: pollTitle,
Expand Down
12 changes: 12 additions & 0 deletions internal/slack-order/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.23-alpine as build
LABEL org.opencontainers.image.source=https://github.com/kndpio/function-poll

WORKDIR /build
COPY ./internal/slack-order .
RUN go mod tidy
RUN go build -o slack-order

FROM golang:1.23-alpine
WORKDIR /app
COPY --from=build /build/slack-order /app/
CMD [ "./slack-order" ]
13 changes: 13 additions & 0 deletions internal/slack-order/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module slack-order

go 1.23.1

require github.com/slack-go/slack v0.14.0

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
)
22 changes: 22 additions & 0 deletions internal/slack-order/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/slack-go/slack v0.14.0 h1:6c0UTfbRnvRssZUsZ2qe0Iu07VAMPjRqOa6oX8ewF4k=
github.com/slack-go/slack v0.14.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 645ae8b

Please sign in to comment.