-
Notifications
You must be signed in to change notification settings - Fork 50
converter for istio in-cluster operator config to sail operator config #616
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bb947a5
converter for istio in-cluster operator config to sail operator config
ctartici f3e7800
Added output argument and a new test for optional arguments
ctartici e995004
removed -i and -o from arguments
ctartici 57f684e
version input to optional and modify validate function
ctartici a6f1f0c
remove validate yaml function
ctartici fece784
adding comments and make code more readable
ctartici 3e8c469
some updates
ctartici ca9b36e
remove executeConfigConverter
ctartici 36b6fd6
some minor changes in readme and converter
ctartici b23cfa1
changing order of testcase items
ctartici 9834b62
fix bug absent output file in converter
ctartici fa93417
converter to work with any order of input
ctartici File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,150 @@ | ||
// Copyright Istio Authors | ||
// | ||
// 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://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 converter | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/istio-ecosystem/sail-operator/pkg/test/project" | ||
"github.com/istio-ecosystem/sail-operator/tests/e2e/util/shell" | ||
. "github.com/onsi/gomega" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
converter = filepath.Join(project.RootDir, "tools", "configuration-converter.sh") | ||
istioFile = filepath.Join(project.RootDir, "tools", "istioConfig.yaml") | ||
sailFile = filepath.Join(project.RootDir, "tools", "istioConfig-sail.yaml") | ||
) | ||
|
||
func TestConversion(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
input string | ||
args string | ||
expectedOutput string | ||
}{ | ||
{ | ||
name: "simple", | ||
input: `apiVersion: install.istio.io/v1alpha1 | ||
kind: IstioOperator | ||
metadata: | ||
name: default | ||
spec:`, | ||
args: fmt.Sprintf("%s %s -n istio-system -v v1.24.3", istioFile, sailFile), | ||
expectedOutput: `apiVersion: sailoperator.io/v1 | ||
kind: Istio | ||
metadata: | ||
name: default | ||
spec: | ||
namespace: istio-system | ||
version: v1.24.3`, | ||
}, | ||
{ | ||
name: "complex", | ||
input: `apiVersion: install.istio.io/v1alpha1 | ||
kind: IstioOperator | ||
metadata: | ||
name: default | ||
spec: | ||
components: | ||
base: | ||
enabled: true | ||
pilot: | ||
enabled: false | ||
values: | ||
global: | ||
externalIstiod: true | ||
operatorManageWebhooks: true | ||
configValidation: false | ||
base: | ||
enableCRDTemplates: true | ||
pilot: | ||
env: | ||
PILOT_ENABLE_STATUS: true`, | ||
args: fmt.Sprintf("-v v1.24.3 %s -n istio-system %s", istioFile, sailFile), | ||
expectedOutput: `apiVersion: sailoperator.io/v1 | ||
kind: Istio | ||
metadata: | ||
name: default | ||
spec: | ||
values: | ||
global: | ||
externalIstiod: true | ||
operatorManageWebhooks: true | ||
configValidation: false | ||
base: | ||
enableCRDTemplates: true | ||
enabled: true | ||
pilot: | ||
env: | ||
PILOT_ENABLE_STATUS: "true" | ||
enabled: false | ||
namespace: istio-system | ||
version: v1.24.3`, | ||
}, | ||
{ | ||
name: "mandatory-arguments-only", | ||
input: `apiVersion: install.istio.io/v1alpha1 | ||
kind: IstioOperator | ||
metadata: | ||
name: default | ||
spec:`, | ||
args: istioFile, | ||
expectedOutput: `apiVersion: sailoperator.io/v1 | ||
kind: Istio | ||
metadata: | ||
name: default | ||
spec: | ||
namespace: istio-system`, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
t.Cleanup(func() { | ||
g.Expect(os.Remove(istioFile)).To(Succeed()) | ||
g.Expect(os.Remove(sailFile)).To(Succeed()) | ||
}) | ||
|
||
g.Expect(os.WriteFile(istioFile, []byte(tc.input), 0o644)).To(Succeed(), "failed to write YAML file") | ||
|
||
_, err := shell.ExecuteCommand(converter + " " + tc.args) | ||
g.Expect(err).NotTo(HaveOccurred(), "error in execution of ./configuration-converter.sh") | ||
|
||
actualOutput, err := os.ReadFile(sailFile) | ||
g.Expect(err).NotTo(HaveOccurred(), "Cannot read %s", sailFile) | ||
|
||
actualData, err := parseYaml(actualOutput) | ||
g.Expect(err).NotTo(HaveOccurred(), "Failed to parse sailFile") | ||
|
||
expectedData, err := parseYaml([]byte(tc.expectedOutput)) | ||
g.Expect(err).NotTo(HaveOccurred(), "Failed to parse expected output") | ||
|
||
g.Expect(cmp.Diff(actualData, expectedData)).To(Equal(""), "Conversion is not as expected") | ||
}) | ||
} | ||
} | ||
|
||
// parseYaml takes a YAML string and unmarshals it into a map | ||
func parseYaml(yamlContent []byte) (map[string]interface{}, error) { | ||
var config map[string]interface{} | ||
err := yaml.Unmarshal(yamlContent, &config) | ||
return config, err | ||
} |
This file contains hidden or 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 hidden or 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,131 @@ | ||
#!/bin/bash | ||
|
||
# Copyright Istio Authors | ||
# | ||
# 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://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. | ||
|
||
# This script is used to convert istio configuration to sail operator configuration. | ||
# In the end of the execution new yaml file will be created with "sail-ISTIO_CONFIG_YAML" name. | ||
# Usage: ./configuration-converter.sh ISTIO_CONFIG_YAML_WITH_PATH, example: ./configuration-converter.sh sample_config.yaml" | ||
set -e | ||
|
||
# Function to show usage | ||
usage() { | ||
echo "Usage: $0 <input> [output] [-n <namespace>] [-v <version>]" | ||
echo " <input> : Input file (required)" | ||
echo " [output] : Output file (optional, defaults to input's file name with '-sail.yaml' suffix)" | ||
echo " -n <namespace> : Namespace (optional, defaults to 'istio-system')" | ||
echo " -v <version> : Istio Version (optional)" | ||
exit 1 | ||
} | ||
|
||
# Initialize variables | ||
INPUT="" | ||
OUTPUT="" | ||
NAMESPACE="istio-system" | ||
VERSION="" | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
-n) | ||
if [[ -z "$2" || "$2" == -* ]]; then | ||
echo "Error: -n requires a non-empty argument." | ||
exit 1 | ||
fi | ||
NAMESPACE="$2" | ||
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. Nit: |
||
shift 2 | ||
;; | ||
-v) | ||
if [[ -z "$2" || "$2" == -* ]]; then | ||
echo "Error: -v requires a non-empty argument." | ||
exit 1 | ||
fi | ||
VERSION="$2" | ||
shift 2 | ||
;; | ||
*) | ||
if [[ -z "$INPUT" ]]; then | ||
INPUT="$1" # First positional argument is the INPUT | ||
elif [[ -z "$OUTPUT" ]]; then | ||
OUTPUT="$1" # Second positional argument is the OUTPUT | ||
fi | ||
shift | ||
;; | ||
esac | ||
done | ||
|
||
# Ensure the input file is provided and is valid | ||
if [[ -z "$INPUT" || ! -f "$INPUT" ]]; then | ||
echo "Error: Input file is missing or invalid." | ||
usage | ||
fi | ||
|
||
ctartici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# If OUTPUT is not specified, generate a default output file name | ||
if [[ -z "$OUTPUT" ]]; then | ||
OUTPUT="$(dirname "$INPUT")/$(basename "$INPUT" .yaml)-sail.yaml" | ||
elif [[ -d "$OUTPUT" ]]; then | ||
echo "Error: OUTPUT must be a file, not a directory." | ||
exit 1 | ||
fi | ||
|
||
if ! command -v yq &>/dev/null; then | ||
echo "Error: 'yq' is not installed. Please install it before running the script." | ||
exit 1 | ||
fi | ||
|
||
function add_mandatory_fields(){ | ||
yq -i eval ".apiVersion = \"sailoperator.io/v1\" | ||
luksa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .kind = \"Istio\" | ||
| (select(.spec.meshConfig) | .spec.values.meshConfig) = .spec.meshConfig | ||
| (select(.spec.values.istio_cni) | .spec.values.pilot.cni) = .spec.values.istio_cni | ||
| .metadata.name = \"default\" | ||
| .spec.namespace = \"$NAMESPACE\" | ||
| del(.spec.values.istio_cni) | ||
| del(.spec.meshConfig) | ||
| del(.spec.hub) | ||
| del(.spec.tag) | ||
| del(.spec.values.gateways)" "$OUTPUT" | ||
|
||
# If VERSION is not empty, add .spec.version | ||
if [[ -n "$VERSION" ]]; then | ||
yq -i ".spec.version = \"$VERSION\"" "$OUTPUT" | ||
fi | ||
|
||
} | ||
|
||
function boolean_2_string(){ | ||
#Convert boolean values to string if they are under *.env | ||
yq -i -e ' | ||
(.spec.values.[].env.[] | select(. == true)) |= "true" | | ||
(.spec.values.[].env.[] | select(. == false)) |= "false" | | ||
del(.. | select(length == 0))' "$OUTPUT" | ||
luksa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
# Note that if there is an entry except spec.components.<component>.enabled: true/false converter will delete them and warn user | ||
function validate_spec_components(){ | ||
if [[ $(yq eval '.spec.components' "$OUTPUT") != "null" ]]; then | ||
yq -i 'del(.spec.components.[] | keys[] | select(. != "enabled")) | .spec.values *= .spec.components | del (.spec.components)' "$OUTPUT" | ||
echo "Only values in the format spec.components.<component>.enabled: true/false are supported for conversion. For more details, refer to the documentation: https://github.com/istio-ecosystem/sail-operator/tree/main/docs#components-field" | ||
fi | ||
} | ||
|
||
# create output file | ||
cp "$INPUT" "$OUTPUT" | ||
|
||
# in-place edit created output file | ||
add_mandatory_fields | ||
boolean_2_string | ||
validate_spec_components | ||
|
||
|
||
echo "Sail configuration file created with name: $(realpath "$OUTPUT")" |
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.