-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add compute-config rig-ops command to dry-run plugin configuration (#…
- Loading branch information
1 parent
50d800d
commit 07f8597
Showing
31 changed files
with
511 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package plugins | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/rigdev/rig-go-api/operator/api/v1/pipeline" | ||
"github.com/rigdev/rig/cmd/common" | ||
"github.com/rigdev/rig/cmd/rig-ops/cmd/base" | ||
"github.com/rigdev/rig/pkg/api/v1alpha2" | ||
"github.com/rigdev/rig/pkg/obj" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (c *Cmd) computeConfig(ctx context.Context, _ *cobra.Command, args []string) error { | ||
cfg, err := base.GetOperatorConfig(ctx, c.OperatorClient, c.Scheme) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var spec string | ||
var capsule v1alpha2.Capsule | ||
if len(args) > 0 { | ||
if err := c.K8s.Get(ctx, client.ObjectKey{ | ||
Namespace: args[0], | ||
Name: args[1], | ||
}, &capsule); err != nil { | ||
return err | ||
} | ||
} else if specPath != "" { | ||
bytes, err := os.ReadFile(specPath) | ||
if err != nil { | ||
return err | ||
} | ||
spec = string(bytes) | ||
if err := obj.Decode([]byte(spec), &capsule); err != nil { | ||
return err | ||
} | ||
} else { | ||
capsuleList := v1alpha2.CapsuleList{} | ||
if err := c.K8s.List(ctx, &capsuleList); err != nil { | ||
return err | ||
} | ||
var choices [][]string | ||
for _, c := range capsuleList.Items { | ||
choices = append(choices, []string{c.Namespace, c.Name}) | ||
} | ||
idx, err := c.Prompter.TableSelect( | ||
"Choose a capsule", choices, []string{"Namespace", "Capsule"}, common.SelectEnableFilterOpt, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
choice := choices[idx] | ||
if err := c.K8s.Get(ctx, client.ObjectKey{ | ||
Namespace: choice[0], | ||
Name: choice[1], | ||
}, &capsule); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
cfgBytes, err := yaml.Marshal(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := c.OperatorClient.Pipeline.DryRunPluginConfig(ctx, connect.NewRequest(&pipeline.DryRunPluginConfigRequest{ | ||
Namespace: capsule.Namespace, | ||
Capsule: capsule.Name, | ||
OperatorConfig: string(cfgBytes), | ||
CapsuleSpec: spec, | ||
})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return common.FormatPrint(resp.Msg.GetSteps(), common.OutputTypeYAML) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/rigdev/rig/pkg/api/v1alpha2" | ||
"github.com/rigdev/rig/pkg/pipeline" | ||
"github.com/rigdev/rig/pkg/scheme" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func Test_ParseCapsuleTemplatedConfig(t *testing.T) { | ||
name, namespace := "name", "namespace" | ||
vm := scheme.NewVersionMapperFromScheme(scheme.New()) | ||
p := pipeline.NewCapsulePipeline(nil, scheme.New(), vm, logr.FromContextOrDiscard(context.Background())) | ||
|
||
req := pipeline.NewCapsuleRequest(p, &v1alpha2.Capsule{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Spec: v1alpha2.CapsuleSpec{ | ||
Extensions: map[string]json.RawMessage{ | ||
"ext": json.RawMessage(`{"field": "value"}`), | ||
}, | ||
Scale: v1alpha2.CapsuleScale{ | ||
Horizontal: v1alpha2.HorizontalScale{ | ||
Instances: v1alpha2.Instances{ | ||
Min: 69, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil) | ||
|
||
s := `hej: asdf | ||
hej2: {{ .capsuleExtensions.ext.field }} | ||
hej3: {{ .capsule.spec.scale.horizontal.instances.min }}` | ||
conf, err := ParseCapsuleTemplatedConfig[config]([]byte(s), req) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, config{ | ||
Hej: "asdf", | ||
Hej2: "value", | ||
Hej3: 69, | ||
}, conf) | ||
} | ||
|
||
type config struct { | ||
Hej string `json:"hej"` | ||
Hej2 string `json:"hej2"` | ||
Hej3 int `json:"hej3"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.