...

Source file src/k8s.io/apiextensions-apiserver/test/integration/apiapproval_test.go

Documentation: k8s.io/apiextensions-apiserver/test/integration

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package integration
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	"k8s.io/apiextensions-apiserver/test/integration/fixtures"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/util/wait"
    28  )
    29  
    30  func TestAPIApproval(t *testing.T) {
    31  	tearDown, apiExtensionClient, dynamicClient, err := fixtures.StartDefaultServerWithClients(t)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	defer tearDown()
    36  
    37  	noxuDefinition := fixtures.NewNoxuV1CustomResourceDefinition(apiextensionsv1.NamespaceScoped)
    38  	noxuDefinition, err = fixtures.CreateNewV1CustomResourceDefinition(noxuDefinition, apiExtensionClient, dynamicClient)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if noxuAPIApproved := findCRDCondition(noxuDefinition, apiextensionsv1.KubernetesAPIApprovalPolicyConformant); noxuAPIApproved != nil {
    43  		t.Fatal(noxuAPIApproved)
    44  	}
    45  
    46  	newSigKubeAPIFn := func(resource, approvalAnnotation string) *apiextensionsv1.CustomResourceDefinition {
    47  		return &apiextensionsv1.CustomResourceDefinition{
    48  			ObjectMeta: metav1.ObjectMeta{Name: resource + ".sigs.k8s.io", Annotations: map[string]string{apiextensionsv1.KubeAPIApprovedAnnotation: approvalAnnotation}},
    49  			Spec: apiextensionsv1.CustomResourceDefinitionSpec{
    50  				Group: "sigs.k8s.io",
    51  				Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
    52  					{
    53  						Name:    "v1beta1",
    54  						Served:  true,
    55  						Storage: true,
    56  						Schema:  fixtures.AllowAllSchema(),
    57  					},
    58  				},
    59  				Names: apiextensionsv1.CustomResourceDefinitionNames{
    60  					Plural:   resource,
    61  					Singular: resource + "singular",
    62  					Kind:     resource + "Kind",
    63  					ListKind: resource + "List",
    64  				},
    65  				Scope: apiextensionsv1.NamespaceScoped,
    66  			},
    67  		}
    68  	}
    69  	// the unit tests cover all variations. We just need to be sure that we see the code being called
    70  	approvedKubeAPI := newSigKubeAPIFn("approved", "https://github.com/kubernetes/kubernetes/pull/79724")
    71  	approvedKubeAPI, err = fixtures.CreateNewV1CustomResourceDefinition(approvedKubeAPI, apiExtensionClient, dynamicClient)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	err = wait.PollImmediate(100*time.Millisecond, 30*time.Second, func() (bool, error) {
    76  		approvedKubeAPI, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), approvedKubeAPI.Name, metav1.GetOptions{})
    77  		if err != nil {
    78  			return false, err
    79  		}
    80  		if approvedKubeAPIApproved := findCRDCondition(approvedKubeAPI, apiextensionsv1.KubernetesAPIApprovalPolicyConformant); approvedKubeAPIApproved == nil || approvedKubeAPIApproved.Status != apiextensionsv1.ConditionTrue {
    81  			t.Log(approvedKubeAPIApproved)
    82  			return false, nil
    83  		}
    84  		return true, nil
    85  	})
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  }
    91  

View as plain text