Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nginx conf #1130

Merged
merged 23 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions cmd/client/commandv2/common/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a more specific package name instead common which is not suggested by any Golang guide.

Ref: https://dave.cheney.net/2019/01/08/avoid-package-names-like-base-util-or-common

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, let me update it!

* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

Check warning on line 18 in cmd/client/commandv2/common/spec.go

View workflow job for this annotation

GitHub Actions / analysis

should have a package comment

import (
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/filters/builder"
"github.com/megaease/easegress/v2/pkg/filters/proxies/httpproxy"
"github.com/megaease/easegress/v2/pkg/object/httpserver"
"github.com/megaease/easegress/v2/pkg/object/pipeline"
"github.com/megaease/easegress/v2/pkg/util/codectool"
)

// HTTPServerSpec is the spec of HTTPServer.
type HTTPServerSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`

httpserver.Spec `json:",inline"`
}

// PipelineSpec is the spec of Pipeline.
type PipelineSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`

pipeline.Spec `json:",inline"`
}

// SetFilters sets the filters of PipelineSpec.
func (p *PipelineSpec) SetFilters(filters []filters.Spec) {
data := codectool.MustMarshalYAML(filters)
maps, _ := general.UnmarshalMapInterface(data, true)
p.Filters = maps
}

// NewHTTPServerSpec returns a new HTTPServerSpec.
func NewHTTPServerSpec(name string) *HTTPServerSpec {
spec := &HTTPServerSpec{
Name: name,
Kind: httpserver.Kind,
Spec: *getDefaultHTTPServerSpec(),
}
spec.Spec.Certs = map[string]string{}
spec.Spec.Keys = map[string]string{}
return spec
}

// NewPipelineSpec returns a new PipelineSpec.
func NewPipelineSpec(name string) *PipelineSpec {
return &PipelineSpec{
Name: name,
Kind: pipeline.Kind,
Spec: *getDefaultPipelineSpec(),
}
}

func getDefaultHTTPServerSpec() *httpserver.Spec {
return (&httpserver.HTTPServer{}).DefaultSpec().(*httpserver.Spec)
}

func getDefaultPipelineSpec() *pipeline.Spec {
return (&pipeline.Pipeline{}).DefaultSpec().(*pipeline.Spec)
}

// NewProxyFilterSpec returns a new ProxyFilterSpec.
func NewProxyFilterSpec(name string) *httpproxy.Spec {
spec := GetDefaultFilterSpec(httpproxy.Kind).(*httpproxy.Spec)
spec.BaseSpec.MetaSpec.Name = name
spec.BaseSpec.MetaSpec.Kind = httpproxy.Kind
return spec
}

// NewWebsocketFilterSpec returns a new WebsocketFilterSpec.
func NewWebsocketFilterSpec(name string) *httpproxy.WebSocketProxySpec {
spec := GetDefaultFilterSpec(httpproxy.WebSocketProxyKind).(*httpproxy.WebSocketProxySpec)
spec.BaseSpec.MetaSpec.Name = name
spec.BaseSpec.MetaSpec.Kind = httpproxy.WebSocketProxyKind
return spec
}

// NewRequestAdaptorFilterSpec returns a new RequestAdaptorFilterSpec.
func NewRequestAdaptorFilterSpec(name string) *builder.RequestAdaptorSpec {
spec := GetDefaultFilterSpec(builder.RequestAdaptorKind).(*builder.RequestAdaptorSpec)
spec.BaseSpec.MetaSpec.Name = name
spec.BaseSpec.MetaSpec.Kind = builder.RequestAdaptorKind
return spec
}

// GetDefaultFilterSpec returns the default filter spec of the kind.
func GetDefaultFilterSpec(kind string) filters.Spec {
return filters.GetKind(kind).DefaultSpec()
}
34 changes: 34 additions & 0 deletions cmd/client/commandv2/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package commandv2

import (
"github.com/spf13/cobra"

"github.com/megaease/easegress/v2/cmd/client/commandv2/convert/nginx"
)

// ConvertCmd returns convert command.
func ConvertCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "convert",
Short: "Convert other format to easegress yaml file",
suchen-sci marked this conversation as resolved.
Show resolved Hide resolved
}
cmd.AddCommand(nginx.Cmd())
return cmd
}
153 changes: 153 additions & 0 deletions cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nginx

import (
"fmt"
"math/rand"
"os"
"path/filepath"

"github.com/megaease/easegress/v2/cmd/client/commandv2/common"
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/util/codectool"
crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/spf13/cobra"
)

// Options contains the options for convert nginx.conf.
type Options struct {
NginxConf string
Output string
Prefix string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix is too general? Can we use --resource-prefix which is allowed to be empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

usedNames map[string]struct{}
}

// Cmd returns convert nginx.conf command.
func Cmd() *cobra.Command {
flags := &Options{}
flags.init()
examples := []general.Example{
{
Desc: "Convert nginx config to easegress yamls",
Command: "egctl convert nginx -f <nginx.conf> -o <output.yaml> --prefix <prefix>",
},
}
cmd := &cobra.Command{
Use: "nginx",
Short: "Convert nginx.conf to easegress yaml file",
Example: general.CreateMultiExample(examples),
Args: func(cmd *cobra.Command, args []string) error {
if flags.NginxConf == "" {
return fmt.Errorf("nginx.conf file path is required")
}
if flags.Output == "" {
return fmt.Errorf("output yaml file path is required")
}
if flags.Prefix == "" {
return fmt.Errorf("prefix is required")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
payload, err := crossplane.Parse(flags.NginxConf, &crossplane.ParseOptions{})
if err != nil {
general.ExitWithErrorf("parse nginx.conf failed: %v", err)
}
for _, e := range payload.Errors {
general.Warnf("parse nginx.conf error: %v in %s of %s", e.Error, e.Line, e.File)
}
config, err := parsePayload(payload)
if err != nil {
general.ExitWithError(err)
}
hs, pls, err := convertConfig(flags, config)
if err != nil {
general.ExitWithError(err)
}
if err := writeYaml(flags.Output, hs, pls); err != nil {
general.ExitWithError(err)
}
},
}
cmd.Flags().StringVarP(&flags.NginxConf, "file", "f", "", "nginx.conf file path")
cmd.Flags().StringVarP(&flags.Output, "output", "o", "", "output yaml file path")
cmd.Flags().StringVar(&flags.Prefix, "prefix", "", "prefix of output yaml resources")
return cmd
}

func (opt *Options) init() {
opt.usedNames = make(map[string]struct{})
opt.usedNames[""] = struct{}{}
}

// GetPipelineName create a global uniq name for pipeline based on path.
suchen-sci marked this conversation as resolved.
Show resolved Hide resolved
func (opt *Options) GetPipelineName(path string) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
nameRunes := make([]rune, 0)
for _, r := range path {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') {
nameRunes = append(nameRunes, r)
}
}
name := string(nameRunes)
if _, ok := opt.usedNames[name]; !ok {
opt.usedNames[name] = struct{}{}
return fmt.Sprintf("%s-%s", opt.Prefix, name)
}
for i := 0; i < 8; i++ {
nameRunes = append(nameRunes, letters[rand.Intn(len(letters))])
}
name = string(nameRunes)
if _, ok := opt.usedNames[name]; !ok {
opt.usedNames[name] = struct{}{}
return fmt.Sprintf("%s-%s", opt.Prefix, name)
}
return opt.GetPipelineName(path)
}

func writeYaml(filename string, servers []*common.HTTPServerSpec, pipelines []*common.PipelineSpec) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
file, err := os.Create(absPath)
if err != nil {
return err
}
defer file.Close()

for _, s := range servers {
data, err := codectool.MarshalYAML(s)
if err != nil {
return err
}
file.WriteString(string(data))
file.WriteString("\n---\n")
}
for _, p := range pipelines {
data, err := codectool.MarshalYAML(p)
if err != nil {
return err
}
file.WriteString(string(data))
file.WriteString("\n---\n")
}
file.Sync()
return nil
}
86 changes: 86 additions & 0 deletions cmd/client/commandv2/convert/nginx/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nginx

import (
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCmd(t *testing.T) {
cmd := Cmd()
assert.NotNil(t, cmd)
cmd.ParseFlags([]string{""})
assert.NotNil(t, cmd.Args(cmd, []string{}))

cmd.ParseFlags([]string{"-f", "test.conf"})
assert.NotNil(t, cmd.Args(cmd, []string{}))

cmd.ParseFlags([]string{"-o", "test.yaml"})
assert.NotNil(t, cmd.Args(cmd, []string{}))

cmd.ParseFlags([]string{"--prefix", "test"})
assert.Nil(t, cmd.Args(cmd, []string{}))

tempDir := newTempTestDir(t)
defer tempDir.Clean()

nginxConf := `
events {}
http {
server {
listen 127.0.0.1:8080;

location = /user {
proxy_pass http:https://localhost:9999;
}
}
}
`
nginxFile := tempDir.Create("nginx.conf", []byte(nginxConf))
outputFile := tempDir.Create("test.yaml", []byte(""))
cmd.ParseFlags([]string{"-f", nginxFile, "-o", outputFile, "--prefix", "test"})
cmd.Run(cmd, []string{})
file, err := os.Open(outputFile)
assert.Nil(t, err)
defer file.Close()
data, err := io.ReadAll(file)
assert.Nil(t, err)
assert.Contains(t, string(data), "test-8080")
assert.Contains(t, string(data), "test-user")
}

func TestOption(t *testing.T) {
option := &Options{
NginxConf: "test.conf",
Output: "test.yaml",
Prefix: "test",
}
option.init()
path := option.GetPipelineName("/user")
assert.Equal(t, "test-user", path)
path = option.GetPipelineName("/apis/v1")
assert.Equal(t, "test-apisv1", path)

path = option.GetPipelineName("/apis/v1/")
assert.Contains(t, path, "test-apisv1")
assert.NotEqual(t, "test-apisv1", path)
}
Loading
Loading