Skip to content

Commit

Permalink
Merge branch 'including-mutex-as-part-of-meta' into helm-framework
Browse files Browse the repository at this point in the history
  • Loading branch information
JaylonmcShan03 committed Oct 24, 2024
2 parents 1da55bd + db5aed3 commit 2bafedf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions helm-framework/helm/data_source_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,17 @@ func (d *HelmTemplate) Read(ctx context.Context, req datasource.ReadRequest, res
state.Namespace = types.StringValue(defaultNamespace)
}

m := d.meta
meta := d.meta

actionConfig, err := m.GetHelmConfiguration(ctx, state.Namespace.ValueString())
actionConfig, err := meta.GetHelmConfiguration(ctx, state.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error getting Helm configuration",
fmt.Sprintf("Error getting Helm configuration might be: %s", err),
)
return
}
diags := OCIRegistryLogin(ctx, actionConfig, m.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
diags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down
21 changes: 9 additions & 12 deletions helm-framework/helm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Meta struct {
HelmDriver string
// Experimental feature toggles
Experiments map[string]bool
Mutex sync.Mutex
}

// HelmProviderModel contains the configuration for the provider
Expand Down Expand Up @@ -623,7 +624,7 @@ func (p *HelmProvider) Configure(ctx context.Context, req provider.ConfigureRequ
return
}

err := OCIRegistryPerformLogin(ctx, meta.RegistryClient, r.URL.ValueString(), r.Username.ValueString(), r.Password.ValueString())
err := OCIRegistryPerformLogin(ctx, meta, meta.RegistryClient, r.URL.ValueString(), r.Username.ValueString(), r.Password.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"OCI Registry login failed",
Expand Down Expand Up @@ -653,12 +654,7 @@ func (p *HelmProvider) Resources(ctx context.Context) []func() resource.Resource
}
}

var (
OCILoginMutex sync.Mutex
loggedInOCIRegistries = make(map[string]string)
)

func OCIRegistryLogin(ctx context.Context, actionConfig *action.Configuration, registryClient *registry.Client, repository, chartName, username, password string) diag.Diagnostics {
func OCIRegistryLogin(ctx context.Context, meta *Meta, actionConfig *action.Configuration, registryClient *registry.Client, repository, chartName, username, password string) diag.Diagnostics {
var diags diag.Diagnostics

actionConfig.RegistryClient = registryClient
Expand All @@ -675,7 +671,7 @@ func OCIRegistryLogin(ctx context.Context, actionConfig *action.Configuration, r
}

if username != "" && password != "" {
err := OCIRegistryPerformLogin(ctx, registryClient, ociURL, username, password)
err := OCIRegistryPerformLogin(ctx, meta, registryClient, ociURL, username, password)
if err != nil {
diags.AddError(
"OCI Registry Login Failed",
Expand All @@ -688,15 +684,16 @@ func OCIRegistryLogin(ctx context.Context, actionConfig *action.Configuration, r
}

// registryClient = client used to comm with the registry, oci urls, un, and pw used for authentication
func OCIRegistryPerformLogin(ctx context.Context, registryClient *registry.Client, ociURL, username, password string) error {
// getting the oci url, and extracting the host.
func OCIRegistryPerformLogin(ctx context.Context, meta *Meta, registryClient *registry.Client, ociURL, username, password string) error {

loggedInOCIRegistries := make(map[string]string)
// getting the oci url, and extracting the host.
u, err := url.Parse(ociURL)
if err != nil {
return fmt.Errorf("could not parse OCI registry URL: %v", err)
}
OCILoginMutex.Lock()
defer OCILoginMutex.Unlock()
meta.Mutex.Lock()
defer meta.Mutex.Unlock()
if _, ok := loggedInOCIRegistries[u.Host]; ok {
tflog.Info(ctx, fmt.Sprintf("Already logged into OCI registry %q", u.Host))
return nil
Expand Down
22 changes: 11 additions & 11 deletions helm-framework/helm/resource_helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, re
resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err))
return
}
ociDiags := OCIRegistryLogin(ctx, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
resp.Diagnostics.Append(ociDiags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -885,7 +885,7 @@ func (r *HelmRelease) Update(ctx context.Context, req resource.UpdateRequest, re
resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err))
return
}
ociDiags := OCIRegistryLogin(ctx, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
resp.Diagnostics.Append(ociDiags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -1636,11 +1636,11 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
logID := fmt.Sprintf("[resourceDiff: %s]", plan.Name.ValueString())
tflog.Debug(ctx, fmt.Sprintf("%s Start", logID))

m := r.meta
meta := r.meta
name := plan.Name.ValueString()
namespace := plan.Namespace.ValueString()

actionConfig, err := m.GetHelmConfiguration(ctx, namespace)
actionConfig, err := meta.GetHelmConfiguration(ctx, namespace)
if err != nil {
resp.Diagnostics.AddError("Error getting Helm configuration", err.Error())
return
Expand All @@ -1652,7 +1652,7 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
repositoryUsername := plan.RepositoryUsername.ValueString()
repositoryPassword := plan.RepositoryPassword.ValueString()
chartName := plan.Chart.ValueString()
ociDiags := OCIRegistryLogin(ctx, actionConfig, m.RegistryClient, repositoryURL, chartName, repositoryUsername, repositoryPassword)
ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, repositoryURL, chartName, repositoryUsername, repositoryPassword)
resp.Diagnostics.Append(ociDiags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -1684,20 +1684,20 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
}

client := action.NewInstall(actionConfig)
cpo, chartName, diags := chartPathOptions(&plan, m, &client.ChartPathOptions)
cpo, chartName, diags := chartPathOptions(&plan, meta, &client.ChartPathOptions)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

chart, path, diags := getChart(ctx, &plan, m, chartName, cpo)
chart, path, diags := getChart(ctx, &plan, meta, chartName, cpo)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, fmt.Sprintf("%s Got chart", logID))

updated, diags := checkChartDependencies(ctx, &plan, chart, path, m)
updated, diags := checkChartDependencies(ctx, &plan, chart, path, meta)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -1710,15 +1710,15 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
}

if plan.Lint.ValueBool() {
diags := resourceReleaseValidate(ctx, &plan, m, cpo)
diags := resourceReleaseValidate(ctx, &plan, meta, cpo)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
}
tflog.Debug(ctx, fmt.Sprintf("%s Release validated", logID))

if m.ExperimentEnabled("manifest") {
if meta.ExperimentEnabled("manifest") {
// Check if all necessary values are known
if valuesUnknown(plan) {
tflog.Debug(ctx, "not all values are known, skipping dry run to render manifest")
Expand Down Expand Up @@ -1822,7 +1822,7 @@ func (r *HelmRelease) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq
return
}

_, err = getRelease(ctx, m, actionConfig, name)
_, err = getRelease(ctx, meta, actionConfig, name)
if err == errReleaseNotFound {
if len(chart.Metadata.Version) > 0 {
plan.Version = types.StringValue(chart.Metadata.Version)
Expand Down

0 comments on commit 2bafedf

Please sign in to comment.