Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add patch for UnitedDeployment #1266

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apis/apps/v1alpha1/uniteddeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
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"
)

Expand Down Expand Up @@ -187,6 +188,13 @@ type Subset struct {
// Controller will try to keep all the subsets with nil replicas have average pods.
// +optional
Replicas *intstr.IntOrString `json:"replicas,omitempty"`
// Patch indicates patching to the templateSpec.
// Patch takes precedence over other fields
// If the Patch also modifies the Replicas, NodeSelectorTerm or Tolerations, use value in the Patch
// +optional
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
Patch runtime.RawExtension `json:"patch,omitempty"`
}

// UnitedDeploymentStatus defines the observed state of UnitedDeployment.
Expand Down
2 changes: 2 additions & 0 deletions apis/apps/v1alpha1/well_known_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (

// ImagePreDownloadIgnoredKey indicates the images of this revision have been ignored to pre-download
ImagePreDownloadIgnoredKey = "apps.kruise.io/image-predownload-ignored"
// AnnotationSubsetPatchKey indicates the patch for every subset
AnnotationSubsetPatchKey = "apps.kruise.io/subset-patch"
)

// Sidecar container environment variable definitions which are used to enable SidecarTerminator to take effect on the sidecar container.
Expand Down
1 change: 1 addition & 0 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,12 @@ spec:
type: object
type: array
type: object
patch:
description: Patch indicates patching to the templateSpec.
Patch takes precedence over other fields If the Patch
also modifies the Replicas, NodeSelectorTerm or Tolerations,
use value in the Patch
x-kubernetes-preserve-unknown-fields: true
replicas:
anyOf:
- type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package adapter

import (
"context"
"encoding/json"
"fmt"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

38% of developers fix this issue

goimports: File is not goimports-ed

❗❗ 2 similar findings have been found in this PR

🔎 Expand here to view all instances of this finding
File Path Line Number
pkg/controller/uniteddeployment/adapter/cloneset_adapter.go 6
pkg/controller/uniteddeployment/adapter/deployment_adapter.go 22

Visit the Lift Web Console to find more details in your report.


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog/v2"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -173,6 +177,26 @@ func (a *AdvancedStatefulSetAdapter) ApplySubsetTemplate(ud *alpha1.UnitedDeploy

attachNodeAffinity(&set.Spec.Template.Spec, subSetConfig)
attachTolerations(&set.Spec.Template.Spec, subSetConfig)
if subSetConfig.Patch.Raw != nil {
TemplateSpecBytes, _ := json.Marshal(set.Spec.Template)
modified, err := strategicpatch.StrategicMergePatch(TemplateSpecBytes, subSetConfig.Patch.Raw, &corev1.PodTemplateSpec{})
if err != nil {
klog.Errorf("failed to merge patch raw %s", subSetConfig.Patch.Raw)
return err
}
patchedTemplateSpec := corev1.PodTemplateSpec{}
if err = json.Unmarshal(modified, &patchedTemplateSpec); err != nil {
klog.Errorf("failed to unmarshal %s to podTemplateSpec", modified)
return err
}

set.Spec.Template = patchedTemplateSpec
klog.V(2).Infof("AdvancedStatefulSet [%s/%s] was patched successfully: %s", set.Namespace, set.GenerateName, subSetConfig.Patch.Raw)
}
if set.Annotations == nil {
set.Annotations = make(map[string]string)
}
set.Annotations[alpha1.AnnotationSubsetPatchKey] = string(subSetConfig.Patch.Raw)

return nil
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/controller/uniteddeployment/adapter/cloneset_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package adapter

import (
"context"
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog/v2"

alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/util"
"github.com/openkruise/kruise/pkg/util/refmanager"
Expand Down Expand Up @@ -134,7 +138,26 @@ func (a *CloneSetAdapter) ApplySubsetTemplate(ud *alpha1.UnitedDeployment, subse

attachNodeAffinity(&set.Spec.Template.Spec, subSetConfig)
attachTolerations(&set.Spec.Template.Spec, subSetConfig)
if subSetConfig.Patch.Raw != nil {
TemplateSpecBytes, _ := json.Marshal(set.Spec.Template)
modified, err := strategicpatch.StrategicMergePatch(TemplateSpecBytes, subSetConfig.Patch.Raw, &corev1.PodTemplateSpec{})
if err != nil {
klog.Errorf("failed to merge patch raw %s", subSetConfig.Patch.Raw)
return err
}
patchedTemplateSpec := corev1.PodTemplateSpec{}
if err = json.Unmarshal(modified, &patchedTemplateSpec); err != nil {
klog.Errorf("failed to unmarshal %s to podTemplateSpec", modified)
return err
}

set.Spec.Template = patchedTemplateSpec
klog.V(2).Infof("CloneSet [%s/%s] was patched successfully: %s", set.Namespace, set.GenerateName, subSetConfig.Patch.Raw)
}
if set.Annotations == nil {
set.Annotations = make(map[string]string)
}
set.Annotations[alpha1.AnnotationSubsetPatchKey] = string(subSetConfig.Patch.Raw)
return nil
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/controller/uniteddeployment/adapter/deployment_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package adapter

import (
"context"
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog/v2"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -146,6 +150,27 @@ func (a *DeploymentAdapter) ApplySubsetTemplate(ud *alpha1.UnitedDeployment, sub
attachNodeAffinity(&set.Spec.Template.Spec, subSetConfig)
attachTolerations(&set.Spec.Template.Spec, subSetConfig)

if subSetConfig.Patch.Raw != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz add ut that contain patches

TemplateSpecBytes, _ := json.Marshal(set.Spec.Template)
modified, err := strategicpatch.StrategicMergePatch(TemplateSpecBytes, subSetConfig.Patch.Raw, &corev1.PodTemplateSpec{})
if err != nil {
klog.Errorf("failed to merge patch raw %s", subSetConfig.Patch.Raw)
return err
}
patchedTemplateSpec := corev1.PodTemplateSpec{}
if err = json.Unmarshal(modified, &patchedTemplateSpec); err != nil {
klog.Errorf("failed to unmarshal %s to podTemplateSpec", modified)
return err
}

set.Spec.Template = patchedTemplateSpec
klog.V(2).Infof("Deployment [%s/%s] was patched successfully: %s", set.Namespace, set.GenerateName, subSetConfig.Patch.Raw)
}
if set.Annotations == nil {
set.Annotations = make(map[string]string)
}
set.Annotations[alpha1.AnnotationSubsetPatchKey] = string(subSetConfig.Patch.Raw)

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/controller/uniteddeployment/adapter/statefulset_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package adapter

import (
"context"
"encoding/json"
"fmt"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

38% of developers fix this issue

goimports: File is not goimports-ed


ℹ️ Expand to see all @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


"k8s.io/apimachinery/pkg/util/strategicpatch"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -157,6 +160,26 @@ func (a *StatefulSetAdapter) ApplySubsetTemplate(ud *alpha1.UnitedDeployment, su

attachNodeAffinity(&set.Spec.Template.Spec, subSetConfig)
attachTolerations(&set.Spec.Template.Spec, subSetConfig)
if subSetConfig.Patch.Raw != nil {
TemplateSpecBytes, _ := json.Marshal(set.Spec.Template)
modified, err := strategicpatch.StrategicMergePatch(TemplateSpecBytes, subSetConfig.Patch.Raw, &corev1.PodTemplateSpec{})
if err != nil {
klog.Errorf("failed to merge patch raw %s", subSetConfig.Patch.Raw)
return err
}
patchedTemplateSpec := corev1.PodTemplateSpec{}
if err = json.Unmarshal(modified, &patchedTemplateSpec); err != nil {
klog.Errorf("failed to unmarshal %s to podTemplateSpec", modified)
return err
}

set.Spec.Template = patchedTemplateSpec
klog.V(2).Infof("StatefulSet [%s/%s] was patched successfully: %s", set.Namespace, set.GenerateName, subSetConfig.Patch.Raw)
}
if set.Annotations == nil {
set.Annotations = make(map[string]string)
}
set.Annotations[alpha1.AnnotationSubsetPatchKey] = string(subSetConfig.Patch.Raw)

return nil
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/uniteddeployment/subset.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ type SubsetUpdateStrategy struct {
Partition int32
}

// SubsetUpdate stores the subset field that may need to be updated
type SubsetUpdate struct {
Replicas int32
Partition int32
Patch string
}

// ResourceRef stores the Subset resource it represents.
type ResourceRef struct {
Resources []metav1.Object
Expand Down
18 changes: 16 additions & 2 deletions pkg/controller/uniteddeployment/uniteddeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ func (r *ReconcileUnitedDeployment) Reconcile(_ context.Context, request reconci
}

nextPartitions := calcNextPartitions(instance, nextReplicas)
klog.V(4).Infof("Get UnitedDeployment %s/%s next partition %v", instance.Namespace, instance.Name, nextPartitions)
nextUpdate := getNextUpdate(instance, nextReplicas, nextPartitions)
klog.V(4).Infof("Get UnitedDeployment %s/%s next update %v", instance.Namespace, instance.Name, nextUpdate)

newStatus, err := r.manageSubsets(instance, nameToSubset, nextReplicas, nextPartitions, currentRevision, updatedRevision, subsetType)
newStatus, err := r.manageSubsets(instance, nameToSubset, nextUpdate, currentRevision, updatedRevision, subsetType)
if err != nil {
klog.Errorf("Fail to update UnitedDeployment %s/%s: %s", instance.Namespace, instance.Name, err)
r.recorder.Event(instance.DeepCopy(), corev1.EventTypeWarning, fmt.Sprintf("Failed%s", eventTypeSubsetsUpdate), err.Error())
Expand Down Expand Up @@ -273,6 +274,19 @@ func calcNextPartitions(ud *appsv1alpha1.UnitedDeployment, nextReplicas *map[str
return &partitions
}

func getNextUpdate(ud *appsv1alpha1.UnitedDeployment, nextReplicas *map[string]int32, nextPartitions *map[string]int32) map[string]SubsetUpdate {
next := make(map[string]SubsetUpdate)
for _, subset := range ud.Spec.Topology.Subsets {
t := SubsetUpdate{}
t.Replicas = (*nextReplicas)[subset.Name]
t.Partition = (*nextPartitions)[subset.Name]
t.Patch = string(subset.Patch.Raw)

next[subset.Name] = t
}
return next
}

func (r *ReconcileUnitedDeployment) deleteDupSubset(ud *appsv1alpha1.UnitedDeployment, nameToSubsets map[string][]*Subset, control ControlInterface) (*map[string]*Subset, error) {
nameToSubset := map[string]*Subset{}
for name, subsets := range nameToSubsets {
Expand Down
Loading