-
Notifications
You must be signed in to change notification settings - Fork 33
/
command.go
151 lines (143 loc) · 4.23 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package config
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
kuttlapi "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kyverno/chainsaw/pkg/apis/v1alpha1"
"github.com/kyverno/chainsaw/pkg/config"
"github.com/kyverno/chainsaw/pkg/resource"
"github.com/kyverno/pkg/ext/resource/convert"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)
const schema = "# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/configuration-chainsaw-v1alpha1.json"
func Command() *cobra.Command {
save := false
cleanup := false
cmd := &cobra.Command{
Use: "config",
Short: "Migrate KUTTL config to Chainsaw",
SilenceUsage: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return execute(
cmd.OutOrStdout(),
cmd.ErrOrStderr(),
save,
cleanup,
args[0],
)
},
}
cmd.Flags().BoolVar(&save, "save", false, "If set, converted files will be saved")
cmd.Flags().BoolVar(&cleanup, "cleanup", false, "If set, delete converted files")
return cmd
}
func execute(stdout io.Writer, stderr io.Writer, save, cleanup bool, path string) error {
resources, err := resource.Load(path, true)
if err != nil {
return err
}
if len(resources) != 1 {
return fmt.Errorf("invalid number of resources found (%d) in %s", len(resources), path)
}
cfg, err := migrate(stderr, path, resources[0])
if err != nil {
return err
}
data, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("converting to yaml: %w", err)
}
if save {
path := filepath.Join(filepath.Dir(path), config.DefaultFileName)
fmt.Fprintf(stderr, "Saving file %s ...\n", path)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
if _, err := f.WriteString(schema + "\n"); err != nil {
return fmt.Errorf("failed to write in file: %w", err)
}
if _, err := f.Write(data); err != nil {
return fmt.Errorf("failed to write in file: %w", err)
}
} else {
fmt.Fprintln(stdout, schema)
fmt.Fprintln(stdout, string(data))
}
if save && cleanup {
fmt.Fprintf(stderr, "Deleting file %s ...\n", path)
if err := os.Remove(path); err != nil {
return err
}
}
return nil
}
func isKuttl(resource unstructured.Unstructured) bool {
return strings.HasPrefix(resource.GetAPIVersion(), "kuttl.dev/")
}
func migrate(stderr io.Writer, path string, resource unstructured.Unstructured) (*v1alpha1.Configuration, error) {
fmt.Fprintf(stderr, "Converting config %s ...\n", path)
if isKuttl(resource) {
switch resource.GetKind() {
case "TestSuite":
configuration, err := testSuite(resource)
if err != nil {
fmt.Fprintf(stderr, "ERROR: failed to convert %s (%s): %s\n", "TestSuite", path, err)
return nil, err
}
if configuration.GetName() == "" {
configuration.SetName("configuration")
}
return configuration, nil
default:
fmt.Fprintf(stderr, "ERROR: unknown kuttl resource (%s): %s\n", path, resource.GetKind())
return nil, fmt.Errorf("unknown kuttl resource %s", resource.GetKind())
}
}
return nil, fmt.Errorf("unknown resource %s / %s", resource.GetAPIVersion(), resource.GetKind())
}
func testSuite(in unstructured.Unstructured) (*v1alpha1.Configuration, error) {
from, err := convert.To[kuttlapi.TestSuite](in)
if err != nil {
return nil, err
}
var timeouts v1alpha1.Timeouts
if from.Timeout != 0 {
d := metav1.Duration{Duration: time.Second * time.Duration(from.Timeout)}
timeouts = v1alpha1.Timeouts{
Apply: &d,
Assert: &d,
Error: &d,
Delete: &d,
Cleanup: &d,
Exec: &d,
}
}
to := &v1alpha1.Configuration{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha1.GroupVersion.String(),
Kind: "Configuration",
},
ObjectMeta: from.ObjectMeta,
Spec: v1alpha1.ConfigurationSpec{
Timeouts: timeouts,
SkipDelete: from.SkipDelete,
ReportFormat: v1alpha1.ReportFormatType(from.ReportFormat),
ReportName: from.ReportName,
Namespace: from.Namespace,
},
}
if from.Parallel != 0 {
to.Spec.Parallel = &from.Parallel
}
return to, nil
}