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

Ensure CRDs are created on boot #3610

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,15 @@ func main() {
}
setupLog.WithValues("provider", provider).Info("Checking type of cluster")

// If configured to managed CRDs, do a preliminary install of them here. The Installation controller
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If configured to managed CRDs, do a preliminary install of them here. The Installation controller
// If configured to manage CRDs, do a preliminary install of them here. The Installation controller

// will reconcile them as well, but we need to make sure they are installed before we start the rest of the controllers.
if manageCRDs {
if err := crds.Ensure(mgr.GetClient()); err != nil {
setupLog.Error(err, "Failed to ensure CRDs are created")
os.Exit(1)
}
}

// Determine if we're running in single or multi-tenant mode.
multiTenant, err := utils.MultiTenant(ctx, clientset)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/crds/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
package crds

import (
"context"
"embed"
"fmt"
"path"
"regexp"
"strings"
"sync"
"time"

apiextenv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml" // gopkg.in/yaml.v2 didn't parse all the fields but this package did

Expand Down Expand Up @@ -184,3 +187,21 @@ func ToRuntimeObjects(crds ...*apiextenv1.CustomResourceDefinition) []client.Obj
}
return objs
}

// Ensure ensures that the CRDs necessary for bootstrapping exist in the cluster.
// Further reconciliation of the CRDs is handled by the core controller.
func Ensure(c client.Client) error {
// Ensure Calico CRDs exist, which will allow us to bootstrap.
for _, crd := range GetCRDs(opv1.Calico) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
if err := c.Create(ctx, crd); err != nil {
// Ignore if the CRD already exists
if !errors.IsAlreadyExists(err) {
cancel()
return fmt.Errorf("failed to create CustomResourceDefinition %s: %s", crd.Name, err)
}
}
cancel()
}
return nil
}
Loading