Skip to content

Commit

Permalink
refactor: use api types in status and core pkg (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Dec 22, 2023
1 parent 1ef33b8 commit d09fa12
Show file tree
Hide file tree
Showing 93 changed files with 737 additions and 902 deletions.
9 changes: 9 additions & 0 deletions pkg/apis/core/v1/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const (
Terraform Type = "Terraform"
)

const (
// ResourceExtensionGVK is the key for resource extension, which is used to
// store the GVK of the resource.
ResourceExtensionGVK = "GVK"
// ResourceExtensionKubeConfig is the key for resource extension, which is used
// to indicate the path of kubeConfig for Kubernetes type resource.
ResourceExtensionKubeConfig = "kubeConfig"
)

// Intent describes the desired state how the infrastructure should look like: which workload to run,
// the load-balancer setup, the location of the database schema, and so on. Based on that information,
// the Kusion engine takes care of updating the production state to match the Intent.
Expand Down
51 changes: 51 additions & 0 deletions pkg/apis/core/v1/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package v1

import "encoding/json"

func (r *Resource) ResourceKey() string {
return r.ID
}

// DeepCopy return a copy of resource
func (r *Resource) DeepCopy() *Resource {
var out Resource
data, err := json.Marshal(r)
if err != nil {
panic(err)
}
_ = json.Unmarshal(data, &out)
return &out
}

func (rs Resources) Index() map[string]*Resource {
m := make(map[string]*Resource)
for i := range rs {
m[rs[i].ResourceKey()] = &rs[i]
}
return m
}

// GVKIndex returns a map of GVK to resources, for now, only Kubernetes resources.
func (rs Resources) GVKIndex() map[string][]*Resource {
m := make(map[string][]*Resource)
for i := range rs {
resource := &rs[i]
if resource.Type != Kubernetes {
continue
}
gvk := resource.Extensions[ResourceExtensionGVK].(string)
m[gvk] = append(m[gvk], resource)
}
return m
}

func (rs Resources) Len() int { return len(rs) }
func (rs Resources) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs Resources) Less(i, j int) bool {
switch {
case rs[i].ID != rs[j].ID:
return rs[i].ID < rs[j].ID
default:
return false
}
}
10 changes: 0 additions & 10 deletions pkg/apis/intent/intent.go

This file was deleted.

88 changes: 0 additions & 88 deletions pkg/apis/intent/resource.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/apis/internal/group.go → pkg/apis/status/group.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package internal
package status

const Group = "internal.kusionstack.io"
79 changes: 0 additions & 79 deletions pkg/apis/status/status.go

This file was deleted.

File renamed without changes.
16 changes: 8 additions & 8 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/pterm/pterm"

"kusionstack.io/kusion/pkg/apis/intent"
"kusionstack.io/kusion/pkg/apis/status"
apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/cmd/build"
cmdintent "kusionstack.io/kusion/pkg/cmd/build/builders"
previewcmd "kusionstack.io/kusion/pkg/cmd/preview"
Expand Down Expand Up @@ -75,7 +75,7 @@ func (o *Options) Run() error {
}

// Generate Intent
var sp *intent.Intent
var sp *apiv1.Intent
if o.IntentFile != "" {
sp, err = build.IntentFromFile(o.IntentFile)
} else {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (o *Options) Run() error {
func Apply(
o *Options,
storage states.StateStorage,
planResources *intent.Intent,
planResources *apiv1.Intent,
changes *opsmodels.Changes,
out io.Writer,
) error {
Expand Down Expand Up @@ -294,7 +294,7 @@ func Apply(
Intent: planResources,
},
})
if status.IsErr(st) {
if v1.IsErr(st) {
return fmt.Errorf("apply failed, status:\n%v", st)
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ func Apply(
// }
func Watch(
o *Options,
planResources *intent.Intent,
planResources *apiv1.Intent,
changes *opsmodels.Changes,
) error {
if o.DryRun {
Expand All @@ -332,7 +332,7 @@ func Watch(
}

// Filter out unchanged resources
toBeWatched := intent.Resources{}
toBeWatched := apiv1.Resources{}
for _, res := range planResources.Resources {
if changes.ChangeOrder.ChangeSteps[res.ResourceKey()].Action != opsmodels.UnChanged {
toBeWatched = append(toBeWatched, res)
Expand All @@ -345,7 +345,7 @@ func Watch(
Request: opsmodels.Request{
Project: changes.Project(),
Stack: changes.Stack(),
Intent: &intent.Intent{Resources: toBeWatched},
Intent: &apiv1.Intent{Resources: toBeWatched},
},
}); err != nil {
return err
Expand Down
Loading

0 comments on commit d09fa12

Please sign in to comment.