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 1 commit
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
Next Next commit
finish include directive
  • Loading branch information
suchen-sci committed Oct 30, 2023
commit 6e0e68275b69f337d4f73727c05b527c9895a2a4
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
}
53 changes: 53 additions & 0 deletions cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 (
"encoding/json"
"fmt"

"github.com/megaease/easegress/v2/cmd/client/general"
crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/spf13/cobra"
)

// Cmd returns convert nginx.conf command.
func Cmd() *cobra.Command {
var nginxConf string
cmd := &cobra.Command{
Use: "nginx",
Short: "Convert nginx.conf to easegress yaml file",
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
payload, err := crossplane.Parse(nginxConf, &crossplane.ParseOptions{})
if err != nil {
general.ExitWithErrorf("parse nginx.conf failed: %v", err)
}
data, err := json.Marshal(payload)
if err != nil {
general.ExitWithError(err)
}
fmt.Println(string(data))
general.Warnf("warn")
},
}
cmd.Flags().StringVarP(&nginxConf, "file", "f", "", "nginx.conf file path")
return cmd
}
62 changes: 62 additions & 0 deletions cmd/client/commandv2/convert/nginx/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 (
"github.com/megaease/easegress/v2/cmd/client/general"
crossplane "github.com/nginxinc/nginx-go-crossplane"
)

const (
DirectiveInclude = "include"
DirectiveHTTP = "http"
DirectiveServer = "server"
DirectiveLocation = "location"
)

// loadIncludes loads include files recursively.
// The result contains all directives in the include files and the original directives.
func loadIncludes(fileName string, directives crossplane.Directives, payload *crossplane.Payload) crossplane.Directives {
res := crossplane.Directives{}
for _, d := range directives {
if d.Directive != DirectiveInclude {
d.File = fileName
res = append(res, d)
continue
}

if len(d.Args) != 1 {
continue
}
name := d.Args[0]
var include crossplane.Directives
for _, config := range payload.Config {
if config.File == name {
include = config.Parsed
break
}
}
if include == nil {
general.Warnf("can't find include file %s in line %d of file %s", name, d.Line, fileName)
continue
}
include = loadIncludes(name, include, payload)
res = append(res, include...)
}
return res
}
111 changes: 111 additions & 0 deletions cmd/client/commandv2/convert/nginx/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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"
"testing"

crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/stretchr/testify/assert"
)

func TestLoadIncludes(t *testing.T) {
tempDir := newTempTestDir(t)
defer tempDir.Clean()

{
conf := `
events {}
http {
include %s;
server {
listen 80;
location / {
proxy_pass http:https://localhost:8888;
}
}
}
`
conf1 := `include %s; proxy_set_header Conf-One One;`
conf2 := `proxy_set_header Conf-Two Two;`

file2 := tempDir.Create(t, []byte(conf2))
file1 := tempDir.Create(t, []byte(fmt.Sprintf(conf1, file2)))
file := tempDir.Create(t, []byte(fmt.Sprintf(conf, file1)))

payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
httpDirectives := payload.Config[0].Parsed[1].Block
httpDirectives = loadIncludes(file, httpDirectives, payload)
assert.Equal(t, 3, len(httpDirectives))

// first directive from conf2
d2 := httpDirectives[0]
assert.Equal(t, "proxy_set_header", d2.Directive)
assert.Equal(t, []string{"Conf-Two", "Two"}, d2.Args)
assert.Equal(t, file2, d2.File)
// second directive from conf1
d1 := httpDirectives[1]
assert.Equal(t, "proxy_set_header", d1.Directive)
assert.Equal(t, []string{"Conf-One", "One"}, d1.Args)
assert.Equal(t, file1, d1.File)
}

{
// test invalid includes
conf := `
events {}
http {
include not-exist.conf;
include %s invalid-args.conf;
include;
include %s;
server {
listen 80;
location / {
proxy_pass http:https://localhost:8888;
}
}
}
`
conf1 := `include %s; proxy_set_header Conf-One One;`
conf2 := `proxy_set_header Conf-Two Two;`

file2 := tempDir.Create(t, []byte(conf2))
file1 := tempDir.Create(t, []byte(fmt.Sprintf(conf1, file2)))
file := tempDir.Create(t, []byte(fmt.Sprintf(conf, file1, file1)))

payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
httpDirectives := payload.Config[0].Parsed[1].Block
httpDirectives = loadIncludes(file, httpDirectives, payload)
assert.Equal(t, 3, len(httpDirectives))

// first directive from conf2
d2 := httpDirectives[0]
assert.Equal(t, "proxy_set_header", d2.Directive)
assert.Equal(t, []string{"Conf-Two", "Two"}, d2.Args)
assert.Equal(t, file2, d2.File)
// second directive from conf1
d1 := httpDirectives[1]
assert.Equal(t, "proxy_set_header", d1.Directive)
assert.Equal(t, []string{"Conf-One", "One"}, d1.Args)
assert.Equal(t, file1, d1.File)
}
}
Loading