@@ -43,6 +43,8 @@ import (
43
43
"k8s.io/apimachinery/pkg/runtime"
44
44
"k8s.io/apimachinery/pkg/runtime/schema"
45
45
"k8s.io/apimachinery/pkg/types"
46
+ appsv1applyconfigurations "k8s.io/client-go/applyconfigurations/apps/v1"
47
+ autoscaling1applyconfigurations "k8s.io/client-go/applyconfigurations/autoscaling/v1"
46
48
corev1applyconfigurations "k8s.io/client-go/applyconfigurations/core/v1"
47
49
kscheme "k8s.io/client-go/kubernetes/scheme"
48
50
"k8s.io/client-go/rest"
@@ -1127,6 +1129,34 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
1127
1129
Expect (err ).NotTo (HaveOccurred ())
1128
1130
Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
1129
1131
})
1132
+
1133
+ It ("should be able to apply the scale subresource" , func (ctx SpecContext ) {
1134
+ cl , err := client .New (cfg , client.Options {})
1135
+ Expect (err ).NotTo (HaveOccurred ())
1136
+ Expect (cl ).NotTo (BeNil ())
1137
+
1138
+ By ("Creating a deployment" )
1139
+ dep , err := clientset .AppsV1 ().Deployments (dep .Namespace ).Create (ctx , dep , metav1.CreateOptions {})
1140
+ Expect (err ).NotTo (HaveOccurred ())
1141
+ replicaCount := * dep .Spec .Replicas + 1
1142
+
1143
+ By ("Applying the scale subresurce" )
1144
+ deploymentAC , err := appsv1applyconfigurations .ExtractDeployment (dep , "foo" )
1145
+ Expect (err ).NotTo (HaveOccurred ())
1146
+ scale := autoscaling1applyconfigurations .Scale ().
1147
+ WithSpec (autoscaling1applyconfigurations .ScaleSpec ().WithReplicas (replicaCount ))
1148
+ err = cl .SubResource ("scale" ).Apply (ctx , deploymentAC ,
1149
+ & client.SubResourceApplyOptions {SubResourceBody : scale },
1150
+ client .FieldOwner ("foo" ),
1151
+ client .ForceOwnership ,
1152
+ )
1153
+ Expect (err ).NotTo (HaveOccurred ())
1154
+
1155
+ By ("Asserting replicas got updated" )
1156
+ dep , err = clientset .AppsV1 ().Deployments (dep .Namespace ).Get (ctx , dep .Name , metav1.GetOptions {})
1157
+ Expect (err ).NotTo (HaveOccurred ())
1158
+ Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
1159
+ })
1130
1160
})
1131
1161
1132
1162
Context ("with unstructured objects" , func () {
@@ -1322,8 +1352,8 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
1322
1352
By ("Creating a deployment" )
1323
1353
dep , err := clientset .AppsV1 ().Deployments (dep .Namespace ).Create (ctx , dep , metav1.CreateOptions {})
1324
1354
Expect (err ).NotTo (HaveOccurred ())
1325
- dep .APIVersion = "apps/v1"
1326
- dep .Kind = "Deployment"
1355
+ dep .APIVersion = appsv1 . SchemeGroupVersion . String ()
1356
+ dep .Kind = "Deployment" //nolint:goconst
1327
1357
depUnstructured , err := toUnstructured (dep )
1328
1358
Expect (err ).NotTo (HaveOccurred ())
1329
1359
@@ -1374,6 +1404,41 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
1374
1404
Expect (err ).NotTo (HaveOccurred ())
1375
1405
Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
1376
1406
})
1407
+
1408
+ It ("should be able to apply the scale subresource" , func (ctx SpecContext ) {
1409
+ cl , err := client .New (cfg , client.Options {Scheme : runtime .NewScheme ()})
1410
+ Expect (err ).NotTo (HaveOccurred ())
1411
+ Expect (cl ).NotTo (BeNil ())
1412
+
1413
+ By ("Creating a deployment" )
1414
+ dep , err := clientset .AppsV1 ().Deployments (dep .Namespace ).Create (ctx , dep , metav1.CreateOptions {})
1415
+ Expect (err ).NotTo (HaveOccurred ())
1416
+ dep .APIVersion = "apps/v1"
1417
+ dep .Kind = "Deployment"
1418
+ depUnstructured , err := toUnstructured (dep )
1419
+ Expect (err ).NotTo (HaveOccurred ())
1420
+
1421
+ By ("Updating the scale subresurce" )
1422
+ replicaCount := * dep .Spec .Replicas + 1
1423
+ scale := & unstructured.Unstructured {}
1424
+ scale .SetAPIVersion ("autoscaling/v1" )
1425
+ scale .SetKind ("Scale" )
1426
+ Expect (unstructured .SetNestedField (scale .Object , int64 (replicaCount ), "spec" , "replicas" )).NotTo (HaveOccurred ())
1427
+ err = cl .SubResource ("scale" ).Apply (ctx ,
1428
+ client .ApplyConfigurationFromUnstructured (depUnstructured ),
1429
+ & client.SubResourceApplyOptions {SubResourceBody : client .ApplyConfigurationFromUnstructured (scale )},
1430
+ client .FieldOwner ("foo" ),
1431
+ client .ForceOwnership ,
1432
+ )
1433
+ Expect (err ).NotTo (HaveOccurred ())
1434
+ Expect (scale .GetAPIVersion ()).To (Equal ("autoscaling/v1" ))
1435
+ Expect (scale .GetKind ()).To (Equal ("Scale" ))
1436
+
1437
+ By ("Asserting replicas got updated" )
1438
+ dep , err = clientset .AppsV1 ().Deployments (dep .Namespace ).Get (ctx , dep .Name , metav1.GetOptions {})
1439
+ Expect (err ).NotTo (HaveOccurred ())
1440
+ Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
1441
+ })
1377
1442
})
1378
1443
1379
1444
})
@@ -1440,6 +1505,29 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
1440
1505
Expect (dep .GroupVersionKind ()).To (Equal (depGvk ))
1441
1506
})
1442
1507
1508
+ It ("should apply status" , func (ctx SpecContext ) {
1509
+ cl , err := client .New (cfg , client.Options {})
1510
+ Expect (err ).NotTo (HaveOccurred ())
1511
+ Expect (cl ).NotTo (BeNil ())
1512
+
1513
+ By ("initially creating a Deployment" )
1514
+ dep , err := clientset .AppsV1 ().Deployments (ns ).Create (ctx , dep , metav1.CreateOptions {})
1515
+ Expect (err ).NotTo (HaveOccurred ())
1516
+ Expect (dep .Status .Replicas ).To (BeEquivalentTo (0 ))
1517
+
1518
+ By ("applying the status of Deployment" )
1519
+ deploymentAC , err := appsv1applyconfigurations .ExtractDeployment (dep , "foo" )
1520
+ Expect (err ).NotTo (HaveOccurred ())
1521
+ deploymentAC .WithStatus (& appsv1applyconfigurations.DeploymentStatusApplyConfiguration {
1522
+ Replicas : ptr .To (int32 (1 )),
1523
+ })
1524
+ Expect (cl .Status ().Apply (ctx , deploymentAC , client .FieldOwner ("foo" ))).To (Succeed ())
1525
+
1526
+ dep , err = clientset .AppsV1 ().Deployments (ns ).Get (ctx , dep .Name , metav1.GetOptions {})
1527
+ Expect (err ).NotTo (HaveOccurred ())
1528
+ Expect (dep .Status .Replicas ).To (BeEquivalentTo (1 ))
1529
+ })
1530
+
1443
1531
It ("should not update spec of an existing object" , func (ctx SpecContext ) {
1444
1532
cl , err := client .New (cfg , client.Options {})
1445
1533
Expect (err ).NotTo (HaveOccurred ())
@@ -1592,6 +1680,34 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
1592
1680
Expect (actual .Status .Replicas ).To (BeEquivalentTo (1 ))
1593
1681
})
1594
1682
1683
+ It ("should apply status and preserve type information" , func (ctx SpecContext ) {
1684
+ cl , err := client .New (cfg , client.Options {})
1685
+ Expect (err ).NotTo (HaveOccurred ())
1686
+ Expect (cl ).NotTo (BeNil ())
1687
+
1688
+ By ("initially creating a Deployment" )
1689
+ dep , err := clientset .AppsV1 ().Deployments (ns ).Create (ctx , dep , metav1.CreateOptions {})
1690
+ Expect (err ).NotTo (HaveOccurred ())
1691
+ Expect (dep .Status .Replicas ).To (BeEquivalentTo (0 ))
1692
+
1693
+ By ("applying the status of Deployment" )
1694
+ dep .Status .Replicas = 1
1695
+ dep .ManagedFields = nil // Must be unset in SSA requests
1696
+ u := & unstructured.Unstructured {}
1697
+ Expect (scheme .Convert (dep , u , nil )).To (Succeed ())
1698
+ err = cl .Status ().Apply (ctx , client .ApplyConfigurationFromUnstructured (u ), client .FieldOwner ("foo" ))
1699
+ Expect (err ).NotTo (HaveOccurred ())
1700
+
1701
+ By ("validating updated Deployment has type information" )
1702
+ Expect (u .GroupVersionKind ()).To (Equal (depGvk ))
1703
+
1704
+ By ("validating patched Deployment has new status" )
1705
+ actual , err := clientset .AppsV1 ().Deployments (ns ).Get (ctx , dep .Name , metav1.GetOptions {})
1706
+ Expect (err ).NotTo (HaveOccurred ())
1707
+ Expect (actual ).NotTo (BeNil ())
1708
+ Expect (actual .Status .Replicas ).To (BeEquivalentTo (1 ))
1709
+ })
1710
+
1595
1711
It ("should not update spec of an existing object" , func (ctx SpecContext ) {
1596
1712
cl , err := client .New (cfg , client.Options {})
1597
1713
Expect (err ).NotTo (HaveOccurred ())
0 commit comments