Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check required crds #686

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion cmd/aws-application-networking-k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package main

import (
"flag"
"fmt"
"os"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"strings"

"github.com/aws/aws-application-networking-k8s/pkg/webhook"
"github.com/go-logr/zapr"
"go.uber.org/zap/zapcore"
k8swebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/aws/aws-application-networking-k8s/pkg/aws"
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
Expand All @@ -45,6 +46,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/external-dns/endpoint"
gwv1 "sigs.k8s.io/gateway-api/apis/v1"

//+kubebuilder:scaffold:imports
anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
"github.com/aws/aws-application-networking-k8s/pkg/config"
Expand Down Expand Up @@ -92,6 +94,51 @@ func addOptionalCRDs(scheme *runtime.Scheme) {
metav1.AddToGroupVersion(scheme, groupVersion)
}

func checkRequiredCRDs(mgr ctrl.Manager) error {
// Add or update the required CRDs when new CRDs are added to the project
requiredCRDs := []schema.GroupVersionKind{
{
Group: gwv1.GroupVersion.Group,
Version: gwv1.GroupVersion.Version,
Kind: "Gateway",
},
{
Group: gwv1.GroupVersion.Group,
Version: gwv1.GroupVersion.Version,
Kind: "GatewayClass",
},
{
Group: gwv1.GroupVersion.Group,
Version: gwv1.GroupVersion.Version,
Kind: "HTTPRoute",
},
{
Group: gwv1.GroupVersion.Group,
Version: gwv1.GroupVersion.Version,
Kind: "GRPCRoute",
},
{
Group: gwv1alpha2.GroupVersion.Group,
Version: gwv1alpha2.GroupVersion.Version,
Kind: "TLSRoute",
},
}
missingCRDs := make([]string, 0, len(requiredCRDs))
for _, crd := range requiredCRDs {
ok, err := k8s.IsGVKSupported(mgr, crd.GroupVersion().String(), crd.Kind)
if err != nil {
return fmt.Errorf("error checking required CRD %s: %w", crd, err)
}
if !ok {
missingCRDs = append(missingCRDs, crd.String())
}
}
if len(missingCRDs) > 0 {
return fmt.Errorf("missing required CRDs: %s", strings.Join(missingCRDs, ", "))
}
return nil
}

func main() {
var metricsAddr string
var enableLeaderElection bool
Expand Down Expand Up @@ -164,6 +211,10 @@ func main() {
setupLog.Fatal("manager setup failed:", err)
}

if err := checkRequiredCRDs(mgr); err != nil {
setupLog.Fatal("required CRDs check failed:", err)
}

if enableWebhook {
logger := log.Named("pod-readiness-gate-injector")
readinessGateInjector := webhook.NewPodReadinessGateInjector(
Expand Down