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

use pointer to update token #8

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions internal/tfhandler/qworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,48 @@ MainLoop:
continue
}

failureRequeueRate := 100 * time.Second

result, err := i.clientset.Cluster(i.clusterName).Poll(namespace, name).Read(ctx, &tf)
if err != nil {
log.Println(err)
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... ERROR: %s)", err))
continue
}

if result == nil {
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... API did not return results)"))
continue
}

if strings.Contains(result.Data.StatusInfo.Message, "workflow has not completed") {
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, 30*time.Second, fmt.Sprintf(".... Waiting for workflow completion)"))
continue
}

if !result.IsSuccess {
log.Printf(".... %s \t(%s/%s)", result.ErrMsg, namespace, name)
if strings.Contains(result.ErrMsg, fmt.Sprintf(`terraforms.tf.galleybytes.com "%s" not found`, name)) {
// Do not requeue since this resource is not even registered with the API
continue
}
i.requeueAfter(tf, 30*time.Second)
// Even after a failure, continue to check for a success. There is a possibility a debug session
// may fix the session.
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... %s)", result.ErrMsg))
continue
}

// The result returned from the API was successful. Validate the data before removing from queue
list, ok := result.Data.Data.([]interface{})
// Validate the API response structure. If faiure are detected, requeue and hope it was a network blip.
// ... else there might be breaking changes in either this client or in the API.
if !ok {
log.Printf("ERROR api response in unexpected format %T \t(%s/%s)", result.Data.Data, namespace, name)
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... ERROR api response in unexpected format %T)", result.Data.Data))
continue
}

for _, item := range list {
_, ok := item.(string)
if !ok {
log.Printf("ERROR api response item in unexpected format %T \t(%s/%s)", item, namespace, name)
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... ERROR api response item in unexpected format %T)", item))
continue MainLoop
}
_, err := base64.StdEncoding.DecodeString(item.(string))
if err != nil {
log.Printf("ERROR api response item cannot be decoded \t(%s/%s)", namespace, name)
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, failureRequeueRate, fmt.Sprintf(".... ERROR api response item cannot be decoded)"))
continue MainLoop
}
}
Expand Down Expand Up @@ -125,10 +126,12 @@ func shouldPoll(tf tfv1beta1.Terraform) bool {
return tf.Spec.OutputsSecret != ""
}

func (i informer) requeueAfter(tf tfv1beta1.Terraform, t time.Duration) {
func (i informer) requeueAfter(tf tfv1beta1.Terraform, t time.Duration, msg string) {
go func() {
time.Sleep(t)
i.queue.PushBack(tf)
}()
log.Printf(".... Waiting for workflow completion. \t(%s/%s)", tf.Namespace, tf.Name)

log.Printf(".... %s \t(%s/%s)", msg, tf.Namespace, tf.Name)

}
2 changes: 1 addition & 1 deletion internal/tfhandler/tfhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (i informer) createAndUpdate(tf *tfv1beta1.Terraform) {
for _, action := range []string{"CREATE", "UPDATE"} {
result := eventQueryRetrier(i.clientset.Cluster(i.clusterName).Event(), action, tf)
if result == nil {
log.Println("An unknown error has occurred")
log.Printf("Unknown error occurred in %s \t(%s/%s)\n", action, tf.Namespace, tf.Name)
return
}
if !result.next {
Expand Down
4 changes: 2 additions & 2 deletions pkg/tfoapiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type config struct {

type Clientset struct {
http.Client
config config
config *config
}

type ClientSetup struct {
Expand All @@ -70,7 +70,7 @@ func NewClientset(host, username, password string, insecureSkipVerify bool) (*Cl
client := http.Client{Transport: tr}
tfoapiClientset := Clientset{
Client: client,
config: config{
config: &config{
Host: host,
Username: username,
Password: password,
Expand Down