Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #54 from magneticio/LAM-154/uninstall-command
Browse files Browse the repository at this point in the history
Uninstall command
  • Loading branch information
dmitry-onishchenko authored Aug 14, 2019
2 parents 50105cd + 6fdcdf3 commit 7157d10
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
51 changes: 51 additions & 0 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright © 2018 Developer [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/magneticio/vampkubistcli/kubernetes"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// installCmd represents the install command
var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall Vamp Management in your cluster",
Long: AddAppName(`
Example:
$AppName uninstall --kubeconfig kube-config.yaml
`),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if kubeConfigPath == "" {
kubeConfigPath = viper.GetString("kubeconfig")
}
if err := kubeclient.UninstallVampService(kubeConfigPath); err != nil {
return err
}
fmt.Printf("Vamp Service Uninstalled.\n")
return nil
},
}

func init() {
rootCmd.AddCommand(uninstallCmd)
uninstallCmd.Flags().StringVarP(&kubeConfigPath, "kubeconfig", "", "", "Kube Config path")
viper.BindEnv("kubeconfig", "KUBECONFIG")
}
30 changes: 27 additions & 3 deletions kubernetes/kubeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var DefaultVampConfig = models.VampConfig{
// TODO: add it to VampConfig when it is configurable
const InstallationNamespace = "vamp-system"

// VampClusterRoleBindingName contains name of ClusterRoleBinding for Vamp
const VampClusterRoleBindingName = InstallationNamespace + "-sa-cluster-admin-binding"

func VampConfigValidateAndSetupDefaults(config *models.VampConfig) (*models.VampConfig, error) {
if config.RootPassword == "" {
// This is enforced
Expand Down Expand Up @@ -128,16 +131,17 @@ func getLocalKubeClient(configPath string) (*kubernetes.Clientset, string, error
This method installs namespace, cluster role binding and image pull secret
TODO: differenciate between already exists and other error types
*/
func SetupVampCredentials(clientset *kubernetes.Clientset, ns string) error {
func SetupVampCredentials(clientset *kubernetes.Clientset, ns string, rbName string) error {
nsSpec := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}
_, namespaceCreationError := clientset.CoreV1().Namespaces().Create(nsSpec)
if namespaceCreationError != nil {
// TODO: handle already exists
fmt.Printf("Warning: %v\n", namespaceCreationError.Error())
}
// Create Cluster Role Binding Vamp Default Service Account

clusterRoleBindingSpec := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: ns + "-sa-cluster-admin-binding"},
ObjectMeta: metav1.ObjectMeta{Name: rbName},
Subjects: []rbacv1.Subject{rbacv1.Subject{Kind: "User", Name: "system:serviceaccount:" + ns + ":default", APIGroup: "rbac.authorization.k8s.io"}},
RoleRef: rbacv1.RoleRef{Kind: "ClusterRole", Name: "cluster-admin", APIGroup: "rbac.authorization.k8s.io"},
}
Expand All @@ -150,6 +154,18 @@ func SetupVampCredentials(clientset *kubernetes.Clientset, ns string) error {
return nil
}

func RemoveVampCredentials(clientset *kubernetes.Clientset, ns string, rbName string) error {
if err := clientset.CoreV1().Namespaces().Delete(ns, nil); err != nil {
fmt.Printf("Canot delete Vamp namespace %v - %v", ns, err)
return err
}
if err := clientset.RbacV1().ClusterRoleBindings().Delete(rbName, nil); err != nil {
fmt.Printf("Canot delete role bindings %v - %v", ns, err)
return err
}
return nil
}

func BootstrapVampService(configPath string) (string, string, string, error) {
// create the clientset
clientset, host, err := getLocalKubeClient(configPath)
Expand All @@ -158,7 +174,7 @@ func BootstrapVampService(configPath string) (string, string, string, error) {
return "", "", "", err
}
ns := InstallationNamespace
errSetup := SetupVampCredentials(clientset, ns)
errSetup := SetupVampCredentials(clientset, ns, VampClusterRoleBindingName)
if errSetup != nil {
fmt.Printf("Warning: %v\n", errSetup.Error())
return host, "", "", errSetup
Expand Down Expand Up @@ -219,6 +235,14 @@ func InstallVampService(config *models.VampConfig, configPath string) (string, [
return *url, cert, key, nil
}

func UninstallVampService(configPath string) error {
clientset, _, err := getLocalKubeClient(configPath)
if err != nil {
return err
}
return RemoveVampCredentials(clientset, InstallationNamespace, VampClusterRoleBindingName)
}

func CheckAndWaitForService(url string, cert []byte) error {
vampClient := client.NewRestClient(url, "", "", false, string(cert), nil)
count := 1
Expand Down

0 comments on commit 7157d10

Please sign in to comment.