@@ -12,6 +12,7 @@ import (
1212 "k8s.io/apimachinery/pkg/api/resource"
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414 api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
15+ "sigs.k8s.io/hierarchical-namespaces/internal/hrq/utils"
1516 "sigs.k8s.io/yaml"
1617
1718 "github.com/google/uuid"
@@ -114,6 +115,24 @@ var _ = Describe("Scoped Hierarchical Resource Quota", Label("pfnet"), func() {
114115
115116 FieldShouldContain ("hrq" , parentNs , hrq .Name , ".status.used" , "pods:1" )
116117 })
118+
119+ It ("should remove the legacy RQ" , func () {
120+ hrqName := "legacy-hrq"
121+
122+ // Legacy RQ remains before the new RQ is created
123+ legacyRQ := mustCreateLegacyRQ (parentNs , hrqName , corev1.ResourceList {corev1 .ResourcePods : resource .MustParse ("1" )})
124+ RunShouldContain (legacyRQ .Name , propagationTime , "kubectl get resourcequota -n" , parentNs )
125+
126+ // Create the HRQ
127+ setScopedHRQ (hrqName , parentNs , corev1.ResourceList {corev1 .ResourcePods : resource .MustParse ("1" )}, & scopeSelector )
128+
129+ // Confirm the new RQ is created
130+ newRQName := api .ResourceQuotaSingletonName + "-" + parentNs + "-" + hrqName + "-" + md5Hash (parentNs + "/" + hrqName )
131+ RunShouldContain (newRQName , propagationTime , "kubectl get resourcequota -n" , parentNs , newRQName )
132+
133+ // Legacy RQ is removed
134+ RunShouldNotContain (legacyRQ .Name , propagationTime , "kubectl get resourcequota -n" , parentNs )
135+ })
117136})
118137
119138func mustCreatePod (prefix , nsnm string ) (corev1.Pod , error ) {
@@ -217,6 +236,32 @@ func createSubNS(parent, prefix string) string {
217236 return nsName
218237}
219238
239+ func mustCreateLegacyRQ (ns , hrqName string , resourceList corev1.ResourceList ) corev1.ResourceQuota {
240+ hrq := corev1.ResourceQuota {
241+ TypeMeta : metav1.TypeMeta {
242+ Kind : "ResourceQuota" ,
243+ APIVersion : "v1" ,
244+ },
245+ ObjectMeta : metav1.ObjectMeta {
246+ Name : utils .LegacyScopedRQName (hrqName ),
247+ Namespace : ns ,
248+ Labels : map [string ]string {
249+ api .HRQLabelCleanup : "true" ,
250+ },
251+ },
252+ Spec : corev1.ResourceQuotaSpec {
253+ Hard : resourceList ,
254+ },
255+ }
256+ manifest , err := yaml .Marshal (hrq )
257+ Expect (err ).NotTo (HaveOccurred ())
258+
259+ MustApplyYAML (string (manifest ))
260+ RunShouldContain (hrq .Name , propagationTime , "kubectl get resourcequota -n" , ns , hrq .Name )
261+
262+ return hrq
263+ }
264+
220265func genPriorityScopeSelector () (corev1.ScopeSelector , string , func ()) {
221266 priority := uuid .New ().String ()
222267 err := TryRun ("kubectl create priorityclass" , priority , "--value=100" )
0 commit comments