Skip to content

Commit

Permalink
(#189) Split api into meta and core groups
Browse files Browse the repository at this point in the history
Previously in v1alpha1, all Holos structs are located in the same
package.  This makes it difficult to focus on only the structs necessary
to transfer configuration data from CUE to the `holos` cli.

This patch splits the structs into `meta` and `core` where the core
package holds the structs end users should refer to and focus on.  Only
the Platform resource is in core now, but other BuildPlan types will be
added shortly.
  • Loading branch information
jeffmccune committed Jun 28, 2024
1 parent 62f96a2 commit 6d2daac
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ vet: ## Vet Go code.
.PHONY: gencue
gencue: ## Generate CUE definitions
cd internal/generate/platforms && cue get go github.com/holos-run/holos/api/v1alpha1/...
cd internal/generate/platforms && cue get go github.com/holos-run/holos/api/v1alpha2/...
cd internal/generate/platforms && cue get go github.com/holos-run/holos/api/core/...
cd internal/generate/platforms && cue get go github.com/holos-run/holos/api/meta/...

.PHONY: rmgen
rmgen: ## Remove generated code
Expand Down
42 changes: 4 additions & 38 deletions api/v1alpha2/v1alpha2.go → api/core/v1alpha2/core.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
// Package v1alpha2 contains the API contract between the holos cli and cue
// Package v1alpha2 contains the core API contract between the holos cli and cue
// configuration code. Platform designers, operators, and software developers
// use this API to define resources which holos uses to render kubernetes yaml.
// use this API to write configuration in CUE which `holos` loads. The overall
// shape of the API defines imperative actions `holos` should carry out to
// render the complete yaml that represents a Platform.
package v1alpha2

import "google.golang.org/protobuf/types/known/structpb"

// TypeMeta describes an individual object in an API response or request with
// strings representing the type of the object and its API schema version.
// Structures that are versioned or persisted should inline TypeMeta.
type TypeMeta struct {
// Kind is a string value representing the resource this object represents.
Kind string `json:"kind" yaml:"kind"`
// APIVersion defines the versioned schema of this representation of an object.
APIVersion string `json:"apiVersion" yaml:"apiVersion" cue:"string | *\"v1alpha2\""`
}

func (tm *TypeMeta) GetKind() string {
return tm.Kind
}

func (tm *TypeMeta) GetAPIVersion() string {
return tm.APIVersion
}

// Discriminator discriminates the kind of an api object.
type Discriminator interface {
// GetKind returns Kind.
GetKind() string
// GetAPIVersion returns APIVersion.
GetAPIVersion() string
}

// ObjectMeta represents metadata of a holos component object. The fields are a
// copy of upstream kubernetes api machinery but are holos objects distinct from
// kubernetes api objects.
type ObjectMeta struct {
// Name uniquely identifies the holos component instance and must be suitable as a file name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Namespace confines a holos component to a single namespace via kustomize if set.
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}

// Platform represents a platform to manage. A Platform resource informs holos
// which components to build. The platform resource also acts as a container
// for the platform model form values provided by the PlatformService. The
Expand Down
37 changes: 37 additions & 0 deletions api/meta/v1alpha2/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package v1alpha2

// TypeMeta describes an individual object in an API response or request with
// strings representing the type of the object and its API schema version.
// Structures that are versioned or persisted should inline TypeMeta.
type TypeMeta struct {
// Kind is a string value representing the resource this object represents.
Kind string `json:"kind" yaml:"kind"`
// APIVersion defines the versioned schema of this representation of an object.
APIVersion string `json:"apiVersion" yaml:"apiVersion" cue:"string | *\"v1alpha2\""`
}

func (tm *TypeMeta) GetKind() string {
return tm.Kind
}

func (tm *TypeMeta) GetAPIVersion() string {
return tm.APIVersion
}

// Discriminator discriminates the kind of an api object.
type Discriminator interface {
// GetKind returns Kind.
GetKind() string
// GetAPIVersion returns APIVersion.
GetAPIVersion() string
}

// ObjectMeta represents metadata of a holos component object. The fields are a
// copy of upstream kubernetes api machinery but are holos objects distinct from
// kubernetes api objects.
type ObjectMeta struct {
// Name uniquely identifies the holos component instance and must be suitable as a file name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Namespace confines a holos component to a single namespace via kustomize if set.
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
11 changes: 6 additions & 5 deletions internal/builder/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"github.com/holos-run/holos"
"github.com/holos-run/holos/api/v1alpha2"
core "github.com/holos-run/holos/api/core/v1alpha2"
meta "github.com/holos-run/holos/api/meta/v1alpha2"
"github.com/holos-run/holos/internal/client"
"github.com/holos-run/holos/internal/errors"
"github.com/holos-run/holos/internal/logger"
)

// Platform builds a platform
func (b *Builder) Platform(ctx context.Context, cfg *client.Config) (*v1alpha2.Platform, error) {
func (b *Builder) Platform(ctx context.Context, cfg *client.Config) (*core.Platform, error) {
log := logger.FromContext(ctx)
log.DebugContext(ctx, "cue: building platform instance")
instances, err := b.Instances(ctx, cfg)
Expand All @@ -38,7 +39,7 @@ func (b *Builder) Platform(ctx context.Context, cfg *client.Config) (*v1alpha2.P
return p, nil
}

func (b Builder) runPlatform(ctx context.Context, instance *build.Instance) (*v1alpha2.Platform, error) {
func (b Builder) runPlatform(ctx context.Context, instance *build.Instance) (*core.Platform, error) {
path := holos.InstancePath(instance.Dir)
log := logger.FromContext(ctx).With("dir", path)

Expand All @@ -63,7 +64,7 @@ func (b Builder) runPlatform(ctx context.Context, instance *build.Instance) (*v1

decoder := json.NewDecoder(bytes.NewReader(jsonBytes))
// Discriminate the type of build plan.
tm := &v1alpha2.TypeMeta{}
tm := &meta.TypeMeta{}
err = decoder.Decode(tm)
if err != nil {
return nil, errors.Wrap(fmt.Errorf("invalid platform: %s: %w", instance.Dir, err))
Expand All @@ -75,7 +76,7 @@ func (b Builder) runPlatform(ctx context.Context, instance *build.Instance) (*v1
decoder = json.NewDecoder(bytes.NewReader(jsonBytes))
decoder.DisallowUnknownFields()

var pf v1alpha2.Platform
var pf core.Platform
switch tm.GetKind() {
case "Platform":
if err = decoder.Decode(&pf); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go github.com/holos-run/holos/api/v1alpha2
//cue:generate cue get go github.com/holos-run/holos/api/core/v1alpha2

// Package v1alpha2 contains the API contract between the holos cli and cue
// Package v1alpha2 contains the core API contract between the holos cli and cue
// configuration code. Platform designers, operators, and software developers
// use this API to define resources which holos uses to render kubernetes yaml.
// use this API to write configuration in CUE which `holos` loads. The overall
// shape of the API defines imperative actions `holos` should carry out to
// render the complete yaml that represents a Platform.
package v1alpha2

import "google.golang.org/protobuf/types/known/structpb"

// TypeMeta describes an individual object in an API response or request with
// strings representing the type of the object and its API schema version.
// Structures that are versioned or persisted should inline TypeMeta.
#TypeMeta: {
// Kind is a string value representing the resource this object represents.
kind: string @go(Kind)

// APIVersion defines the versioned schema of this representation of an object.
apiVersion: string & (string | *"v1alpha2") @go(APIVersion)
}

// Discriminator discriminates the kind of an api object.
#Discriminator: _

// ObjectMeta represents metadata of a holos component object. The fields are a
// copy of upstream kubernetes api machinery but are holos objects distinct from
// kubernetes api objects.
#ObjectMeta: {
// Name uniquely identifies the holos component instance and must be suitable as a file name.
name?: string @go(Name)

// Namespace confines a holos component to a single namespace via kustomize if set.
namespace?: string @go(Namespace)
}

// Platform represents a platform to manage. A Platform resource informs holos
// which components to build. The platform resource also acts as a container
// for the platform model form values provided by the PlatformService. The
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Code generated by cue get go. DO NOT EDIT.

//cue:generate cue get go github.com/holos-run/holos/api/meta/v1alpha2

package v1alpha2

// TypeMeta describes an individual object in an API response or request with
// strings representing the type of the object and its API schema version.
// Structures that are versioned or persisted should inline TypeMeta.
#TypeMeta: {
// Kind is a string value representing the resource this object represents.
kind: string @go(Kind)

// APIVersion defines the versioned schema of this representation of an object.
apiVersion: string & (string | *"v1alpha2") @go(APIVersion)
}

// Discriminator discriminates the kind of an api object.
#Discriminator: _

// ObjectMeta represents metadata of a holos component object. The fields are a
// copy of upstream kubernetes api machinery but are holos objects distinct from
// kubernetes api objects.
#ObjectMeta: {
// Name uniquely identifies the holos component instance and must be suitable as a file name.
name?: string @go(Name)

// Namespace confines a holos component to a single namespace via kustomize if set.
namespace?: string @go(Namespace)
}
6 changes: 3 additions & 3 deletions internal/generate/platforms/holos/schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package holos

import (
"encoding/json"
v1 "github.com/holos-run/holos/api/v1alpha2"
core "github.com/holos-run/holos/api/core/v1alpha2"
dto "github.com/holos-run/holos/service/gen/holos/object/v1alpha1:object"
corev1 "k8s.io/api/core/v1"
certv1 "cert-manager.io/certificate/v1"
Expand Down Expand Up @@ -50,12 +50,12 @@ _Platform: #Platform & {
Name: string | *"holos"

// Components represent the platform components to render.
Components: [string]: v1.#PlatformSpecComponent
Components: [string]: core.#PlatformSpecComponent

// Model represents the platform model from the web app form.
Model: dto.#PlatformConfig.platform_model

Output: v1.#Platform & {
Output: core.#Platform & {
metadata: name: Name

spec: {
Expand Down
4 changes: 2 additions & 2 deletions internal/render/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"io"
"time"

"github.com/holos-run/holos/api/v1alpha2"
core "github.com/holos-run/holos/api/core/v1alpha2"
"github.com/holos-run/holos/internal/errors"
"github.com/holos-run/holos/internal/server/middleware/logger"
"github.com/holos-run/holos/internal/util"
"golang.org/x/sync/errgroup"
)

func Platform(ctx context.Context, concurrency int, pf *v1alpha2.Platform, stderr io.Writer) error {
func Platform(ctx context.Context, concurrency int, pf *core.Platform, stderr io.Writer) error {
total := len(pf.Spec.Components)

g, ctx := errgroup.WithContext(ctx)
Expand Down

0 comments on commit 6d2daac

Please sign in to comment.