Skip to content

Commit

Permalink
Delete embedded addons from manifests if delete flag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
mate4st committed Sep 2, 2021
1 parent af88ef6 commit c983bd9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/addons/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var (
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl apply -f - --prune -l "%s=%s"
`)

kubectlDeleteScript = heredoc.Doc(`
sudo KUBECONFIG=/etc/kubernetes/admin.conf \
kubectl delete -f - -l "%s=%s"
`)
)

// Applier holds structure used to fetch, parse, and apply addons
Expand Down
46 changes: 45 additions & 1 deletion pkg/addons/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func EnsureUserAddons(s *state.State) error {
}

if embeddedAddon.Delete {
//Todo: delete
s.Logger.Infof("Deleting addon %q...", embeddedAddon.Name)
if err := applier.loadAndDeleteAddon(s, applier.EmbededFS, embeddedAddon.Name); err != nil {
return errors.Wrapf(err, "failed to load and delete the addon %q", embeddedAddon.Name)
}
continue
}

Expand Down Expand Up @@ -192,6 +195,27 @@ func (a *applier) loadAndApplyAddon(s *state.State, fsys fs.FS, addonName string
)
}

// loadAndApplyAddon parses the addons manifests and runs kubectl apply.
func (a *applier) loadAndDeleteAddon(s *state.State, fsys fs.FS, addonName string) error {
manifest, err := a.getManifestsFromDirectory(s, fsys, addonName)
if err != nil {
return errors.WithStack(err)
}

if len(strings.TrimSpace(manifest)) == 0 {
if len(addonName) != 0 {
s.Logger.Warnf("Addon directory %q is empty, skipping...", addonName)
}

return nil
}

return errors.Wrap(
runKubectlDelete(s, manifest, addonName),
"failed to apply addons",
)
}

// runKubectlApply runs kubectl apply command
func runKubectlApply(s *state.State, manifest string, addonName string) error {
return s.RunTaskOnLeader(func(s *state.State, _ *kubeoneapi.HostConfig, conn ssh.Connection) error {
Expand All @@ -211,3 +235,23 @@ func runKubectlApply(s *state.State, manifest string, addonName string) error {
return err
})
}

// runKubectlDelete runs kubectl delete command
func runKubectlDelete(s *state.State, manifest string, addonName string) error {
return s.RunTaskOnLeader(func(s *state.State, _ *kubeoneapi.HostConfig, conn ssh.Connection) error {
var (
cmd = fmt.Sprintf(kubectlDeleteScript, addonLabel, addonName)
stdin = strings.NewReader(manifest)
stdout, stderr strings.Builder
)

_, err := conn.POpen(cmd, stdin, &stdout, &stderr)
if s.Verbose {
fmt.Printf("+ %s\n", cmd)
fmt.Printf("%s", stderr.String())
fmt.Printf("%s", stdout.String())
}

return err
})
}

0 comments on commit c983bd9

Please sign in to comment.