blob: d4277efeeb34c99418112b0accdc7e89ffb0b7f2 [file] [log] [blame]
// -
// ========================LICENSE_START=================================
// O-RAN-SC
// %%
// Copyright (C) 2022: Nordix Foundation
// %%
// 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.
// ========================LICENSE_END===================================
//
package main
import (
"context"
"flag"
"fmt"
"github.com/Nerzal/gocloak/v10"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/kube"
versioned "istio.io/client-go/pkg/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
var settings *cli.EnvSettings
var chartName string
var releaseName string
var namespace string
func main() {
flag.StringVar(&chartName, "chartName", "hello-world", "Chart name")
flag.StringVar(&releaseName, "releaseName", "hello-world", "Release name")
flag.StringVar(&namespace, "namespace", "istio-nonrtric", "namespace for install")
flag.Parse()
settings = cli.New()
uninstallHelmChart(releaseName, namespace)
deleteIstio(namespace)
deleteClient()
}
func deleteIstio(namespace string) {
config, err := rest.InClusterConfig()
if err != nil {
// fallback to kubeconfig
home, exists := os.LookupEnv("HOME")
if !exists {
home = "/root"
}
kubeconfig := filepath.Join(home, ".kube", "config")
if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
kubeconfig = envvar
}
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Println("failed to create K8s config")
}
}
ic, err := versioned.NewForConfig(config)
if err != nil {
fmt.Println("Failed to create istio client: %s", err)
}
gtClient := ic.NetworkingV1beta1().Gateways(namespace)
err = gtClient.Delete(context.TODO(), "nonrtric-istio-hw-gateway", metav1.DeleteOptions{})
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Deleted Gateway nonrtric-istio-hw-gateway")
}
vsClient := ic.NetworkingV1beta1().VirtualServices(namespace)
err = vsClient.Delete(context.TODO(), "nonrtric-istio-hw-vs", metav1.DeleteOptions{})
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Deleted VirtualServices nonrtric-istio-hw-vs")
}
apClient := ic.SecurityV1beta1().AuthorizationPolicies(namespace)
err = apClient.Delete(context.TODO(), "hw-policy", metav1.DeleteOptions{})
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Deleted AuthorizationPolicy hw-policy")
}
raClient := ic.SecurityV1beta1().RequestAuthentications(namespace)
err = raClient.Delete(context.TODO(), "jwt-hw", metav1.DeleteOptions{})
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Deleted RequestAuthentication jwt-hello-world")
}
}
func deleteClient() {
adminClient := gocloak.NewClient("http://192.168.49.2:31560")
ctx := context.Background()
token, err := adminClient.LoginAdmin(ctx, "admin", "admin", "master")
if err != nil {
fmt.Println(err)
}
clients, err := adminClient.GetClients(ctx, token.AccessToken, "hwrealm",
gocloak.GetClientsParams{
ClientID: gocloak.StringP("hwclient"),
},
)
if err != nil {
panic("List clients failed:" + err.Error())
}
for _, client := range clients {
err = adminClient.DeleteClient(ctx, token.AccessToken, "hwrealm", *client.ID)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Deleted client hwclient", *client.ID)
}
}
}
func uninstallHelmChart(name, ns string) {
actionConfig, err := getActionConfig(ns)
if err != nil {
fmt.Println(err)
}
iCli := action.NewUninstall(actionConfig)
//iCli.ReleaseName = name
resp, err := iCli.Run(name)
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully uninstalled release: ", resp.Release.Name)
}
func getActionConfig(namespace string) (*action.Configuration, error) {
actionConfig := new(action.Configuration)
// Create the rest config instance with ServiceAccount values loaded in them
config, err := rest.InClusterConfig()
if err != nil {
err = nil
// fallback to kubeconfig
home, exists := os.LookupEnv("HOME")
if !exists {
home = "/root"
}
kubeconfigPath := filepath.Join(home, ".kube", "config")
//kubeconfigPath := filepath.Join("~", ".kube", "config")
if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
kubeconfigPath = envvar
}
if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"),
func(format string, v ...interface{}) {
fmt.Sprintf(format, v)
}); err != nil {
//return actionConfig, err
fmt.Println(err)
}
} else {
// Create the ConfigFlags struct instance with initialized values from ServiceAccount
var kubeConfig *genericclioptions.ConfigFlags
kubeConfig = genericclioptions.NewConfigFlags(false)
kubeConfig.APIServer = &config.Host
kubeConfig.BearerToken = &config.BearerToken
kubeConfig.CAFile = &config.CAFile
kubeConfig.Namespace = &namespace
if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
fmt.Sprintf(format, v)
}); err != nil {
fmt.Println(err)
}
}
return actionConfig, err
}