...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s/crds.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/k8s

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8s
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/labels"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  // ListCRDs returns the list of KCC CRDs on the API Server. The function returns
    28  // the CRDs in paginated fashion, i.e. one chunk of at most 100 CRDs at a time,
    29  // and the nextPageToken to be used by the caller to fetch the next chunk of CRDs.
    30  // When there are no more CRDs to list, the function returns an empty string nextPageToken.
    31  // Callers are expected to pass an empty string for pageToken when initiating a
    32  // new list operation.
    33  func ListCRDs(ctx context.Context, kubeClient client.Client, pageToken string) (crds []v1.CustomResourceDefinition, nextPageToken string, err error) {
    34  	list := v1.CustomResourceDefinitionList{}
    35  	labelSelector, err := labels.Parse(KCCSystemLabelSelectorRaw)
    36  	if err != nil {
    37  		return nil, "", fmt.Errorf("error parsing '%v' as a label selector: %v", KCCSystemLabelSelectorRaw, err)
    38  	}
    39  	opts := &client.ListOptions{
    40  		Limit:         100,
    41  		Raw:           &metav1.ListOptions{},
    42  		LabelSelector: labelSelector,
    43  		Continue:      pageToken,
    44  	}
    45  	if err := kubeClient.List(ctx, &list, opts); err != nil {
    46  		return nil, "", fmt.Errorf("error listing CRDs: %w", err)
    47  	}
    48  
    49  	for _, crd := range list.Items {
    50  		if _, ok := IgnoredCRDList[crd.Name]; ok {
    51  			continue
    52  		}
    53  		crds = append(crds, crd)
    54  	}
    55  	return crds, list.GetContinue(), nil
    56  }
    57  

View as plain text