Skip to content

Commit

Permalink
chore: add usage of kustomize for KIC's webhook config generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Jun 13, 2024
1 parent 93e4154 commit f610301
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
43 changes: 43 additions & 0 deletions hack/generators/kic/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package kic

import (
"context"
"fmt"
"io"
"log"
"os/exec"
)

// BuildKustomizeForURLAndRef runs kustomize build for the provided URL and ref.
// It returns the output of the kustomize build command.
func BuildKustomizeForURLAndRef(ctx context.Context, url, ref string) ([]byte, error) {
kustomizeResourceURL := fmt.Sprintf("%s?ref=%s", url, ref)

log.Printf("Running 'kustomize build %s'\n", kustomizeResourceURL)
cmd := exec.CommandContext(ctx, "kustomize", "build", kustomizeResourceURL)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}

if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start kustomize command %v: %w", cmd, err)
}
b, err := io.ReadAll(stdout)
if err != nil {
return nil, fmt.Errorf("failed to read kustomize stdout: %w", err)
}
berr, err := io.ReadAll(stderr)
if err != nil {
return nil, fmt.Errorf("failed to read kustomize stderr: %w", err)
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("failed to wait for kustomize to finish, output %s: %w", string(berr), err)
}

return b, nil
}
30 changes: 24 additions & 6 deletions hack/generators/kic/webhook-config-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bytes"
"context"
"fmt"
"log"
"os"
"text/template"

"github.com/kong/semver/v4"
"github.com/samber/lo"
admregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/util/yaml"
Expand All @@ -18,25 +20,41 @@ import (

const (
validatingWebhookConfigurationPath = "config/webhook/manifests.yaml"
validatingWebhookConfigurationKustomizeURL = "https://github.com/kong/kubernetes-ingress-controller/config/webhook"
validatingWebhookConfigurationGeneratorForVersionOutputPath = "pkg/utils/kubernetes/resources/validatingwebhookconfig/zz_generated_kic_%s.go"
validatingWebhookConfigurationGeneratorMasterOutputPath = "pkg/utils/kubernetes/resources/zz_generated_kic_validatingwebhookconfig.go"
)

func main() {
generateHelpersForAllConfiguredVersions()
generateHelpersForAllConfiguredVersions(context.Background())
generateMasterHelper()
}

// generateHelpersForAllConfiguredVersions iterates over kicversions.ManifestsVersionsForKICVersions map and generates
// GenerateValidatingWebhookConfigurationForKIC_{versionConstraint} function for each configured version.
func generateHelpersForAllConfiguredVersions() {
func generateHelpersForAllConfiguredVersions(ctx context.Context) {
for versionConstraint, version := range kicversions.ManifestsVersionsForKICVersions {
log.Printf("Generating ValidatingWebhook Configuration for KIC versions %s (using manifests: %s)\n", versionConstraint, version)

// Download KIC-generated ValidatingWebhookConfiguration.
manifestContent, err := kic.GetFileFromKICRepositoryForVersion(validatingWebhookConfigurationPath, version)
if err != nil {
log.Fatalf("Failed to download %s from KIC repository: %s", validatingWebhookConfigurationPath, err)
var (
manifestContent []byte
err error
)
// Before KIC 3.2 config/webhook directory contained only the generated manifes YAML.
// 3.2 and later versions contain a kustomization.yaml file that use the patches from config/webhook
// directory to generate the ValidatingWebhookConfiguration.
if version.LT(semver.MustParse("3.2.0")) {
// Download KIC-generated ValidatingWebhookConfiguration.
manifestContent, err = kic.GetFileFromKICRepositoryForVersion(validatingWebhookConfigurationPath, version)
if err != nil {
log.Fatalf("Failed to download %s from KIC repository: %s", validatingWebhookConfigurationPath, err)
}
} else {
// Generate ValidatingWebhookConfiguration using KIC's webhook kustomize dir.
manifestContent, err = kic.BuildKustomizeForURLAndRef(ctx, validatingWebhookConfigurationKustomizeURL, "v"+version.String())
if err != nil {
log.Fatalf("Failed to generate KIC's ValidatingWebhookConfiguration based on %s: %s", validatingWebhookConfigurationKustomizeURL, err)
}
}

// Get rid of the YAML objects separator as we know there's only one ValidatingWebhookConfiguration in the file.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f610301

Please sign in to comment.