Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework kubectl minio init as kustomize based #548

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017
github.com/google/go-containerregistry v0.1.2
github.com/gorilla/mux v1.8.0
github.com/minio/controller-tools v0.4.7 // indirect
github.com/minio/minio v0.0.0-20210128013121-e79829b5b368
github.com/minio/minio-go/v7 v7.0.8-0.20210127003153-c40722862654
github.com/secure-io/sio-go v0.3.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ github.com/go-toolsmith/typep v1.0.2 h1:8xdsa1+FSIH/RhEkgnD1j2CJOy5mNllW1Q9tRiYw
github.com/go-toolsmith/typep v1.0.2/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b h1:khEcpUM4yFcxg4/FHQWkvVRmgijNXRfzkIDHh23ggEo=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM=
github.com/gobuffalo/flect v0.2.2 h1:PAVD7sp0KOdfswjAw9BpLCU9hXo7wFSzgpQ+zNeks/A=
github.com/gobuffalo/flect v0.2.2/go.mod h1:vmkQwuZYhN5Pc4ljYQZzP+1sq+NEkK+lh20jmEmX3jc=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b h1:ekuhfTjngPhisSjOJ0QWKpPQE8/rbknHaes6WVJj5Hw=
Expand Down Expand Up @@ -908,6 +910,8 @@ github.com/miekg/dns v1.1.35 h1:oTfOaDH+mZkdcgdIjH6yBajRGtIwcwcaR+rt23ZSrJs=
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/minio/cli v1.22.0 h1:VTQm7lmXm3quxO917X3p+el1l0Ca5X3S4PM2ruUYO68=
github.com/minio/cli v1.22.0/go.mod h1:bYxnK0uS629N3Bq+AOZZ+6lwF77Sodk4+UL9vNuXhOY=
github.com/minio/controller-tools v0.4.7 h1:g4UUbTkZXF8MQLRp+5rQnCsmI01f/4+sFoSTTjouF+c=
github.com/minio/controller-tools v0.4.7/go.mod h1:xES4iNis9dGrLQuP6nquTZZNg2T0/EM8wduC4/GWfZ0=
github.com/minio/highwayhash v1.0.0 h1:iMSDhgUILCr0TNm8LWlSjF8N0ZIj2qbO8WHp6Q/J2BA=
github.com/minio/highwayhash v1.0.0/go.mod h1:xQboMTeM9nY9v/LlAOxFctujiv5+Aq2hR5dxBpaMbdc=
github.com/minio/md5-simd v1.1.0 h1:QPfiOqlZH+Cj9teu0t9b1nTBfPbyTl16Of5MeuShdK4=
Expand Down Expand Up @@ -1676,6 +1680,7 @@ golang.org/x/tools v0.0.0-20200502202811-ed308ab3e770/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200616195046-dc31b401abb5/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201105001634-bc3cf281b174/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210115202250-e0d201561e39 h1:BTs2GMGSMWpgtCpv1CE7vkJTv7XcHdcLLnAMu7UbgTY=
Expand Down
108 changes: 77 additions & 31 deletions kubectl-minio/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@
package cmd

import (
"bufio"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"

"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/klog/v2"
Expand All @@ -39,8 +48,6 @@ import (
"github.com/minio/minio/pkg/color"
"github.com/spf13/cobra"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -77,7 +84,7 @@ func newDeleteCmd(out io.Writer, errOut io.Writer) *cobra.Command {
return errors.New("delete command does not accept arguments")
}
klog.Info("delete command started")
err := o.run()
err := o.run(out)
if err != nil {
klog.Warning(err)
return err
Expand All @@ -91,48 +98,87 @@ func newDeleteCmd(out io.Writer, errOut io.Writer) *cobra.Command {
return cmd
}

func (o *deleteCmd) run() error {
path, _ := rootCmd.Flags().GetString(kubeconfig)
ctx := context.Background()
client, err := helpers.GetKubeClient(path)
if err != nil {
return err
}
extclient, err := helpers.GetKubeExtensionClient()
func (o *deleteCmd) run(writer io.Writer) error {
inMemSys, err := resources.GetResourceFileSys()
if err != nil {
return err
}
dynclient, err := helpers.GetKubeDynamicClient()
if err != nil {
return err

// write the kustomization file

kustomizationYaml := types.Kustomization{
TypeMeta: types.TypeMeta{
Kind: "Kustomization",
APIVersion: "kustomize.config.k8s.io/v1beta1",
},
Resources: []string{
"operator/",
},
}
// Load Resources
decode := resources.GetSchemeDecoder()
crdObj := resources.LoadTenantCRD(decode)
if err := client.CoreV1().ServiceAccounts(o.operatorOpts.Namespace).Delete(ctx, helpers.DefaultServiceAccount, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
return err

if o.operatorOpts.Namespace != "" {
kustomizationYaml.Namespace = o.operatorOpts.Namespace
}
if err := client.AppsV1().Deployments(o.operatorOpts.Namespace).Delete(ctx, helpers.DeploymentName, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
// Compile the kustomization to a file and create on the in memory filesystem
kustYaml, _ := yaml.Marshal(kustomizationYaml)
kustFile, err := inMemSys.Create("kustomization.yaml")
_, err = kustFile.Write(kustYaml)
if err != nil {
log.Println(err)
return err
}
if err := client.CoreV1().Services(o.operatorOpts.Namespace).Delete(ctx, helpers.DefaultOperatorServiceName, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {

// kustomize build the target location
k := krusty.MakeKustomizer(
krusty.MakeDefaultOptions(),
)

m, err := k.Run(inMemSys, ".")
if err != nil {
return err
}
if err := extclient.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, crdObj.Name, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {

yml, err := m.AsYaml()
if err != nil {
return err
}
crObj := resources.LoadClusterRole(decode)
if err := client.RbacV1().ClusterRoles().Delete(ctx, crObj.Name, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
return err

if o.output {
_, err = writer.Write(yml)
//done
return nil
}
if err := client.RbacV1().ClusterRoleBindings().Delete(ctx, helpers.ClusterRoleBindingName, v1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
return err

// do kubectl apply
cmd := exec.Command("kubectl", "delete", "-f", "-")

cmd.Stdin = strings.NewReader(string(yml))

stdoutReader, _ := cmd.StdoutPipe()
stdoutScanner := bufio.NewScanner(stdoutReader)
go func() {
for stdoutScanner.Scan() {
fmt.Println(stdoutScanner.Text())
}
}()
stderrReader, _ := cmd.StderrPipe()
stderrScanner := bufio.NewScanner(stderrReader)
go func() {
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Printf("Error : %v \n", err)
os.Exit(1)
}
consoleResources := resources.LoadConsoleUI(decode, &o.operatorOpts)
if err := deleteConsoleResources(o.operatorOpts, extclient, dynclient, consoleResources); err != nil && !k8serrors.IsNotFound(err) {
klog.Info(err)
return errors.New("Cannot delete console resources")
err = cmd.Wait()
if err != nil {
fmt.Printf("Error: %v \n", err)
os.Exit(1)
}

return nil
}

Expand Down
Loading