Merge pull request #647 from XiShanYongYe-Chang/refactor-imageoverride
refactor imageoverride: use converted resouce to extract json patches
This commit is contained in:
commit
9336af6316
|
@ -5,16 +5,19 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
|
||||||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
"github.com/karmada-io/karmada/pkg/util"
|
"github.com/karmada-io/karmada/pkg/util"
|
||||||
|
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||||
"github.com/karmada-io/karmada/pkg/util/imageparser"
|
"github.com/karmada-io/karmada/pkg/util/imageparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pathSplit = "/"
|
pathSplit = "/"
|
||||||
imageString = "image"
|
podSpecPrefix = "/spec"
|
||||||
|
podTemplatePrefix = "/spec/template/spec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// buildPatches parse JSON patches from resource object by imageOverriders
|
// buildPatches parse JSON patches from resource object by imageOverriders
|
||||||
|
@ -29,44 +32,65 @@ func buildPatches(rawObj *unstructured.Unstructured, imageOverrider *policyv1alp
|
||||||
func buildPatchesWithEmptyPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
func buildPatchesWithEmptyPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
||||||
switch rawObj.GetKind() {
|
switch rawObj.GetKind() {
|
||||||
case util.PodKind:
|
case util.PodKind:
|
||||||
return buildPatchesWithPath("spec/containers", rawObj, imageOverrider)
|
podObj, err := helper.ConvertToPod(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert Pod from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(podObj.Spec, podSpecPrefix, imageOverrider)
|
||||||
case util.ReplicaSetKind:
|
case util.ReplicaSetKind:
|
||||||
fallthrough
|
replicaSetObj, err := helper.ConvertToReplicaSet(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert ReplicaSet from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(replicaSetObj.Spec.Template.Spec, podTemplatePrefix, imageOverrider)
|
||||||
case util.DeploymentKind:
|
case util.DeploymentKind:
|
||||||
fallthrough
|
deploymentObj, err := helper.ConvertToDeployment(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert Deployment from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(deploymentObj.Spec.Template.Spec, podTemplatePrefix, imageOverrider)
|
||||||
case util.DaemonSetKind:
|
case util.DaemonSetKind:
|
||||||
fallthrough
|
daemonSetObj, err := helper.ConvertToDaemonSet(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert DaemonSet from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(daemonSetObj.Spec.Template.Spec, podTemplatePrefix, imageOverrider)
|
||||||
case util.StatefulSetKind:
|
case util.StatefulSetKind:
|
||||||
return buildPatchesWithPath("spec/template/spec/containers", rawObj, imageOverrider)
|
statefulSetObj, err := helper.ConvertToStatefulSet(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert StatefulSet from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(statefulSetObj.Spec.Template.Spec, podTemplatePrefix, imageOverrider)
|
||||||
|
case util.JobKind:
|
||||||
|
jobObj, err := helper.ConvertToJob(rawObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert Job from unstructured object: %v", err)
|
||||||
|
}
|
||||||
|
return extractPatchesBy(jobObj.Spec.Template.Spec, podTemplatePrefix, imageOverrider)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPatchesWithPath(specContainersPath string, rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
func extractPatchesBy(podSpec corev1.PodSpec, prefixPath string, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
||||||
patches := make([]overrideOption, 0)
|
patches := make([]overrideOption, 0)
|
||||||
|
|
||||||
containers, ok, err := unstructured.NestedSlice(rawObj.Object, strings.Split(specContainersPath, pathSplit)...)
|
for containerIndex, container := range podSpec.Containers {
|
||||||
if err != nil {
|
patch, err := acquireOverrideOption(spliceImagePath(prefixPath, containerIndex), container.Image, imageOverrider)
|
||||||
return nil, fmt.Errorf("failed to retrieves path(%s) from rawObj, error: %v", specContainersPath, err)
|
|
||||||
}
|
|
||||||
if !ok || len(containers) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for index := range containers {
|
|
||||||
imagePath := fmt.Sprintf("/%s/%d/image", specContainersPath, index)
|
|
||||||
imageValue := containers[index].(map[string]interface{})[imageString].(string)
|
|
||||||
patch, err := acquireOverrideOption(imagePath, imageValue, imageOverrider)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
patches = append(patches, patch)
|
patches = append(patches, patch)
|
||||||
}
|
}
|
||||||
|
|
||||||
return patches, nil
|
return patches, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func spliceImagePath(prefixPath string, containerIndex int) string {
|
||||||
|
return fmt.Sprintf("%s/containers/%d/image", prefixPath, containerIndex)
|
||||||
|
}
|
||||||
|
|
||||||
func buildPatchesWithPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
func buildPatchesWithPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) {
|
||||||
patches := make([]overrideOption, 0)
|
patches := make([]overrideOption, 0)
|
||||||
|
|
||||||
|
|
|
@ -258,6 +258,7 @@ func applyImageOverriders(rawObj *unstructured.Unstructured, imageOverriders []p
|
||||||
for index := range imageOverriders {
|
for index := range imageOverriders {
|
||||||
patches, err := buildPatches(rawObj, &imageOverriders[index])
|
patches, err := buildPatches(rawObj, &imageOverriders[index])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
klog.Errorf("Build patches with imageOverrides err: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue