|
| 1 | +package samples |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/operator-framework/operator-controller/hack/generate-testdata/internal/utils" |
| 6 | + "path/filepath" |
| 7 | + pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util" |
| 8 | +) |
| 9 | + |
| 10 | +// BuildSampleV2 generate sample v2.0.0 with breaking changes |
| 11 | +// --------------------------------------------- |
| 12 | +// This version introduces a new API version with webhook conversion. |
| 13 | +// on top of v1.0.0 version. |
| 14 | +func BuildSampleV2(samplesPath string) { |
| 15 | + BuildSampleV1(samplesPath) |
| 16 | + |
| 17 | + // Create v2 API version without controller |
| 18 | + utils.RunKubebuilderCommand(samplesPath, |
| 19 | + "create", "api", |
| 20 | + "--group", "example", |
| 21 | + "--version", "v2", |
| 22 | + "--kind", "Busybox", |
| 23 | + "--resource", |
| 24 | + "--controller=false", |
| 25 | + ) |
| 26 | + |
| 27 | + // Create conversion webhook for Busybox v1 -> v2 |
| 28 | + // Create webhook with defaulting and validation |
| 29 | + utils.RunKubebuilderCommand(samplesPath, |
| 30 | + "create", "webhook", |
| 31 | + "--group", "example", |
| 32 | + "--version", "v1", |
| 33 | + "--kind", "Busybox", |
| 34 | + "--conversion", |
| 35 | + "--programmatic-validation", |
| 36 | + "--defaulting", |
| 37 | + "--spoke", "v2", |
| 38 | + ) |
| 39 | + |
| 40 | + implementV2Type(samplesPath) |
| 41 | + implementV2WebhookConversion(samplesPath) |
| 42 | + implementValidationDefaultWebhookV1(samplesPath) |
| 43 | + |
| 44 | + utils.RunMake(samplesPath, "generate", "manifests", "fmt", "vet") |
| 45 | +} |
| 46 | + |
| 47 | +func implementV2Type(path string) { |
| 48 | + fmt.Println("Adding `Replicas` field to Busybox v2 spec") |
| 49 | + v2TypesPath := filepath.Join(path, "api", "v2", "busybox_types.go") |
| 50 | + if err := pluginutil.ReplaceInFile( |
| 51 | + v2TypesPath, |
| 52 | + "Foo *string `json:\"foo,omitempty\"`", |
| 53 | + "Replicas *int32 `json:\"replicas,omitempty\"` // Number of replicas", |
| 54 | + ); err != nil { |
| 55 | + panic(fmt.Sprintf("failed to insert replicas field in v2 BusyboxSpec: %v", err)) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func implementV2WebhookConversion(path string) { |
| 60 | + fmt.Println("Implementing conversion logic for v2 <-> v1") |
| 61 | + conversionPath := filepath.Join(path, "api", "v2", "busybox_conversion.go") |
| 62 | + if err := pluginutil.UncommentCode( |
| 63 | + conversionPath, |
| 64 | + "// dst.Spec.Size = src.Spec.Replicas", |
| 65 | + "//", |
| 66 | + ); err != nil { |
| 67 | + panic(fmt.Sprintf("failed to implement v2->v1 conversion: %v", err)) |
| 68 | + } |
| 69 | + |
| 70 | + if err := pluginutil.UncommentCode( |
| 71 | + conversionPath, |
| 72 | + "// dst.Spec.Replicas = src.Spec.Size", |
| 73 | + "//", |
| 74 | + ); err != nil { |
| 75 | + panic(fmt.Sprintf("failed to implement v1->v2 conversion: %v", err)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// implementValidationDefaultWebhookV1 injects validation and defaulting logic into the Busybox v1 webhook. |
| 80 | +func implementValidationDefaultWebhookV1(samplesPath string) { |
| 81 | + webhookPath := filepath.Join(samplesPath, "internal", "webhook", "v1", "busybox_webhook.go") |
| 82 | + |
| 83 | + fmt.Println("Injecting validation logic into Busybox v1 webhook") |
| 84 | + // ValidateCreate logic |
| 85 | + if err := pluginutil.ReplaceInFile( |
| 86 | + webhookPath, |
| 87 | + "// TODO(user): fill in your validation logic upon object creation.", |
| 88 | + `if busybox.Spec.Size != nil && *busybox.Spec.Size < 0 { |
| 89 | + return nil, fmt.Errorf("spec.size must be >= 0") |
| 90 | + }`, |
| 91 | + ); err != nil { |
| 92 | + panic(fmt.Sprintf("failed to apply ValidateCreate logic: %v", err)) |
| 93 | + } |
| 94 | + |
| 95 | + // ValidateUpdate logic |
| 96 | + if err := pluginutil.ReplaceInFile( |
| 97 | + webhookPath, |
| 98 | + "// TODO(user): fill in your validation logic upon object update.", |
| 99 | + `if busybox.Spec.Size != nil && *busybox.Spec.Size < 0 { |
| 100 | + return nil, fmt.Errorf("spec.size must be >= 0") |
| 101 | + }`, |
| 102 | + ); err != nil { |
| 103 | + panic(fmt.Sprintf("failed to apply ValidateUpdate logic: %v", err)) |
| 104 | + } |
| 105 | +} |
0 commit comments