Skip to content

Commit

Permalink
first run at overlays (#966)
Browse files Browse the repository at this point in the history
* first run at overlays

Signed-off-by: jasmingacic <[email protected]>

* some more work on overlays

Signed-off-by: jasmingacic <[email protected]>

* checking for doc type before parsin openapi specs

Signed-off-by: jasmingacic <[email protected]>

* added overlay parameter to generate and deploy

Signed-off-by: jasmingacic <[email protected]>

* cleaned up overlays_test.go

Signed-off-by: jasmingacic <[email protected]>

* some minor clean up

Signed-off-by: jasmingacic <[email protected]>

* addressed PR remarks and
extended overlay_test.go

Signed-off-by: jasmingacic <[email protected]>

* refactoring and docs

Signed-off-by: jasmingacic <[email protected]>

* go mod tidy

Signed-off-by: jasmingacic <[email protected]>

* changed docker image

Signed-off-by: jasmingacic <[email protected]>

* revert manager_auth_proxy_patch.yaml

Signed-off-by: jasmingacic <[email protected]>

* revert manager_auth_proxy_patch.yaml

Signed-off-by: jasmingacic <[email protected]>

* refactoring

Signed-off-by: jasmingacic <[email protected]>

* build fix

Signed-off-by: jasmingacic <[email protected]>

Signed-off-by: jasmingacic <[email protected]>
  • Loading branch information
jasmingacic authored Nov 22, 2022
1 parent f3401c9 commit 608295c
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 150 deletions.
58 changes: 38 additions & 20 deletions cmd/kusk/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,30 @@ import (
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/errors"
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/kuskui"
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/mocking/filewatcher"
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/overlays"
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/utils"
"github.com/kubeshop/kusk-gateway/cmd/kusk/templates"
"github.com/kubeshop/kusk-gateway/pkg/options"
"github.com/kubeshop/kusk-gateway/pkg/spec"
)

var (
file string
watch bool
)

func init() {
//add to root command
rootCmd.AddCommand(deployCmd)

deployCmd.Flags().StringVarP(&file, "in", "i", "", "file path or URL to OpenAPI spec file to generate mappings from. e.g. --in apispec.yaml")
deployCmd.MarkFlagRequired("in")

deployCmd.Flags().StringVarP(&apiSpecPath, "in", "i", "", "file path or URL to OpenAPI spec file to generate mappings from. e.g. --in apispec.yaml")
deployCmd.Flags().BoolVarP(&watch, "watch", "w", false, "watch file changes and deploy on change")
deployCmd.Flags().StringVar(&name, "name", "", "name of the API")
deployCmd.Flags().StringVar(&namespace, "namespace", "default", "name of the API")
deployCmd.Flags().StringVar(&name, "name", "", "the name of the API resource")
deployCmd.Flags().StringVar(&namespace, "namespace", "default", "the namespace of the API resource")
deployCmd.Flags().StringVarP(&envoyFleetName, "envoyfleet.name", "", "kusk-gateway-envoy-fleet", "name of envoyfleet to use for this API. Default: kusk-gateway-envoy-fleet")

deployCmd.Flags().StringVarP(&envoyFleetNamespace, "envoyfleet.namespace", "", kusknamespace, "namespace of envoyfleet to use for this API. Default: kusk-system")

deployCmd.Flags().StringVarP(&overlaySpecPath, "overlay", "", "", "file path or URL to Overlay spec file to generate mappings from. e.g. --overlay overlay.yaml")

}

// apiCmd represents the api command
Expand All @@ -86,13 +85,13 @@ var deployCmd = &cobra.Command{
}
}

originalManifest, err := getParsedAndValidatedOpenAPISpec(file)
originalManifest, err := getParsedAndValidatedOpenAPISpec(overlaySpecPath, apiSpecPath)
if err != nil {
reportError(err)
return err
}

kuskui.PrintSuccess(fmt.Sprintf("successfully parsed %s", file))
kuskui.PrintSuccess(fmt.Sprintf("successfully parsed %s", apiSpecPath))
kuskui.PrintStart(fmt.Sprintf("initiallizing deployment to fleet %s", envoyFleetName))

k8sclient, err := utils.GetK8sClient()
Expand Down Expand Up @@ -137,11 +136,11 @@ var deployCmd = &cobra.Command{
kuskui.PrintInfo(fmt.Sprintf("api.gateway.kusk.io/%s created\n", api.Name))
}

if _, e := url.ParseRequestURI(file); e != nil {
if _, e := url.ParseRequestURI(apiSpecPath); e != nil {
if watch {
var watcher *filewatcher.FileWatcher

watcher, err = filewatcher.New(file)
watcher, err = filewatcher.New(apiSpecPath)
if err != nil {
reportError(err)
return err
Expand All @@ -152,13 +151,13 @@ var deployCmd = &cobra.Command{
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

if watcher != nil {
kuskui.PrintInfo(fmt.Sprintf("⏳ watching for API changes in %s", file))
kuskui.PrintInfo(fmt.Sprintf("⏳ watching for API changes in %s", apiSpecPath))
go watcher.Watch(func() {
kuskui.PrintStart(fmt.Sprintf("✍️ change detected in %s", file))
kuskui.PrintSuccess(fmt.Sprintf("successfully parsed %s", file))
kuskui.PrintStart(fmt.Sprintf("✍️ change detected in %s", apiSpecPath))
kuskui.PrintSuccess(fmt.Sprintf("successfully parsed %s", apiSpecPath))
kuskui.PrintStart(fmt.Sprintf("initiallizing deployment to fleet %s", envoyFleetName))

manifest, err := getParsedAndValidatedOpenAPISpec(file)
manifest, err := getParsedAndValidatedOpenAPISpec(overlaySpecPath, apiSpecPath)
if err != nil {
reportError(err)
kuskui.PrintError(err.Error())
Expand Down Expand Up @@ -203,12 +202,32 @@ var deployCmd = &cobra.Command{
},
}

func getParsedAndValidatedOpenAPISpec(apiSpecPath string) (string, error) {
func getParsedAndValidatedOpenAPISpec(overlaySpecPath, apiSpecPath string) (string, error) {
const KuskExtensionKey = "x-kusk"

parsedApiSpec, err := spec.NewParser(&openapi3.Loader{IsExternalRefsAllowed: true}).Parse(apiSpecPath)
if err != nil {
return "", err
parsedApiSpec := &openapi3.T{}
var err error

if overlaySpecPath != "" {
overlay, err := overlays.NewOverlay(overlaySpecPath)
if err != nil {
return "", err
}

overlayPath, err := overlay.Apply()
if err != nil {
return "", err
}

parsedApiSpec, err = spec.NewParser(&openapi3.Loader{IsExternalRefsAllowed: true}).Parse(overlayPath)
if err != nil {
return "", err
}
} else {
parsedApiSpec, err = spec.NewParser(&openapi3.Loader{IsExternalRefsAllowed: true}).Parse(apiSpecPath)
if err != nil {
return "", err
}
}

if _, ok := parsedApiSpec.ExtensionProps.Extensions[KuskExtensionKey]; !ok {
Expand All @@ -226,7 +245,6 @@ func getParsedAndValidatedOpenAPISpec(apiSpecPath string) (string, error) {
if err != nil {
return "", err
}

if err := opts.Validate(); err != nil {
return "", err
}
Expand Down
110 changes: 38 additions & 72 deletions cmd/kusk/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import (
"github.com/spf13/cobra"

"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/errors"
"github.com/kubeshop/kusk-gateway/cmd/kusk/internal/overlays"
"github.com/kubeshop/kusk-gateway/cmd/kusk/templates"
"github.com/kubeshop/kusk-gateway/pkg/options"
"github.com/kubeshop/kusk-gateway/pkg/spec"
)

var (
apiTemplate *template.Template
apiSpecPath string
apiTemplate *template.Template
apiSpecPath string
overlaySpecPath string

name string
namespace string
Expand All @@ -66,11 +68,31 @@ var generateCmd = &cobra.Command{
errors.NewErrorReporter(cmd, err).Report()
}
}
parsedApiSpec := &openapi3.T{}
parser := spec.NewParser(&openapi3.Loader{IsExternalRefsAllowed: true})
var err error

parsedApiSpec, err := spec.NewParser(&openapi3.Loader{IsExternalRefsAllowed: true}).Parse(apiSpecPath)
if err != nil {
reportError(err)
return err
if overlaySpecPath != "" {
overlay, err := overlays.NewOverlay(overlaySpecPath)
if err != nil {
return err
}
overlayPath, err := overlay.Apply()
if err != nil {
return err
}

parsedApiSpec, err = parser.Parse(overlayPath)
if err != nil {
reportError(err)
return err
}
} else if apiSpecPath != "" {
parsedApiSpec, err = parser.Parse(apiSpecPath)
if err != nil {
reportError(err)
return err
}
}

if _, ok := parsedApiSpec.ExtensionProps.Extensions["x-kusk"]; !ok {
Expand Down Expand Up @@ -170,74 +192,18 @@ func getAPISpecString(apiSpec *openapi3.T) (string, error) {

func init() {
rootCmd.AddCommand(generateCmd)
// This should be deprecated soon.
// See `apiCmd.Deprecated`.
apiCmd.AddCommand(generateCmd)

generateCmd.Flags().StringVarP(
&name,
"name",
"",
"",
"the name to give the API resource e.g. --name my-api",
)

generateCmd.Flags().StringVarP(
&namespace,
"namespace",
"n",
"default",
"the namespace of the API resource e.g. --namespace my-namespace, -n my-namespace",
)

generateCmd.Flags().StringVarP(
&apiSpecPath,
"in",
"i",
"",
"file path or URL to OpenAPI spec file to generate mappings from. e.g. --in apispec.yaml",
)
generateCmd.MarkFlagRequired("in")

generateCmd.Flags().StringVarP(
&serviceName,
"upstream.service",
"",
"",
"name of upstream service",
)

generateCmd.Flags().StringVarP(
&serviceNamespace,
"upstream.namespace",
"",
"default",
"namespace of upstream service",
)

generateCmd.Flags().Uint32VarP(
&servicePort,
"upstream.port",
"",
80,
"port of upstream service",
)

generateCmd.Flags().StringVarP(
&envoyFleetName,
"envoyfleet.name",
"",
"kusk-gateway-envoy-fleet",
"name of envoyfleet to use for this API. Default: kusk-gateway-envoy-fleet",
)

generateCmd.Flags().StringVarP(
&envoyFleetNamespace,
"envoyfleet.namespace",
"",
kusknamespace,
"namespace of envoyfleet to use for this API. Default: kusk-system",
)
generateCmd.Flags().StringVarP(&name, "name", "", "", "the name to give the API resource e.g. --name my-api")
generateCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "the namespace of the API resource e.g. --namespace my-namespace, -n my-namespace")
generateCmd.Flags().StringVarP(&apiSpecPath, "in", "i", "", "file path or URL to OpenAPI spec file to generate mappings from. e.g. --in apispec.yaml")

generateCmd.Flags().StringVarP(&serviceName, "upstream.service", "", "", "name of upstream service")
generateCmd.Flags().StringVarP(&serviceNamespace, "upstream.namespace", "", "default", "namespace of upstream service")
generateCmd.Flags().Uint32VarP(&servicePort, "upstream.port", "", 80, "port of upstream service")
generateCmd.Flags().StringVarP(&envoyFleetName, "envoyfleet.name", "", "kusk-gateway-envoy-fleet", "name of envoyfleet to use for this API. Default: kusk-gateway-envoy-fleet")
generateCmd.Flags().StringVarP(&envoyFleetNamespace, "envoyfleet.namespace", "", kusknamespace, "namespace of envoyfleet to use for this API. Default: kusk-system")
generateCmd.Flags().StringVarP(&overlaySpecPath, "overlay", "", "", "file path or URL to Overlay spec file to generate mappings from. e.g. --overlay overlay.yaml")

apiTemplate = template.Must(template.New("api").Parse(templates.APITemplate))
}
Expand Down
1 change: 0 additions & 1 deletion cmd/kusk/cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,5 @@ var ipCmd = &cobra.Command{
return
}
fmt.Println(ip)
return
},
}
2 changes: 1 addition & 1 deletion cmd/kusk/cmd/manifest_data.go

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

Loading

0 comments on commit 608295c

Please sign in to comment.