Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Bundle Go type to the public API #461

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions api/v1alpha1/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1alpha1

import "cuelang.org/go/cue"

const (
// BundleAPIVersionSelector is the CUE path for the Timoni's bundle API version.
BundleAPIVersionSelector Selector = "bundle.apiVersion"
Expand Down Expand Up @@ -66,3 +68,35 @@ import "strings"

bundle: #Bundle
`

// Bundle holds the information about the bundle name and the list of instances.
// +k8s:deepcopy-gen=false
type Bundle struct {
// Name is the name of the bundle.
Name string `json:"name"`

// Instances is a list of instances defined in the bundle.
Instances []*BundleInstance `json:"instances"`
}

// BundleInstance holds the information about the instance name, namespace, module and values.
// +k8s:deepcopy-gen=false
type BundleInstance struct {
// Bundle is the name of the bundle this instance belongs to.
Bundle string `json:"bundle"`

// Cluster is the name of the cluster this instance belongs to.
Cluster string `json:"cluster,omitempty"`

// Name is the name of the instance.
Name string `json:"name"`

// Namespace is the namespace where the instance will be installed.
Namespace string `json:"namespace"`

// Module is a reference to the module's artifact in the registry.
Module ModuleReference `json:"module"`

// Values hold the user-supplied configuration of this instance.
Values cue.Value `json:"values,omitempty"`
}
2 changes: 1 addition & 1 deletion cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), rootArgs.timeout)
defer cancel()

instance := &engine.BundleInstance{
instance := &apiv1.BundleInstance{
Name: applyArgs.name,
Namespace: *kubeconfigArgs.Namespace,
Module: *mod,
Expand Down
6 changes: 3 additions & 3 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
return nil
}

func fetchBundleInstanceModule(ctx context.Context, instance *engine.BundleInstance, rootDir string) error {
func fetchBundleInstanceModule(ctx context.Context, instance *apiv1.BundleInstance, rootDir string) error {
modDir := path.Join(rootDir, instance.Name)
if err := os.MkdirAll(modDir, os.ModePerm); err != nil {
return err
Expand Down Expand Up @@ -279,7 +279,7 @@ func fetchBundleInstanceModule(ctx context.Context, instance *engine.BundleInsta
return nil
}

func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance *engine.BundleInstance, kubeVersion string, rootDir string, diffOutput io.Writer) error {
func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance *apiv1.BundleInstance, kubeVersion string, rootDir string, diffOutput io.Writer) error {
log := loggerBundleInstance(ctx, instance.Bundle, instance.Cluster, instance.Name, true)

modDir := path.Join(rootDir, instance.Name, "module")
Expand Down Expand Up @@ -363,7 +363,7 @@ func saveReaderToFile(reader io.Reader) (string, error) {
return f.Name(), nil
}

func bundleInstancesOwnershipConflicts(bundleInstances []*engine.BundleInstance) error {
func bundleInstancesOwnershipConflicts(bundleInstances []*apiv1.BundleInstance) error {
var conflicts reconciler.InstanceOwnershipConflictErr
rm, err := runtime.NewResourceManager(kubeconfigArgs)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/bundle_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {
return nil
}

func buildBundleInstance(cuectx *cue.Context, instance *engine.BundleInstance, rootDir string) (string, error) {
func buildBundleInstance(cuectx *cue.Context, instance *apiv1.BundleInstance, rootDir string) (string, error) {
modDir := path.Join(rootDir, instance.Name, "module")

builder := engine.NewModuleBuilder(
Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {
instance := instances[index]
log.Info(fmt.Sprintf("deleting instance %s in namespace %s",
logger.ColorizeSubject(instance.Name), logger.ColorizeSubject(instance.Namespace)))
if err := deleteBundleInstance(ctx, &engine.BundleInstance{
if err := deleteBundleInstance(ctx, &apiv1.BundleInstance{
Bundle: bundleDelArgs.name,
Cluster: cluster.Name,
Name: instance.Name,
Expand All @@ -146,7 +146,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {
return nil
}

func deleteBundleInstance(ctx context.Context, instance *engine.BundleInstance, wait bool, dryrun bool) error {
func deleteBundleInstance(ctx context.Context, instance *apiv1.BundleInstance, wait bool, dryrun bool) error {
log := loggerBundle(ctx, instance.Bundle, instance.Cluster, true)

sm, err := runtime.NewResourceManager(kubeconfigArgs)
Expand Down
22 changes: 4 additions & 18 deletions internal/engine/bundle_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ type BundleBuilder struct {
injector *RuntimeInjector
}

type Bundle struct {
Name string
Instances []*BundleInstance
}

type BundleInstance struct {
Bundle string
Cluster string
Name string
Namespace string
Module apiv1.ModuleReference
Values cue.Value
}

// NewBundleBuilder creates a BundleBuilder for the given module and package.
func NewBundleBuilder(ctx *cue.Context, files []string) *BundleBuilder {
if ctx == nil {
Expand Down Expand Up @@ -172,7 +158,7 @@ func (b *BundleBuilder) getInstanceUrl(v cue.Value) string {
}

// GetBundle returns a Bundle from the bundle CUE value.
func (b *BundleBuilder) GetBundle(v cue.Value) (*Bundle, error) {
func (b *BundleBuilder) GetBundle(v cue.Value) (*apiv1.Bundle, error) {
bundleNameValue := v.LookupPath(cue.ParsePath(apiv1.BundleName.String()))
bundleName, err := bundleNameValue.String()
if err != nil {
Expand All @@ -184,7 +170,7 @@ func (b *BundleBuilder) GetBundle(v cue.Value) (*Bundle, error) {
return nil, fmt.Errorf("lookup %s failed: %w", apiv1.BundleInstancesSelector.String(), instances.Err())
}

var list []*BundleInstance
var list []*apiv1.BundleInstance
iter, err := instances.Fields(cue.Concrete(true))
if err != nil {
return nil, err
Expand All @@ -208,7 +194,7 @@ func (b *BundleBuilder) GetBundle(v cue.Value) (*Bundle, error) {

values := expr.LookupPath(cue.ParsePath(apiv1.BundleValuesSelector.String()))

list = append(list, &BundleInstance{
list = append(list, &apiv1.BundleInstance{
Bundle: bundleName,
Name: name,
Namespace: namespace,
Expand All @@ -221,7 +207,7 @@ func (b *BundleBuilder) GetBundle(v cue.Value) (*Bundle, error) {
})
}

return &Bundle{
return &apiv1.Bundle{
Name: bundleName,
Instances: list,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewReconciler(log logr.Logger, opts *CommonOptions, timeout time.Duration)
return reconciler
}

func (r *Reconciler) Init(ctx context.Context, builder *engine.ModuleBuilder, buildResult cue.Value, instance *engine.BundleInstance, rcg genericclioptions.RESTClientGetter) error {
func (r *Reconciler) Init(ctx context.Context, builder *engine.ModuleBuilder, buildResult cue.Value, instance *apiv1.BundleInstance, rcg genericclioptions.RESTClientGetter) error {
finalValues, err := builder.GetDefaultValues()
if err != nil {
return fmt.Errorf("failed to extract values: %w", err)
Expand Down
Loading