@@ -14,7 +14,6 @@ import (
14
14
"time"
15
15
16
16
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
17
- "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
18
17
19
18
"github.com/sirupsen/logrus"
20
19
"github.com/stretchr/testify/require"
@@ -1459,7 +1458,7 @@ func TestCompetingCRDOwnersExist(t *testing.T) {
1459
1458
}
1460
1459
}
1461
1460
1462
- func TestValidateExistingCRs (t * testing.T ) {
1461
+ func TestValidateV1Beta1CRDCompatibility (t * testing.T ) {
1463
1462
unstructuredForFile := func (file string ) * unstructured.Unstructured {
1464
1463
data , err := os .ReadFile (file )
1465
1464
require .NoError (t , err )
@@ -1469,22 +1468,21 @@ func TestValidateExistingCRs(t *testing.T) {
1469
1468
return k8sFile
1470
1469
}
1471
1470
1472
- unversionedCRDForV1beta1File := func (file string ) * apiextensions .CustomResourceDefinition {
1471
+ unversionedCRDForV1beta1File := func (file string ) * apiextensionsv1beta1 .CustomResourceDefinition {
1473
1472
data , err := os .ReadFile (file )
1474
1473
require .NoError (t , err )
1475
1474
dec := utilyaml .NewYAMLOrJSONDecoder (strings .NewReader (string (data )), 30 )
1476
1475
k8sFile := & apiextensionsv1beta1.CustomResourceDefinition {}
1477
1476
require .NoError (t , dec .Decode (k8sFile ))
1478
- convertedCRD := & apiextensions.CustomResourceDefinition {}
1479
- require .NoError (t , apiextensionsv1beta1 .Convert_v1beta1_CustomResourceDefinition_To_apiextensions_CustomResourceDefinition (k8sFile , convertedCRD , nil ))
1480
- return convertedCRD
1477
+ return k8sFile
1481
1478
}
1482
1479
1483
1480
tests := []struct {
1484
1481
name string
1485
1482
existingObjects []runtime.Object
1486
1483
gvr schema.GroupVersionResource
1487
- newCRD * apiextensions.CustomResourceDefinition
1484
+ oldCRD * apiextensionsv1beta1.CustomResourceDefinition
1485
+ newCRD * apiextensionsv1beta1.CustomResourceDefinition
1488
1486
want error
1489
1487
}{
1490
1488
{
@@ -1497,6 +1495,7 @@ func TestValidateExistingCRs(t *testing.T) {
1497
1495
Version : "v1" ,
1498
1496
Resource : "machinepools" ,
1499
1497
},
1498
+ oldCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1500
1499
newCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1501
1500
},
1502
1501
{
@@ -1509,16 +1508,144 @@ func TestValidateExistingCRs(t *testing.T) {
1509
1508
Version : "v1" ,
1510
1509
Resource : "machinepools" ,
1511
1510
},
1511
+ oldCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1512
1512
newCRD : unversionedCRDForV1beta1File ("testdata/hivebug/crd.yaml" ),
1513
- want : fmt .Errorf ("error validating custom resource against new schema for MachinePool /test: [[].spec.clusterDeploymentRef: Invalid value: \" null\" : spec.clusterDeploymentRef in body must be of type object: \" null\" , [].spec.name: Required value, [].spec.platform: Required value]" ),
1513
+ want : fmt .Errorf ("error validating hive.openshift.io/v1, Kind=MachinePool \" test\" : updated validation is too restrictive: [[].spec.clusterDeploymentRef: Invalid value: \" null\" : spec.clusterDeploymentRef in body must be of type object: \" null\" , [].spec.name: Required value, [].spec.platform: Required value]" ),
1514
+ },
1515
+ {
1516
+ name : "backwards incompatible change" ,
1517
+ existingObjects : []runtime.Object {
1518
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.yaml" ),
1519
+ },
1520
+ gvr : schema.GroupVersionResource {
1521
+ Group : "cluster.com" ,
1522
+ Version : "v1alpha1" ,
1523
+ Resource : "testcrd" ,
1524
+ },
1525
+ oldCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.old.yaml" ),
1526
+ newCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.yaml" ),
1527
+ want : fmt .Errorf ("error validating cluster.com/v1alpha1, Kind=testcrd \" my-cr-1\" : updated validation is too restrictive: [].spec.scalar: Invalid value: 2: spec.scalar in body should be greater than or equal to 3" ),
1528
+ },
1529
+ {
1530
+ name : "unserved version" ,
1531
+ existingObjects : []runtime.Object {
1532
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.yaml" ),
1533
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.v2.yaml" ),
1534
+ },
1535
+ gvr : schema.GroupVersionResource {
1536
+ Group : "cluster.com" ,
1537
+ Version : "v1alpha1" ,
1538
+ Resource : "testcrd" ,
1539
+ },
1540
+ oldCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.old.yaml" ),
1541
+ newCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.unserved.yaml" ),
1542
+ },
1543
+ {
1544
+ name : "cr not validated against currently unserved version" ,
1545
+ existingObjects : []runtime.Object {
1546
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.yaml" ),
1547
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.v2.yaml" ),
1548
+ },
1549
+ oldCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.unserved.yaml" ),
1550
+ newCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.yaml" ),
1551
+ },
1552
+ {
1553
+ name : "crd with no versions list" ,
1554
+ existingObjects : []runtime.Object {
1555
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.yaml" ),
1556
+ unstructuredForFile ("testdata/apiextensionsv1beta1/cr.v2.yaml" ),
1557
+ },
1558
+ oldCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.no-versions-list.old.yaml" ),
1559
+ newCRD : unversionedCRDForV1beta1File ("testdata/apiextensionsv1beta1/crd.no-versions-list.yaml" ),
1560
+ want : fmt .Errorf ("error validating cluster.com/v1alpha1, Kind=testcrd \" my-cr-1\" : updated validation is too restrictive: [].spec.scalar: Invalid value: 2: spec.scalar in body should be greater than or equal to 3" ),
1561
+ },
1562
+ }
1563
+ for _ , tt := range tests {
1564
+ t .Run (tt .name , func (t * testing.T ) {
1565
+ client := fakedynamic .NewSimpleDynamicClient (runtime .NewScheme (), tt .existingObjects ... )
1566
+ require .Equal (t , tt .want , validateV1Beta1CRDCompatibility (client , tt .oldCRD , tt .newCRD ))
1567
+ })
1568
+ }
1569
+ }
1570
+
1571
+ func TestValidateV1CRDCompatibility (t * testing.T ) {
1572
+ unstructuredForFile := func (file string ) * unstructured.Unstructured {
1573
+ data , err := os .ReadFile (file )
1574
+ require .NoError (t , err )
1575
+ dec := utilyaml .NewYAMLOrJSONDecoder (strings .NewReader (string (data )), 30 )
1576
+ k8sFile := & unstructured.Unstructured {}
1577
+ require .NoError (t , dec .Decode (k8sFile ))
1578
+ return k8sFile
1579
+ }
1580
+
1581
+ unversionedCRDForV1File := func (file string ) * apiextensionsv1.CustomResourceDefinition {
1582
+ data , err := os .ReadFile (file )
1583
+ require .NoError (t , err )
1584
+ dec := utilyaml .NewYAMLOrJSONDecoder (strings .NewReader (string (data )), 30 )
1585
+ k8sFile := & apiextensionsv1.CustomResourceDefinition {}
1586
+ require .NoError (t , dec .Decode (k8sFile ))
1587
+ return k8sFile
1588
+ }
1589
+
1590
+ tests := []struct {
1591
+ name string
1592
+ existingCRs []runtime.Object
1593
+ gvr schema.GroupVersionResource
1594
+ oldCRD * apiextensionsv1.CustomResourceDefinition
1595
+ newCRD * apiextensionsv1.CustomResourceDefinition
1596
+ want error
1597
+ }{
1598
+ {
1599
+ name : "valid" ,
1600
+ existingCRs : []runtime.Object {
1601
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v1.yaml" ),
1602
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v2.yaml" ),
1603
+ },
1604
+ oldCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.old.yaml" ),
1605
+ newCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.yaml" ),
1606
+ },
1607
+ {
1608
+ name : "validation failure" ,
1609
+ existingCRs : []runtime.Object {
1610
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v1.yaml" ),
1611
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.fail.v2.yaml" ),
1612
+ },
1613
+ oldCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.old.yaml" ),
1614
+ newCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.yaml" ),
1615
+ want : fmt .Errorf ("error validating stable.example.com/v2, Kind=CronTab \" my-crontab\" : updated validation is too restrictive: [].spec.replicas: Invalid value: 10: spec.replicas in body should be less than or equal to 9" ),
1616
+ },
1617
+ {
1618
+ name : "cr not invalidated by unserved version" ,
1619
+ existingCRs : []runtime.Object {
1620
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v1.yaml" ),
1621
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v2.yaml" ),
1622
+ },
1623
+ oldCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.old.yaml" ),
1624
+ newCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.unserved.yaml" ),
1625
+ },
1626
+ {
1627
+ name : "cr not validated against currently unserved version" ,
1628
+ existingCRs : []runtime.Object {
1629
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v1.yaml" ),
1630
+ unstructuredForFile ("testdata/apiextensionsv1/crontabs.cr.valid.v2.yaml" ),
1631
+ },
1632
+ oldCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.old.unserved.yaml" ),
1633
+ newCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/crontabs.crd.yaml" ),
1634
+ },
1635
+ {
1636
+ name : "validation failure with single CRD version" ,
1637
+ existingCRs : []runtime.Object {
1638
+ unstructuredForFile ("testdata/apiextensionsv1/single-version-cr.yaml" ),
1639
+ },
1640
+ oldCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/single-version-crd.old.yaml" ),
1641
+ newCRD : unversionedCRDForV1File ("testdata/apiextensionsv1/single-version-crd.yaml" ),
1642
+ want : fmt .Errorf ("error validating cluster.com/v1alpha1, Kind=testcrd \" my-cr-1\" : updated validation is too restrictive: [].spec.scalar: Invalid value: 100: spec.scalar in body should be less than or equal to 50" ),
1514
1643
},
1515
1644
}
1516
1645
for _ , tt := range tests {
1517
1646
t .Run (tt .name , func (t * testing.T ) {
1518
- client := fakedynamic .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
1519
- tt .gvr : "UnstructuredList" ,
1520
- }, tt .existingObjects ... )
1521
- require .Equal (t , tt .want , validateExistingCRs (client , tt .gvr , tt .newCRD ))
1647
+ client := fakedynamic .NewSimpleDynamicClient (runtime .NewScheme (), tt .existingCRs ... )
1648
+ require .Equal (t , tt .want , validateV1CRDCompatibility (client , tt .oldCRD , tt .newCRD ))
1522
1649
})
1523
1650
}
1524
1651
}
0 commit comments