Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Dec 21, 2023
1 parent 0413e0c commit 901ddee
Show file tree
Hide file tree
Showing 74 changed files with 1,176 additions and 585 deletions.
25 changes: 25 additions & 0 deletions pkg/apis/core/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ type (
MonitorType string
)

const (
KCLBuilder BuilderType = "KCL"
AppConfigurationBuilder BuilderType = "AppConfiguration"
PodMonitorType MonitorType = "Pod"
ServiceMonitorType MonitorType = "Service"
)

// Project is a definition of Kusion Project resource.
//
// A project is composed of one or more applications and is linked to a Git repository,
Expand All @@ -18,8 +25,17 @@ type Project struct {
// Labels is the list of labels that are assigned to this project.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

// Path is a directory path within the Git repository.
Path string `json:"path,omitempty" yaml:"path,omitempty"`

// Generator controls how to generate the Intent.
Generator *GeneratorConfig `json:"generator,omitempty" yaml:"generator,omitempty"`

// Prometheus configs
Prometheus *PrometheusConfig `json:"prometheus,omitempty" yaml:"prometheus,omitempty"`

// The set of stacks that are known about this project.
Stacks []*Stack `json:"stacks,omitempty" yaml:"stacks,omitempty"`
}

// GeneratorConfig holds the intent generation configurations defined in Project resource.
Expand All @@ -30,6 +46,12 @@ type GeneratorConfig struct {
Configs map[string]interface{} `json:"configs,omitempty" yaml:"configs,omitempty"`
}

// PrometheusConfig represent Prometheus configs saved in project.yaml
type PrometheusConfig struct {
OperatorMode bool `yaml:"operatorMode,omitempty" json:"operatorMode,omitempty"`
MonitorType MonitorType `yaml:"monitorType,omitempty" json:"monitorType,omitempty"`
}

// Stack is a definition of Kusion Stack resource.
//
// Stack provides a mechanism to isolate multiple deploys of same application,
Expand All @@ -43,4 +65,7 @@ type Stack struct {
Description *string `json:"description,omitempty" yaml:"description,omitempty"`
// Labels is the list of labels that are assigned to this stack.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

// Path is a directory path within the Git repository.
Path string `json:"path,omitempty" yaml:"path,omitempty"`
}
4 changes: 2 additions & 2 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/pterm/pterm"

"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/status"
"kusionstack.io/kusion/pkg/cmd/build"
cmdintent "kusionstack.io/kusion/pkg/cmd/build/builders"
Expand All @@ -22,6 +21,7 @@ import (
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/project"
"kusionstack.io/kusion/pkg/util/pretty"
)

Expand Down Expand Up @@ -286,7 +286,7 @@ func Apply(
cluster := o.Arguments["cluster"]
_, st := ac.Apply(&operation.ApplyRequest{
Request: opsmodels.Request{
Tenant: changes.Project().Tenant,
Tenant: "",
Project: changes.Project(),
Stack: changes.Stack(),
Cluster: cluster,
Expand Down
47 changes: 21 additions & 26 deletions pkg/cmd/apply/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/apis/status"
"kusionstack.io/kusion/pkg/cmd/build"
"kusionstack.io/kusion/pkg/cmd/build/builders"
Expand All @@ -24,14 +23,15 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/runtime/kubernetes"
"kusionstack.io/kusion/pkg/engine/states/local"
"kusionstack.io/kusion/pkg/project"
)

func TestApplyOptions_Run(t *testing.T) {
mockey.PatchConvey("Detail is true", t, func() {
mockeyPatchDetectProjectAndStack()
mockeyPatchBuildIntent()
mockeyPatchNewKubernetesRuntime()
mockeyPatchOperationPreview()
mockPatchDetectProjectAndStack()
mockPatchBuildIntent()
mockPatchNewKubernetesRuntime()
mockPatchOperationPreview()

o := NewApplyOptions()
o.Detail = true
Expand All @@ -42,10 +42,10 @@ func TestApplyOptions_Run(t *testing.T) {
})

mockey.PatchConvey("DryRun is true", t, func() {
mockeyPatchDetectProjectAndStack()
mockeyPatchBuildIntent()
mockeyPatchNewKubernetesRuntime()
mockeyPatchOperationPreview()
mockPatchDetectProjectAndStack()
mockPatchBuildIntent()
mockPatchNewKubernetesRuntime()
mockPatchOperationPreview()
mockOperationApply(opsmodels.Success)

o := NewApplyOptions()
Expand All @@ -57,38 +57,33 @@ func TestApplyOptions_Run(t *testing.T) {
}

var (
p = &project.Project{
Configuration: project.Configuration{
Name: "testdata",
Tenant: "admin",
},
p = &apiv1.Project{
Name: "testdata",
}
s = &stack.Stack{
Configuration: stack.Configuration{
Name: "dev",
},
s = &apiv1.Stack{
Name: "dev",
}
)

func mockeyPatchDetectProjectAndStack() *mockey.Mocker {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*project.Project, *stack.Stack, error) {
func mockPatchDetectProjectAndStack() *mockey.Mocker {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*apiv1.Project, *apiv1.Stack, error) {
p.Path = stackDir
s.Path = stackDir
return p, s, nil
}).Build()
}

func mockeyPatchBuildIntent() *mockey.Mocker {
func mockPatchBuildIntent() *mockey.Mocker {
return mockey.Mock(build.Intent).To(func(
o *builders.Options,
project *project.Project,
stack *stack.Stack,
project *apiv1.Project,
stack *apiv1.Stack,
) (*intent.Intent, error) {
return &intent.Intent{Resources: []intent.Resource{sa1, sa2, sa3}}, nil
}).Build()
}

func mockeyPatchNewKubernetesRuntime() *mockey.Mocker {
func mockPatchNewKubernetesRuntime() *mockey.Mocker {
return mockey.Mock(kubernetes.NewKubernetesRuntime).To(func() (runtime.Runtime, error) {
return &fakerRuntime{}, nil
}).Build()
Expand Down Expand Up @@ -130,7 +125,7 @@ func (f *fakerRuntime) Watch(ctx context.Context, request *runtime.WatchRequest)
return nil
}

func mockeyPatchOperationPreview() *mockey.Mocker {
func mockPatchOperationPreview() *mockey.Mocker {
return mockey.Mock((*operation.PreviewOperation).Preview).To(func(
*operation.PreviewOperation,
*operation.PreviewRequest,
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/build/builders/appconfig_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package builders
import (
"kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/modules"
"kusionstack.io/kusion/pkg/modules/generators"
"kusionstack.io/kusion/pkg/modules/inputs"
Expand All @@ -17,8 +15,8 @@ type AppsConfigBuilder struct {

func (acg *AppsConfigBuilder) Build(
_ *Options,
project *project.Project,
stack *stack.Stack,
project *v1.Project,
stack *v1.Stack,
) (*intent.Intent, error) {
i := &intent.Intent{
Resources: []intent.Resource{},
Expand Down
16 changes: 5 additions & 11 deletions pkg/cmd/build/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/stretchr/testify/assert"

"kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
appmodel "kusionstack.io/kusion/pkg/modules/inputs"
"kusionstack.io/kusion/pkg/modules/inputs/workload"
"kusionstack.io/kusion/pkg/modules/inputs/workload/network"
Expand Down Expand Up @@ -86,17 +84,13 @@ func buildMockWorkspace() *v1.Workspace {
}
}

func buildMockProjectAndStack() (*project.Project, *stack.Stack) {
p := &project.Project{
Configuration: project.Configuration{
Name: "test-project",
},
func buildMockProjectAndStack() (*v1.Project, *v1.Stack) {
p := &v1.Project{
Name: "test-project",
}

s := &stack.Stack{
Configuration: stack.Configuration{
Name: "test-stack",
},
s := &v1.Stack{
Name: "test-project",
}

return p, s
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/build/builders/builder.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package builders

import (
v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
)

// Builder represents a method to build an Intent. Typically, it is implemented by the AppConfigureBuilder,
// but we have designed it as an interface to allow for more general usage. Any struct that implements this interface
// is considered a Builder and can be integrated into the Kusion workflow.
type Builder interface {
Build(o *Options, project *project.Project, stack *stack.Stack) (*intent.Intent, error)
Build(o *Options, project *v1.Project, stack *v1.Stack) (*intent.Intent, error)
}

type Options struct {
Expand Down
7 changes: 3 additions & 4 deletions pkg/cmd/build/builders/kcl/kcl_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/opt"

v1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/build/builders/crd"
"kusionstack.io/kusion/pkg/cmd/build/builders/kcl/rest"
Expand Down Expand Up @@ -52,7 +51,7 @@ func EnableRPC() bool {
return !enableRest
}

func (g *Builder) Build(o *builders.Options, _ *project.Project, stack *stack.Stack) (*intent.Intent, error) {
func (g *Builder) Build(o *builders.Options, _ *v1.Project, stack *v1.Stack) (*intent.Intent, error) {
compileResult, err := Run(o, stack)
if err != nil {
return nil, err
Expand All @@ -66,7 +65,7 @@ func (g *Builder) Build(o *builders.Options, _ *project.Project, stack *stack.St
return i, nil
}

func Run(o *builders.Options, stack *stack.Stack) (*CompileResult, error) {
func Run(o *builders.Options, stack *v1.Stack) (*CompileResult, error) {
optList, err := BuildKCLOptions(o)
if err != nil {
return nil, err
Expand Down
11 changes: 7 additions & 4 deletions pkg/cmd/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
yamlv2 "gopkg.in/yaml.v2"
"kcl-lang.io/kpm/pkg/api"

"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/project"
)

const (
KclFile = "kcl.yaml"
)

type Options struct {
Expand Down Expand Up @@ -42,7 +45,7 @@ func NewBuildOptions() *Options {

func (o *Options) Complete(args []string) error {
o.Filenames = args
return o.PreSet(stack.IsStack)
return o.PreSet(project.IsStack)
}

func (o *Options) Validate() error {
Expand Down Expand Up @@ -124,7 +127,7 @@ func (o *Options) PreSet(preCheck func(cur string) bool) error {
}

if len(o.Settings) == 0 {
o.Settings = []string{project.KclFile}
o.Settings = []string{KclFile}
}
return nil
}
29 changes: 12 additions & 17 deletions pkg/cmd/build/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/project"
"kusionstack.io/kusion/pkg/apis/stack"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/engine"
"kusionstack.io/kusion/pkg/project"
)

var (
apiVersion = "v1"
kind = "ServiceAccount"
namespace = "test-ns"

p = &project.Project{
Configuration: project.Configuration{
Name: "testdata",
Tenant: "admin",
},
p = &apiv1.Project{
Name: "testdata",
}
s = &stack.Stack{
Configuration: stack.Configuration{
Name: "dev",
},
s = &apiv1.Stack{
Name: "dev",
}

sa1 = newSA("sa1")
Expand Down Expand Up @@ -140,15 +135,15 @@ func newSA(name string) intent.Resource {
}

func mockDetectProjectAndStack() *mockey.Mocker {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*project.Project, *stack.Stack, error) {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*apiv1.Project, *apiv1.Stack, error) {
p.Path = stackDir
s.Path = stackDir
return p, s, nil
}).Build()
}

func mockDetectProjectAndStackFail() *mockey.Mocker {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*project.Project, *stack.Stack, error) {
return mockey.Mock(project.DetectProjectAndStack).To(func(stackDir string) (*apiv1.Project, *apiv1.Stack, error) {
p.Path = stackDir
s.Path = stackDir
return p, s, errTest
Expand All @@ -158,8 +153,8 @@ func mockDetectProjectAndStackFail() *mockey.Mocker {
func mockGenerateIntent() *mockey.Mocker {
return mockey.Mock(IntentWithSpinner).To(func(
o *builders.Options,
project *project.Project,
stack *stack.Stack,
project *apiv1.Project,
stack *apiv1.Stack,
) (*intent.Intent, error) {
return &intent.Intent{Resources: []intent.Resource{sa1, sa2, sa3}}, nil
}).Build()
Expand All @@ -168,8 +163,8 @@ func mockGenerateIntent() *mockey.Mocker {
func mockGenerateIntentFail() *mockey.Mocker {
return mockey.Mock(IntentWithSpinner).To(func(
o *builders.Options,
project *project.Project,
stack *stack.Stack,
project *apiv1.Project,
stack *apiv1.Stack,
) (*intent.Intent, error) {
return &intent.Intent{Resources: []intent.Resource{sa1, sa2, sa3}}, errTest
}).Build()
Expand Down

0 comments on commit 901ddee

Please sign in to comment.