Skip to content

Commit

Permalink
update state on next steps
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykorwat committed Aug 26, 2024
1 parent 0219c2b commit bfb9c24
Showing 1 changed file with 81 additions and 60 deletions.
141 changes: 81 additions & 60 deletions pkg/registry/hykube/provider/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"archive/zip"
"context"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform/providers"
"hykube.io/apiserver/pkg/apis/hykube"
"hykube.io/apiserver/pkg/apis/hykube/v1alpha1"
Expand All @@ -32,6 +33,7 @@ import (
"os"
"os/exec"
"strings"
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
Expand All @@ -44,9 +46,9 @@ type watcher struct {
}

func (p *watcher) Start() {
//client := resty.New().
// SetRetryCount(3).
// SetRetryWaitTime(1 * time.Second)
client := resty.New().
SetRetryCount(3).
SetRetryWaitTime(1 * time.Second)
go func() {
for {
//var event *watch2.Event
Expand All @@ -67,69 +69,88 @@ func (p *watcher) Start() {
preconditions := storage.Preconditions{UID: &provider.UID, ResourceVersion: &provider.ResourceVersion}

out := p.store.NewFunc()
err = p.store.Storage.GuaranteedUpdate(
ctx, key, out, false, &preconditions,
storage.SimpleUpdate(func(existing runtime.Object) (runtime.Object, error) {
existingProvider, ok := existing.(*hykube.Provider)
if !ok {
// wrong type
return nil, fmt.Errorf("expected *hykube.Provider, got %v", existing)
}
existingProvider.Status = "WOW"

return existingProvider, nil
}),
false, // watcher doesn't get notified if it's' dry run
nil,
)

err = p.updateProviderStatus(ctx, key, out, preconditions, "downloading provider")
if err != nil {
klog.ErrorS(err, "cannot update provider")
continue
}

fullURLFile := "https://releases.hashicorp.com/terraform-provider-aws/5.62.0/terraform-provider-aws_5.62.0_linux_amd64.zip" // TODO: detect latest version by default

filename := "aws-provider.zip"
_, err = client.R().
SetOutput(filename).
Get(fullURLFile)
if err != nil {
klog.ErrorS(err, "Couldn't download file")
return
}

klog.Infof("Downloaded a provider from: %s", fullURLFile)
err = p.updateProviderStatus(ctx, key, out, preconditions, "downloaded provider")
if err != nil {
klog.ErrorS(err, "cannot update provider")
continue
}

err = p.updateProviderStatus(ctx, key, out, preconditions, "extracting provider")
if err != nil {
klog.ErrorS(err, "cannot update provider")
continue
}

providerFilename, err := p.extractFile(err, filename)
if err != nil {
klog.ErrorS(err, "Couldn't extract file")
return
}

err = p.updateProviderStatus(ctx, key, out, preconditions, "getting provider schema")
if err != nil {
klog.ErrorS(err, "cannot update provider")
continue
}

schema, err := p.getProviderSchema(providerFilename, false)
if err != nil {
klog.ErrorS(err, "Couldn't get provider schema")
return
}

err = p.updateProviderStatus(ctx, key, out, preconditions, "adding CRDs")
if err != nil {
klog.ErrorS(err, "cannot update provider")
continue
}

err = p.addCDRs(schema)
if err != nil {
klog.ErrorS(err, "Couldn't get provider schema")
return
}

klog.Infof("Downloaded a provider from: %s", fullURLFile)
}
}
}()
//return func(obj runtime.Object, options *meta_v1.CreateOptions) {
// go func() {
//
// p.store.Storage.GuaranteedUpdate()
//
// //fullURLFile := "https://releases.hashicorp.com/terraform-provider-aws/5.62.0/terraform-provider-aws_5.62.0_linux_amd64.zip" // TODO: detect latest version by default
// //
// //jj, _ := json.Marshal(obj)
// //klog.Info(string(jj))
// //
// //filename := "aws-provider.zip"
// //_, err := client.R().
// // SetOutput(filename).
// // Get(fullURLFile)
// //if err != nil {
// // klog.ErrorS(err, "Couldn't download file")
// // return
// //}
// //
// //klog.Infof("Downloaded a provider from: %s", fullURLFile)
// //
// //providerFilename, err := extractFile(err, filename)
// //if err != nil {
// // klog.ErrorS(err, "Couldn't extract file")
// // return
// //}
// //
// //schema, err := getProviderSchema(providerFilename, false)
// //if err != nil {
// // klog.ErrorS(err, "Couldn't get provider schema")
// // return
// //}
//
// //err := p.addCDRs()
// //if err != nil {
// // klog.ErrorS(err, "Couldn't get provider schema")
// // return
// //}
//
// }()
//}
}

func (p *watcher) updateProviderStatus(ctx context.Context, key string, out runtime.Object, preconditions storage.Preconditions, statusValue string) error {
return p.store.Storage.GuaranteedUpdate(
ctx, key, out, false, &preconditions,
storage.SimpleUpdate(func(existing runtime.Object) (runtime.Object, error) {
existingProvider, ok := existing.(*hykube.Provider)
if !ok {
// wrong type
return nil, fmt.Errorf("expected *hykube.Provider, got %v", existing)
}
existingProvider.Status = statusValue
return existingProvider, nil
}),
false, // watcher doesn't get notified if it's dry run
nil,
)
}

func (p *watcher) extractFile(err error, filename string) (string, error) {
Expand Down Expand Up @@ -204,7 +225,7 @@ func (p *watcher) getProviderSchema(providerFilename string, verbose bool) (*pro
return &schema, nil
}

func (p *watcher) addCDRs() error {
func (p *watcher) addCDRs(*providers.GetSchemaResponse) error {
config, err := rest.InClusterConfig()
if err != nil {
return err
Expand Down

0 comments on commit bfb9c24

Please sign in to comment.