-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(k8sd)!: introduce support for storing helm charts on microcluste…
…r db - Added a table to the microcluster database for storing helm charts. - Added queries and helper functions for inserting and selecting helm charts. - Added the ChartLoader interface and updated the helm client to enable loading helm charts in different ways. - Added the database based ChartLoader to fetch charts from microcluser. - Added the embed based ChartLoader to fetch charts from the embedded charts, used in testing. - Added a helm chart controller that synchronizes available charts in the snap to the microcluster database. - Moved helm charts from `k8s/manifests` into respective feature directories and embedded the charts using go embed package. - Packed helm charts that are in directory form to tarballs. - Updated feature controller to create the helm char with the database loader and updated features to consume a helm client. - Fixed the bug of ports still being in use, in the testing util `WithState` by adding a retry loop for microcluster bootstrapping. - Updated InstallableChart type to include the name and version of the chart.
- Loading branch information
Showing
90 changed files
with
708 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package loader | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/client/helm" | ||
"github.com/canonical/k8s/pkg/k8sd/database" | ||
"github.com/canonical/k8s/pkg/k8sd/types" | ||
"github.com/canonical/microcluster/v2/state" | ||
"helm.sh/helm/v3/pkg/chart" | ||
"helm.sh/helm/v3/pkg/chart/loader" | ||
) | ||
|
||
// DatabaseLoader is a helm chart loader that loads charts from the microcluster database. | ||
type databaseLoader struct { | ||
s state.State | ||
} | ||
|
||
// NewDatabaseLoader creates a new database loader. | ||
func NewDatabaseLoader(s state.State) *databaseLoader { | ||
return &databaseLoader{ | ||
s: s, | ||
} | ||
} | ||
|
||
// Load loads a helm chart from the microcluster database by name and version. | ||
func (l *databaseLoader) Load(ctx context.Context, f helm.InstallableChart) (*chart.Chart, error) { | ||
var chartEntry *types.HelmChartEntry | ||
if err := l.s.Database().Transaction(ctx, func(ctx context.Context, tx *sql.Tx) error { | ||
var err error | ||
chartEntry, err = database.GetHelmChart(ctx, tx, f.Name, f.Version) | ||
return err | ||
}); err != nil { | ||
return nil, fmt.Errorf("failed to get helm chart: %w", err) | ||
} | ||
|
||
chart, err := loader.LoadArchive(bytes.NewReader(chartEntry.Contents)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load helm chart: %w", err) | ||
} | ||
|
||
return chart, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package loader | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"embed" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/canonical/k8s/pkg/client/helm" | ||
"helm.sh/helm/v3/pkg/chart" | ||
"helm.sh/helm/v3/pkg/chart/loader" | ||
) | ||
|
||
// embedLoader is a helm chart loader that loads charts from the embedded filesystem. | ||
type embedLoader struct { | ||
chartFS *embed.FS | ||
} | ||
|
||
// NewEmbedLoader creates a new embed loader. | ||
func NewEmbedLoader(chartFS *embed.FS) *embedLoader { | ||
return &embedLoader{ | ||
chartFS: chartFS, | ||
} | ||
} | ||
|
||
// Load loads a helm chart from the filesystem by name and version. | ||
func (l *embedLoader) Load(_ context.Context, f helm.InstallableChart) (*chart.Chart, error) { | ||
chartBytes, err := l.chartFS.ReadFile(filepath.Join("charts", fmt.Sprintf("%s-%s.tgz", f.Name, f.Version))) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read helm chart: %w", err) | ||
} | ||
|
||
chart, err := loader.LoadArchive(bytes.NewReader(chartBytes)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load helm chart: %w", err) | ||
} | ||
|
||
return chart, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package charts | ||
|
||
import ( | ||
"embed" | ||
) | ||
|
||
// registeredCharts is a list of filesystems that contain charts for features used by k8s-snap. | ||
var registeredCharts []*embed.FS | ||
|
||
// Charts returns the list of registered charts. | ||
func Charts() []*embed.FS { | ||
if registeredCharts == nil { | ||
return nil | ||
} | ||
chartFSList := make([]*embed.FS, len(registeredCharts)) | ||
copy(chartFSList, registeredCharts) | ||
return chartFSList | ||
} | ||
|
||
// Register charts that are used by k8s-snap. | ||
// Register is used by the `init()` method in individual packages. | ||
func Register(charts *embed.FS) { | ||
registeredCharts = append(registeredCharts, charts) | ||
} |
Oops, something went wrong.