ricardozanini commented on code in PR #2711:
URL:
https://github.com/apache/incubator-kie-tools/pull/2711#discussion_r1821406849
##########
packages/kn-plugin-workflow/pkg/command/deploy_undeploy_common.go:
##########
@@ -122,14 +122,14 @@ func generateManifests(cfg *DeployUndeployCmdConfig)
error {
supportFileExtensions := []string{metadata.JSONExtension,
metadata.YAMLExtension, metadata.YMLExtension}
fmt.Println("🔍 Looking for specs files...")
- files, err = common.FindFilesWithExtensions(cfg.SpecsDir,
supportFileExtensions)
+ minifiedfiles, err := common.NewMinifier(&common.OpenApiMinifierParams{
Review Comment:
Then if you accept my other suggestion we can have something like this:
```suggestion
minifiedfiles, err := specs.NewMinifier(&common.OpenApiMinifierParams{
```
##########
packages/kn-plugin-workflow/pkg/common/openapi_minifier.go:
##########
@@ -0,0 +1,278 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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://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 (
+ "encoding/json"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+
+
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
+
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08"
+ "github.com/getkin/kin-openapi/openapi3"
+ "gopkg.in/yaml.v3"
+ "k8s.io/apimachinery/pkg/util/sets"
+ yamlk8s "k8s.io/apimachinery/pkg/util/yaml"
+)
+
+type OpenApiMinifier struct {
+ workflows []string
+ params *OpenApiMinifierParams
+ operations map[string]sets.Set[string]
+}
+
+type OpenApiMinifierParams struct {
+ RootPath string
+ SpecsDir string
+ SubflowsDir string
+}
+
+var workflowExtensionsType = []string{metadata.YAMLSWExtension,
metadata.YMLSWExtension, metadata.JSONSWExtension}
+
+var minifiedExtensionsType = []string{metadata.YAMLExtension,
metadata.YMLExtension, metadata.JSONExtension}
+
+// k8sFileSizeLimit defines the maximum file size allowed (e.g., Kubernetes
ConfigMap size limit is 1MB)
+const k8sFileSizeLimit = 3145728 // 3MB
+
+func NewMinifier(params *OpenApiMinifierParams) *OpenApiMinifier {
+ return &OpenApiMinifier{params: params, operations:
make(map[string]sets.Set[string]), workflows: []string{}}
+}
+
+// Minify removes unused operations from OpenAPI specs based on the functions
used in workflows.
+func (m *OpenApiMinifier) Minify() (map[string]string, error) {
+ if err := m.findWorkflowFile(); err != nil {
+ return nil, err
+ }
+
+ m.findSubflowsFiles(m.params)
+
+ if err := m.processFunctions(); err != nil {
+ return nil, err
+ }
+
+ if err := m.validateSpecsFiles(); err != nil {
+ return nil, err
+ }
+
+ minifySpecsFiles, err := m.minifySpecsFiles()
+ if err != nil {
+ return nil, err
+ }
+
+ return minifySpecsFiles, nil
+}
+
+func (m *OpenApiMinifier) processFunctions() error {
+ for _, workflowFile := range m.workflows {
+ err := m.processFunction(workflowFile)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (m *OpenApiMinifier) processFunction(workflowFile string) error {
+ workflow, err := m.GetWorkflow(workflowFile)
+ if err != nil {
+ return err
+ }
+
+ relativePath, err := filepath.Rel(filepath.Dir(workflowFile),
m.params.SpecsDir)
+ if err != nil {
+ return err
+ }
+
+ if workflow.Functions == nil {
+ return nil
+ }
+
+ for _, function := range workflow.Functions {
+ if strings.HasPrefix(function.Operation, relativePath) {
+ trimmedPrefix := strings.TrimPrefix(function.Operation,
relativePath+"/")
+ parts := strings.Split(trimmedPrefix, "#")
+ apiFileName := parts[0]
+ operation := parts[1]
+
+ if _, ok := m.operations[apiFileName]; !ok {
+ m.operations[apiFileName] = sets.Set[string]{}
+ }
+ m.operations[apiFileName].Insert(operation)
+ }
+ }
+ return nil
+}
+
+func (m *OpenApiMinifier) validateSpecsFiles() error {
+ for specFile := range m.operations {
+ specFileName := filepath.Join(m.params.SpecsDir, specFile)
+ if _, err := os.Stat(specFileName); err != nil {
+ return fmt.Errorf("❌ ERROR: file %s not found or can't
be open", specFileName)
+ }
+ }
+ return nil
+}
+
+func (m *OpenApiMinifier) minifySpecsFiles() (map[string]string, error) {
+ minifySpecsFiles := map[string]string{}
+ for key, value := range m.operations {
+ minifiedSpecName, err := m.minifySpecsFile(key, value)
+ if err != nil {
+ return nil, err
+ }
+ minifySpecsFiles[key] = minifiedSpecName
+ }
+ return minifySpecsFiles, nil
+}
+
+func (m *OpenApiMinifier) minifySpecsFile(specFileName string, operations
sets.Set[string]) (string, error) {
+ specFile := filepath.Join(m.params.SpecsDir, specFileName)
+ data, err := os.ReadFile(specFile)
+ if err != nil {
+ return "", fmt.Errorf("❌ ERROR: failed to read OpenAPI
document: %w", err)
+ }
+
+ doc, err := openapi3.NewLoader().LoadFromData(data)
+ if err != nil {
+ return "", fmt.Errorf("❌ ERROR: failed to load OpenAPI
document: %w", err)
+ }
+ if doc.Paths == nil {
+ return "", fmt.Errorf("OpenAPI document %s has no paths",
specFileName)
+ }
+ for key, value := range doc.Paths.Map() {
Review Comment:
So we are removing `paths`, but we keep `components`?
https://spec.openapis.org/oas/latest.html#components-object
Some components that were only referenced by these paths might also be
removed.
##########
packages/kn-plugin-workflow/pkg/common/openapi_minifier.go:
##########
@@ -0,0 +1,278 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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://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 (
+ "encoding/json"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+
+
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
+
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08"
+ "github.com/getkin/kin-openapi/openapi3"
+ "gopkg.in/yaml.v3"
+ "k8s.io/apimachinery/pkg/util/sets"
+ yamlk8s "k8s.io/apimachinery/pkg/util/yaml"
+)
+
+type OpenApiMinifier struct {
Review Comment:
Can you move this to the `pkg/specs` module?
##########
packages/kn-plugin-workflow/pkg/common/openapi_minifier.go:
##########
@@ -0,0 +1,278 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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://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 (
+ "encoding/json"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+
+
"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
+
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08"
+ "github.com/getkin/kin-openapi/openapi3"
+ "gopkg.in/yaml.v3"
+ "k8s.io/apimachinery/pkg/util/sets"
+ yamlk8s "k8s.io/apimachinery/pkg/util/yaml"
+)
+
+type OpenApiMinifier struct {
+ workflows []string
+ params *OpenApiMinifierParams
+ operations map[string]sets.Set[string]
+}
+
+type OpenApiMinifierParams struct {
Review Comment:
```suggestion
type OpenApiMinifierOpts struct {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]