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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
finish convert code
  • Loading branch information
suchen-sci committed Nov 2, 2023
commit f81a0c940e70eb16f9a1dfae8120909fa092f880
88 changes: 88 additions & 0 deletions cmd/client/commandv2/common/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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

import (
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/filters"
"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"`
}

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 {
return &HTTPServerSpec{
Name: name,
Kind: httpserver.Kind,
Spec: *getDefaultHTTPServerSpec(),
}
}

// 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
}

// GetDefaultFilterSpec returns the default filter spec of the kind.
func GetDefaultFilterSpec(kind string) filters.Spec {
return filters.GetKind(kind).DefaultSpec()
}
57 changes: 53 additions & 4 deletions cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,83 @@ package nginx
import (
"encoding/json"
"fmt"
"math/rand"

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

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 {
var nginxConf string
flags := &Options{}
flags.init()
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{})
payload, err := crossplane.Parse(flags.NginxConf, &crossplane.ParseOptions{})
if err != nil {
general.ExitWithErrorf("parse nginx.conf failed: %v", err)
}
parsePayload(payload)
config, err := parsePayload(payload)
if err != nil {
general.ExitWithError(err)
}
hs, pls, err := convertConfig(flags, config)
if err != nil {
general.ExitWithError(err)
}
fmt.Println(hs, pls)
data, err := json.Marshal(payload)
if err != nil {
general.ExitWithError(err)
}
fmt.Println(string(data))
},
}
cmd.Flags().StringVarP(&nginxConf, "file", "f", "", "nginx.conf file path")
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)
}
95 changes: 95 additions & 0 deletions cmd/client/commandv2/convert/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,98 @@
*/

package nginx

import (
"fmt"

"github.com/megaease/easegress/v2/cmd/client/commandv2/common"
"github.com/megaease/easegress/v2/cmd/client/general"
"github.com/megaease/easegress/v2/pkg/filters"
"github.com/megaease/easegress/v2/pkg/object/httpserver/routers"
)

func convertConfig(options *Options, config *Config) ([]*common.HTTPServerSpec, []*common.PipelineSpec, error) {
httpServers := make([]*common.HTTPServerSpec, 0)
pipelines := make([]*common.PipelineSpec, 0)
for _, server := range config.Servers {
s, p, err := convertServer(options, server)
if err != nil {
return nil, nil, err
}
httpServers = append(httpServers, s)
pipelines = append(pipelines, p...)
}
return httpServers, pipelines, nil
}

func convertServer(options *Options, server *Server) (*common.HTTPServerSpec, []*common.PipelineSpec, error) {
pipelines := make([]*common.PipelineSpec, 0)
httpServer := common.NewHTTPServerSpec(fmt.Sprintf("%s-%d", options.Prefix, server.Port))
httpServer = convertServerBase(httpServer, server.ServerBase)

httpServer.Rules = make([]*routers.Rule, 0)
for _, rule := range server.Rules {
routerRule, pls := convertRule(options, rule)
httpServer.Rules = append(httpServer.Rules, routerRule)
pipelines = append(pipelines, pls...)
}
return httpServer, pipelines, nil
}

func convertServerBase(spec *common.HTTPServerSpec, base ServerBase) *common.HTTPServerSpec {
spec.Port = uint16(base.Port)
spec.Address = base.Address
spec.HTTPS = base.HTTPS
spec.CaCertBase64 = base.CaCert
for k, v := range base.Certs {
spec.Certs[k] = v
}
for k, v := range base.Keys {
spec.Keys[k] = v
}
return spec
}

func convertRule(options *Options, rule *Rule) (*routers.Rule, []*common.PipelineSpec) {
router := &routers.Rule{
Hosts: make([]routers.Host, 0),
Paths: make([]*routers.Path, 0),
}
for _, h := range rule.Hosts {
router.Hosts = append(router.Hosts, routers.Host{
Value: h.Value,
IsRegexp: h.IsRegexp,
})
}
pipelines := make([]*common.PipelineSpec, 0)
for _, p := range rule.Paths {
name := options.GetPipelineName(p.Path)
path := routers.Path{
Backend: name,
}
switch p.Type {
case PathTypeExact:
path.Path = p.Path
case PathTypePrefix:
path.PathPrefix = p.Path
case PathTypeRe:
path.PathRegexp = p.Path
case PathTypeReInsensitive:
path.PathRegexp = fmt.Sprintf("(?i)%s", p.Path)
default:
general.Warnf("unknown path type: %s", p.Type)
}
router.Paths = append(router.Paths, &path)
pipelineSpec := convertProxy(name, p.Backend)
pipelines = append(pipelines, pipelineSpec)
}
return router, pipelines
}

func convertProxy(name string, info *ProxyInfo) *common.PipelineSpec {
pipeline := common.NewPipelineSpec(name)
// TODO: set header, update proxy filter
proxy := common.NewProxyFilterSpec("proxy")
pipeline.SetFilters([]filters.Spec{proxy})
return pipeline
}
3 changes: 2 additions & 1 deletion cmd/client/commandv2/convert/nginx/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ func processSSLCertificates(certs []*Directive, keys []*Directive) (map[string]s
if err != nil {
return nil, nil, fmt.Errorf("%s: %v", directiveInfo(key), err)
}
// cert and keys should have the same key to match.
certMap[certName] = certData
keyMap[keyName] = keyData
keyMap[certName] = keyData
}
return certMap, keyMap, nil
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/client/commandv2/convert/nginx/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ func TestParsePayload(t *testing.T) {
file := tempDir.Create("nginx.conf", []byte(nginxConf))
payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)

config, err := parsePayload(payload)
assert.Nil(t, err)
printJson(config)
// printJson(config)

opt := &Options{Prefix: "test"}
opt.init()
hs, pls, err := convertConfig(opt, config)
assert.Nil(t, err)
printYaml(hs)
printYaml(pls)
}
}
7 changes: 7 additions & 0 deletions cmd/client/commandv2/convert/nginx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package nginx
import (
"encoding/json"
"fmt"

"gopkg.in/yaml.v3"
)

func directiveInfo(d *Directive) string {
Expand All @@ -30,3 +32,8 @@ func printJson(v interface{}) {
b, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(b))
}

func printYaml(v interface{}) {
b, _ := yaml.Marshal(v)
fmt.Println(string(b))
}