@@ -43,6 +43,8 @@ import (
4343 "k8s.io/apimachinery/pkg/runtime"
4444 "k8s.io/apimachinery/pkg/runtime/schema"
4545 "k8s.io/apimachinery/pkg/types"
46+ appsv1applyconfigurations "k8s.io/client-go/applyconfigurations/apps/v1"
47+ autoscaling1applyconfigurations "k8s.io/client-go/applyconfigurations/autoscaling/v1"
4648 corev1applyconfigurations "k8s.io/client-go/applyconfigurations/core/v1"
4749 kscheme "k8s.io/client-go/kubernetes/scheme"
4850 "k8s.io/client-go/rest"
@@ -1127,6 +1129,34 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
11271129 Expect (err ).NotTo (HaveOccurred ())
11281130 Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
11291131 })
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+ })
11301160 })
11311161
11321162 Context ("with unstructured objects" , func () {
@@ -1322,8 +1352,8 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
13221352 By ("Creating a deployment" )
13231353 dep , err := clientset .AppsV1 ().Deployments (dep .Namespace ).Create (ctx , dep , metav1.CreateOptions {})
13241354 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
13271357 depUnstructured , err := toUnstructured (dep )
13281358 Expect (err ).NotTo (HaveOccurred ())
13291359
@@ -1374,6 +1404,41 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
13741404 Expect (err ).NotTo (HaveOccurred ())
13751405 Expect (* dep .Spec .Replicas ).To (Equal (replicaCount ))
13761406 })
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+ })
13771442 })
13781443
13791444 })
@@ -1440,6 +1505,29 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
14401505 Expect (dep .GroupVersionKind ()).To (Equal (depGvk ))
14411506 })
14421507
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+
14431531 It ("should not update spec of an existing object" , func (ctx SpecContext ) {
14441532 cl , err := client .New (cfg , client.Options {})
14451533 Expect (err ).NotTo (HaveOccurred ())
@@ -1592,6 +1680,34 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
15921680 Expect (actual .Status .Replicas ).To (BeEquivalentTo (1 ))
15931681 })
15941682
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+
15951711 It ("should not update spec of an existing object" , func (ctx SpecContext ) {
15961712 cl , err := client .New (cfg , client.Options {})
15971713 Expect (err ).NotTo (HaveOccurred ())
0 commit comments