Skip to content

Commit

Permalink
add status.labelSelector field to unitedDeployment to support scale s…
Browse files Browse the repository at this point in the history
…ub-resource (openkruise#1314)

Signed-off-by: liuzhenwei <dui_zhang@163.com>
  • Loading branch information
diannaowa committed Aug 29, 2023
1 parent 9f6600a commit b304c64
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
8 changes: 6 additions & 2 deletions apis/apps/v1alpha1/uniteddeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ limitations under the License.
package v1alpha1

import (
"github.com/openkruise/kruise/apis/apps/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/openkruise/kruise/apis/apps/v1beta1"
)

// UpdateStrategyType is a string enumeration type that enumerates
Expand Down Expand Up @@ -238,6 +239,9 @@ type UnitedDeploymentStatus struct {
// Records the information of update progress.
// +optional
UpdateStatus *UpdateStatus `json:"updateStatus,omitempty"`

// LabelSelector is label selectors for query over pods that should match the replica count used by HPA.
LabelSelector string `json:"labelSelector,omitempty"`
}

// UnitedDeploymentCondition describes current state of a UnitedDeployment.
Expand Down Expand Up @@ -275,7 +279,7 @@ type UpdateStatus struct {
// +k8s:openapi-gen=true
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.labelSelector
// +kubebuilder:resource:shortName=ud
// +kubebuilder:printcolumn:name="DESIRED",type="integer",JSONPath=".spec.replicas",description="The desired number of pods."
// +kubebuilder:printcolumn:name="CURRENT",type="integer",JSONPath=".status.replicas",description="The number of currently all pods."
Expand Down
6 changes: 5 additions & 1 deletion config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,10 @@ spec:
description: CurrentRevision, if not empty, indicates the current
version of the UnitedDeployment.
type: string
labelSelector:
description: LabelSelector is label selectors for query over pods
that should match the replica count used by HPA.
type: string
observedGeneration:
description: ObservedGeneration is the most recent generation observed
for this UnitedDeployment. It corresponds to the UnitedDeployment's
Expand Down Expand Up @@ -1240,7 +1244,7 @@ spec:
storage: true
subresources:
scale:
labelSelectorPath: .status.selector
labelSelectorPath: .status.labelSelector
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
status: {}
Expand Down
23 changes: 17 additions & 6 deletions pkg/controller/uniteddeployment/uniteddeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import (
"fmt"
"reflect"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1"
"github.com/openkruise/kruise/pkg/controller/uniteddeployment/adapter"
utilclient "github.com/openkruise/kruise/pkg/util/client"
utildiscovery "github.com/openkruise/kruise/pkg/util/discovery"
"github.com/openkruise/kruise/pkg/util/ratelimiter"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -40,6 +34,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1"
"github.com/openkruise/kruise/pkg/controller/uniteddeployment/adapter"
"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
utildiscovery "github.com/openkruise/kruise/pkg/util/discovery"
"github.com/openkruise/kruise/pkg/util/ratelimiter"
)

func init() {
Expand Down Expand Up @@ -232,6 +234,14 @@ func (r *ReconcileUnitedDeployment) Reconcile(_ context.Context, request reconci
return reconcile.Result{}, err
}

selector, err := util.ValidatedLabelSelectorAsSelector(instance.Spec.Selector)
if err != nil {
klog.Errorf("Error converting UnitedDeployment %s selector: %v", request, err)
// This is a non-transient error, so don't retry.
return reconcile.Result{}, nil
}
newStatus.LabelSelector = selector.String()

return r.updateStatus(instance, newStatus, oldStatus, nameToSubset, nextReplicas, nextPartitions, currentRevision, updatedRevision, collisionCount, control)
}

Expand Down Expand Up @@ -426,6 +436,7 @@ func (r *ReconcileUnitedDeployment) updateUnitedDeployment(ud *appsv1alpha1.Unit
oldStatus.UpdatedReadyReplicas == newStatus.UpdatedReadyReplicas &&
oldStatus.CurrentRevision == newStatus.CurrentRevision &&
oldStatus.CollisionCount == newStatus.CollisionCount &&
oldStatus.LabelSelector == newStatus.LabelSelector &&
ud.Generation == newStatus.ObservedGeneration &&
reflect.DeepEqual(oldStatus.SubsetReplicas, newStatus.SubsetReplicas) &&
reflect.DeepEqual(oldStatus.UpdateStatus, newStatus.UpdateStatus) &&
Expand Down

0 comments on commit b304c64

Please sign in to comment.