Skip to content

Commit

Permalink
k8s: Only register CRDs for enabled resources
Browse files Browse the repository at this point in the history
Some resources may be dynamically disabled by configuration options in
the agent / operator. It doesn't make sense to register these resources
into Kubernetes if they are disabled in the agent, so don't register
them in that case.

Signed-off-by: Joe Stringer <joe@cilium.io>
  • Loading branch information
joestringer committed Oct 19, 2021
1 parent e1ff289 commit 6c6ad94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
53 changes: 22 additions & 31 deletions pkg/k8s/apis/cilium.io/client/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cilium/cilium/pkg/k8s"
k8sconstv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
k8sconstv2alpha1 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
"github.com/cilium/cilium/pkg/k8s/synced"
k8sversion "github.com/cilium/cilium/pkg/k8s/version"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -65,42 +66,32 @@ var (
comparableCRDSchemaVersion = versioncheck.MustVersion(k8sconstv2.CustomResourceDefinitionSchemaVersion)
)

type crdCreationFn func(clientset apiextensionsclient.Interface) error

// CreateCustomResourceDefinitions creates our CRD objects in the Kubernetes
// cluster.
func CreateCustomResourceDefinitions(clientset apiextensionsclient.Interface) error {
g, _ := errgroup.WithContext(context.Background())

g.Go(func() error {
return createCNPCRD(clientset)
})

g.Go(func() error {
return createCCNPCRD(clientset)
})

g.Go(func() error {
return createCEPCRD(clientset)
})

g.Go(func() error {
return createNodeCRD(clientset)
})

g.Go(func() error {
return createCEWCRD(clientset)
})

g.Go(func() error {
return createIdentityCRD(clientset)
})

g.Go(func() error {
return createCLRPCRD(clientset)
})

g.Go(func() error {
return createCENPCRD(clientset)
})
resourceToCreateFnMapping := map[string]crdCreationFn{
synced.CRDResourceName(k8sconstv2.CNPName): createCNPCRD,
synced.CRDResourceName(k8sconstv2.CCNPName): createCCNPCRD,
synced.CRDResourceName(k8sconstv2.CNName): createNodeCRD,
synced.CRDResourceName(k8sconstv2.CIDName): createIdentityCRD,
synced.CRDResourceName(k8sconstv2.CEPName): createCEPCRD,
synced.CRDResourceName(k8sconstv2.CEWName): createCEWCRD,
synced.CRDResourceName(k8sconstv2.CLRPName): createCLRPCRD,
synced.CRDResourceName(k8sconstv2alpha1.CENPName): createCENPCRD,
}
for _, r := range synced.OperatorCRDResourceNames() {
fn, ok := resourceToCreateFnMapping[r]
if !ok {
log.Fatalf("Unknown resource %s. Please update pkg/k8s/apis/cilium.io/client to understand this type.", r)
}
g.Go(func() error {
return fn(clientset)
})
}

return g.Wait()
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/k8s/synced/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func AgentCRDResourceNames() []string {
return agentCRDResourceNames(false)
}

// OperatorCRDResourceNames returns a list of all CRD resource names that the
// operator may register.
func OperatorCRDResourceNames() []string {
return append(agentCRDResourceNames(false), CRDResourceName(v2.CEWName))
}

// AllCRDResourceNames returns a list of all CRD resource names that the
// clustermesh-apiserver or testsuite may register.
func AllCRDResourceNames() []string {
Expand Down

0 comments on commit 6c6ad94

Please sign in to comment.