From 017d56687e0e44dcc3b32f246002bdeb59f5147a Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Fri, 5 Sep 2025 14:37:34 +0200 Subject: [PATCH] feat(client): improved logging, renaming --- config/config.go | 23 ++++++++++-- internal/client/client.go | 25 ++++++++++++- provider/provider.go | 77 +++++++++++++++++++++++++++------------ 3 files changed, 95 insertions(+), 30 deletions(-) diff --git a/config/config.go b/config/config.go index f7d24f6..be6004b 100644 --- a/config/config.go +++ b/config/config.go @@ -33,11 +33,26 @@ func NewConfig(cfgFile string) (*Config, error) { return &config, nil } +func NewCredentials(credFile string) (*Credentials, error) { + var creds Credentials + if _, err := toml.DecodeFile(credFile, &creds); err != nil { + return nil, fmt.Errorf("error decoding creds: %w", err) + } + + return &creds, nil +} + type Config struct { - LogFile string `toml:"log_file"` - Organization string `toml:"organization"` - CloudletKey CloudletKey `toml:"cloudlet"` - Region string `toml:"region"` + LogFile string `toml:"log_file"` + CredentialsFile string `toml:"credentials_file"` + Organization string `toml:"organization"` + CloudletKey CloudletKey `toml:"cloudlet"` + Region string `toml:"region"` +} + +type Credentials struct { + Username string `toml:"username"` + Password string `toml:"password"` } type CloudletKey struct { diff --git a/internal/client/client.go b/internal/client/client.go index f4d81f3..95278d2 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "io" "log" "net/http" @@ -51,26 +52,35 @@ func (e *EdgeConnect) NewAppInstance(ctx context.Context, input NewAppInstanceIn } defer resp.Body.Close() - if resp.StatusCode != 200 { - log.Printf("failed received non 200 status code %v\n", resp.Status) + bytes, err := io.ReadAll(resp.Body) + if err != nil { + log.Printf("err while io.ReadAll: %v\n", err) + return err } + log.Printf("Body %v\n", string(bytes)) + + log.Printf("status code %v\n", resp.Status) + return nil } func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error { token, err := e.RetrieveToken(ctx) if err != nil { + log.Printf("err while RetrieveToken: %v\n", err) return err } json_data, err := json.Marshal(input) if err != nil { + log.Printf("err while Marshal: %v\n", err) return err } request, err := http.NewRequestWithContext(ctx, "POST", e.BaseURL+"/api/v1/auth/ctrl/CreateApp", bytes.NewBuffer(json_data)) if err != nil { + log.Printf("err while NewRequestWithContext: %v\n", err) return err } request.Header.Set("Content-Type", "application/json") @@ -78,10 +88,21 @@ func (e *EdgeConnect) NewApp(ctx context.Context, input NewAppInput) error { resp, err := e.HttpClient.Do(request) if err != nil { + log.Printf("err while HttpClient.Do: %v\n", err) return err } + defer resp.Body.Close() + log.Printf("status code %v\n", resp.Status) + bytes, err := io.ReadAll(resp.Body) + if err != nil { + log.Printf("err while io.ReadAll: %v\n", err) + return err + } + + log.Printf("Body %v\n", string(bytes)) + return nil } diff --git a/provider/provider.go b/provider/provider.go index e96df86..f31a5c6 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -20,7 +20,6 @@ import ( "fmt" "log" "net/http" - "os" "strings" "edp.buildth.ing/DevFW-CICD/garm-provider-edge-connect/config" @@ -42,12 +41,17 @@ func NewEdgeConnectProvider(configPath, controllerID string) (execution.External return nil, "", fmt.Errorf("error loading config: %w", err) } + creds, err := config.NewCredentials(conf.CredentialsFile) + if err != nil { + return nil, "", fmt.Errorf("error loading config: %w", err) + } + client := client.EdgeConnect{ BaseURL: "https://hub.apps.edge.platform.mg3.mdb.osc.live", HttpClient: &http.Client{}, Credentials: client.Credentials{ - Username: os.Getenv("EDGEXR_USERNAME"), - Password: os.Getenv("EDGEXR_PASSWORD"), + Username: creds.Username, + Password: creds.Password, }, } @@ -68,7 +72,7 @@ type edgeConnectProvider struct { func (a *edgeConnectProvider) CreateInstance(ctx context.Context, bootstrapParams params.BootstrapInstance) (params.ProviderInstance, error) { log.Printf("Executing CreateInstance with %v\n", bootstrapParams) - instancename := fmt.Sprintf("garm-%v-%v-%v", a.controllerID, bootstrapParams.PoolID, bootstrapParams.Name) + instancename := fmt.Sprintf("garm-%v-%v-%v", a.controllerID[:8], bootstrapParams.PoolID[:8], strings.ToLower(bootstrapParams.Name)) podv1 := corev1.Pod{ TypeMeta: metav1.TypeMeta{ @@ -194,20 +198,42 @@ func (a *edgeConnectProvider) CreateInstance(ctx context.Context, bootstrapParam func (a *edgeConnectProvider) DeleteInstance(ctx context.Context, instance string) error { log.Printf("Executing DeleteInstance %s\n", instance) - appinstkey := client.AppInstanceKey{ + appsinstances, err := a.client.ShowAppInstances(ctx, client.AppInstanceKey{ Organization: a.cfg.Organization, - Name: instance, - CloudletKey: client.CloudletKey{ - Organization: a.cfg.CloudletKey.Organization, - Name: a.cfg.CloudletKey.Name, - }, - } - - err := a.client.DeleteAppInstance(ctx, appinstkey, a.cfg.Region) + }, a.cfg.Region) if err != nil { return err } + myappintances := filter(appsinstances, func(app client.AppInstance) bool { + return strings.HasSuffix(app.Key.Name, strings.ToLower(instance)) + }) + + for _, v := range myappintances { + err = a.client.DeleteAppInstance(ctx, v.Key, a.cfg.Region) + if err != nil { + return err + } + } + + apps, err := a.client.ShowApps(ctx, client.AppKey{ + Organization: a.cfg.Organization, + }, a.cfg.Region) + if err != nil { + return err + } + + myapps := filter(apps, func(app client.App) bool { + return strings.HasSuffix(app.Key.Name, strings.ToLower(instance)) + }) + + for _, v := range myapps { + err = a.client.DeleteApp(ctx, v.Key, a.cfg.Region) + if err != nil { + return err + } + } + appkey := client.AppKey{ Organization: a.cfg.Organization, Name: instance, @@ -235,20 +261,23 @@ func (a *edgeConnectProvider) GetInstance(ctx context.Context, instance string) Status: params.InstanceStatusUnknown, } - appinstkey := client.AppInstanceKey{ + appinstances, err := a.client.ShowAppInstances(ctx, client.AppInstanceKey{ Organization: a.cfg.Organization, - Name: instance, - CloudletKey: client.CloudletKey{ - Organization: a.cfg.CloudletKey.Organization, - Name: a.cfg.CloudletKey.Name, - }, - } - - appinst, err := a.client.ShowAppInstance(ctx, appinstkey, a.cfg.Region) + }, a.cfg.Region) if err != nil { return params.ProviderInstance{}, err } + myappintances := filter(appinstances, func(app client.AppInstance) bool { + return strings.HasSuffix(app.Key.Name, strings.ToLower(instance)) + }) + + if len(myappintances) == 0 { + return params.ProviderInstance{}, fmt.Errorf("AppInstance not found!") + } + + appinst := myappintances[0] + if appinst.State == "Ready" { providerInstance.Status = params.InstanceRunning } @@ -268,7 +297,7 @@ func (a *edgeConnectProvider) ListInstances(ctx context.Context, poolID string) } myappintances := filter(apps, func(app client.AppInstance) bool { - return strings.HasPrefix(app.Key.Name, fmt.Sprintf("garm-%v-%v", a.controllerID, poolID)) + return strings.HasPrefix(app.Key.Name, fmt.Sprintf("garm-%v-%v", a.controllerID[:8], poolID[:8])) }) providerinstances := []params.ProviderInstance{} @@ -316,7 +345,7 @@ func (a *edgeConnectProvider) RemoveAllInstances(ctx context.Context) error { } myappintances := filter(apps, func(app client.AppInstance) bool { - return strings.HasPrefix(app.Key.Name, fmt.Sprintf("garm-%v", a.controllerID)) + return strings.HasPrefix(app.Key.Name, fmt.Sprintf("garm-%v", a.controllerID[:8])) }) for _, v := range myappintances {