You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by "treblereel (via GitHub)" <gi...@apache.org> on 2024/10/27 23:06:56 UTC

[PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

treblereel opened a new pull request, #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711

   issue: https://github.com/apache/incubator-kie-tools/issues/2601
   
   tests are missed atm, I'll add it in a couple of days


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "ricardozanini (via GitHub)" <gi...@apache.org>.
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "treblereel (via GitHub)" <gi...@apache.org>.
treblereel commented on code in PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#discussion_r1823730608


##########
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:
   @ricardozanini https://github.com/apache/incubator-kie-tools/issues/2718



-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "domhanak (via GitHub)" <gi...@apache.org>.
domhanak commented on PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#issuecomment-2460290983

   > @domhanak can you please verify this?
   
   Yup, working on it, investigating some issue with compilation after the minifications, Will let you know.


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "ricardozanini (via GitHub)" <gi...@apache.org>.
ricardozanini commented on code in PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#discussion_r1825980701


##########
packages/kn-plugin-workflow/pkg/specs/openapi_minifier.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 specs
+
+import (
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
+	"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     *OpenApiMinifierOpts
+	operations map[string]sets.Set[string]
+}
+
+type OpenApiMinifierOpts 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 *OpenApiMinifierOpts) *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 {

Review Comment:
   ```suggestion
   func (m *OpenApiMinifier) fetchSpecFromFunctions() error {
   ```



##########
packages/kn-plugin-workflow/pkg/specs/openapi_minifier.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 specs
+
+import (
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
+	"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     *OpenApiMinifierOpts
+	operations map[string]sets.Set[string]
+}
+
+type OpenApiMinifierOpts 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 *OpenApiMinifierOpts) *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 {

Review Comment:
   I believe this name gives a stronger meaning to what this func does.



##########
packages/kn-plugin-workflow/pkg/specs/openapi_minifier.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 specs
+
+import (
+	"encoding/json"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
+	"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     *OpenApiMinifierOpts
+	operations map[string]sets.Set[string]
+}
+
+type OpenApiMinifierOpts 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 *OpenApiMinifierOpts) *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 {

Review Comment:
   ```suggestion
   func (m *OpenApiMinifier) fetchSpecFromFunction(workflowFile string) error {
   ```



-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "domhanak (via GitHub)" <gi...@apache.org>.
domhanak commented on PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#issuecomment-2467759296

   # Documentation of the command
   
   - `kn workflow specs help`  - :heavy_check_mark:  
   - `kn workflow specs minify` -> this does nothing, and when I do `kn workflow specs minify help` the result is the same. I think we can better explain the command to the user, in a sense he understands `openapi` needs to be added as argument to start the minification. For example adding more targeted HELP description here :construction: 
   - `kn workflow specs minify openapi help`  -> there seems to be an issue with the HELP description, notice the  {{.Name}} - `{{.Name}} specs minify openapi --specs-dir=<full_directory_path>`  :construction: 
   
   # Functional
   
   - `kn workflow specs minify openapi`
   - [Input](https://gist.github.com/domhanak/dc00ed7547fa92f70bd9e000422c4914) results in [output](https://gist.github.com/domhanak/60d4df3c940f7a36d21f5c5afcf0c392) -> Is that correct? I can see a ConfigMap `01-hello-resources-specs.yaml` when deploying but the workflow is failing with compilation errors.
   
   :eyes: investigating why :eyes:
   
   More information about testing to follow 
   


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "ricardozanini (via GitHub)" <gi...@apache.org>.
ricardozanini commented on PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#issuecomment-2455256600

   @domhanak can you please verify this?


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "treblereel (via GitHub)" <gi...@apache.org>.
treblereel commented on PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711#issuecomment-2451182220

   @ricardozanini I have added `--minify` option to the deploy, it allows us to skip minification if needed
   By default it's true


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#2601: [kn-plugin-workflow] Minify the openAPI spec files to trim operations only used by the workflows in the current project [incubator-kie-tools]

Posted by "ricardozanini (via GitHub)" <gi...@apache.org>.
ricardozanini merged PR #2711:
URL: https://github.com/apache/incubator-kie-tools/pull/2711


-- 
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: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org