Skip to content

Commit

Permalink
Fix Group{Kind -> Resource} everywhere in CRD bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sttts committed Feb 9, 2022
1 parent dfc490d commit 5887e9b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 49 deletions.
8 changes: 4 additions & 4 deletions cmd/cluster-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func main() {
crdSharedInformerFactory.WaitForCacheSync(ctx.Done())

// TODO(sttts): remove CRD creation from controller startup
requiredCrds := []metav1.GroupKind{
{Group: apiresourceapi.GroupName, Kind: "apiresourceimports"},
{Group: apiresourceapi.GroupName, Kind: "negotiatedapiresources"},
{Group: clusterapi.GroupName, Kind: "clusters"},
requiredCrds := []metav1.GroupResource{
{Group: apiresourceapi.GroupName, Resource: "apiresourceimports"},
{Group: apiresourceapi.GroupName, Resource: "negotiatedapiresources"},
{Group: clusterapi.GroupName, Resource: "clusters"},
}
for _, contextName := range []string{"admin", "user"} {
logicalClusterConfig, err := clientcmd.NewNonInteractiveClientConfig(kubeconfig, contextName, &clientcmd.ConfigOverrides{}, nil).ClientConfig()
Expand Down
40 changes: 20 additions & 20 deletions config/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ var rawCustomResourceDefinitions embed.FS

// BootstrapCustomResourceDefinitions creates the CRDs using the target client and waits
// for all of them to become established in parallel. This call is blocking.
func BootstrapCustomResourceDefinitions(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, gks []metav1.GroupKind) error {
func BootstrapCustomResourceDefinitions(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, grs []metav1.GroupResource) error {
wg := sync.WaitGroup{}
bootstrapErrChan := make(chan error, len(gks))
for _, gk := range gks {
bootstrapErrChan := make(chan error, len(grs))
for _, gk := range grs {
wg.Add(1)
go func(gk metav1.GroupKind) {
go func(gr metav1.GroupResource) {
defer wg.Done()
bootstrapErrChan <- BootstrapCustomResourceDefinition(ctx, client, gk)
bootstrapErrChan <- BootstrapCustomResourceDefinition(ctx, client, gr)
}(gk)
}
wg.Wait()
Expand All @@ -65,60 +65,60 @@ func BootstrapCustomResourceDefinitions(ctx context.Context, client apiextension

// BootstrapCustomResourceDefinition creates the CRD using the target client and waits
// for it to become established. This call is blocking.
func BootstrapCustomResourceDefinition(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, gk metav1.GroupKind) error {
return BootstrapCustomResourceDefinitionFromFS(ctx, client, gk, rawCustomResourceDefinitions)
func BootstrapCustomResourceDefinition(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, gr metav1.GroupResource) error {
return BootstrapCustomResourceDefinitionFromFS(ctx, client, gr, rawCustomResourceDefinitions)
}

// BootstrapCustomResourceDefinitionFromFS creates the CRD using the target client from the
// provided filesystem handle and waits for it to become established. This call is blocking.
func BootstrapCustomResourceDefinitionFromFS(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, gk metav1.GroupKind, fs embed.FS) error {
func BootstrapCustomResourceDefinitionFromFS(ctx context.Context, client apiextensionsv1client.CustomResourceDefinitionInterface, gr metav1.GroupResource, fs embed.FS) error {
start := time.Now()
klog.Infof("bootstrapping %v", gk.String())
klog.Infof("bootstrapping %v", gr.String())
defer func() {
klog.Infof("bootstrapped %v after %s", gk.String(), time.Since(start).String())
klog.Infof("bootstrapped %v after %s", gr.String(), time.Since(start).String())
}()
raw, err := fs.ReadFile(fmt.Sprintf("%s_%s.yaml", gk.Group, gk.Kind))
raw, err := fs.ReadFile(fmt.Sprintf("%s_%s.yaml", gr.Group, gr.Resource))
if err != nil {
return fmt.Errorf("could not read CRD %s: %w", gk.String(), err)
return fmt.Errorf("could not read CRD %s: %w", gr.String(), err)
}
expectedGvk := &schema.GroupVersionKind{Group: apiextensionsv1.GroupName, Version: "v1", Kind: "CustomResourceDefinition"}
obj, gvk, err := extensionsapiserver.Codecs.UniversalDeserializer().Decode(raw, expectedGvk, &apiextensionsv1.CustomResourceDefinition{})
if err != nil {
return fmt.Errorf("could not decode raw CRD %s: %w", gk.String(), err)
return fmt.Errorf("could not decode raw CRD %s: %w", gr.String(), err)
}
if !equality.Semantic.DeepEqual(gvk, expectedGvk) {
return fmt.Errorf("decoded CRD %s into incorrect GroupVersionKind, got %#v, wanted %#v", gk.String(), gvk, expectedGvk)
return fmt.Errorf("decoded CRD %s into incorrect GroupVersionKind, got %#v, wanted %#v", gr.String(), gvk, expectedGvk)
}
rawCrd, ok := obj.(*apiextensionsv1.CustomResourceDefinition)
if !ok {
return fmt.Errorf("decoded CRD %s into incorrect type, got %T, wanted %T", gk.String(), rawCrd, &apiextensionsv1.CustomResourceDefinition{})
return fmt.Errorf("decoded CRD %s into incorrect type, got %T, wanted %T", gr.String(), rawCrd, &apiextensionsv1.CustomResourceDefinition{})
}

crdResource, err := client.Get(ctx, rawCrd.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
_, err = client.Create(ctx, rawCrd, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("Error creating CRD %s: %w", gk.String(), err)
return fmt.Errorf("Error creating CRD %s: %w", gr.String(), err)
}
} else {
return fmt.Errorf("Error fetching CRD %s: %w", gk.String(), err)
return fmt.Errorf("Error fetching CRD %s: %w", gr.String(), err)
}
} else {
rawCrd.ResourceVersion = crdResource.ResourceVersion
_, err = client.Update(ctx, rawCrd, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("Error updating CRD %s: %w", gk.String(), err)
return fmt.Errorf("Error updating CRD %s: %w", gr.String(), err)
}
}

return wait.PollImmediateInfiniteWithContext(ctx, 100*time.Millisecond, func(ctx context.Context) (bool, error) {
crd, err := client.Get(ctx, rawCrd.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return false, fmt.Errorf("CRD %s was deleted before being established", gk.String())
return false, fmt.Errorf("CRD %s was deleted before being established", gr.String())
}
return false, fmt.Errorf("Error fetching CRD %s: %w", gk.String(), err)
return false, fmt.Errorf("Error fetching CRD %s: %w", gr.String(), err)
}

return crdhelpers.IsCRDConditionTrue(crd, apiextensionsv1.Established), nil
Expand Down
14 changes: 7 additions & 7 deletions pkg/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func (s *Server) installWorkspaceScheduler(ctx context.Context, clientConfig cli
}

// Register CRDs in both the admin and user logical clusters
requiredCrds := []metav1.GroupKind{
{Group: tenancyapi.GroupName, Kind: "workspaces"},
{Group: tenancyapi.GroupName, Kind: "workspaceshards"},
requiredCrds := []metav1.GroupResource{
{Group: tenancyapi.GroupName, Resource: "workspaces"},
{Group: tenancyapi.GroupName, Resource: "workspaceshards"},
}
crdClient := apiextensionsv1client.NewForConfigOrDie(adminConfig).CustomResourceDefinitions()
if err := config.BootstrapCustomResourceDefinitions(ctx, crdClient, requiredCrds); err != nil {
Expand Down Expand Up @@ -226,10 +226,10 @@ func (s *Server) installClusterController(clientConfig clientcmdapi.Config, serv
}

// TODO(sttts): remove CRD creation from controller startup
requiredCrds := []metav1.GroupKind{
{Group: apiresourceapi.GroupName, Kind: "apiresourceimports"},
{Group: apiresourceapi.GroupName, Kind: "negotiatedapiresources"},
{Group: clusterapi.GroupName, Kind: "clusters"},
requiredCrds := []metav1.GroupResource{
{Group: apiresourceapi.GroupName, Resource: "apiresourceimports"},
{Group: apiresourceapi.GroupName, Resource: "negotiatedapiresources"},
{Group: clusterapi.GroupName, Resource: "clusters"},
}
for _, contextName := range []string{"admin", "user"} {
logicalClusterConfig, err := clientcmd.NewNonInteractiveClientConfig(*kubeconfig, contextName, &clientcmd.ConfigOverrides{}, nil).ClientConfig()
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/api_inheritance/api_inheritance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestAPIInheritance(t *testing.T) {
}

rootCRDClient := apiExtensionsClients.Cluster(helper.OrganizationCluster).ApiextensionsV1().CustomResourceDefinitions()
rootCRDs := []metav1.GroupKind{
{Group: "apiresource.kcp.dev", Kind: "apiresourceimports"},
rootCRDs := []metav1.GroupResource{
{Group: "apiresource.kcp.dev", Resource: "apiresourceimports"},
}

if err := config.BootstrapCustomResourceDefinitions(ctx, rootCRDClient, rootCRDs); err != nil {
Expand All @@ -94,8 +94,8 @@ func TestAPIInheritance(t *testing.T) {

crdClient := apiExtensionsClients.Cluster(testCase.logicalClusterName).ApiextensionsV1().CustomResourceDefinitions()

workspaceCRDs := []metav1.GroupKind{
{Group: tenancy.GroupName, Kind: "workspaces"},
workspaceCRDs := []metav1.GroupResource{
{Group: tenancy.GroupName, Resource: "workspaces"},
}

t.Logf("Bootstrapping CRDs")
Expand Down Expand Up @@ -151,8 +151,8 @@ func TestAPIInheritance(t *testing.T) {
)

t.Logf("Install a clusters CRD into source workspace")
crdsForWorkspaces := []metav1.GroupKind{
{Group: cluster.GroupName, Kind: "clusters"},
crdsForWorkspaces := []metav1.GroupResource{
{Group: cluster.GroupName, Resource: "clusters"},
}
sourceCrdClient := apiExtensionsClients.Cluster(sourceWorkspaceClusterName).ApiextensionsV1().CustomResourceDefinitions()
if err := config.BootstrapCustomResourceDefinitions(ctx, sourceCrdClient, crdsForWorkspaces); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/conformance/cross_logical_cluster_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func TestCrossLogicalClusterList(t *testing.T) {

crdClient := apiExtensionsClients.Cluster(logicalCluster).ApiextensionsV1().CustomResourceDefinitions()

workspaceCRDs := []metav1.GroupKind{
{Group: tenancy.GroupName, Kind: "workspaces"},
workspaceCRDs := []metav1.GroupResource{
{Group: tenancy.GroupName, Resource: "workspaces"},
}

if err := config.BootstrapCustomResourceDefinitions(ctx, crdClient, workspaceCRDs); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func GetFreePort(t TestingTInterface) (string, error) {
}

// InstallCrd installs a CRD on one or multiple servers.
func InstallCrd(ctx context.Context, gvk metav1.GroupKind, servers map[string]RunningServer, embeddedResources embed.FS) error {
func InstallCrd(ctx context.Context, gr metav1.GroupResource, servers map[string]RunningServer, embeddedResources embed.FS) error {
wg := sync.WaitGroup{}
bootstrapErrChan := make(chan error, len(servers))
for _, server := range servers {
Expand All @@ -252,7 +252,7 @@ func InstallCrd(ctx context.Context, gvk metav1.GroupKind, servers map[string]Ru
bootstrapErrChan <- fmt.Errorf("failed to construct client for server: %w", err)
return
}
bootstrapErrChan <- config.BootstrapCustomResourceDefinitionFromFS(ctx, crdClient.CustomResourceDefinitions(), gvk, embeddedResources)
bootstrapErrChan <- config.BootstrapCustomResourceDefinitionFromFS(ctx, crdClient.CustomResourceDefinitions(), gr, embeddedResources)
}(server)
}
wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/reconciler/cluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestClusterController(t *testing.T) {
require.Equalf(t, len(f.Servers), 2, "incorrect number of servers")

t.Log("Installing test CRDs...")
err := framework.InstallCrd(ctx, metav1.GroupKind{Group: wildwest.GroupName, Kind: "cowboys"}, f.Servers, rawCustomResourceDefinitions)
err := framework.InstallCrd(ctx, metav1.GroupResource{Group: wildwest.GroupName, Resource: "cowboys"}, f.Servers, rawCustomResourceDefinitions)
require.NoError(t, err)

t.Logf("Installed test CRDs after %s", time.Since(start))
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/reconciler/ingress/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ func TestIngressController(t *testing.T) {
return
}
t.Log("Installing test CRDs...")
requiredCrds := []metav1.GroupKind{
{Group: "core.k8s.io", Kind: "services"},
{Group: "apps.k8s.io", Kind: "deployments"},
{Group: "networking.k8s.io", Kind: "ingresses"},
requiredCrds := []metav1.GroupResource{
{Group: "core.k8s.io", Resource: "services"},
{Group: "apps.k8s.io", Resource: "deployments"},
{Group: "networking.k8s.io", Resource: "ingresses"},
}
for _, requiredCrd := range requiredCrds {
if err := framework.InstallCrd(ctx, requiredCrd, servers, embeddedResources); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/reconciler/workspaceindex/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func resolveRunningServer(ctx context.Context, t framework.TestingTInterface, se
return runningServer{}, fmt.Errorf("failed to construct extensions client for server: %w", err)
}
extensionsClient := extensionsClients.Cluster(clusterName)
requiredCrds := []metav1.GroupKind{
{Group: tenancyapi.GroupName, Kind: "workspaces"},
{Group: tenancyapi.GroupName, Kind: "workspaceshards"},
requiredCrds := []metav1.GroupResource{
{Group: tenancyapi.GroupName, Resource: "workspaces"},
{Group: tenancyapi.GroupName, Resource: "workspaceshards"},
}
crdClient := extensionsClient.ApiextensionsV1().CustomResourceDefinitions()
if err := config.BootstrapCustomResourceDefinitions(ctx, crdClient, requiredCrds); err != nil {
Expand Down

0 comments on commit 5887e9b

Please sign in to comment.