This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wire kubeyaml into the fluxd deployment
To be able to use kubeyaml from within a pod, fluxd must have access to the binaries. This requires a bit of a highwire performance: - use an initContainer in the Deployment to copy the files over to a shared volume - use an env entry to tell fluxd where to look for the binary I took the opportunity to factor out the code that executes `kubeyaml`.
- Loading branch information
Showing
4 changed files
with
72 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// KubeYAML is a placeholder value for calling the helper executable | ||
// `kubeyaml`. | ||
type KubeYAML struct { | ||
} | ||
|
||
// Image calls the kubeyaml subcommand `image` with the arguments given. | ||
func (k KubeYAML) Image(in []byte, ns, kind, name, container, image string) ([]byte, error) { | ||
args := []string{"image", "--namespace", ns, "--kind", kind, "--name", name} | ||
args = append(args, "--container", container, "--image", image) | ||
return execKubeyaml(in, args) | ||
} | ||
|
||
// Annotate calls the kubeyaml subcommand `annotate` with the arguments as given. | ||
func (k KubeYAML) Annotate(in []byte, ns, kind, name string, policies ...string) ([]byte, error) { | ||
args := []string{"annotate", "--namespace", ns, "--kind", kind, "--name", name} | ||
args = append(args, policies...) | ||
return execKubeyaml(in, args) | ||
} | ||
|
||
func execKubeyaml(in []byte, args []string) ([]byte, error) { | ||
kubeyaml, err := exec.LookPath("kubeyaml") | ||
if err != nil { | ||
kubeyaml = os.ExpandEnv("${PLUGINS_PATH}/kubeyaml/kubeyaml") | ||
} | ||
cmd := exec.Command(kubeyaml, args...) | ||
out := &bytes.Buffer{} | ||
errOut := &bytes.Buffer{} | ||
cmd.Stdin = bytes.NewBuffer(in) | ||
cmd.Stdout = out | ||
cmd.Stderr = errOut | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
return nil, errors.New(strings.TrimSpace(errOut.String())) | ||
} | ||
return out.Bytes(), nil | ||
} |
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 |
---|---|---|
@@ -1,42 +1,21 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/weaveworks/flux" | ||
"github.com/weaveworks/flux/image" | ||
) | ||
|
||
// updatePodController takes the body of a Deployment resource definition | ||
// (specified in YAML) and the name of the new image that should be put in the | ||
// definition (in the format "repo.org/group/name:tag"). It returns a new | ||
// resource definition body where all references to the old image have been | ||
// replaced with the new one. | ||
// | ||
// This function has many additional requirements that are likely in flux. Read | ||
// the source to learn about them. | ||
func updatePodController(file []byte, resource flux.ResourceID, container string, newImageID image.Ref) ([]byte, error) { | ||
// updatePodController takes a YAML document stream (one or more YAML | ||
// docs, as bytes), a resource ID referring to a controller, a | ||
// container name, and the name of the new image that should be used | ||
// for the container. It returns a new YAML stream where the image for | ||
// the container has been replaced with the imageRef supplied. | ||
func updatePodController(in []byte, resource flux.ResourceID, container string, newImageID image.Ref) ([]byte, error) { | ||
namespace, kind, name := resource.Components() | ||
if _, ok := resourceKinds[strings.ToLower(kind)]; !ok { | ||
return nil, UpdateNotSupportedError(kind) | ||
} | ||
|
||
args := []string{"image", "--namespace", namespace, "--kind", kind, "--name", name} | ||
args = append(args, "--container", container, "--image", newImageID.String()) | ||
|
||
println("TRACE:", "kubeyaml", strings.Join(args, " ")) | ||
cmd := exec.Command("kubeyaml", args...) | ||
out := &bytes.Buffer{} | ||
errOut := &bytes.Buffer{} | ||
cmd.Stdin = bytes.NewBuffer(file) | ||
cmd.Stdout = out | ||
cmd.Stderr = errOut | ||
if err := cmd.Run(); err != nil { | ||
return nil, errors.New(strings.TrimSpace(errOut.String())) | ||
} | ||
return out.Bytes(), nil | ||
return (KubeYAML{}).Image(in, namespace, kind, name, container, newImageID.String()) | ||
} |
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