Skip to content

Commit

Permalink
use pointer to update token
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaguilar committed Sep 25, 2023
1 parent 5ebabd8 commit 5eef67e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
43 changes: 26 additions & 17 deletions internal/tfhandler/qworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,54 @@ 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)
log.Printf(".... ERROR: %s \t(%s/%s)", err, namespace, name)
i.requeueAfter(tf, failureRequeueRate, false)
continue
}

if result == nil {
log.Printf(".... API did not return results \t(%s/%s)", namespace, name)
i.requeueAfter(tf, failureRequeueRate, false)
continue
}

if strings.Contains(result.Data.StatusInfo.Message, "workflow has not completed") {
i.requeueAfter(tf, 30*time.Second)
i.requeueAfter(tf, 30*time.Second, true)
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. Slowing down requeue rate.
i.requeueAfter(tf, failureRequeueRate, false)
continue
}

// The result returned from the API was successful. Validate the data before removing from queue
list, ok := result.Data.Data.([]interface{})
// In the following results, if validate the API response structure occurs, there might be
// breaking changes in either this client or in the API. Requeue and hope it was a blip.
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)
log.Printf(".... ERROR api response in unexpected format %T \t(%s/%s)", result.Data.Data, namespace, name)
i.requeueAfter(tf, failureRequeueRate, false)
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)
log.Printf(".... ERROR api response item in unexpected format %T \t(%s/%s)", item, namespace, name)
i.requeueAfter(tf, failureRequeueRate, false)
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)
log.Printf(".... ERROR api response item cannot be decoded \t(%s/%s)", namespace, name)
i.requeueAfter(tf, failureRequeueRate, false)
continue MainLoop
}
}
Expand Down Expand Up @@ -125,10 +132,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, showGenericWaitingMsg bool) {
go func() {
time.Sleep(t)
i.queue.PushBack(tf)
}()
log.Printf(".... Waiting for workflow completion. \t(%s/%s)", tf.Namespace, tf.Name)
if showGenericWaitingMsg {
log.Printf(".... Waiting for workflow completion. \t(%s/%s)", 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

0 comments on commit 5eef67e

Please sign in to comment.