Skip to content

Commit

Permalink
moved voters key to status of poll xr
Browse files Browse the repository at this point in the history
  • Loading branch information
ca7alindev committed Sep 25, 2024
1 parent e06dc9c commit e0676b9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
9 changes: 5 additions & 4 deletions example/xr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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:"
status:
lastNotificationTime: 1
35 changes: 25 additions & 10 deletions internal/slack-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ 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"`
Done bool `json:"done"`
LastNotificationTime int64 `json:"lastNotificationTime"`
} `json:"status"`
}

Expand Down Expand Up @@ -117,9 +117,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 +130,30 @@ 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,
},
})

// Use the "/status" subresource to update just the status
_, 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
8 changes: 4 additions & 4 deletions internal/slack-notify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ 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"`
Done bool `json:"done"`
LastNotificationTime int64 `json:"lastNotificationTime"`
} `json:"status"`
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func main() {
pollResource.GetObjectMeta().GetName(),
types.MergePatchType,
statusBytes,
metav1.PatchOptions{FieldManager: "slack-collector"},
metav1.PatchOptions{FieldManager: "slack-notify"},
"/status",
)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/slackchannel/slackchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ 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"`
Done bool `json:"done"`
LastNotificationTime int64 `json:"lastNotificationTime"`
} `json:"status"`
}

Expand Down Expand Up @@ -101,7 +101,7 @@ func SlackOrder(input *v1beta1.Input, api *slack.Client, xr *resource.Composite,
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(xr.Resource.Object, &poll); err != nil {
logger.Info("error converting Unstructured to Poll:", err)
}
textContent := countUsers(poll.Spec.Voters)
textContent := countUsers(poll.Status.Voters)

attachment := slack.Attachment{
Color: "#f9a41b",
Expand All @@ -124,7 +124,7 @@ func SlackOrder(input *v1beta1.Input, api *slack.Client, xr *resource.Composite,
} else {
logger.Info("message successfully sent to channel", channelID, timestamp)
}
poll.Spec.Voters = []Voter{}
poll.Status.Voters = []Voter{}
xr.Resource.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(&poll)
if err != nil {
logger.Info("error converting Poll to Unstructured:", err)
Expand Down
18 changes: 9 additions & 9 deletions package/poll.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ spec:
spec:
type: object
properties:
voters:
type: array
items:
properties:
name:
type: string
status:
type: string
type: object
dueOrderTime:
type: integer
dueTakeTime:
Expand All @@ -49,6 +40,15 @@ spec:
status:
type: object
properties:
voters:
type: array
items:
properties:
name:
type: string
status:
type: string
type: object
done:
type: boolean
lastNotificationTime:
Expand Down

0 comments on commit e0676b9

Please sign in to comment.