|
| 1 | +// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/runtime/serializer" |
| 16 | + "k8s.io/client-go/kubernetes/scheme" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 19 | + "sigs.k8s.io/yaml" |
| 20 | + |
| 21 | + // Import all supported provider implementations. |
| 22 | + _ "github.com/ironcore-dev/network-operator/internal/provider/openconfig" |
| 23 | + |
| 24 | + "github.com/ironcore-dev/network-operator/api/v1alpha1" |
| 25 | + "github.com/ironcore-dev/network-operator/internal/clientutil" |
| 26 | + "github.com/ironcore-dev/network-operator/internal/provider" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + address = flag.String("address", "", "API endpoint address (required)") |
| 31 | + username = flag.String("username", "", "Username for authentication (required)") |
| 32 | + password = flag.String("password", "", "Password for authentication (required)") |
| 33 | + file = flag.String("file", "", "Path to Kubernetes resource manifest file (required)") |
| 34 | + providerName = flag.String("provider", "openconfig", "Provider implementation to use") |
| 35 | +) |
| 36 | + |
| 37 | +func usage() { |
| 38 | + fmt.Fprintf(os.Stderr, "Usage: %s [flags] <create|delete>\n\n", os.Args[0]) |
| 39 | + fmt.Fprintf(os.Stderr, "A debug tool for testing provider implementations.\n\n") |
| 40 | + fmt.Fprintf(os.Stderr, "Arguments:\n") |
| 41 | + fmt.Fprintf(os.Stderr, " create|delete Operation to perform on the resource\n\n") |
| 42 | + fmt.Fprintf(os.Stderr, "Flags:\n") |
| 43 | + flag.PrintDefaults() |
| 44 | + fmt.Fprintf(os.Stderr, "\nExample:\n") |
| 45 | + fmt.Fprintf(os.Stderr, " %s -address=192.168.1.1:9339 -username=admin -password=secret -file=config/samples/v1alpha1_device.yaml create\n", os.Args[0]) |
| 46 | +} |
| 47 | + |
| 48 | +func validateFlags() error { |
| 49 | + if *address == "" { |
| 50 | + return errors.New("address flag is required") |
| 51 | + } |
| 52 | + if *username == "" { |
| 53 | + return errors.New("username flag is required") |
| 54 | + } |
| 55 | + if *password == "" { |
| 56 | + return errors.New("password flag is required") |
| 57 | + } |
| 58 | + if *file == "" { |
| 59 | + return errors.New("file flag is required") |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func validatePositionalArgs() (string, error) { |
| 65 | + if len(flag.Args()) != 1 { |
| 66 | + return "", errors.New("exactly one positional argument (create|delete) is required") |
| 67 | + } |
| 68 | + |
| 69 | + operation := flag.Args()[0] |
| 70 | + if operation != "create" && operation != "delete" { |
| 71 | + return "", fmt.Errorf("positional argument must be either 'create' or 'delete', got: %s", operation) |
| 72 | + } |
| 73 | + |
| 74 | + return operation, nil |
| 75 | +} |
| 76 | + |
| 77 | +func loadAndUnmarshalResource(filePath string) (runtime.Object, error) { |
| 78 | + if _, err := os.Stat(filePath); os.IsNotExist(err) { |
| 79 | + return nil, fmt.Errorf("file does not exist: %s", filePath) |
| 80 | + } |
| 81 | + |
| 82 | + data, err := os.ReadFile(filePath) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to read file %s: %w", filePath, err) |
| 85 | + } |
| 86 | + |
| 87 | + if err = v1alpha1.AddToScheme(scheme.Scheme); err != nil { |
| 88 | + return nil, fmt.Errorf("failed to add scheme: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + decoder := serializer.NewCodecFactory(scheme.Scheme).UniversalDeserializer() |
| 92 | + |
| 93 | + json, err := yaml.YAMLToJSON(data) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("failed to convert YAML to JSON: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + obj, _, err := decoder.Decode(json, nil, nil) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("failed to decode resource: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + return obj, nil |
| 104 | +} |
| 105 | + |
| 106 | +func printResourceInfo(obj runtime.Object) { |
| 107 | + switch resource := obj.(type) { |
| 108 | + case *v1alpha1.Interface: |
| 109 | + fmt.Printf("Loaded Interface: %s\n", resource.Name) |
| 110 | + fmt.Printf(" Namespace: %s\n", resource.Namespace) |
| 111 | + fmt.Printf(" Interface Name: %s\n", resource.Spec.Name) |
| 112 | + fmt.Printf(" Admin State: %s\n", resource.Spec.AdminState) |
| 113 | + default: |
| 114 | + fmt.Printf("Loaded resource of unknown type: %T\n", resource) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func main() { |
| 119 | + flag.Usage = usage |
| 120 | + |
| 121 | + for _, arg := range os.Args[1:] { |
| 122 | + if arg == "-h" || arg == "--help" { |
| 123 | + flag.Usage() |
| 124 | + os.Exit(0) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + flag.Parse() |
| 129 | + |
| 130 | + if err := validateFlags(); err != nil { |
| 131 | + fmt.Fprintf(os.Stderr, "Error: %v\n\n", err) |
| 132 | + flag.Usage() |
| 133 | + os.Exit(1) |
| 134 | + } |
| 135 | + |
| 136 | + operation, err := validatePositionalArgs() |
| 137 | + if err != nil { |
| 138 | + fmt.Fprintf(os.Stderr, "Error: %v\n\n", err) |
| 139 | + flag.Usage() |
| 140 | + os.Exit(1) |
| 141 | + } |
| 142 | + |
| 143 | + resource, err := loadAndUnmarshalResource(*file) |
| 144 | + if err != nil { |
| 145 | + fmt.Fprintf(os.Stderr, "Error loading resource: %v\n", err) |
| 146 | + os.Exit(1) |
| 147 | + } |
| 148 | + |
| 149 | + obj, ok := resource.(client.Object) |
| 150 | + if !ok { |
| 151 | + fmt.Fprintf(os.Stderr, "Error: resource is not a client.Object\n") |
| 152 | + os.Exit(1) |
| 153 | + } |
| 154 | + if obj.GetNamespace() == "" { |
| 155 | + obj.SetNamespace(metav1.NamespaceDefault) |
| 156 | + } |
| 157 | + |
| 158 | + fmt.Printf("=== Debug Tool Configuration ===\n") |
| 159 | + fmt.Printf("Address: %s\n", *address) |
| 160 | + fmt.Printf("Username: %s\n", *username) |
| 161 | + fmt.Printf("Password: %s\n", "[REDACTED]") |
| 162 | + fmt.Printf("Resource File: %s\n", *file) |
| 163 | + fmt.Printf("Provider: %s\n", *providerName) |
| 164 | + fmt.Printf("Operation: %s\n", operation) |
| 165 | + fmt.Printf("\n=== Resource Information ===\n") |
| 166 | + printResourceInfo(resource) |
| 167 | + |
| 168 | + prov, err := provider.Get(*providerName) |
| 169 | + if err != nil { |
| 170 | + fmt.Fprintf(os.Stderr, "Error getting provider: %v\n", err) |
| 171 | + os.Exit(1) |
| 172 | + } |
| 173 | + |
| 174 | + device := &v1alpha1.Device{ |
| 175 | + ObjectMeta: metav1.ObjectMeta{ |
| 176 | + Name: "test-device", |
| 177 | + Namespace: obj.GetNamespace(), |
| 178 | + }, |
| 179 | + Spec: v1alpha1.DeviceSpec{ |
| 180 | + Endpoint: &v1alpha1.Endpoint{ |
| 181 | + Address: *address, |
| 182 | + SecretRef: &corev1.SecretReference{ |
| 183 | + Name: "test-secret", |
| 184 | + Namespace: obj.GetNamespace(), |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + secret := &corev1.Secret{ |
| 191 | + ObjectMeta: metav1.ObjectMeta{ |
| 192 | + Name: "test-secret", |
| 193 | + Namespace: obj.GetNamespace(), |
| 194 | + }, |
| 195 | + Data: map[string][]byte{ |
| 196 | + "username": []byte(*username), |
| 197 | + "password": []byte(*password), |
| 198 | + }, |
| 199 | + Type: corev1.SecretTypeBasicAuth, |
| 200 | + } |
| 201 | + |
| 202 | + obj.SetLabels(map[string]string{v1alpha1.DeviceLabel: device.Name}) |
| 203 | + |
| 204 | + c := fake.NewClientBuilder(). |
| 205 | + WithScheme(scheme.Scheme). |
| 206 | + WithObjects(device, secret). |
| 207 | + Build() |
| 208 | + |
| 209 | + ctx := clientutil.IntoContext(context.Background(), c, obj.GetNamespace()) |
| 210 | + |
| 211 | + fmt.Printf("\n=== Operation Status ===\n") |
| 212 | + switch operation { |
| 213 | + case "create": |
| 214 | + switch resource := obj.(type) { |
| 215 | + case *v1alpha1.Interface: |
| 216 | + err = prov.CreateInterface(ctx, resource) |
| 217 | + default: |
| 218 | + fmt.Printf("Loaded resource of unknown type: %T\n", resource) |
| 219 | + } |
| 220 | + case "delete": |
| 221 | + switch resource := obj.(type) { |
| 222 | + case *v1alpha1.Interface: |
| 223 | + err = prov.DeleteInterface(ctx, resource) |
| 224 | + default: |
| 225 | + fmt.Printf("Loaded resource of unknown type: %T\n", resource) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if err != nil { |
| 230 | + fmt.Fprintf(os.Stderr, "Error performing operation: %v\n", err) |
| 231 | + os.Exit(1) |
| 232 | + } |
| 233 | + |
| 234 | + fmt.Printf("Provider tool completed successfully.\n") |
| 235 | +} |
0 commit comments