diff --git a/go.mod b/go.mod index c9a8e82..c3125b4 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/markkurossi/tabulate v0.0.0-20211112080948-67dabd3f2db2 github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 - github.com/otterize/intents-operator/src v0.0.0-20240808131010-d1eeabedcfeb + github.com/otterize/intents-operator/src v0.0.0-20240813142213-b5e9505b69bf github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/samber/lo v1.33.0 github.com/sirupsen/logrus v1.9.0 diff --git a/go.sum b/go.sum index 7f4813e..0463b6a 100644 --- a/go.sum +++ b/go.sum @@ -425,8 +425,8 @@ github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 h1:Qd1Ml+uEhpesT8Og0ysEhu5+DGhbhW+qxjapH8t1Kvs= github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263/go.mod h1:odkMeLkWS8G6+WP2z3Pn2vkzhPSvBtFhAUYTKXAtZMQ= -github.com/otterize/intents-operator/src v0.0.0-20240808131010-d1eeabedcfeb h1:3el1ggFKbpKc6M6lJ/jl8oE8KW9XWUtDx/U3LBz6VMw= -github.com/otterize/intents-operator/src v0.0.0-20240808131010-d1eeabedcfeb/go.mod h1:R4tfDkJToFDE931Qk7gi47KhXX5WJ3bBYIfskQ1l3Wg= +github.com/otterize/intents-operator/src v0.0.0-20240813142213-b5e9505b69bf h1:o02K4bOAD7yZEq0gcbh92PjBZKjGUS7j+NSFkO+DsXE= +github.com/otterize/intents-operator/src v0.0.0-20240813142213-b5e9505b69bf/go.mod h1:R4tfDkJToFDE931Qk7gi47KhXX5WJ3bBYIfskQ1l3Wg= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= diff --git a/src/cmd/networkmapper/export/mapper-export.go b/src/cmd/networkmapper/export/mapper-export.go index c7bd984..59d93a2 100644 --- a/src/cmd/networkmapper/export/mapper-export.go +++ b/src/cmd/networkmapper/export/mapper-export.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/otterize/intents-operator/src/operator/api/v1alpha3" "github.com/otterize/intents-operator/src/operator/api/v2alpha1" mappershared "github.com/otterize/otterize-cli/src/cmd/networkmapper/shared" "github.com/otterize/otterize-cli/src/pkg/config" @@ -23,6 +24,9 @@ const ( OutputTypeDefault = OutputTypeSingleFile OutputTypeSingleFile = "single-file" OutputTypeDirectory = "dir" + OutputVersionKey = "output-version" + OutputVersionV1 = "v1" + OutputVersionV2 = "v2" ) var ExportCmd = &cobra.Command{ @@ -66,10 +70,23 @@ func getFormattedIntents(intentList []v2alpha1.ClientIntents) (string, error) { buf := bytes.Buffer{} printer := intentsoutput.IntentsPrinter{} + printerV1 := intentsoutput.IntentsPrinterV1{} for _, intentYAML := range intentList { - err := printer.PrintObj(&intentYAML, &buf) - if err != nil { - return "", err + if viper.GetString(OutputVersionKey) == OutputVersionV2 { + err := printer.PrintObj(&intentYAML, &buf) + if err != nil { + return "", err + } + } else { + intentV1 := v1alpha3.ClientIntents{} + err := intentV1.ConvertFrom(&intentYAML) + if err != nil { + return "", err + } + err = printerV1.PrintObj(&intentV1, &buf) + if err != nil { + return "", err + } } } return buf.String(), nil @@ -155,4 +172,5 @@ func init() { ExportCmd.Flags().String(OutputTypeKey, "", fmt.Sprintf("whether to write output to file or dir: %s/%s", OutputTypeSingleFile, OutputTypeDirectory)) ExportCmd.Flags().String(config.OutputFormatKey, config.OutputFormatYAML, fmt.Sprintf("Output format - %s/%s", config.OutputFormatYAML, config.OutputFormatJSON)) ExportCmd.Flags().String(mappershared.ServerKey, "", "Export only intents that call this server - .") + ExportCmd.Flags().String(OutputVersionKey, OutputVersionV1, fmt.Sprintf("Output ClientIntents api version - %s/%s", OutputVersionV1, OutputVersionV2)) } diff --git a/src/pkg/intentsoutput/printerv1.go b/src/pkg/intentsoutput/printerv1.go new file mode 100644 index 0000000..9376ff1 --- /dev/null +++ b/src/pkg/intentsoutput/printerv1.go @@ -0,0 +1,87 @@ +package intentsoutput + +import ( + "github.com/otterize/intents-operator/src/operator/api/v1alpha3" + "io" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sync/atomic" + "text/template" +) + +type IntentsPrinterV1 struct { + printCount int64 +} + +const crdTemplateV1 = `apiVersion: {{ .APIVersion }} +kind: {{ .Kind }} +metadata: + name: {{ .Name }} +{{- if .Namespace }} + namespace: {{ .Namespace }} +{{- end }} +spec: + service: + name: {{ .Spec.Service.Name }} + calls: +{{- range $intent := .Spec.Calls }} + - name: {{ $intent.Name }} +{{- if $intent.Type }} + type: {{ $intent.Type }} +{{- end -}} +{{- if $intent.Topics }} + kafkaTopics: +{{- range $topic := $intent.Topics }} + - name: {{ $topic.Name }} +{{- if $topic.Operations }} + operations: +{{- range $op := $topic.Operations }} + - {{ $op }} +{{- end -}} +{{- end -}} +{{- end -}} +{{- end -}} +{{- if $intent.HTTPResources }} + HTTPResources: +{{- range $resource := $intent.HTTPResources }} + - path: {{ $resource.Path }} +{{- if $resource.Methods }} + methods: +{{- range $method := $resource.Methods }} + - {{ $method }} +{{- end -}} +{{- end -}} +{{- end -}} +{{- end -}} +{{ end }}` + +var crdTemplateParsedV1 = template.Must(template.New("intents").Parse(crdTemplateV1)) + +// Keep this bit here so we have a compile time check that the structure the template assumes is correct. +var _ = v1alpha3.ClientIntents{ + TypeMeta: v1.TypeMeta{Kind: "", APIVersion: ""}, + ObjectMeta: v1.ObjectMeta{Name: "", Namespace: ""}, + Spec: &v1alpha3.IntentsSpec{ + Service: v1alpha3.Service{Name: ""}, + Calls: []v1alpha3.Intent{{ + Type: "", Name: "", + Topics: []v1alpha3.KafkaTopic{{ + Name: "", + Operations: []v1alpha3.KafkaOperation{}, + }}, + HTTPResources: []v1alpha3.HTTPResource{{ + Path: "", + Methods: []v1alpha3.HTTPMethod{}, + }}, + }}, + }, +} + +func (p *IntentsPrinterV1) PrintObj(intents *v1alpha3.ClientIntents, w io.Writer) error { + count := atomic.AddInt64(&p.printCount, 1) + if count > 1 { + if _, err := w.Write([]byte("\n---\n")); err != nil { + return err + } + } + return crdTemplateParsedV1.Execute(w, intents) +}