Skip to content

Commit

Permalink
Replace kubectl with Go server-side apply
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Oct 2, 2021
1 parent 7c77a97 commit c850006
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, ssa ]

jobs:
github:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, ssa ]

jobs:
kind:
Expand Down
45 changes: 0 additions & 45 deletions cmd/flux/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package main

import (
"context"
"encoding/json"
"os"
"os/exec"
"time"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -73,18 +71,11 @@ func init() {
}

func runCheckCmd(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

logger.Actionf("checking prerequisites")
checkFailed := false

fluxCheck()

if !kubectlCheck(ctx, ">=1.18.0-0") {
checkFailed = true
}

if !kubernetesCheck(">=1.16.0-0") {
checkFailed = true
}
Expand Down Expand Up @@ -130,42 +121,6 @@ func fluxCheck() {
}
}

func kubectlCheck(ctx context.Context, constraint string) bool {
_, err := exec.LookPath("kubectl")
if err != nil {
logger.Failuref("kubectl not found")
return false
}

kubectlArgs := []string{"version", "--client", "--output", "json"}
output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...)
if err != nil {
logger.Failuref("kubectl version can't be determined")
return false
}

kv := &kubectlVersion{}
if err = json.Unmarshal([]byte(output), kv); err != nil {
logger.Failuref("kubectl version output can't be unmarshalled")
return false
}

v, err := version.ParseVersion(kv.ClientVersion.GitVersion)
if err != nil {
logger.Failuref("kubectl version can't be parsed")
return false
}

c, _ := semver.NewConstraint(constraint)
if !c.Check(v) {
logger.Failuref("kubectl version %s < %s", v.Original(), constraint)
return false
}

logger.Successf("kubectl %s %s", v.String(), constraint)
return true
}

func kubernetesCheck(constraint string) bool {
cfg, err := utils.KubeConfig(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/flux/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ func TestCheckPre(t *testing.T) {
t.Fatalf("Error unmarshalling: %v", err.Error())
}

clientVersion := strings.TrimPrefix(versions["clientVersion"].GitVersion, "v")
serverVersion := strings.TrimPrefix(versions["serverVersion"].GitVersion, "v")

cmd := cmdTestCase{
args: "check --pre",
assert: assertGoldenTemplateFile("testdata/check/check_pre.golden", map[string]string{
"clientVersion": clientVersion,
"serverVersion": serverVersion,
}),
}
Expand Down
25 changes: 10 additions & 15 deletions cmd/flux/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ If a previous version is installed, then an in-place upgrade will be performed.`
flux install --version=latest --namespace=flux-system
# Install a specific version and a series of components
flux install --dry-run --version=v0.0.7 --components="source-controller,kustomize-controller"
flux install --version=v0.0.7 --components="source-controller,kustomize-controller"
# Install Flux onto tainted Kubernetes nodes
flux install --toleration-keys=node.kubernetes.io/dedicated-to-flux
# Dry-run install with manifests preview
flux install --dry-run --verbose
# Dry-run install
flux install --export | kubectl apply --dry-run=client -f-
# Write install manifests to file
flux install --export > flux-system.yaml`,
Expand Down Expand Up @@ -102,6 +102,7 @@ func init() {
"list of toleration keys used to schedule the components pods onto nodes with matching taints")
installCmd.Flags().MarkHidden("manifests")
installCmd.Flags().MarkDeprecated("arch", "multi-arch container image is now available for AMD64, ARMv7 and ARM64")
installCmd.Flags().MarkDeprecated("dry-run", "use 'flux install --export | kubectl apply --dry-run=client -f-'")
rootCmd.AddCommand(installCmd)
}

Expand Down Expand Up @@ -188,24 +189,18 @@ func installCmdRun(cmd *cobra.Command, args []string) error {

logger.Successf("manifests build completed")
logger.Actionf("installing components in %s namespace", rootArgs.namespace)
applyOutput := utils.ModeStderrOS
if rootArgs.verbose {
applyOutput = utils.ModeOS
}

kubectlArgs := []string{"apply", "-f", filepath.Join(tmpDir, manifest.Path)}
if installArgs.dryRun {
kubectlArgs = append(kubectlArgs, "--dry-run=client")
applyOutput = utils.ModeOS
logger.Successf("install dry-run finished")
return nil
}
if _, err := utils.ExecKubectlCommand(ctx, applyOutput, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...); err != nil {

applyOutput, err := utils.Apply(ctx, rootArgs.kubeconfig, rootArgs.kubecontext, filepath.Join(tmpDir, manifest.Path))
if err != nil {
return fmt.Errorf("install failed: %w", err)
}

if installArgs.dryRun {
logger.Successf("install dry-run finished")
return nil
}
fmt.Fprintln(os.Stderr, applyOutput)

kubeConfig, err := utils.KubeConfig(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/flux/testdata/check/check_pre.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
► checking prerequisites
✔ kubectl {{ .clientVersion }} >=1.18.0-0
✔ Kubernetes {{ .serverVersion }} >=1.16.0-0
✔ prerequisites checks passed
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@ require (
github.com/fluxcd/notification-controller/api v0.16.0
github.com/fluxcd/pkg/apis/meta v0.10.0
github.com/fluxcd/pkg/runtime v0.12.0
github.com/fluxcd/pkg/ssa v0.0.0-20211001071904-2da41b6c8ff8
github.com/fluxcd/pkg/ssh v0.0.5
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/pkg/version v0.0.1
github.com/fluxcd/source-controller/api v0.15.4
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.5
github.com/google/go-cmp v0.5.6
github.com/google/go-containerregistry v0.2.0
github.com/manifoldco/promptui v0.7.0
github.com/mattn/go-shellwords v1.0.12
github.com/olekukonko/tablewriter v0.0.4
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
k8s.io/api v0.21.3
k8s.io/apiextensions-apiserver v0.21.3
k8s.io/apimachinery v0.21.3
k8s.io/client-go v0.21.3
k8s.io/api v0.22.2
k8s.io/apiextensions-apiserver v0.22.2
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
k8s.io/kubectl v0.21.1
sigs.k8s.io/cli-utils v0.25.1-0.20210608181808-f3974341173a
sigs.k8s.io/controller-runtime v0.9.5
sigs.k8s.io/controller-runtime v0.10.1
sigs.k8s.io/kustomize/api v0.8.10
sigs.k8s.io/yaml v1.2.0
sigs.k8s.io/yaml v1.3.0
)

// drop LGPL dependency manifoldco/promptui -> juju/ansiterm
Expand Down
Loading

0 comments on commit c850006

Please sign in to comment.