Skip to content

Commit

Permalink
Support v1 and v2
Browse files Browse the repository at this point in the history
  • Loading branch information
omris94 committed Aug 13, 2024
1 parent e0f1162 commit a103ef2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/cmd/networkmapper/export/mapper-export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/otterize/intents-operator/src/operator/api/v1alpha3"

Check failure on line 7 in src/cmd/networkmapper/export/mapper-export.go

View workflow job for this annotation

GitHub Actions / golangci

github.com/otterize/intents-operator/src@v0.0.0-20240813142213-b5e9505b69bf: invalid version: unknown revision b5e9505b69bf

Check failure on line 7 in src/cmd/networkmapper/export/mapper-export.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

github.com/otterize/intents-operator/src@v0.0.0-20240813142213-b5e9505b69bf: invalid version: unknown revision b5e9505b69bf
"github.com/otterize/intents-operator/src/operator/api/v2alpha1"

Check failure on line 8 in src/cmd/networkmapper/export/mapper-export.go

View workflow job for this annotation

GitHub Actions / golangci

github.com/otterize/intents-operator/src@v0.0.0-20240813142213-b5e9505b69bf: invalid version: unknown revision b5e9505b69bf

Check failure on line 8 in src/cmd/networkmapper/export/mapper-export.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

github.com/otterize/intents-operator/src@v0.0.0-20240813142213-b5e9505b69bf: invalid version: unknown revision b5e9505b69bf
mappershared "github.com/otterize/otterize-cli/src/cmd/networkmapper/shared"
"github.com/otterize/otterize-cli/src/pkg/config"
Expand All @@ -23,6 +24,9 @@ const (
OutputTypeDefault = OutputTypeSingleFile
OutputTypeSingleFile = "single-file"
OutputTypeDirectory = "dir"
OutputVersionKey = "output-version"
OutputVersionV1 = "v1"
OutputVersionV2 = "v2"
)

var ExportCmd = &cobra.Command{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 - <server-name>.<namespace>")
ExportCmd.Flags().String(OutputVersionKey, OutputVersionV1, fmt.Sprintf("Output ClientIntents api version - %s/%s", OutputVersionV1, OutputVersionV2))
}
87 changes: 87 additions & 0 deletions src/pkg/intentsoutput/printerv1.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a103ef2

Please sign in to comment.