-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: changzhen <changzhen5@huawei.com>
- Loading branch information
1 parent
196c394
commit a0a1060
Showing
9 changed files
with
201 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package overridemanager | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
"github.com/karmada-io/karmada/pkg/util" | ||
"github.com/karmada-io/karmada/pkg/util/imageparser" | ||
) | ||
|
||
const pathSplit = "/" | ||
|
||
func parseJSONPatchesByImageOverriders(rawObj *unstructured.Unstructured, imageOverriders []policyv1alpha1.ImageOverrider) ([]overrideOption, error) { | ||
imagePatches := make([]overrideOption, 0) | ||
for index := range imageOverriders { | ||
patches, err := parseJSONPatchesByImageOverrider(rawObj, &imageOverriders[index]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
imagePatches = append(imagePatches, patches...) | ||
} | ||
return imagePatches, nil | ||
} | ||
|
||
func parseJSONPatchesByImageOverrider(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) { | ||
if imageOverrider.Predicate == nil { | ||
return parseJSONPatchesWithEmptyPredicate(rawObj, imageOverrider) | ||
} | ||
|
||
return parseJSONPatchesWithPredicate(rawObj, imageOverrider) | ||
} | ||
|
||
func parseJSONPatchesWithEmptyPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) { | ||
switch rawObj.GetKind() { | ||
case util.PodKind: | ||
return parseJSONPatchesWithSpecContainersPath("spec/containers", rawObj, imageOverrider) | ||
case util.ReplicaSetKind: | ||
return parseJSONPatchesWithSpecContainersPath("spec/template/spec/containers", rawObj, imageOverrider) | ||
case util.DeploymentKind: | ||
return parseJSONPatchesWithSpecContainersPath("spec/template/spec/containers", rawObj, imageOverrider) | ||
case util.StatefulSetKind: | ||
return parseJSONPatchesWithSpecContainersPath("spec/template/spec/containers", rawObj, imageOverrider) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func parseJSONPatchesWithSpecContainersPath(specContainersPath string, rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) { | ||
patches := make([]overrideOption, 0) | ||
|
||
containers, ok, err := unstructured.NestedSlice(rawObj.Object, strings.Split(specContainersPath, pathSplit)...) | ||
if err != nil { | ||
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 { | ||
patch, err := parseJSONPatchWithPath(fmt.Sprintf("%s/%d/image", specContainersPath, index), rawObj, imageOverrider) | ||
if err != nil { | ||
return nil, err | ||
} | ||
patches = append(patches, patch) | ||
} | ||
|
||
return patches, nil | ||
} | ||
|
||
func parseJSONPatchesWithPredicate(rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) ([]overrideOption, error) { | ||
patches := make([]overrideOption, 0) | ||
patch, err := parseJSONPatchWithPath(imageOverrider.Predicate.Path, rawObj, imageOverrider) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
patches = append(patches, patch) | ||
return patches, nil | ||
} | ||
|
||
func parseJSONPatchWithPath(imagePath string, rawObj *unstructured.Unstructured, imageOverrider *policyv1alpha1.ImageOverrider) (overrideOption, error) { | ||
imageValue, ok, err := unstructured.NestedString(rawObj.Object, strings.Split(imagePath, pathSplit)...) | ||
if err != nil { | ||
return overrideOption{}, fmt.Errorf("failed to retrieves path(%s) from rawObj, errpr: %v", imagePath, err) | ||
} | ||
if !ok { | ||
return overrideOption{}, nil | ||
} | ||
|
||
imageComponent, err := imageparser.Parse(imageValue) | ||
if err != nil { | ||
return overrideOption{}, fmt.Errorf("failed to parse image value with path(%s), error: %v", imagePath, err) | ||
} | ||
|
||
return overrideOption{ | ||
Op: string(policyv1alpha1.OverriderOpReplace), | ||
Path: imageOverrider.Predicate.Path, | ||
Value: parsedUpdatedImageValue(imageComponent, imageOverrider), | ||
}, nil | ||
} | ||
|
||
func parsedUpdatedImageValue(imageComponent *imageparser.Components, imageOverrider *policyv1alpha1.ImageOverrider) string { | ||
switch imageOverrider.Component { | ||
case policyv1alpha1.Registry: | ||
switch imageOverrider.Operator { | ||
case policyv1alpha1.OverriderOpAdd: | ||
imageComponent.SetHostname(imageComponent.Hostname() + imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpReplace: | ||
imageComponent.SetHostname(imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpRemove: | ||
imageComponent.RemoveHostname() | ||
} | ||
return imageComponent.String() | ||
case policyv1alpha1.Repository: | ||
switch imageOverrider.Operator { | ||
case policyv1alpha1.OverriderOpAdd: | ||
imageComponent.SetRepository(imageComponent.Repository() + imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpReplace: | ||
imageComponent.SetRepository(imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpRemove: | ||
imageComponent.RemoveRepository() | ||
} | ||
return imageComponent.String() | ||
case policyv1alpha1.Tag: | ||
switch imageOverrider.Operator { | ||
case policyv1alpha1.OverriderOpAdd: | ||
imageComponent.SetTagOrDigest(imageComponent.TagOrDigest() + imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpReplace: | ||
imageComponent.SetTagOrDigest(imageOverrider.Value) | ||
case policyv1alpha1.OverriderOpRemove: | ||
imageComponent.RemoveTagOrDigest() | ||
} | ||
return imageComponent.String() | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters