Skip to content

Commit

Permalink
chore: add a converter script
Browse files Browse the repository at this point in the history
Signed-off-by: RealAnna <[email protected]>
  • Loading branch information
RealAnna committed Feb 5, 2024
1 parent 0c49cf2 commit 3b352da
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
79 changes: 79 additions & 0 deletions lifecycle-operator/converter/convert_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"

Check failure on line 4 in lifecycle-operator/converter/convert_app.go

View workflow job for this annotation

GitHub Actions / golangci-lint (lifecycle-operator, lifecycle-operator/)

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1alpha3"
klcv1beta1 "github.com/keptn/lifecycle-toolkit/lifecycle-operator/apis/lifecycle/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"log"

Check failure on line 8 in lifecycle-operator/converter/convert_app.go

View workflow job for this annotation

GitHub Actions / golangci-lint (lifecycle-operator, lifecycle-operator/)

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"os"
"sigs.k8s.io/yaml"
)

func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: go run main.go app_cr_input_file.yaml output_file.yaml")
os.Exit(1)
}

inputFile := os.Args[1]
outputFile := os.Args[2]

inputContent, err := os.ReadFile(inputFile)
if err != nil {
log.Fatalf("Error reading input file: %v", err)
}

var keptnApp klcv1alpha3.KeptnApp
if err := yaml.Unmarshal(inputContent, &keptnApp); err != nil {
log.Fatalf("Error unmarshalling YAML: %v", err)
}

var keptnAppV1beta1 klcv1beta1.KeptnApp
if err := yaml.Unmarshal(inputContent, &keptnAppV1beta1); err != nil {
log.Fatalf("Error unmarshalling YAML: %v", err)
}

keptnAppV1beta1.TypeMeta.APIVersion = "lifecycle.keptn.sh/v1beta1"

// Transform KeptnAppContext
keptnAppContext := klcv1beta1.KeptnAppContext{
TypeMeta: metav1.TypeMeta{
Kind: "KeptnAppContext",
APIVersion: "lifecycle.keptn.sh/v1beta1",
},

ObjectMeta: metav1.ObjectMeta{
Name: keptnApp.Name,
Namespace: keptnApp.Namespace,
},
Spec: klcv1beta1.KeptnAppContextSpec{
DeploymentTaskSpec: klcv1beta1.DeploymentTaskSpec{

PreDeploymentTasks: keptnApp.Spec.PreDeploymentTasks,
PreDeploymentEvaluations: keptnApp.Spec.PreDeploymentEvaluations,
PostDeploymentTasks: keptnApp.Spec.PostDeploymentTasks,
PostDeploymentEvaluations: keptnApp.Spec.PostDeploymentEvaluations,
},
},
}

// Convert to YAML and write to output file
keptnAppV1beta1YAML, err := yaml.Marshal(keptnAppV1beta1)
if err != nil {
log.Fatalf("Error marshalling to YAML: %v", err)
}

keptnAppContextYAML, err := yaml.Marshal(keptnAppContext)
if err != nil {
log.Fatalf("Error marshalling to YAML: %v", err)
}

// Combine and write to output file
outputContent := fmt.Sprintf("%s\n---\n%s", string(keptnAppV1beta1YAML), string(keptnAppContextYAML))
if err := os.WriteFile(outputFile, []byte(outputContent), 0644); err != nil {
log.Fatalf("Error writing to output file: %v", err)
}

fmt.Println("Transformation completed. Output written to", outputFile)
}
35 changes: 35 additions & 0 deletions lifecycle-operator/converter/convert_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"os"
"testing"
)

const inputFileName = "example_keptnapp.yaml"
const outputFileName = "example_output.yaml"

func TestMigration(t *testing.T) {
// Set up a temporary directory for test files
// Run the main function with test arguments
inputFile := inputFileName
outputFile := outputFileName
os.Args = []string{"main", inputFile, outputFile}
main()

// Read the expected output file content
expectedOutput, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Error reading expected output file: %v", err)
}

// Read the actual output file content
actualOutput, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Error reading actual output file: %v", err)
}

// Assert that the actual output file content matches the expected output
if string(actualOutput) != string(expectedOutput) {
t.Errorf("Unexpected output content. Expected:\n%s\n\nActual:\n%s", string(expectedOutput), string(actualOutput))
}
}
18 changes: 18 additions & 0 deletions lifecycle-operator/converter/example_keptnapp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnApp
metadata:
name: "some-keptn-app"
namespace: "my-app-ns"
spec:
version: "1.2.3"
workloads:
- name: podtato-head-left-arm
version: 0.2.7
preDeploymentTasks:
- pre-deployment-task
preDeploymentEvaluations:
- pre-deployment-evaluation
postDeploymentTasks:
- post-deployment-task
postDeploymentEvaluations:
- post-deployment-evaluation
30 changes: 30 additions & 0 deletions lifecycle-operator/converter/example_output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: lifecycle.keptn.sh/v1beta1
kind: KeptnApp
metadata:
creationTimestamp: null
name: some-keptn-app
namespace: my-app-ns
spec:
version: 1.2.3
workloads:
- name: podtato-head-left-arm
version: 0.2.7
status: {}

---
apiVersion: lifecycle.keptn.sh/v1beta1
kind: KeptnAppContext
metadata:
creationTimestamp: null
name: some-keptn-app
namespace: my-app-ns
spec:
postDeploymentEvaluations:
- post-deployment-evaluation
postDeploymentTasks:
- post-deployment-task
preDeploymentEvaluations:
- pre-deployment-evaluation
preDeploymentTasks:
- pre-deployment-task
status: {}

0 comments on commit 3b352da

Please sign in to comment.