Skip to content

Commit

Permalink
improve abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ludusrusso committed Apr 12, 2021
1 parent cead46d commit 16c49f6
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions cmd/clusterctl/internal/util/objs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func FixImages(objs []unstructured.Unstructured, alterImageFunc func(image strin
// look for resources of kind Deployment and alter the image
for i := range objs {
o := &objs[i]
err := fixDeploymentImage(o, alterImageFunc)
err := fixDeploymentImages(o, alterImageFunc)
if err != nil {
return nil, err
}
err = fixDaemonSetImage(o, alterImageFunc)
err = fixDaemonSetImages(o, alterImageFunc)
if err != nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func FixImages(objs []unstructured.Unstructured, alterImageFunc func(image strin
return objs, nil
}

func fixDeploymentImage(o *unstructured.Unstructured, alterImageFunc func(image string) (string, error)) error {
func fixDeploymentImages(o *unstructured.Unstructured, alterImageFunc func(image string) (string, error)) error {
if o.GetKind() != deploymentKind {
return nil
}
Expand All @@ -145,14 +145,10 @@ func fixDeploymentImage(o *unstructured.Unstructured, alterImageFunc func(image
}

var err error
d.Spec.Template.Spec.Containers, err = fixContainersImage(d.Spec.Template.Spec.Containers, alterImageFunc)
d.Spec.Template.Spec, err = fixPodSpecImages(d.Spec.Template.Spec, alterImageFunc)
if err != nil {
return errors.Wrapf(err, "failed to fix containers in deployment %s", d.Name)
}
d.Spec.Template.Spec.InitContainers, err = fixContainersImage(d.Spec.Template.Spec.InitContainers, alterImageFunc)
if err != nil {
return errors.Wrapf(err, "failed to fix init containers in deployment %s", d.Name)
}

// Convert typed object back to Unstructured
if err := scheme.Scheme.Convert(d, o, nil); err != nil {
Expand All @@ -161,7 +157,7 @@ func fixDeploymentImage(o *unstructured.Unstructured, alterImageFunc func(image
return nil
}

func fixDaemonSetImage(o *unstructured.Unstructured, alterImageFunc func(image string) (string, error)) error {
func fixDaemonSetImages(o *unstructured.Unstructured, alterImageFunc func(image string) (string, error)) error {
if o.GetKind() != daemonSetKind {
return nil
}
Expand All @@ -173,22 +169,30 @@ func fixDaemonSetImage(o *unstructured.Unstructured, alterImageFunc func(image s
}

var err error
d.Spec.Template.Spec.Containers, err = fixContainersImage(d.Spec.Template.Spec.Containers, alterImageFunc)
d.Spec.Template.Spec, err = fixPodSpecImages(d.Spec.Template.Spec, alterImageFunc)
if err != nil {
return errors.Wrapf(err, "failed to fix containers in deamonSet %s", d.Name)
}
d.Spec.Template.Spec.InitContainers, err = fixContainersImage(d.Spec.Template.Spec.InitContainers, alterImageFunc)
if err != nil {
return errors.Wrapf(err, "failed to fix init containers in deamonSet %s", d.Name)
}

// Convert typed object back to Unstructured
if err := scheme.Scheme.Convert(d, o, nil); err != nil {
return err
}
return nil
}

func fixPodSpecImages(podSpec v1.PodSpec, alterImageFunc func(image string) (string, error)) (v1.PodSpec, error) {
var err error
podSpec.Containers, err = fixContainersImage(podSpec.Containers, alterImageFunc)
if err != nil {
return podSpec, errors.Wrapf(err, "failed to fix containers")
}
podSpec.InitContainers, err = fixContainersImage(podSpec.InitContainers, alterImageFunc)
if err != nil {
return podSpec, errors.Wrapf(err, "failed to fix init containers")
}
return podSpec, nil
}

func fixContainersImage(containers []v1.Container, alterImageFunc func(image string) (string, error)) ([]v1.Container, error) {
for j := range containers {
container := &containers[j]
Expand Down

0 comments on commit 16c49f6

Please sign in to comment.