Skip to content

Commit

Permalink
auto patch webhook objectSelector label on workload
Browse files Browse the repository at this point in the history
  • Loading branch information
zmberg committed Jul 4, 2023
1 parent 7139171 commit 5e6d7bd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
22 changes: 22 additions & 0 deletions config/webhook/patch_manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,32 @@ webhooks:
matchExpressions:
- key: rollouts.kruise.io/workload-type
operator: Exists
- name: mcloneset.kb.io
objectSelector:
matchExpressions:
- key: rollouts.kruise.io/workload-type
operator: Exists
- name: mdaemonset.kb.io
objectSelector:
matchExpressions:
- key: rollouts.kruise.io/workload-type
operator: Exists
- name: mstatefulset.kb.io
objectSelector:
matchExpressions:
- key: rollouts.kruise.io/workload-type
operator: Exists
- name: madvancedstatefulset.kb.io
objectSelector:
matchExpressions:
- key: rollouts.kruise.io/workload-type
operator: Exists
- name: mdeployment.kb.io
objectSelector:
matchExpressions:
- key: control-plane
operator: NotIn
values:
- controller-manager
- key: rollouts.kruise.io/workload-type
operator: Exists
29 changes: 27 additions & 2 deletions pkg/controller/rollout/rollout_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/openkruise/rollouts/api/v1alpha1"
"github.com/openkruise/rollouts/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/util/retry"
Expand Down Expand Up @@ -66,8 +67,32 @@ func (r *RolloutReconciler) calculateRolloutStatus(rollout *v1alpha1.Rollout) (r
klog.Infof("rollout(%s/%s) workload not found, and reset status be Initial", rollout.Namespace, rollout.Name)
return false, newStatus, nil
}
klog.V(5).Infof("rollout(%s/%s) workload(%s)", rollout.Namespace, rollout.Name, util.DumpJSON(workload))
// todo, patch workload webhook labels
klog.V(5).Infof("rollout(%s/%s) fetch workload(%s)", rollout.Namespace, rollout.Name, util.DumpJSON(workload))
// Patch objectSelector in workload labels[rollouts.kruise.io/workload-type],
// then rollout only webhook the workload which contains labels[rollouts.kruise.io/workload-type].
var workloadType util.WorkloadType
switch workload.Kind {
case util.ControllerKruiseKindCS.Kind:
workloadType = util.CloneSetType
case util.ControllerKindDep.Kind:
workloadType = util.DeploymentType
case util.ControllerKindSts.Kind:
workloadType = util.StatefulSetType
case util.ControllerKruiseKindDS.Kind:
workloadType = util.DaemonSetType
}
if workload.Annotations[util.WorkloadTypeLabel] == "" && workloadType != "" {
workloadGVK := schema.FromAPIVersionAndKind(workload.APIVersion, workload.Kind)
obj := util.GetEmptyWorkloadObject(workloadGVK)
obj.SetNamespace(workload.Namespace)
obj.SetName(workload.Name)
body := fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`, util.WorkloadTypeLabel, workloadType)
if err = r.Patch(context.TODO(), obj, client.RawPatch(types.MergePatchType, []byte(body))); err != nil {
klog.Errorf("rollout(%s/%s) patch workload(%s) failed: %s", rollout.Namespace, rollout.Name, workload.Name, err.Error())
return false, nil, err
}
klog.Infof("rollout(%s/%s) patch workload(%s) labels[%s] success", rollout.Namespace, rollout.Name, workload.Name, util.WorkloadTypeLabel)
}
// workload status generation is not equal to workload.generation
if !workload.IsStatusConsistent {
klog.Infof("rollout(%s/%s) workload status is inconsistent, then wait a moment", rollout.Namespace, rollout.Name)
Expand Down

0 comments on commit 5e6d7bd

Please sign in to comment.