From d363fa29d6b2b00425d1c6c4ea8adfd211a3b9d0 Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Fri, 12 Dec 2025 10:37:30 +0100 Subject: [PATCH] Context propagation for ProfileClient - pass context into ProfileClient Issue-ID: MULTICLOUD-1538 Change-Id: Ia77f784d03ff810b9f65d079a9964a7707813c52 Signed-off-by: Fiete Ostkamp --- src/k8splugin/api/profilehandler.go | 14 +++---- src/k8splugin/api/profilehandler_test.go | 17 ++++---- src/k8splugin/internal/app/config_backend.go | 4 +- src/k8splugin/internal/app/instance.go | 12 +++--- src/k8splugin/internal/app/subscription.go | 16 ++++---- src/k8splugin/internal/rb/definition.go | 6 +-- src/k8splugin/internal/rb/profile.go | 60 ++++++++++++++-------------- src/k8splugin/internal/rb/profile_test.go | 15 +++---- 8 files changed, 74 insertions(+), 70 deletions(-) diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go index 1babc4ae..9162c792 100644 --- a/src/k8splugin/api/profilehandler.go +++ b/src/k8splugin/api/profilehandler.go @@ -56,7 +56,7 @@ func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) return } - ret, err := h.client.CreateOrUpdate(p, false) + ret, err := h.client.CreateOrUpdate(r.Context(), p, false) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -89,7 +89,7 @@ func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request) return } - err = h.client.Upload(rbName, rbVersion, prName, inpBytes) + err = h.client.Upload(r.Context(), rbName, rbVersion, prName, inpBytes) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -106,7 +106,7 @@ func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) { rbVersion := vars["rbversion"] prName := vars["prname"] - ret, err := h.client.Get(rbName, rbVersion, prName) + ret, err := h.client.Get(r.Context(), rbName, rbVersion, prName) if err != nil { // Separate "Not found" from generic DB errors if strings.Contains(err.Error(), "Error finding") { @@ -135,7 +135,7 @@ func (h rbProfileHandler) updateHandler(w http.ResponseWriter, r *http.Request) rbVersion := vars["rbversion"] prName := vars["prname"] - ret, err := h.client.Get(rbName, rbVersion, prName) + ret, err := h.client.Get(r.Context(), rbName, rbVersion, prName) if err != nil { // Separate "Not found" from generic DB errors if strings.Contains(err.Error(), "Error finding") { @@ -178,7 +178,7 @@ func (h rbProfileHandler) updateHandler(w http.ResponseWriter, r *http.Request) p.RBVersion = ret.RBVersion p.RBName = ret.RBName - ret, err = h.client.CreateOrUpdate(p, true) + ret, err = h.client.CreateOrUpdate(r.Context(), p, true) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) @@ -196,7 +196,7 @@ func (h rbProfileHandler) listHandler(w http.ResponseWriter, r *http.Request) { rbName := vars["rbname"] rbVersion := vars["rbversion"] - ret, err := h.client.List(rbName, rbVersion) + ret, err := h.client.List(r.Context(), rbName, rbVersion) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -218,7 +218,7 @@ func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) rbVersion := vars["rbversion"] prName := vars["prname"] - err := h.client.Delete(rbName, rbVersion, prName) + err := h.client.Delete(r.Context(), rbName, rbVersion, prName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index 57040668..d010ad3c 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -18,6 +18,7 @@ package api import ( "bytes" + "context" "encoding/json" "io" "net/http" @@ -31,9 +32,9 @@ import ( pkgerrors "github.com/pkg/errors" ) -//Creating an embedded interface via anonymous variable -//This allows us to make mockDB satisfy the DatabaseConnection -//interface even if we are not implementing all the methods in it +// Creating an embedded interface via anonymous variable +// This allows us to make mockDB satisfy the DatabaseConnection +// interface even if we are not implementing all the methods in it type mockRBProfile struct { rb.ProfileManager // Items and err will be used to customize each test @@ -42,7 +43,7 @@ type mockRBProfile struct { Err error } -func (m *mockRBProfile) CreateOrUpdate(inp rb.Profile, update bool) (rb.Profile, error) { +func (m *mockRBProfile) CreateOrUpdate(ctx context.Context, inp rb.Profile, update bool) (rb.Profile, error) { if m.Err != nil { return rb.Profile{}, m.Err } @@ -50,7 +51,7 @@ func (m *mockRBProfile) CreateOrUpdate(inp rb.Profile, update bool) (rb.Profile, return m.Items[0], nil } -func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error) { +func (m *mockRBProfile) Get(ctx context.Context, rbname, rbversion, prname string) (rb.Profile, error) { if m.Err != nil { return rb.Profile{}, m.Err } @@ -58,7 +59,7 @@ func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error return m.Items[0], nil } -func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) { +func (m *mockRBProfile) List(ctx context.Context, rbname, rbversion string) ([]rb.Profile, error) { if m.Err != nil { return []rb.Profile{}, m.Err } @@ -66,11 +67,11 @@ func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) { return m.Items, nil } -func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error { +func (m *mockRBProfile) Delete(ctx context.Context, rbname, rbversion, prname string) error { return m.Err } -func (m *mockRBProfile) Upload(rbname, rbversion, prname string, inp []byte) error { +func (m *mockRBProfile) Upload(ctx context.Context, rbname, rbversion, prname string, inp []byte) error { return m.Err } diff --git a/src/k8splugin/internal/app/config_backend.go b/src/k8splugin/internal/app/config_backend.go index b49f0708..7dcb797d 100644 --- a/src/k8splugin/internal/app/config_backend.go +++ b/src/k8splugin/internal/app/config_backend.go @@ -619,7 +619,7 @@ var resolve = func(rbName, rbVersion, profileName, instanceId string, p Config, var resTemplates []helm.KubernetesResourceTemplate profileClient := rb.NewProfileClient() - profile, err := profileClient.Get(rbName, rbVersion, profileName) + profile, err := profileClient.Get(context.TODO(), rbName, rbVersion, profileName) if err != nil { return configResourceList{}, pkgerrors.Wrap(err, "Reading Profile Data") } @@ -718,7 +718,7 @@ var resolve = func(rbName, rbVersion, profileName, instanceId string, p Config, //Download and process the profile first //If everything seems okay, then download the config templates - prYamlClient, err := profileClient.GetYamlClient(rbName, rbVersion, profileName) + prYamlClient, err := profileClient.GetYamlClient(context.TODO(), rbName, rbVersion, profileName) if err != nil { return configResourceList{}, pkgerrors.Wrap(err, "Processing Profile Manifest") } diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index db779622..920c65c7 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -263,7 +263,7 @@ func (v *InstanceClient) Create(ctx context.Context, i InstanceRequest, newId st } //Check if profile exists - profile, err := rb.NewProfileClient().Get(i.RBName, i.RBVersion, i.ProfileName) + profile, err := rb.NewProfileClient().Get(ctx, i.RBName, i.RBVersion, i.ProfileName) if err != nil { return InstanceResponse{}, pkgerrors.New("Unable to find Profile to create instance") } @@ -282,7 +282,7 @@ func (v *InstanceClient) Create(ctx context.Context, i InstanceRequest, newId st overrideValues = append(overrideValues, "k8s-rb-instance-id="+finalId) //Execute the kubernetes create command - sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName) + sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(ctx, i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName) if err != nil { namegenerator.Release(ctx, generatedId) return InstanceResponse{}, pkgerrors.Wrap(err, "Error resolving helm charts") @@ -527,7 +527,7 @@ func (v *InstanceClient) Upgrade(ctx context.Context, id string, u UpgradeReques } //Check if profile exists - profile, err := rb.NewProfileClient().Get(i.RBName, i.RBVersion, i.ProfileName) + profile, err := rb.NewProfileClient().Get(ctx, i.RBName, i.RBVersion, i.ProfileName) if err != nil { return InstanceResponse{}, pkgerrors.New("Unable to find Profile to create instance") } @@ -537,7 +537,7 @@ func (v *InstanceClient) Upgrade(ctx context.Context, id string, u UpgradeReques overrideValues = append(overrideValues, "k8s-rb-instance-id="+id) //Execute the kubernetes create command - sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName) + sortedTemplates, crdList, hookList, releaseName, err := rb.NewProfileClient().Resolve(ctx, i.RBName, i.RBVersion, i.ProfileName, overrideValues, i.ReleaseName) if err != nil { return InstanceResponse{}, pkgerrors.Wrap(err, "Error resolving helm charts") } @@ -852,7 +852,7 @@ func (v *InstanceClient) Status(ctx context.Context, id string, checkReady bool) return InstanceStatus{}, pkgerrors.Wrap(err, "Getting CloudRegion Information") } req := resResp.Request - profile, err := rb.NewProfileClient().Get(req.RBName, req.RBVersion, req.ProfileName) + profile, err := rb.NewProfileClient().Get(ctx, req.RBName, req.RBVersion, req.ProfileName) if err != nil { return InstanceStatus{}, pkgerrors.New("Unable to find Profile instance status") } @@ -1191,7 +1191,7 @@ func (v *InstanceClient) RecoverCreateOrDelete(ctx context.Context, id string) e ID: id, } log.Printf(" Resolving template for release %s", instance.Request.ReleaseName) - _, _, hookList, _, _ := rb.NewProfileClient().Resolve(instance.Request.RBName, instance.Request.RBVersion, instance.Request.ProfileName, overrideValues, instance.Request.ReleaseName) + _, _, hookList, _, _ := rb.NewProfileClient().Resolve(ctx, instance.Request.RBName, instance.Request.RBVersion, instance.Request.ProfileName, overrideValues, instance.Request.ReleaseName) instance.Hooks = hookList err = db.DBconn.Update(ctx, v.storeName, key, v.tagInst, instance) if err != nil { diff --git a/src/k8splugin/internal/app/subscription.go b/src/k8splugin/internal/app/subscription.go index 6b4e5654..cad23e21 100644 --- a/src/k8splugin/internal/app/subscription.go +++ b/src/k8splugin/internal/app/subscription.go @@ -148,7 +148,8 @@ type InstanceStatusSubManager interface { // Create Status Subscription func (iss *InstanceStatusSubClient) Create(ctx context.Context, instanceId string, subDetails SubscriptionRequest) (StatusSubscription, error) { - + ctx, span := tracer.Start(ctx, "InstanceStatusSubClient.Create") + defer span.End() _, err := iss.Get(ctx, instanceId, subDetails.Name) if err == nil { return StatusSubscription{}, pkgerrors.New("Subscription already exists") @@ -174,7 +175,7 @@ func (iss *InstanceStatusSubClient) Create(ctx context.Context, instanceId strin sub.NotifyMetadata = make(map[string]interface{}) } - err = iss.refreshWatchers(instanceId, subDetails.Name) + err = iss.refreshWatchers(ctx, instanceId, subDetails.Name) if err != nil { return sub, pkgerrors.Wrap(err, "Creating Status Subscription DB Entry") } @@ -198,6 +199,8 @@ func (iss *InstanceStatusSubClient) Create(ctx context.Context, instanceId strin // Get Status subscription func (iss *InstanceStatusSubClient) Get(ctx context.Context, instanceId, subId string) (StatusSubscription, error) { + ctx, span := tracer.Start(ctx, "InstanceStatusSubClient.Get") + defer span.End() lock, _, _ := getSubscriptionData(instanceId) // Acquire Mutex lock.Lock() @@ -240,7 +243,7 @@ func (iss *InstanceStatusSubClient) Update(ctx context.Context, instanceId, subI sub.NotifyMetadata = make(map[string]interface{}) } - err = iss.refreshWatchers(instanceId, subDetails.Name) + err = iss.refreshWatchers(ctx, instanceId, subDetails.Name) if err != nil { return sub, pkgerrors.Wrap(err, "Updating Status Subscription DB Entry") } @@ -372,7 +375,7 @@ func (iss *InstanceStatusSubClient) RestoreWatchers(ctx context.Context) { } for _, sub := range subList { - err = iss.refreshWatchers(instance.ID, sub.Name) + err = iss.refreshWatchers(ctx, instance.ID, sub.Name) if err != nil { log.Error("Error on refreshing watchers", log.Fields{ "error": err.Error(), @@ -387,19 +390,18 @@ func (iss *InstanceStatusSubClient) RestoreWatchers(ctx context.Context) { }() } -func (iss *InstanceStatusSubClient) refreshWatchers(instanceId, subId string) error { +func (iss *InstanceStatusSubClient) refreshWatchers(ctx context.Context, instanceId, subId string) error { log.Info("REFRESH WATCHERS", log.Fields{ "instance": instanceId, "subscription": subId, }) v := NewInstanceClient() k8sClient := KubernetesClient{} - ctx := context.TODO() instance, err := v.Get(ctx, instanceId) if err != nil { return pkgerrors.Wrap(err, "Cannot get instance for notify thread") } - profile, err := rb.NewProfileClient().Get(instance.Request.RBName, instance.Request.RBVersion, + profile, err := rb.NewProfileClient().Get(ctx, instance.Request.RBName, instance.Request.RBVersion, instance.Request.ProfileName) if err != nil { return pkgerrors.Wrap(err, "Unable to find Profile instance status") diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 55c2c7cf..bd3cfac1 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -106,7 +106,7 @@ func (v *DefinitionClient) Create(ctx context.Context, def Definition) (Definiti // Create a default profile automatically prc := NewProfileClient() - pr, err := prc.CreateOrUpdate(Profile{ + pr, err := prc.CreateOrUpdate(ctx, Profile{ RBName: def.RBName, RBVersion: def.RBVersion, ProfileName: "default", @@ -126,7 +126,7 @@ func (v *DefinitionClient) Create(ctx context.Context, def Definition) (Definiti return Definition{}, pkgerrors.Wrap(err, "Creating Default Profile") } - err = prc.Upload(pr.RBName, pr.RBVersion, pr.ProfileName, prc.getEmptyProfile()) + err = prc.Upload(ctx, pr.RBName, pr.RBVersion, pr.ProfileName, prc.getEmptyProfile()) if err != nil { logutils.Error("Upload Empty Profile", logutils.Fields{ "error": err, @@ -232,7 +232,7 @@ func (v *DefinitionClient) Delete(ctx context.Context, name string, version stri //Delete the default profile as well prc := NewProfileClient() - err = prc.Delete(name, version, "default") + err = prc.Delete(ctx, name, version, "default") if err != nil { logutils.Error("Delete Default Profile", logutils.Fields{ "error": err, diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index 1f935541..c8b58cd8 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -47,11 +47,11 @@ type Profile struct { // ProfileManager is an interface exposes the resource bundle profile functionality type ProfileManager interface { - CreateOrUpdate(def Profile, update bool) (Profile, error) - Get(rbName, rbVersion, prName string) (Profile, error) - List(rbName, rbVersion string) ([]Profile, error) - Delete(rbName, rbVersion, prName string) error - Upload(rbName, rbVersion, prName string, inp []byte) error + CreateOrUpdate(ctx context.Context, def Profile, update bool) (Profile, error) + Get(ctx context.Context, rbName, rbVersion, prName string) (Profile, error) + List(ctx context.Context, rbName, rbVersion string) ([]Profile, error) + Delete(ctx context.Context, rbName, rbVersion, prName string) error + Upload(ctx context.Context, rbName, rbVersion, prName string, inp []byte) error } type ProfileKey struct { @@ -91,7 +91,7 @@ func NewProfileClient() *ProfileClient { } // CreateOrUpdate an entry for the resource bundle profile in the database -func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) { +func (v *ProfileClient) CreateOrUpdate(ctx context.Context, p Profile, update bool) (Profile, error) { // Name is required if p.ProfileName == "" { @@ -99,7 +99,7 @@ func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) } //Check if profile already exists - _, err := v.Get(p.RBName, p.RBVersion, p.ProfileName) + _, err := v.Get(ctx, p.RBName, p.RBVersion, p.ProfileName) if err == nil && !update { return Profile{}, pkgerrors.New("Profile already exists for this Definition") } @@ -107,7 +107,7 @@ func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) return Profile{}, pkgerrors.New("Profile does not exists for this Definition") } //Check if provided resource bundle information is valid - _, err = NewDefinitionClient().Get(context.TODO(), p.RBName, p.RBVersion) + _, err = NewDefinitionClient().Get(ctx, p.RBName, p.RBVersion) if err != nil { return Profile{}, pkgerrors.Errorf("Invalid Resource Bundle ID provided: %s", err.Error()) } @@ -124,12 +124,12 @@ func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) } if update { - err = db.DBconn.Update(context.TODO(), v.storeName, key, v.tagMeta, p) + err = db.DBconn.Update(ctx, v.storeName, key, v.tagMeta, p) if err != nil { return Profile{}, pkgerrors.Wrap(err, "Updating Profile DB Entry") } } else { - err = db.DBconn.Create(context.TODO(), v.storeName, key, v.tagMeta, p) + err = db.DBconn.Create(ctx, v.storeName, key, v.tagMeta, p) if err != nil { return Profile{}, pkgerrors.Wrap(err, "Creating Profile DB Entry") } @@ -139,13 +139,13 @@ func (v *ProfileClient) CreateOrUpdate(p Profile, update bool) (Profile, error) } // Get returns the Resource Bundle Profile for corresponding ID -func (v *ProfileClient) Get(rbName, rbVersion, prName string) (Profile, error) { +func (v *ProfileClient) Get(ctx context.Context, rbName, rbVersion, prName string) (Profile, error) { key := ProfileKey{ RBName: rbName, RBVersion: rbVersion, ProfileName: prName, } - value, err := db.DBconn.Read(context.TODO(), v.storeName, key, v.tagMeta) + value, err := db.DBconn.Read(ctx, v.storeName, key, v.tagMeta) if err != nil { return Profile{}, pkgerrors.Wrap(err, "Get Resource Bundle Profile") } @@ -164,10 +164,10 @@ func (v *ProfileClient) Get(rbName, rbVersion, prName string) (Profile, error) { } // List returns the Resource Bundle Profile for corresponding ID -func (v *ProfileClient) List(rbName, rbVersion string) ([]Profile, error) { +func (v *ProfileClient) List(ctx context.Context, rbName, rbVersion string) ([]Profile, error) { //Get all profiles - dbres, err := db.DBconn.ReadAll(context.TODO(), v.storeName, v.tagMeta) + dbres, err := db.DBconn.ReadAll(ctx, v.storeName, v.tagMeta) if err != nil || len(dbres) == 0 { return []Profile{}, pkgerrors.Wrap(err, "No Profiles Found") } @@ -196,18 +196,18 @@ func (v *ProfileClient) List(rbName, rbVersion string) ([]Profile, error) { } // Delete the Resource Bundle Profile from database -func (v *ProfileClient) Delete(rbName, rbVersion, prName string) error { +func (v *ProfileClient) Delete(ctx context.Context, rbName, rbVersion, prName string) error { key := ProfileKey{ RBName: rbName, RBVersion: rbVersion, ProfileName: prName, } - err := db.DBconn.Delete(context.TODO(), v.storeName, key, v.tagMeta) + err := db.DBconn.Delete(ctx, v.storeName, key, v.tagMeta) if err != nil { return pkgerrors.Wrap(err, "Delete Resource Bundle Profile") } - err = db.DBconn.Delete(context.TODO(), v.storeName, key, v.tagContent) + err = db.DBconn.Delete(ctx, v.storeName, key, v.tagContent) if err != nil { return pkgerrors.Wrap(err, "Delete Resource Bundle Profile Content") } @@ -216,10 +216,10 @@ func (v *ProfileClient) Delete(rbName, rbVersion, prName string) error { } // Upload the contents of resource bundle into database -func (v *ProfileClient) Upload(rbName, rbVersion, prName string, inp []byte) error { +func (v *ProfileClient) Upload(ctx context.Context, rbName, rbVersion, prName string, inp []byte) error { //ignore the returned data here. - _, err := v.Get(rbName, rbVersion, prName) + _, err := v.Get(ctx, rbName, rbVersion, prName) if err != nil { return pkgerrors.Errorf("Invalid Profile Name provided %s", err.Error()) } @@ -236,7 +236,7 @@ func (v *ProfileClient) Upload(rbName, rbVersion, prName string, inp []byte) err } //Encode given byte stream to text for storage encodedStr := base64.StdEncoding.EncodeToString(inp) - err = db.DBconn.Create(context.TODO(), v.storeName, key, v.tagContent, encodedStr) + err = db.DBconn.Create(ctx, v.storeName, key, v.tagContent, encodedStr) if err != nil { return pkgerrors.Errorf("Error uploading data to db %s", err.Error()) } @@ -247,11 +247,11 @@ func (v *ProfileClient) Upload(rbName, rbVersion, prName string, inp []byte) err // Download the contents of the resource bundle profile from DB // Returns a byte array of the contents which is used by the // ExtractTarBall code to create the folder structure on disk -func (v *ProfileClient) Download(rbName, rbVersion, prName string) ([]byte, error) { +func (v *ProfileClient) Download(ctx context.Context, rbName, rbVersion, prName string) ([]byte, error) { //ignore the returned data here //Check if id is valid - _, err := v.Get(rbName, rbVersion, prName) + _, err := v.Get(ctx, rbName, rbVersion, prName) if err != nil { return nil, pkgerrors.Errorf("Invalid Profile Name provided: %s", err.Error()) } @@ -261,7 +261,7 @@ func (v *ProfileClient) Download(rbName, rbVersion, prName string) ([]byte, erro RBVersion: rbVersion, ProfileName: prName, } - value, err := db.DBconn.Read(context.TODO(), v.storeName, key, v.tagContent) + value, err := db.DBconn.Read(ctx, v.storeName, key, v.tagContent) if err != nil { return nil, pkgerrors.Wrap(err, "Get Resource Bundle Profile content") } @@ -281,8 +281,8 @@ func (v *ProfileClient) Download(rbName, rbVersion, prName string) ([]byte, erro } // GetYamlClient GEt Yaml Files client for profile -func (v *ProfileClient) GetYamlClient(rbName string, rbVersion string, profileName string) (ProfileYamlClient, error) { - prData, err := v.Download(rbName, rbVersion, profileName) +func (v *ProfileClient) GetYamlClient(ctx context.Context, rbName string, rbVersion string, profileName string) (ProfileYamlClient, error) { + prData, err := v.Download(ctx, rbName, rbVersion, profileName) if err != nil { return ProfileYamlClient{}, pkgerrors.Wrap(err, "Downloading Profile") } @@ -301,7 +301,7 @@ func (v *ProfileClient) GetYamlClient(rbName string, rbVersion string, profileNa // Resolve returns the path where the helm chart merged with // configuration overrides resides and final ReleaseName picked for instantiation -func (v *ProfileClient) Resolve(rbName string, rbVersion string, +func (v *ProfileClient) Resolve(ctx context.Context, rbName string, rbVersion string, profileName string, values []string, overrideReleaseName string) ([]helm.KubernetesResourceTemplate, []helm.KubernetesResourceTemplate, []*helm.Hook, string, error) { var sortedTemplates []helm.KubernetesResourceTemplate @@ -311,19 +311,19 @@ func (v *ProfileClient) Resolve(rbName string, rbVersion string, //Download and process the profile first //If everything seems okay, then download the definition - prYamlClient, err := v.GetYamlClient(rbName, rbVersion, profileName) + prYamlClient, err := v.GetYamlClient(ctx, rbName, rbVersion, profileName) if err != nil { return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Processing Profile Manifest") } definitionClient := NewDefinitionClient() - definition, err := definitionClient.Get(context.TODO(), rbName, rbVersion) + definition, err := definitionClient.Get(ctx, rbName, rbVersion) if err != nil { return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Getting Definition Metadata") } - defData, err := definitionClient.Download(context.TODO(), rbName, rbVersion) + defData, err := definitionClient.Download(ctx, rbName, rbVersion) if err != nil { return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Downloading Definition") } @@ -334,7 +334,7 @@ func (v *ProfileClient) Resolve(rbName string, rbVersion string, } //Get the definition ID and download its contents - profile, err := v.Get(rbName, rbVersion, profileName) + profile, err := v.Get(ctx, rbName, rbVersion, profileName) if err != nil { return sortedTemplates, crdList, hookList, finalReleaseName, pkgerrors.Wrap(err, "Getting Profile") } diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index e52897ce..314815c8 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -18,6 +18,7 @@ package rb import ( "bytes" + "context" "os" "reflect" "sort" @@ -105,7 +106,7 @@ func TestCreateProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - got, err := impl.CreateOrUpdate(testCase.inp, false) + got, err := impl.CreateOrUpdate(context.TODO(), testCase.inp, false) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) @@ -173,7 +174,7 @@ func TestGetProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - got, err := impl.Get(testCase.rbname, testCase.rbversion, testCase.prname) + got, err := impl.Get(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname) if err != nil { if testCase.expectedError == "" { t.Fatalf("Get returned an unexpected error %s", err) @@ -262,7 +263,7 @@ func TestListProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - got, err := impl.List(testCase.rbdef, testCase.version) + got, err := impl.List(context.TODO(), testCase.rbdef, testCase.version) if err != nil { if testCase.expectedError == "" { t.Fatalf("List returned an unexpected error %s", err) @@ -319,7 +320,7 @@ func TestDeleteProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - err := impl.Delete(testCase.rbname, testCase.rbversion, testCase.prname) + err := impl.Delete(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname) if err != nil { if testCase.expectedError == "" { t.Fatalf("Delete returned an unexpected error %s", err) @@ -479,7 +480,7 @@ func TestUploadProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - err := impl.Upload(testCase.rbname, testCase.rbversion, testCase.prname, testCase.content) + err := impl.Upload(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname, testCase.content) if err != nil { if testCase.expectedError == "" { t.Errorf("Upload returned an unexpected error %s", err) @@ -580,7 +581,7 @@ func TestDownloadProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - data, err := impl.Download(testCase.rbname, testCase.rbversion, testCase.prname) + data, err := impl.Download(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname) if err != nil { if testCase.expectedError == "" { t.Errorf("Download returned an unexpected error %s", err) @@ -773,7 +774,7 @@ func TestResolveProfile(t *testing.T) { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProfileClient() - data, _, _, releaseName, err := impl.Resolve(testCase.rbname, + data, _, _, releaseName, err := impl.Resolve(context.TODO(), testCase.rbname, testCase.rbversion, testCase.prname, []string{}, testCase.releaseName) defer cleanup(data) if err != nil { -- 2.16.6