Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 48c2a32

Browse files
authored
🚸 Improve interactivity for plugin dry-run (#1243)
1 parent c3a040e commit 48c2a32

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

cmd/rig-ops/cmd/plugins/dryrun.go

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"maps"
78
"os"
89
"slices"
910
"strconv"
@@ -13,13 +14,12 @@ import (
1314
"github.com/rigdev/rig-go-api/operator/api/v1/pipeline"
1415
"github.com/rigdev/rig/cmd/common"
1516
"github.com/rigdev/rig/cmd/rig-ops/cmd/base"
16-
"github.com/rigdev/rig/cmd/rig-ops/cmd/migrate"
17+
"github.com/rigdev/rig/cmd/rig/cmd/capsule"
1718
"github.com/rigdev/rig/pkg/api/config/v1alpha1"
1819
"github.com/rigdev/rig/pkg/api/v1alpha2"
1920
"github.com/rigdev/rig/pkg/obj"
2021
"github.com/spf13/cobra"
2122
"gopkg.in/yaml.v3"
22-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2323
"sigs.k8s.io/controller-runtime/pkg/client"
2424
)
2525

@@ -74,12 +74,12 @@ func (c *Cmd) dryRun(ctx context.Context, _ *cobra.Command, args []string) error
7474
}
7575

7676
var spec string
77-
var capsule v1alpha2.Capsule
77+
var capsuleSpec v1alpha2.Capsule
7878
if len(args) > 0 {
7979
if err := c.K8s.Get(ctx, client.ObjectKey{
8080
Namespace: args[0],
8181
Name: args[1],
82-
}, &capsule); err != nil {
82+
}, &capsuleSpec); err != nil {
8383
return err
8484
}
8585
} else if specPath != "" {
@@ -88,7 +88,7 @@ func (c *Cmd) dryRun(ctx context.Context, _ *cobra.Command, args []string) error
8888
return err
8989
}
9090
spec = string(bytes)
91-
if err := obj.Decode([]byte(spec), &capsule); err != nil {
91+
if err := obj.Decode([]byte(spec), &capsuleSpec); err != nil {
9292
return err
9393
}
9494
} else {
@@ -110,7 +110,7 @@ func (c *Cmd) dryRun(ctx context.Context, _ *cobra.Command, args []string) error
110110
if err := c.K8s.Get(ctx, client.ObjectKey{
111111
Namespace: choice[0],
112112
Name: choice[1],
113-
}, &capsule); err != nil {
113+
}, &capsuleSpec); err != nil {
114114
return err
115115
}
116116
}
@@ -121,26 +121,29 @@ func (c *Cmd) dryRun(ctx context.Context, _ *cobra.Command, args []string) error
121121
}
122122

123123
dryRun, err := c.OperatorClient.Pipeline.DryRun(ctx, connect.NewRequest(&pipeline.DryRunRequest{
124-
Namespace: capsule.Namespace,
125-
Capsule: capsule.Name,
124+
Namespace: capsuleSpec.Namespace,
125+
Capsule: capsuleSpec.Name,
126126
OperatorConfig: string(cfgBytes),
127127
CapsuleSpec: spec,
128128
}))
129129
if err != nil {
130130
return err
131131
}
132132

133-
var objects []any
134-
for _, o := range dryRun.Msg.GetOutputObjects() {
135-
object := &unstructured.Unstructured{}
136-
if err := obj.DecodeInto([]byte(o.GetObject().GetContent()), object, c.Scheme); err != nil {
137-
return err
138-
}
139-
objects = append(objects, object)
133+
dryOutput, err := c.processDryRunOutput(dryRun.Msg)
134+
if err != nil {
135+
return err
140136
}
141137

142138
if interactive {
143-
return c.interactiveDiff(dryRun.Msg)
139+
return capsule.PromptDryOutput(ctx, dryOutput, c.Scheme)
140+
}
141+
142+
var objects []any
143+
for _, o := range dryOutput.KubernetesObjects {
144+
if o.New.Object != nil {
145+
objects = append(objects, o.New.Object)
146+
}
144147
}
145148

146149
out, err := common.Format(objects, common.OutputTypeYAML)
@@ -156,37 +159,51 @@ func (c *Cmd) dryRun(ctx context.Context, _ *cobra.Command, args []string) error
156159
return os.WriteFile(output, []byte(out), 0o666)
157160
}
158161

159-
func (c *Cmd) interactiveDiff(dryRun *pipeline.DryRunResponse) error {
160-
current := migrate.NewResources()
161-
for _, o := range dryRun.InputObjects {
162-
object, err := obj.DecodeAny([]byte(o.GetContent()), c.Scheme)
163-
if err != nil {
164-
return err
162+
func (c *Cmd) processDryRunOutput(resp *pipeline.DryRunResponse) (capsule.DryOutput, error) {
163+
var res capsule.DryOutput
164+
165+
objects := map[string]capsule.KubernetesDryObject{}
166+
var err error
167+
for _, o := range resp.GetInputObjects() {
168+
name := fmt.Sprintf("%s %s", o.GetGvk(), o.GetName())
169+
var co client.Object
170+
if content := o.GetContent(); content != "" {
171+
co, err = obj.DecodeUnstructured([]byte(content))
172+
if err != nil {
173+
return capsule.DryOutput{}, err
174+
}
165175
}
166-
if err := current.AddObject(o.GetGvk().Kind, o.GetName(), object); err != nil {
167-
return err
176+
k8s := objects[name]
177+
k8s.Old = capsule.KubernetesObject{
178+
Object: co,
179+
YAML: o.GetContent(),
168180
}
169-
}
170-
overview := current.CreateOverview("Current Resources")
171-
172-
migrated := migrate.NewResources()
173-
if err := migrate.ProcessOperatorOutput(migrated, dryRun.GetOutputObjects(), c.Scheme); err != nil {
174-
return err
181+
objects[name] = k8s
175182
}
176183

177-
migratedOverview := migrated.CreateOverview("New Resources")
178-
179-
reports, err := migrated.Compare(current, c.Scheme)
180-
if err != nil {
181-
return err
184+
for _, oo := range resp.GetOutputObjects() {
185+
o := oo.GetObject()
186+
name := fmt.Sprintf("%s %s", o.GetGvk(), o.GetName())
187+
var co client.Object
188+
if content := o.GetContent(); content != "" {
189+
co, err = obj.DecodeUnstructured([]byte(content))
190+
if err != nil {
191+
return capsule.DryOutput{}, err
192+
}
193+
}
194+
k8s := objects[name]
195+
k8s.New = capsule.KubernetesObject{
196+
Object: co,
197+
YAML: o.GetContent(),
198+
}
199+
objects[name] = k8s
182200
}
183201

184-
warnings := map[string][]*migrate.Warning{}
185-
for _, k := range reports.GetKinds() {
186-
warnings[k] = nil
202+
for _, key := range slices.Sorted(maps.Keys(objects)) {
203+
res.KubernetesObjects = append(res.KubernetesObjects, objects[key])
187204
}
188205

189-
return migrate.PromptDiffingChanges(reports, warnings, overview, migratedOverview, nil, c.Prompter)
206+
return res, nil
190207
}
191208

192209
func readPlugin(path string) (v1alpha1.Step, error) {

cmd/rig/cmd/capsule/dry_run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ func makeTViewContent(outcome DryOutput, scheme *runtime.Scheme) ([]content, err
218218
}
219219

220220
for _, o := range outcome.KubernetesObjects {
221-
name := fmt.Sprintf("%s/%s", o.New.Object.GetObjectKind().GroupVersionKind().Kind, o.New.Object.GetName())
221+
obj := o.New.Object
222+
if obj == nil {
223+
obj = o.Old.Object
224+
}
225+
226+
name := fmt.Sprintf("%s/%s", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName())
222227

223228
diff, err := makeTViewDiff(o.Old.Object, o.New.Object, scheme)
224229
if err != nil {

0 commit comments

Comments
 (0)