-
Notifications
You must be signed in to change notification settings - Fork 492
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
Support nginx conf #1130
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6e0e682
finish include directive
suchen-sci 52cddb0
httpserver spec support wildcards
suchen-sci c8fa3e2
get server info from nginx.conf
suchen-sci 4d4c0b0
finish parse part of nginx convert
suchen-sci 4219c60
fix bug
suchen-sci db56904
optim
suchen-sci f81a0c9
finish convert code
suchen-sci 09a055b
mv omitempty from jsonschema to json, to the generated yaml is more c…
suchen-sci 977554f
finish nginx convert code
suchen-sci 17d5e70
fix go.mod
suchen-sci 970e3af
add more test for parse
suchen-sci 0d472e4
add more test for convert
suchen-sci 5a8c606
add more test in cmd
suchen-sci 6d47267
add tests
suchen-sci 96f36eb
Merge branch 'main' into support-nginx-conf
suchen-sci d966e40
fix conflict
suchen-sci 3fdb6fd
add more test
suchen-sci 1f88675
add doc
suchen-sci 423867c
add comments to variables and functions
suchen-sci 64689a2
Update cmd/client/commandv2/convert.go
suchen-sci 1f9f2cb
Update cmd/client/commandv2/convert/nginx/cmd.go
suchen-sci f7c6ba2
udpate based on review advice
suchen-sci 8471544
fix test
suchen-sci File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
finish convert code
- Loading branch information
commit f81a0c940e70eb16f9a1dfae8120909fa092f880
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefix is too general? Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!